1 |
gbeauche |
1.1 |
/* |
2 |
|
|
* vm_alloc.h - Wrapper to various virtual memory allocation schemes |
3 |
|
|
* (supports mmap, vm_allocate or fallbacks to malloc) |
4 |
|
|
* |
5 |
gbeauche |
1.13 |
* Basilisk II (C) 1997-2008 Christian Bauer |
6 |
gbeauche |
1.1 |
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#ifndef VM_ALLOC_H |
23 |
|
|
#define VM_ALLOC_H |
24 |
|
|
|
25 |
|
|
#ifdef HAVE_UNISTD_H |
26 |
|
|
#include <unistd.h> |
27 |
gbeauche |
1.3 |
#include <sys/types.h> |
28 |
gbeauche |
1.1 |
#endif |
29 |
|
|
#ifdef HAVE_SYS_MMAN_H |
30 |
|
|
#include <sys/mman.h> |
31 |
|
|
#endif |
32 |
gbeauche |
1.4 |
#ifdef HAVE_MACH_VM |
33 |
|
|
extern "C" { |
34 |
|
|
#include <mach/mach.h> |
35 |
asvitkine |
1.15 |
#include <mach/task.h> |
36 |
gbeauche |
1.4 |
} |
37 |
|
|
#endif |
38 |
gbeauche |
1.1 |
|
39 |
|
|
/* Return value of `vm_acquire' in case of an error. */ |
40 |
|
|
#ifdef HAVE_MACH_VM |
41 |
|
|
#define VM_MAP_FAILED ((void *)-1) |
42 |
|
|
#else |
43 |
|
|
#ifdef HAVE_MMAP_VM |
44 |
|
|
#define VM_MAP_FAILED ((void *)-1) |
45 |
|
|
#else |
46 |
|
|
#define VM_MAP_FAILED 0 |
47 |
|
|
#endif |
48 |
|
|
#endif |
49 |
|
|
|
50 |
gbeauche |
1.14 |
/* Option to vm_get_write_watch() to reset the write-tracking state |
51 |
|
|
once it was retrieved. Otherwise, you have to manually call |
52 |
|
|
vm_reset_write_watch() and potentially lose some info. */ |
53 |
|
|
#define VM_WRITE_WATCH_RESET 0x01 |
54 |
|
|
|
55 |
gbeauche |
1.5 |
/* Mapping options. */ |
56 |
|
|
#define VM_MAP_SHARED 0x01 |
57 |
|
|
#define VM_MAP_PRIVATE 0x02 |
58 |
|
|
#define VM_MAP_FIXED 0x04 |
59 |
|
|
#define VM_MAP_32BIT 0x08 |
60 |
gbeauche |
1.14 |
#define VM_MAP_WRITE_WATCH 0x10 |
61 |
gbeauche |
1.5 |
|
62 |
|
|
/* Default mapping options. */ |
63 |
|
|
#define VM_MAP_DEFAULT (VM_MAP_PRIVATE) |
64 |
|
|
|
65 |
gbeauche |
1.1 |
/* Protection bits. */ |
66 |
|
|
#ifdef HAVE_MACH_VM |
67 |
|
|
#define VM_PAGE_NOACCESS VM_PROT_NONE |
68 |
|
|
#define VM_PAGE_READ VM_PROT_READ |
69 |
|
|
#define VM_PAGE_WRITE VM_PROT_WRITE |
70 |
|
|
#define VM_PAGE_EXECUTE VM_PROT_EXECUTE |
71 |
|
|
#else |
72 |
|
|
#ifdef HAVE_MMAP_VM |
73 |
|
|
#define VM_PAGE_NOACCESS PROT_NONE |
74 |
|
|
#define VM_PAGE_READ PROT_READ |
75 |
|
|
#define VM_PAGE_WRITE PROT_WRITE |
76 |
|
|
#define VM_PAGE_EXECUTE PROT_EXEC |
77 |
|
|
#else |
78 |
|
|
#define VM_PAGE_NOACCESS 0x0 |
79 |
|
|
#define VM_PAGE_READ 0x1 |
80 |
|
|
#define VM_PAGE_WRITE 0x2 |
81 |
|
|
#define VM_PAGE_EXECUTE 0x4 |
82 |
|
|
#endif |
83 |
|
|
#endif |
84 |
|
|
|
85 |
|
|
/* Default protection bits. */ |
86 |
|
|
#define VM_PAGE_DEFAULT (VM_PAGE_READ | VM_PAGE_WRITE) |
87 |
|
|
|
88 |
|
|
/* Initialize the VM system. Returns 0 if successful, -1 for errors. */ |
89 |
|
|
|
90 |
|
|
extern int vm_init(void); |
91 |
|
|
|
92 |
|
|
/* Deallocate all internal data used to wrap virtual memory allocators. */ |
93 |
|
|
|
94 |
|
|
extern void vm_exit(void); |
95 |
|
|
|
96 |
|
|
/* Allocate zero-filled memory of SIZE bytes. The mapping is private |
97 |
|
|
and default protection bits are read / write. The return value |
98 |
|
|
is the actual mapping address chosen or VM_MAP_FAILED for errors. */ |
99 |
|
|
|
100 |
gbeauche |
1.5 |
extern void * vm_acquire(size_t size, int options = VM_MAP_DEFAULT); |
101 |
gbeauche |
1.1 |
|
102 |
|
|
/* Allocate zero-filled memory at exactly ADDR (which must be page-aligned). |
103 |
|
|
Returns 0 if successful, -1 on errors. */ |
104 |
|
|
|
105 |
gbeauche |
1.5 |
extern int vm_acquire_fixed(void * addr, size_t size, int options = VM_MAP_DEFAULT); |
106 |
gbeauche |
1.1 |
|
107 |
|
|
/* Deallocate any mapping for the region starting at ADDR and extending |
108 |
|
|
LEN bytes. Returns 0 if successful, -1 on errors. */ |
109 |
|
|
|
110 |
|
|
extern int vm_release(void * addr, size_t size); |
111 |
|
|
|
112 |
|
|
/* Change the memory protection of the region starting at ADDR and |
113 |
gbeauche |
1.14 |
extending SIZE bytes to PROT. Returns 0 if successful, -1 for errors. */ |
114 |
gbeauche |
1.1 |
|
115 |
|
|
extern int vm_protect(void * addr, size_t size, int prot); |
116 |
|
|
|
117 |
gbeauche |
1.14 |
/* Return the addresses of the pages that got modified since the last |
118 |
|
|
reset of the write-tracking state for the specified range [ ADDR, |
119 |
|
|
ADDR + SIZE [. Returns 0 if successful, -1 for errors. */ |
120 |
|
|
|
121 |
|
|
extern int vm_get_write_watch(void * addr, size_t size, |
122 |
|
|
void ** pages, unsigned int * n_pages, |
123 |
|
|
int options = 0); |
124 |
|
|
|
125 |
|
|
/* Reset the write-tracking state for the specified range [ ADDR, ADDR |
126 |
|
|
+ SIZE [. Returns 0 if successful, -1 for errors. */ |
127 |
|
|
|
128 |
|
|
extern int vm_reset_write_watch(void * addr, size_t size); |
129 |
|
|
|
130 |
gbeauche |
1.8 |
/* Returns the size of a page. */ |
131 |
|
|
|
132 |
gbeauche |
1.9 |
extern int vm_get_page_size(void); |
133 |
gbeauche |
1.8 |
|
134 |
gbeauche |
1.1 |
#endif /* VM_ALLOC_H */ |