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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* pager.c - The pager for the display component of a virtual console.
Copyright (C) 2002 Free Software Foundation, Inc.
Written by Marcus Brinkmann.
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 <errno.h>
#include <assert.h>
#include <error.h>
#include <stdio.h>
#include <sys/mman.h>
#include <pthread.h>
#include <hurd.h>
#include <hurd/pager.h>
#include <hurd/console.h>
#include "pager.h"
struct user_pager_info
{
size_t memobj_npages;
vm_address_t memobj_pages[0];
};
/* We need a separate bucket for the pager ports. */
static struct port_bucket *pager_bucket;
static struct pager_requests *pager_requests;
/* Implement the pager_clear_user_data callback from the pager library. */
void
pager_clear_user_data (struct user_pager_info *upi)
{
size_t idx;
for (idx = 0; idx < upi->memobj_npages; idx++)
if (upi->memobj_pages[idx])
vm_deallocate (mach_task_self (), upi->memobj_pages[idx], vm_page_size);
}
error_t
pager_read_page (struct user_pager_info *upi, vm_offset_t page,
vm_address_t *buf, int *writelock)
{
/* XXX clients should get a read only object. */
*writelock = 0;
if (upi->memobj_pages[page / vm_page_size] != (vm_address_t) NULL)
{
*buf = upi->memobj_pages[page / vm_page_size];
upi->memobj_pages[page / vm_page_size] = (vm_address_t) NULL;
}
else
*buf = (vm_address_t) mmap (0, vm_page_size, PROT_READ|PROT_WRITE,
MAP_ANON, 0, 0);
return 0;
}
error_t
pager_write_page (struct user_pager_info *upi, vm_offset_t page,
vm_address_t buf)
{
assert (upi->memobj_pages[page / vm_page_size] == (vm_address_t) NULL);
upi->memobj_pages[page / vm_page_size] = buf;
return 0;
}
error_t
pager_unlock_page (struct user_pager_info *pager,
vm_offset_t address)
{
assert (!"unlocking requested on unlocked page");
return 0;
}
void
pager_notify_evict (struct user_pager_info *pager,
vm_offset_t page)
{
assert (!"unrequested notification on eviction");
}
/* Tell how big the file is. */
error_t
pager_report_extent (struct user_pager_info *upi,
vm_address_t *offset,
vm_size_t *size)
{
*offset = 0;
*size = upi->memobj_npages * vm_page_size;
return 0;
}
void
pager_dropweak (struct user_pager_info *upi)
{
}
/* Initialize the pager for the display component. */
void
user_pager_init (void)
{
error_t err;
/* Create the pager bucket, and start to serve paging requests. */
pager_bucket = ports_create_bucket ();
if (! pager_bucket)
error (5, errno, "Cannot create pager bucket");
/* Start libpagers worker threads. */
err = pager_start_workers (pager_bucket, &pager_requests);
if (err)
error (5, err, "Cannot start pager worker threads");
}
/* Create a new pager in USER_PAGER with NPAGES pages, and return a
mapping to the memory in *USER. */
error_t
user_pager_create (struct user_pager *user_pager, unsigned int npages,
struct cons_display **user)
{
error_t err;
struct user_pager_info *upi;
/* XXX Are the values 1 and MEMORY_OBJECT_COPY_DELAY correct? */
user_pager->pager = \
pager_create_alloc (sizeof *upi + sizeof (vm_address_t) * npages,
pager_bucket, 1, MEMORY_OBJECT_COPY_DELAY, 0);
if (!user_pager->pager)
return errno;
upi = pager_get_upi (user_pager->pager);
upi->memobj_npages = npages;
memset (upi->memobj_pages, 0, sizeof (vm_address_t) * npages);
user_pager->memobj = pager_get_port (user_pager->pager);
ports_port_deref (user_pager->pager);
mach_port_insert_right (mach_task_self (), user_pager->memobj,
user_pager->memobj, MACH_MSG_TYPE_MAKE_SEND);
*user = 0;
err = vm_map (mach_task_self (),
(vm_address_t *) user,
(vm_size_t) npages * vm_page_size,
(vm_address_t) 0,
1 /* ! (flags & MAP_FIXED) */,
user_pager->memobj, 0 /* (vm_offset_t) offset */,
0 /* ! (flags & MAP_SHARED) */,
VM_PROT_READ | VM_PROT_WRITE,
VM_PROT_READ | VM_PROT_WRITE,
VM_INHERIT_NONE);
if (err)
{
mach_port_deallocate (mach_task_self (), user_pager->memobj);
return err;
}
return 0;
}
/* Destroy the pager USER_PAGER and the mapping at USER. */
void
user_pager_destroy (struct user_pager *user_pager, struct cons_display *user)
{
/* The pager will be deallocated by libpager. */
vm_deallocate (mach_task_self (), (vm_offset_t) user,
pager_get_upi (user_pager->pager)->memobj_npages
* vm_page_size);
mach_port_deallocate (mach_task_self (), user_pager->memobj);
}
/* Allocate a reference for the memory object backing the pager
USER_PAGER with protection PROT and return it. */
mach_port_t
user_pager_get_filemap (struct user_pager *user_pager, vm_prot_t prot)
{
error_t err;
/* Add a reference for each call, the caller will deallocate it. */
err = mach_port_mod_refs (mach_task_self (), user_pager->memobj,
MACH_PORT_RIGHT_SEND, +1);
assert_perror (err);
return user_pager->memobj;
}
|