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
|
/*
* Mach Operating System
* Copyright (c) 1993,1992,1991,1990 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.
*/
/*
* Code to manipulate IO permission bitmaps.
*/
#include <string.h>
#include <mach/boolean.h>
#include <mach/kern_return.h>
#include <ipc/ipc_port.h>
#include <kern/kalloc.h>
#include <kern/lock.h>
#include <kern/queue.h>
#include <kern/thread.h>
#include <device/dev_hdr.h>
#include "io_port.h"
#include "iopb.h"
#include "seg.h"
#include "gdt.h"
#include "vm_param.h"
/*
* A set of ports for an IO device.
*/
struct io_port {
mach_device_t device; /* Mach device */
queue_chain_t dev_list; /* link in device list */
queue_chain_t io_use_list; /* List of threads that use it */
io_reg_t *io_port_list; /* list of IO ports that use it */
/* list ends with IO_REG_NULL */
};
typedef struct io_port *io_port_t;
/*
* Lookup table for device -> io_port mapping
* (a linked list - I don't expect too many)
*/
queue_head_t device_to_io_port_list;
/*
* Cross-reference:
* all threads that have IO ports mapped
* all IO ports that have threads mapped
*/
struct io_use {
queue_chain_t psq; /* Links from port set */
queue_chain_t tsq; /* links from tss */
io_port_t ps; /* Port set */
iopb_tss_t ts; /* Task segment */
};
typedef struct io_use *io_use_t;
/*
* Big lock for the whole mess.
*/
decl_simple_lock_data(, iopb_lock)
/*
* Initialize the package.
*/
void
iopb_init(void)
{
queue_init(&device_to_io_port_list);
simple_lock_init(&iopb_lock);
}
/*
* Initialize bitmap (set all bits to OFF == 1)
*/
void
io_bitmap_init(
isa_iopb bp,
boolean_t on_off)
{
register unsigned char *b = bp;
register int s;
unsigned char c;
/*
* Disallow access to ports 0x00 .. 0xff
*/
for (s = 0; s < (0xff+1)/8; s++) {
*b++ = ~0; /* no access */
}
if (on_off)
c = 0;
else
c = ~0;
for (; s < sizeof(isa_iopb); s++) {
*b++ = c;
}
}
/*
* Set selected bits in bitmap to ON == 0
*/
void
io_bitmap_set(
isa_iopb bp,
io_reg_t *bit_list)
{
io_reg_t io_bit;
while ((io_bit = *bit_list++) != IO_REG_NULL) {
bp[io_bit>>3] &= ~(1 << (io_bit & 0x7));
}
}
/*
* Set selected bits in bitmap to OFF == 1
*/
void
io_bitmap_clear(
isa_iopb bp,
io_reg_t *bit_list)
{
io_reg_t io_bit;
while ((io_bit = *bit_list++) != IO_REG_NULL) {
bp[io_bit>>3] |= (1 << (io_bit & 0x7));
}
}
/*
* Lookup an io-port set by device
*/
io_port_t
device_to_io_port_lookup(
mach_device_t device)
{
register io_port_t io_port;
queue_iterate(&device_to_io_port_list, io_port, io_port_t, dev_list) {
if (io_port->device == device) {
return io_port;
}
}
return 0;
}
/*
* [exported]
* Create an io_port set
*/
void
io_port_create(
mach_device_t device,
io_reg_t *io_port_list)
{
register io_port_t io_port;
io_port = (io_port_t) kalloc(sizeof(struct io_port));
simple_lock(&iopb_lock);
if (device_to_io_port_lookup(device) != 0) {
simple_unlock(&iopb_lock);
kfree((vm_offset_t) io_port, sizeof(struct io_port));
return;
}
io_port->device = device;
queue_init(&io_port->io_use_list);
io_port->io_port_list = io_port_list;
/*
* Enter in lookup list.
*/
queue_enter(&device_to_io_port_list, io_port, io_port_t, dev_list);
simple_unlock(&iopb_lock);
}
/*
* [exported]
* Destroy an io port set, removing any IO mappings.
*/
void
io_port_destroy(
mach_device_t device)
{
io_port_t io_port;
io_use_t iu;
simple_lock(&iopb_lock);
io_port = device_to_io_port_lookup(device);
if (io_port == 0) {
simple_unlock(&iopb_lock);
return;
}
queue_iterate(&io_port->io_use_list, iu, io_use_t, psq) {
iopb_tss_t io_tss;
io_tss = iu->ts;
io_bitmap_clear(io_tss->bitmap, io_port->io_port_list);
queue_remove(&io_tss->io_port_list, iu, io_use_t, tsq);
}
queue_remove(&device_to_io_port_list, io_port, io_port_t, dev_list);
simple_unlock(&iopb_lock);
while (!queue_empty(&io_port->io_use_list)) {
iu = (io_use_t) queue_first(&io_port->io_use_list);
queue_remove(&io_port->io_use_list, iu, io_use_t, psq);
kfree((vm_offset_t)iu, sizeof(struct io_use));
}
kfree((vm_offset_t)io_port, sizeof(struct io_port));
}
/*
* Initialize an IO TSS.
*/
void
io_tss_init(
iopb_tss_t io_tss,
boolean_t access_all) /* allow access or not */
{
vm_offset_t addr = kvtolin (io_tss);
vm_size_t limit = (char *)&io_tss->barrier - (char *)io_tss;
memset(&io_tss->tss, 0, sizeof(struct i386_tss));
io_tss->tss.io_bit_map_offset
= (char *)&io_tss->bitmap - (char *)io_tss;
io_tss->tss.ss0 = KERNEL_DS;
io_bitmap_init(io_tss->bitmap, access_all);
io_tss->barrier = ~0;
queue_init(&io_tss->io_port_list);
io_tss->iopb_desc[0] = (limit & 0xffff)
| ((addr & 0xffff) << 16);
io_tss->iopb_desc[1] = ((addr & 0x00ff0000) >> 16)
| ((ACC_TSS|ACC_PL_K|ACC_P) << 8)
| (limit & 0x000f0000)
| (addr & 0xff000000);
}
/*
* [exported]
* Create an IOPB_TSS
*/
iopb_tss_t
iopb_create(void)
{
register iopb_tss_t ts;
ts = (iopb_tss_t) kalloc(sizeof (struct iopb_tss));
io_tss_init(ts, TRUE); /* XXX */
return ts;
}
/*
* [exported]
* Destroy an IOPB_TSS
*/
void
iopb_destroy(
iopb_tss_t io_tss)
{
io_use_t iu;
io_port_t io_port;
simple_lock(&iopb_lock);
queue_iterate(&io_tss->io_port_list, iu, io_use_t, tsq) {
io_port = iu->ps;
/* skip bitmap clear - entire bitmap will vanish */
queue_remove(&io_port->io_use_list, iu, io_use_t, psq);
}
simple_unlock(&iopb_lock);
while (!queue_empty(&io_tss->io_port_list)) {
iu = (io_use_t) queue_first(&io_tss->io_port_list);
queue_remove(&io_tss->io_port_list, iu, io_use_t, tsq);
kfree((vm_offset_t)iu, sizeof(struct io_use));
}
kfree((vm_offset_t)io_tss, sizeof(struct iopb_tss));
}
/*
* Add an IO mapping to a thread.
*/
kern_return_t
i386_io_port_add(
thread_t thread,
mach_device_t device)
{
pcb_t pcb;
iopb_tss_t io_tss, new_io_tss;
io_port_t io_port;
io_use_t iu, old_iu;
if (thread == THREAD_NULL
|| device == DEVICE_NULL)
return KERN_INVALID_ARGUMENT;
pcb = thread->pcb;
new_io_tss = 0;
iu = (io_use_t) kalloc(sizeof(struct io_use));
Retry:
simple_lock(&iopb_lock);
/* find the io_port_t for the device */
io_port = device_to_io_port_lookup(device);
if (io_port == 0) {
/*
* Device does not have IO ports available.
*/
simple_unlock(&iopb_lock);
if (new_io_tss)
kfree((vm_offset_t)new_io_tss, sizeof(struct iopb_tss));
kfree((vm_offset_t) iu, sizeof(struct io_use));
return KERN_INVALID_ARGUMENT;
}
/* Have the IO port. */
/* Make sure the thread has a TSS. */
simple_lock(&pcb->lock);
io_tss = pcb->ims.io_tss;
if (io_tss == 0) {
if (new_io_tss == 0) {
/*
* Allocate an IO-tss.
*/
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
new_io_tss = (iopb_tss_t) kalloc(sizeof(struct iopb_tss));
io_tss_init(new_io_tss, TRUE); /* XXX */
goto Retry;
}
io_tss = new_io_tss;
pcb->ims.io_tss = io_tss;
new_io_tss = 0;
}
/*
* Have io_port and io_tss.
* See whether device is already mapped.
*/
queue_iterate(&io_tss->io_port_list, old_iu, io_use_t, tsq) {
if (old_iu->ps == io_port) {
/*
* Already mapped.
*/
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
kfree((vm_offset_t)iu, sizeof(struct io_use));
if (new_io_tss)
kfree((vm_offset_t)new_io_tss, sizeof(struct iopb_tss));
return KERN_SUCCESS;
}
}
/*
* Add mapping.
*/
iu->ps = io_port;
iu->ts = io_tss;
queue_enter(&io_port->io_use_list, iu, io_use_t, psq);
queue_enter(&io_tss->io_port_list, iu, io_use_t, tsq);
io_bitmap_set(io_tss->bitmap, io_port->io_port_list);
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
if (new_io_tss)
kfree((vm_offset_t)new_io_tss, sizeof(struct iopb_tss));
return KERN_SUCCESS;
}
/*
* Remove an IO mapping from a thread.
*/
kern_return_t
i386_io_port_remove(thread, device)
thread_t thread;
mach_device_t device;
{
pcb_t pcb;
iopb_tss_t io_tss;
io_port_t io_port;
io_use_t iu;
if (thread == THREAD_NULL
|| device == DEVICE_NULL)
return KERN_INVALID_ARGUMENT;
pcb = thread->pcb;
simple_lock(&iopb_lock);
/* find the io_port_t for the device */
io_port = device_to_io_port_lookup(device);
if (io_port == 0) {
/*
* Device does not have IO ports available.
*/
simple_unlock(&iopb_lock);
return KERN_INVALID_ARGUMENT;
}
simple_lock(&pcb->lock);
io_tss = pcb->ims.io_tss;
if (io_tss == 0) {
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
return KERN_INVALID_ARGUMENT; /* not mapped */
}
/*
* Find the mapping.
*/
queue_iterate(&io_tss->io_port_list, iu, io_use_t, tsq) {
if (iu->ps == io_port) {
/*
* Found mapping. Remove it.
*/
io_bitmap_clear(io_tss->bitmap, io_port->io_port_list);
queue_remove(&io_port->io_use_list, iu, io_use_t, psq);
queue_remove(&io_tss->io_port_list, iu, io_use_t, tsq);
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
kfree((vm_offset_t)iu, sizeof(struct io_use));
return KERN_SUCCESS;
}
}
/*
* No mapping.
*/
return KERN_INVALID_ARGUMENT;
}
/*
* Return the IO ports mapped into a thread.
*/
extern ipc_port_t mach_convert_device_to_port(/* device_t */);
kern_return_t
i386_io_port_list(thread, list, list_count)
thread_t thread;
mach_device_t **list;
unsigned int *list_count;
{
register pcb_t pcb;
register iopb_tss_t io_tss;
unsigned int count, alloc_count;
mach_device_t *devices;
vm_size_t size_needed, size;
vm_offset_t addr;
int i;
if (thread == THREAD_NULL)
return KERN_INVALID_ARGUMENT;
pcb = thread->pcb;
alloc_count = 16; /* a guess */
do {
size_needed = alloc_count * sizeof(ipc_port_t);
if (size_needed <= size)
break;
if (size != 0)
kfree(addr,size);
assert(size_needed > 0);
size = size_needed;
addr = kalloc(size);
if (addr == 0)
return KERN_RESOURCE_SHORTAGE;
devices = (mach_device_t *)addr;
count = 0;
simple_lock(&iopb_lock);
simple_lock(&pcb->lock);
io_tss = pcb->ims.io_tss;
if (io_tss != 0) {
register io_use_t iu;
queue_iterate(&io_tss->io_port_list, iu, io_use_t, tsq) {
if (++count < alloc_count) {
*devices = iu->ps->device;
device_reference(*devices);
devices++;
}
}
}
simple_unlock(&pcb->lock);
simple_unlock(&iopb_lock);
} while (count > alloc_count);
if (count == 0) {
/*
* No IO ports
*/
*list = 0;
*list_count = 0;
if (size != 0)
kfree(addr, size);
}
else {
/*
* If we allocated too much, must copy.
*/
size_needed = count * sizeof(ipc_port_t);
if (size_needed < size) {
vm_offset_t new_addr;
new_addr = kalloc(size_needed);
if (new_addr == 0) {
for (i = 0; i < count; i++)
device_deallocate(devices[i]);
kfree(addr, size);
return KERN_RESOURCE_SHORTAGE;
}
memcpy((void *)new_addr, (void *)addr, size_needed);
kfree(addr, size);
devices = (mach_device_t *)new_addr;
}
for (i = 0; i < count; i++)
((ipc_port_t *)devices)[i] =
mach_convert_device_to_port(devices[i]);
}
*list = devices;
*list_count = count;
return KERN_SUCCESS;
}
/*
* Check whether an IO device is mapped to a particular thread.
* Used to support the 'iopl' device automatic mapping.
*/
boolean_t
iopb_check_mapping(thread, device)
thread_t thread;
mach_device_t device;
{
pcb_t pcb;
io_port_t io_port;
io_use_t iu;
pcb = thread->pcb;
simple_lock(&iopb_lock);
/* Find the io port for the device */
io_port = device_to_io_port_lookup(device);
if (io_port == 0) {
simple_unlock(&iopb_lock);
return FALSE;
}
/* Look up the mapping in the device`s mapping list. */
queue_iterate(&io_port->io_use_list, iu, io_use_t, psq) {
if (iu->ts == pcb->ims.io_tss) {
/*
* Device is mapped.
*/
simple_unlock(&iopb_lock);
return TRUE;
}
}
simple_unlock(&iopb_lock);
return FALSE;
}
|