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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/*
* Mach Operating System
* Copyright (c) 1993-1989 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* Author: David B. Golub, Carnegie Mellon University
* Date: 3/89
*
* Device pager.
*/
#include <string.h>
#include <mach/boolean.h>
#include <mach/port.h>
#include <mach/message.h>
#include <mach/std_types.h>
#include <mach/mach_types.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_space.h>
#include <kern/debug.h>
#include <kern/printf.h>
#include <kern/queue.h>
#include <kern/slab.h>
#include <vm/vm_page.h>
#include <vm/vm_kern.h>
#include <vm/vm_user.h>
#include <device/device_types.h>
#include <device/ds_routines.h>
#include <device/dev_hdr.h>
#include <device/io_req.h>
#include <device/memory_object_reply.user.h>
extern vm_offset_t block_io_mmap(); /* dummy routine to allow
mmap for block devices */
/*
* The device pager routines are called directly from the message
* system (via mach_msg), and thus run in the kernel-internal
* environment. All ports are in internal form (ipc_port_t),
* and must be correctly reference-counted in order to be saved
* in other data structures. Kernel routines may be called
* directly. Kernel types are used for data objects (tasks,
* memory objects, ports). The only IPC routines that may be
* called are ones that masquerade as the kernel task (via
* msg_send_from_kernel).
*
* Port rights and references are maintained as follows:
* Memory object port:
* The device_pager task has all rights.
* Memory object control port:
* The device_pager task has only send rights.
* Memory object name port:
* The device_pager task has only send rights.
* The name port is not even recorded.
* Regardless how the object is created, the control and name
* ports are created by the kernel and passed through the memory
* management interface.
*
* The device_pager assumes that access to its memory objects
* will not be propagated to more that one host, and therefore
* provides no consistency guarantees beyond those made by the
* kernel.
*
* In the event that more than one host attempts to use a device
* memory object, the device_pager will only record the last set
* of port names. [This can happen with only one host if a new
* mapping is being established while termination of all previous
* mappings is taking place.] Currently, the device_pager assumes
* that its clients adhere to the initialization and termination
* protocols in the memory management interface; otherwise, port
* rights or out-of-line memory from erroneous messages may be
* allowed to accumulate.
*
* [The phrase "currently" has been used above to denote aspects of
* the implementation that could be altered without changing the rest
* of the basic documentation.]
*/
/*
* Basic device pager structure.
*/
struct dev_pager {
decl_simple_lock_data(, lock) /* lock for reference count */
int ref_count; /* reference count */
int client_count; /* How many memory_object_create
* calls have we received */
ipc_port_t pager; /* pager port */
ipc_port_t pager_request; /* Known request port */
ipc_port_t pager_name; /* Known name port */
mach_device_t device; /* Device handle */
int type; /* to distinguish */
#define DEV_PAGER_TYPE 0
#define CHAR_PAGER_TYPE 1
/* char pager specifics */
int prot;
vm_size_t size;
};
typedef struct dev_pager *dev_pager_t;
#define DEV_PAGER_NULL ((dev_pager_t)0)
struct kmem_cache dev_pager_cache;
void dev_pager_reference(register dev_pager_t ds)
{
simple_lock(&ds->lock);
ds->ref_count++;
simple_unlock(&ds->lock);
}
void dev_pager_deallocate(register dev_pager_t ds)
{
simple_lock(&ds->lock);
if (--ds->ref_count > 0) {
simple_unlock(&ds->lock);
return;
}
simple_unlock(&ds->lock);
kmem_cache_free(&dev_pager_cache, (vm_offset_t)ds);
}
/*
* A hash table of ports for device_pager backed objects.
*/
#define DEV_PAGER_HASH_COUNT 127
struct dev_pager_entry {
queue_chain_t links;
ipc_port_t name;
dev_pager_t pager_rec;
};
typedef struct dev_pager_entry *dev_pager_entry_t;
queue_head_t dev_pager_hashtable[DEV_PAGER_HASH_COUNT];
struct kmem_cache dev_pager_hash_cache;
decl_simple_lock_data(,
dev_pager_hash_lock)
#define dev_pager_hash(name_port) \
(((natural_t)(name_port) & 0xffffff) % DEV_PAGER_HASH_COUNT)
void dev_pager_hash_init(void)
{
register int i;
register vm_size_t size;
size = sizeof(struct dev_pager_entry);
kmem_cache_init(&dev_pager_hash_cache, "dev_pager_entry", size, 0,
NULL, NULL, NULL, 0);
for (i = 0; i < DEV_PAGER_HASH_COUNT; i++)
queue_init(&dev_pager_hashtable[i]);
simple_lock_init(&dev_pager_hash_lock);
}
void dev_pager_hash_insert(
ipc_port_t name_port,
dev_pager_t rec)
{
register dev_pager_entry_t new_entry;
new_entry = (dev_pager_entry_t) kmem_cache_alloc(&dev_pager_hash_cache);
new_entry->name = name_port;
new_entry->pager_rec = rec;
simple_lock(&dev_pager_hash_lock);
queue_enter(&dev_pager_hashtable[dev_pager_hash(name_port)],
new_entry, dev_pager_entry_t, links);
simple_unlock(&dev_pager_hash_lock);
}
void dev_pager_hash_delete(ipc_port_t name_port)
{
register queue_t bucket;
register dev_pager_entry_t entry;
bucket = &dev_pager_hashtable[dev_pager_hash(name_port)];
simple_lock(&dev_pager_hash_lock);
for (entry = (dev_pager_entry_t)queue_first(bucket);
!queue_end(bucket, &entry->links);
entry = (dev_pager_entry_t)queue_next(&entry->links)) {
if (entry->name == name_port) {
queue_remove(bucket, entry, dev_pager_entry_t, links);
break;
}
}
simple_unlock(&dev_pager_hash_lock);
if (entry)
kmem_cache_free(&dev_pager_hash_cache, (vm_offset_t)entry);
}
dev_pager_t dev_pager_hash_lookup(ipc_port_t name_port)
{
register queue_t bucket;
register dev_pager_entry_t entry;
register dev_pager_t pager;
bucket = &dev_pager_hashtable[dev_pager_hash(name_port)];
simple_lock(&dev_pager_hash_lock);
for (entry = (dev_pager_entry_t)queue_first(bucket);
!queue_end(bucket, &entry->links);
entry = (dev_pager_entry_t)queue_next(&entry->links)) {
if (entry->name == name_port) {
pager = entry->pager_rec;
dev_pager_reference(pager);
simple_unlock(&dev_pager_hash_lock);
return (pager);
}
}
simple_unlock(&dev_pager_hash_lock);
return (DEV_PAGER_NULL);
}
kern_return_t device_pager_setup(
mach_device_t device,
int prot,
vm_offset_t offset,
vm_size_t size,
mach_port_t *pager)
{
register dev_pager_t d;
/*
* Verify the device is indeed mappable
*/
if (!device->dev_ops->d_mmap || (device->dev_ops->d_mmap == nomap))
return (D_INVALID_OPERATION);
/*
* Allocate a structure to hold the arguments
* and port to represent this object.
*/
d = dev_pager_hash_lookup((ipc_port_t)device); /* HACK */
if (d != DEV_PAGER_NULL) {
*pager = (mach_port_t) ipc_port_make_send(d->pager);
dev_pager_deallocate(d);
return (D_SUCCESS);
}
d = (dev_pager_t) kmem_cache_alloc(&dev_pager_cache);
if (d == DEV_PAGER_NULL)
return (KERN_RESOURCE_SHORTAGE);
simple_lock_init(&d->lock);
d->ref_count = 1;
/*
* Allocate the pager port.
*/
d->pager = ipc_port_alloc_kernel();
if (d->pager == IP_NULL) {
dev_pager_deallocate(d);
return (KERN_RESOURCE_SHORTAGE);
}
d->client_count = 0;
d->pager_request = IP_NULL;
d->pager_name = IP_NULL;
d->device = device;
mach_device_reference(device);
d->prot = prot;
d->size = round_page(size);
if (device->dev_ops->d_mmap == block_io_mmap) {
d->type = DEV_PAGER_TYPE;
} else {
d->type = CHAR_PAGER_TYPE;
}
dev_pager_hash_insert(d->pager, d);
dev_pager_hash_insert((ipc_port_t)device, d); /* HACK */
*pager = (mach_port_t) ipc_port_make_send(d->pager);
return (KERN_SUCCESS);
}
/*
* Routine: device_pager_release
* Purpose:
* Relinquish any references or rights that were
* associated with the result of a call to
* device_pager_setup.
*/
void device_pager_release(memory_object_t object)
{
if (MACH_PORT_VALID(object))
ipc_port_release_send((ipc_port_t) object);
}
boolean_t device_pager_debug = FALSE;
boolean_t device_pager_data_request_done(); /* forward */
boolean_t device_pager_data_write_done(); /* forward */
kern_return_t device_pager_data_request(
ipc_port_t pager,
ipc_port_t pager_request,
vm_offset_t offset,
vm_size_t length,
vm_prot_t protection_required)
{
register dev_pager_t ds;
#ifdef lint
protection_required++;
#endif /* lint */
if (device_pager_debug)
printf("(device_pager)data_request: pager=%p, offset=0x%x, length=0x%x\n",
pager, offset, length);
ds = dev_pager_hash_lookup((ipc_port_t)pager);
if (ds == DEV_PAGER_NULL)
panic("(device_pager)data_request: lookup failed");
if (ds->pager_request != pager_request)
panic("(device_pager)data_request: bad pager_request");
if (ds->type == CHAR_PAGER_TYPE) {
register vm_object_t object;
vm_offset_t device_map_page(void *,vm_offset_t);
object = vm_object_lookup(pager_request);
if (object == VM_OBJECT_NULL) {
(void) r_memory_object_data_error(pager_request,
offset, length,
KERN_FAILURE);
dev_pager_deallocate(ds);
return (KERN_SUCCESS);
}
vm_object_page_map(object,
offset, length,
device_map_page, (char *)ds);
vm_object_deallocate(object);
}
else {
register io_req_t ior;
register mach_device_t device;
io_return_t result;
panic("(device_pager)data_request: dev pager");
device = ds->device;
mach_device_reference(device);
dev_pager_deallocate(ds);
/*
* Package the read for the device driver.
*/
io_req_alloc(ior, 0);
ior->io_device = device;
ior->io_unit = device->dev_number;
ior->io_op = IO_READ | IO_CALL;
ior->io_mode = 0;
ior->io_recnum = offset / device->bsize;
ior->io_data = 0; /* driver must allocate */
ior->io_count = length;
ior->io_alloc_size = 0; /* no data allocated yet */
ior->io_residual = 0;
ior->io_error = 0;
ior->io_done = device_pager_data_request_done;
ior->io_reply_port = pager_request;
ior->io_reply_port_type = MACH_MSG_TYPE_PORT_SEND;
result = (*device->dev_ops->d_read)(device->dev_number, ior);
if (result == D_IO_QUEUED)
return (KERN_SUCCESS);
/*
* Return by queuing IOR for io_done thread, to reply in
* correct environment (kernel).
*/
ior->io_error = result;
iodone(ior);
}
dev_pager_deallocate(ds);
return (KERN_SUCCESS);
}
/*
* Always called by io_done thread.
*/
boolean_t device_pager_data_request_done(register io_req_t ior)
{
vm_offset_t start_alloc, end_alloc;
vm_size_t size_read;
if (ior->io_error == D_SUCCESS) {
size_read = ior->io_count;
if (ior->io_residual) {
if (device_pager_debug)
printf("(device_pager)data_request_done: r: 0x%lx\n",ior->io_residual);
memset((&ior->io_data[ior->io_count - ior->io_residual]), 0,
(unsigned) ior->io_residual);
}
} else {
size_read = ior->io_count - ior->io_residual;
}
start_alloc = trunc_page((vm_offset_t)ior->io_data);
end_alloc = start_alloc + round_page(ior->io_alloc_size);
if (ior->io_error == D_SUCCESS) {
vm_map_copy_t copy;
kern_return_t kr;
kr = vm_map_copyin(kernel_map, (vm_offset_t)ior->io_data,
size_read, TRUE, ©);
if (kr != KERN_SUCCESS)
panic("device_pager_data_request_done");
(void) r_memory_object_data_provided(
ior->io_reply_port,
ior->io_recnum * ior->io_device->bsize,
(vm_offset_t)copy,
size_read,
VM_PROT_NONE);
}
else {
(void) r_memory_object_data_error(
ior->io_reply_port,
ior->io_recnum * ior->io_device->bsize,
(vm_size_t)ior->io_count,
ior->io_error);
}
(void)vm_deallocate(kernel_map,
start_alloc,
end_alloc - start_alloc);
mach_device_deallocate(ior->io_device);
return (TRUE);
}
kern_return_t device_pager_data_write(
ipc_port_t pager,
ipc_port_t pager_request,
register vm_offset_t offset,
register pointer_t addr,
vm_size_t data_count)
{
register dev_pager_t ds;
register mach_device_t device;
register io_req_t ior;
kern_return_t result;
panic("(device_pager)data_write: called");
ds = dev_pager_hash_lookup((ipc_port_t)pager);
if (ds == DEV_PAGER_NULL)
panic("(device_pager)data_write: lookup failed");
if (ds->pager_request != pager_request)
panic("(device_pager)data_write: bad pager_request");
if (ds->type == CHAR_PAGER_TYPE)
panic("(device_pager)data_write: char pager");
device = ds->device;
mach_device_reference(device);
dev_pager_deallocate(ds);
/*
* Package the write request for the device driver.
*/
io_req_alloc(ior, data_count);
ior->io_device = device;
ior->io_unit = device->dev_number;
ior->io_op = IO_WRITE | IO_CALL;
ior->io_mode = 0;
ior->io_recnum = offset / device->bsize;
ior->io_data = (io_buf_ptr_t)addr;
ior->io_count = data_count;
ior->io_alloc_size = data_count; /* amount to deallocate */
ior->io_residual = 0;
ior->io_error = 0;
ior->io_done = device_pager_data_write_done;
ior->io_reply_port = IP_NULL;
result = (*device->dev_ops->d_write)(device->dev_number, ior);
if (result != D_IO_QUEUED) {
device_write_dealloc(ior);
io_req_free((vm_offset_t)ior);
mach_device_deallocate(device);
}
return (KERN_SUCCESS);
}
boolean_t device_pager_data_write_done(ior)
register io_req_t ior;
{
device_write_dealloc(ior);
mach_device_deallocate(ior->io_device);
return (TRUE);
}
kern_return_t device_pager_copy(
ipc_port_t pager,
ipc_port_t pager_request,
register vm_offset_t offset,
register vm_size_t length,
ipc_port_t new_pager)
{
panic("(device_pager)copy: called");
}
kern_return_t
device_pager_supply_completed(
ipc_port_t pager,
ipc_port_t pager_request,
vm_offset_t offset,
vm_size_t length,
kern_return_t result,
vm_offset_t error_offset)
{
panic("(device_pager)supply_completed: called");
}
kern_return_t
device_pager_data_return(
ipc_port_t pager,
ipc_port_t pager_request,
vm_offset_t offset,
register pointer_t addr,
vm_size_t data_cnt,
boolean_t dirty,
boolean_t kernel_copy)
{
panic("(device_pager)data_return: called");
}
kern_return_t
device_pager_change_completed(
ipc_port_t pager,
boolean_t may_cache,
memory_object_copy_strategy_t copy_strategy)
{
panic("(device_pager)change_completed: called");
}
/*
* The mapping function takes a byte offset, but returns
* a machine-dependent page frame number. We convert
* that into something that the pmap module will
* accept later.
*/
vm_offset_t device_map_page(
void *dsp,
vm_offset_t offset)
{
register dev_pager_t ds = (dev_pager_t) dsp;
return pmap_phys_address(
(*(ds->device->dev_ops->d_mmap))
(ds->device->dev_number, offset, ds->prot));
}
kern_return_t device_pager_init_pager(
ipc_port_t pager,
ipc_port_t pager_request,
ipc_port_t pager_name,
vm_size_t pager_page_size)
{
register dev_pager_t ds;
if (device_pager_debug)
printf("(device_pager)init: pager=%p, request=%p, name=%p\n",
pager, pager_request, pager_name);
assert(pager_page_size == PAGE_SIZE);
assert(IP_VALID(pager_request));
assert(IP_VALID(pager_name));
ds = dev_pager_hash_lookup(pager);
assert(ds != DEV_PAGER_NULL);
assert(ds->client_count == 0);
assert(ds->pager_request == IP_NULL);
assert(ds->pager_name == IP_NULL);
ds->client_count = 1;
/*
* We save the send rights for the request and name ports.
*/
ds->pager_request = pager_request;
ds->pager_name = pager_name;
if (ds->type == CHAR_PAGER_TYPE) {
/*
* Reply that the object is ready
*/
(void) r_memory_object_set_attributes(pager_request,
TRUE, /* ready */
FALSE, /* do not cache */
MEMORY_OBJECT_COPY_NONE);
} else {
(void) r_memory_object_set_attributes(pager_request,
TRUE, /* ready */
TRUE, /* cache */
MEMORY_OBJECT_COPY_DELAY);
}
dev_pager_deallocate(ds);
return (KERN_SUCCESS);
}
kern_return_t device_pager_terminate(
ipc_port_t pager,
ipc_port_t pager_request,
ipc_port_t pager_name)
{
register dev_pager_t ds;
assert(IP_VALID(pager_request));
assert(IP_VALID(pager_name));
ds = dev_pager_hash_lookup(pager);
assert(ds != DEV_PAGER_NULL);
assert(ds->client_count == 1);
assert(ds->pager_request == pager_request);
assert(ds->pager_name == pager_name);
dev_pager_hash_delete(ds->pager);
dev_pager_hash_delete((ipc_port_t)ds->device); /* HACK */
mach_device_deallocate(ds->device);
/* release the send rights we have saved from the init call */
ipc_port_release_send(pager_request);
ipc_port_release_send(pager_name);
/* release the naked receive rights we just acquired */
ipc_port_release_receive(pager_request);
ipc_port_release_receive(pager_name);
/* release the kernel's receive right for the pager port */
ipc_port_dealloc_kernel(pager);
/* once for ref from lookup, once to make it go away */
dev_pager_deallocate(ds);
dev_pager_deallocate(ds);
return (KERN_SUCCESS);
}
kern_return_t device_pager_data_unlock(
ipc_port_t memory_object,
ipc_port_t memory_control_port,
vm_offset_t offset,
vm_size_t length,
vm_prot_t desired_access)
{
#ifdef lint
memory_object++; memory_control_port++; offset++; length++; desired_access++;
#endif /* lint */
panic("(device_pager)data_unlock: called");
return (KERN_FAILURE);
}
kern_return_t device_pager_lock_completed(
ipc_port_t memory_object,
ipc_port_t pager_request_port,
vm_offset_t offset,
vm_size_t length)
{
#ifdef lint
memory_object++; pager_request_port++; offset++; length++;
#endif /* lint */
panic("(device_pager)lock_completed: called");
return (KERN_FAILURE);
}
void device_pager_init(void)
{
register vm_size_t size;
/*
* Initialize cache of paging structures.
*/
size = sizeof(struct dev_pager);
kmem_cache_init(&dev_pager_cache, "dev_pager", size, 0,
NULL, NULL, NULL, 0);
/*
* Initialize the name port hashing stuff.
*/
dev_pager_hash_init();
}
|