1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* Store backend using a Mach memory object
Copyright (C) 2001 Free Software Foundation, Inc.
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
#include "store.h"
#include <hurd.h>
#include <hurd/sigpreempt.h>
#include <sys/mman.h>
#include <string.h>
#include <assert.h>
/* Return a new store in STORE referring to the memory object MEMOBJ.
Consumes the send right MEMOBJ. */
error_t
store_memobj_create (memory_object_t memobj, int flags, size_t block_size,
const struct store_run *runs, size_t num_runs,
struct store **store)
{
return _store_create (&store_memobj_class,
memobj, flags, block_size, runs, num_runs, 0, store);
}
/* This one is pretty easy. */
static error_t
memobj_map (const struct store *store, vm_prot_t prot, mach_port_t *memobj)
{
*memobj = store->port;
return mach_port_mod_refs (mach_task_self (), *memobj,
MACH_PORT_RIGHT_SEND, +1);
}
static error_t /* swiped from libpager's pager_memcpy */
memobj_memcpy (memory_object_t memobj,
vm_offset_t offset, void *other, size_t *size,
vm_prot_t prot)
{
vm_address_t window = 0;
vm_size_t windowsize = 8 * vm_page_size;
size_t to_copy = *size;
error_t err;
error_t copy (struct hurd_signal_preemptor *preemptor)
{
while (to_copy > 0)
{
size_t pageoff = offset & (vm_page_size - 1);
if (window)
/* Deallocate the old window. */
munmap ((caddr_t) window, windowsize);
/* Map in and copy a standard-sized window, unless that is
more than the total left to be copied. */
if (windowsize > pageoff + to_copy)
windowsize = pageoff + to_copy;
window = 0;
err = vm_map (mach_task_self (), &window, windowsize, 0, 1,
memobj, offset - pageoff, 0,
prot, prot, VM_INHERIT_NONE);
if (err)
return 0;
/* Realign the fault preemptor for the new mapping window. */
preemptor->first = window;
preemptor->last = window + windowsize;
if (prot == VM_PROT_READ)
memcpy (other, (const void *) window + pageoff,
windowsize - pageoff);
else
memcpy ((void *) window + pageoff, other, windowsize - pageoff);
offset += windowsize - pageoff;
other += windowsize - pageoff;
to_copy -= windowsize - pageoff;
}
return 0;
}
jmp_buf buf;
void fault (int signo, long int sigcode, struct sigcontext *scp)
{
assert (scp->sc_error == EKERN_MEMORY_ERROR);
err = EIO;
to_copy -= sigcode - window;
longjmp (buf, 1);
}
if (to_copy == 0)
/* Short-circuit return if nothing to do.
ERR would not be initialized by the copy loop in this case. */
return 0;
if (setjmp (buf) == 0)
hurd_catch_signal (sigmask (SIGSEGV) | sigmask (SIGBUS),
window, window + windowsize,
©, (sighandler_t) &fault);
if (window)
munmap ((caddr_t) window, windowsize);
*size -= to_copy;
return err;
}
static error_t
memobj_read (struct store *store,
store_offset_t addr, size_t index, size_t amount,
void **buf, size_t *len)
{
addr <<= store->log2_block_size;
if (((size_t) addr & (vm_page_size - 1)) == 0)
{
*len = amount;
return vm_map (mach_task_self (), (vm_address_t *) buf, amount,
0, 1, store->port, addr << store->log2_block_size, 0,
VM_PROT_READ, VM_PROT_ALL, VM_INHERIT_NONE);
}
else
{
error_t err;
int alloced = 0;
if (*len < amount)
{
*buf = mmap (0, amount, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
if (*buf == MAP_FAILED)
return errno;
alloced = 1;
}
*len = amount;
err = memobj_memcpy (store->port, addr, *buf, len, VM_PROT_READ);
if (err && alloced)
munmap (*buf, amount);
else if (alloced && round_page (*len) < round_page (amount))
munmap (*buf + round_page (*len),
round_page (amount) - round_page (*len));
return err;
}
}
static error_t
memobj_write (struct store *store,
store_offset_t addr, size_t index,
const void *buf, size_t len, size_t *amount)
{
*amount = len;
return memobj_memcpy (store->port, addr << store->log2_block_size,
(void *) buf, amount, VM_PROT_WRITE);
}
static error_t
memobj_decode (struct store_enc *enc, const struct store_class *const *classes,
struct store **store)
{
return store_std_leaf_decode (enc, store_memobj_create, store);
}
const struct store_class
store_memobj_class =
{
STORAGE_MEMORY, "memobj",
map: memobj_map,
read: memobj_read,
write: memobj_write,
allocate_encoding: store_std_leaf_allocate_encoding,
encode: store_std_leaf_encode,
decode: memobj_decode,
};
|