summaryrefslogtreecommitdiff
path: root/debian/patches/45_io_per_task.patch
blob: aac4c177b6dd7c02444a469406b51a827de1d601 (plain)
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
2006-01-02  Samuel Thibault <samuel.thibault@ens-lyon.org>

	* i386/i386/iopb.c: IO ports permissions are now task-based.
	(i386_io_port_add): Fix TSS access and locking accordingly.
	(i386_io_port_remove): Likewise.
	(i386_io_port_list): Likewise.
	(iopb_check_mapping): Likewise.
	* i386/i386/pcb.c (switch_ktss): Now takes next thread as parameter
	for TSS switch to be task-based.  Fix all callers.
	(switch_context): Likewise.
	(pcb_module_init): Move iopb initialization to ...
	(machine_task_module_init): ... here.  New function.
	(pcb_terminate): Move iopb termination to ...
	(machine_task_terminate): ... here.  New function.
	(machine_task_init): New function.
	(machine_task_collect): Likewise.
	(thread_setstatus): TSS is now task-based, fix TSS and locking
	accordingly.
	(thread_getstatus): Likewise.
	* i386/i386/thread.h (i386_machine_state): Move io_tss member to ...
	(machine_task): ... here.  New structure.

	* kern/task.c (task_init): Call new machine_task_module_init() function.
	(task_create): Call new machine_task_init() function.
	(task_deallocate): Call new machine_task_terminate() function.
	(task_collect_scan): Call new machine_task_collect() function.
	* kern/task.h: Include <machine/thread.h> for machine_task_t.
	(task): Add new machine member.


Index: i386/i386/iopb.c
===================================================================
--- i386/i386/iopb.c.orig	2006-11-14 04:16:04.000000000 +0200
+++ i386/i386/iopb.c	2006-11-14 04:16:29.000000000 +0200
@@ -67,8 +67,8 @@ queue_head_t	device_to_io_port_list;
 
 /*
  * Cross-reference:
- *	all threads that have IO ports mapped
- *	all IO ports that have threads mapped
+ *	all tasks that have IO ports mapped
+ *	all IO ports that have tasks mapped
  */
 struct io_use {
 	queue_chain_t	psq;	/* Links from port set */
@@ -308,7 +308,7 @@ iopb_destroy(
 }
 
 /*
- * Add an IO mapping to a thread.
+ * Add an IO mapping to a task.
  */
 #ifdef i386
 kern_return_t
@@ -322,7 +322,7 @@ i386_io_port_add(
 	mach_device_t	device)
 #endif
 {
-	pcb_t		pcb;
+	task_t		task;
 #ifdef i386
 	mach_device_t	device = d->emul_data;
 #endif
@@ -334,7 +334,7 @@ i386_io_port_add(
 	 || device == DEVICE_NULL)
 	    return KERN_INVALID_ARGUMENT;
 
-	pcb = thread->pcb;
+	task = thread->task;
 
 	new_io_tss = 0;
 	iu = (io_use_t) kalloc(sizeof(struct io_use));
@@ -357,16 +357,16 @@ i386_io_port_add(
 
 	/* Have the IO port. */
 
-	/* Make sure the thread has a TSS. */
+	/* Make sure the task has a TSS. */
 
-	simple_lock(&pcb->lock);
-	io_tss = pcb->ims.io_tss;
+	task_lock(task);
+	io_tss = task->machine.io_tss;
 	if (io_tss == 0) {
 	    if (new_io_tss == 0) {
 		/*
 		 * Allocate an IO-tss.
 		 */
-		simple_unlock(&pcb->lock);
+		task_unlock(task);
 		simple_unlock(&iopb_lock);
 
 		new_io_tss = (iopb_tss_t) kalloc(sizeof(struct iopb_tss));
@@ -375,7 +375,12 @@ i386_io_port_add(
 		goto Retry;
 	    }
 	    io_tss = new_io_tss;
-	    pcb->ims.io_tss = io_tss;
+	    task->machine.io_tss = io_tss;
+
+	    /* Update hardware if needed.  */
+	    if (task == current_thread()->task)
+		switch_ktss(thread);
+
 	    new_io_tss = 0;
 	}
 
@@ -388,7 +393,7 @@ i386_io_port_add(
 		/*
 		 * Already mapped.
 		 */
-		simple_unlock(&pcb->lock);
+		task_unlock(task);
 		simple_unlock(&iopb_lock);
 
 		kfree((vm_offset_t)iu, sizeof(struct io_use));
@@ -407,7 +412,7 @@ i386_io_port_add(
 	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);
+	task_unlock(task);
 	simple_unlock(&iopb_lock);
 
 	if (new_io_tss)
@@ -417,7 +422,7 @@ i386_io_port_add(
 }
 
 /*
- * Remove an IO mapping from a thread.
+ * Remove an IO mapping from a task.
  */
 #ifdef i386
 kern_return_t
@@ -431,7 +436,7 @@ i386_io_port_remove(thread, device)
 	mach_device_t	device;
 #endif
 {
-	pcb_t		pcb;
+	task_t		task;
 #ifdef i386
 	mach_device_t	device = d->emul_data;
 #endif
@@ -443,7 +448,7 @@ i386_io_port_remove(thread, device)
 	 || device == DEVICE_NULL)
 	    return KERN_INVALID_ARGUMENT;
 
-	pcb = thread->pcb;
+	task = thread->task;
 
 	simple_lock(&iopb_lock);
 
@@ -458,10 +463,10 @@ i386_io_port_remove(thread, device)
 	    return KERN_INVALID_ARGUMENT;
 	}
 
-	simple_lock(&pcb->lock);
-	io_tss = pcb->ims.io_tss;
+	task_lock(task);
+	io_tss = task->machine.io_tss;
 	if (io_tss == 0) {
-	    simple_unlock(&pcb->lock);
+	    task_unlock(task);
 	    simple_unlock(&iopb_lock);
 	    return KERN_INVALID_ARGUMENT;	/* not mapped */
 	}
@@ -479,7 +484,7 @@ i386_io_port_remove(thread, device)
 		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);
+		task_unlock(task);
 		simple_unlock(&iopb_lock);
 
 		kfree((vm_offset_t)iu, sizeof(struct io_use));
@@ -488,6 +493,9 @@ i386_io_port_remove(thread, device)
 	    }
 	}
 
+	task_unlock(task);
+	simple_unlock(&iopb_lock);
+
 	/*
 	 * No mapping.
 	 */
@@ -499,7 +507,7 @@ i386_io_port_remove(thread, device)
 }
 
 /*
- * Return the IO ports mapped into a thread.
+ * Return the IO ports mapped into a task.
  */
 extern ipc_port_t	mach_convert_device_to_port(/* device_t */);
 
@@ -509,7 +517,7 @@ i386_io_port_list(thread, list, list_cou
 	mach_device_t	**list;
 	unsigned int	*list_count;
 {
-	register pcb_t	pcb;
+	task_t		task;
 	register iopb_tss_t io_tss;
 	unsigned int	count, alloc_count;
 	mach_device_t	*devices;
@@ -520,7 +528,7 @@ i386_io_port_list(thread, list, list_cou
 	if (thread == THREAD_NULL)
 	    return KERN_INVALID_ARGUMENT;
 
-	pcb = thread->pcb;
+	task = thread->task;
 
 	alloc_count = 16;		/* a guess */
 
@@ -543,8 +551,8 @@ i386_io_port_list(thread, list, list_cou
 	    count = 0;
 
 	    simple_lock(&iopb_lock);
-	    simple_lock(&pcb->lock);
-	    io_tss = pcb->ims.io_tss;
+	    task_lock(task);
+	    io_tss = task->machine.io_tss;
 	    if (io_tss != 0) {
 		register io_use_t iu;
 
@@ -556,7 +564,7 @@ i386_io_port_list(thread, list, list_cou
 		    }
 		}
 	    }
-	    simple_unlock(&pcb->lock);
+	    task_unlock(task);
 	    simple_unlock(&iopb_lock);
 	} while (count > alloc_count);
 
@@ -602,7 +610,7 @@ i386_io_port_list(thread, list, list_cou
 }
 
 /*
- * Check whether an IO device is mapped to a particular thread.
+ * Check whether an IO device is mapped to a particular task.
  * Used to support the 'iopl' device automatic mapping.
  */
 boolean_t
@@ -610,11 +618,11 @@ iopb_check_mapping(thread, device)
 	thread_t	thread;
 	mach_device_t	device;
 {
-	pcb_t		pcb;
+	task_t		task;
 	io_port_t	io_port;
 	io_use_t	iu;
 
-	pcb = thread->pcb;
+	task = thread->task;
 
 	simple_lock(&iopb_lock);
 
@@ -628,15 +636,18 @@ iopb_check_mapping(thread, device)
 
 	/* Look up the mapping in the device`s mapping list. */
 
+	task_lock(task);
 	queue_iterate(&io_port->io_use_list, iu, io_use_t, psq) {
-	    if (iu->ts == pcb->ims.io_tss) {
+	    if (iu->ts == task->machine.io_tss) {
 		/*
 		 * Device is mapped.
 		 */
+		task_unlock(task);
 		simple_unlock(&iopb_lock);
 		return TRUE;
 	    }
 	}
+	task_unlock(task);
 	simple_unlock(&iopb_lock);
 	return FALSE;
 }
Index: i386/i386/pcb.c
===================================================================
--- i386/i386/pcb.c.orig	2006-11-14 03:58:56.000000000 +0200
+++ i386/i386/pcb.c	2006-11-14 04:20:29.000000000 +0200
@@ -132,12 +132,13 @@ vm_offset_t stack_detach(thread)
 #define	gdt_desc_p(mycpu,sel) \
 	((struct real_descriptor *)&curr_gdt(mycpu)[sel_idx(sel)])
 
-void switch_ktss(pcb)
-	register pcb_t	pcb;
+void switch_ktss(thread)
+	register thread_t	thread;
 {
 	int			mycpu = cpu_number();
+	register pcb_t		pcb = thread->pcb;
     {
-	register iopb_tss_t	tss = pcb->ims.io_tss;
+	register iopb_tss_t	tss = thread->task->machine.io_tss;
 	vm_offset_t		pcb_stack_top;
 
 	/*
@@ -242,7 +243,7 @@ void stack_handoff(old, new)
 	/*
 	 *	Load the rest of the user state for the new thread
 	 */
-	switch_ktss(new->pcb);
+	switch_ktss(new);
 
 	/*
 	 *	Switch to new thread
@@ -267,7 +268,7 @@ void stack_handoff(old, new)
 void load_context(new)
 	register thread_t	new;
 {
-	switch_ktss(new->pcb);
+	switch_ktss(new);
 	Load_context(new);
 }
 
@@ -304,7 +305,7 @@ thread_t switch_context(old, continuatio
 	/*
 	 *	Load the rest of the user state for the new thread
 	 */
-	switch_ktss(new->pcb);
+	switch_ktss(new);
 
 	return Switch_context(old, continuation, new);
 }
@@ -317,7 +318,6 @@ void pcb_module_init()
 			 0, "i386 pcb state");
 
 	fpu_module_init();
-	iopb_init();
 }
 
 void pcb_init(thread)
@@ -361,8 +361,6 @@ void pcb_terminate(thread)
 	counter(if (--c_threads_current < c_threads_min)
 			c_threads_min = c_threads_current);
 
-	if (pcb->ims.io_tss != 0)
-		iopb_destroy(pcb->ims.io_tss);
 	if (pcb->ims.ifps != 0)
 		fp_free(pcb->ims.ifps);
 	if (pcb->ims.ldt != 0)
@@ -382,6 +380,30 @@ void pcb_collect(thread)
 {
 }
 
+void machine_task_module_init(void)
+{
+	iopb_init();
+}
+
+void machine_task_init(new_task)
+	task_t new_task;
+{
+	new_task->machine.io_tss = 0;
+}
+
+void machine_task_terminate(task)
+	task_t task;
+{
+	if (task->machine.io_tss != 0)
+		iopb_destroy(task->machine.io_tss);
+}
+
+void machine_task_collect(task)
+	task_t task;
+{
+	/* TODO: compare io_tss with 0xff, XXX: not if it is still in use, and
+	 * beware of races with threads adding io perms!  */
+}
 
 /*
  *	thread_setstatus:
@@ -516,28 +538,40 @@ kern_return_t thread_setstatus(thread, f
 	     */
 	    case i386_ISA_PORT_MAP_STATE: {
 		register struct i386_isa_port_map_state *state;
-		register iopb_tss_t	tss;
+		register iopb_tss_t	tss, old_tss;
+		task_t			task;
 
 		if (count < i386_ISA_PORT_MAP_STATE_COUNT)
 			return(KERN_INVALID_ARGUMENT);
 
-#if 0
 		/*
 		 *	If the thread has no ktss yet,
 		 *	we must allocate one.
 		 */
 
 		state = (struct i386_isa_port_map_state *) tstate;
-		tss = thread->pcb->ims.io_tss;
+		task = thread->task;
+		task_lock(task);
+		tss = task->machine.io_tss;
 		if (tss == 0) {
+			task_unlock(task);
 			tss = iopb_create();
-			thread->pcb->ims.io_tss = tss;
+			task_lock(task);
+			old_tss = task->machine.io_tss;
+			if (old_tss == 0) {
+				task->machine.io_tss = tss;
+			} else {
+				task_unlock(task);
+				iopb_destroy(tss);
+				tss = old_tss;
+				task_lock(task);
+			}
 		}
 
 		memcpy(tss->bitmap,
 		       state->pm,
 		       sizeof state->pm);
-#endif
+		task_unlock(task);
 		break;
 	    }
 
@@ -674,16 +708,21 @@ kern_return_t thread_getstatus(thread, f
 	    case i386_ISA_PORT_MAP_STATE: {
 		register struct i386_isa_port_map_state *state;
 		register iopb_tss_t tss;
+		task_t task;
 
 		if (*count < i386_ISA_PORT_MAP_STATE_COUNT)
 			return(KERN_INVALID_ARGUMENT);
 
 		state = (struct i386_isa_port_map_state *) tstate;
-		tss = thread->pcb->ims.io_tss;
+		task = thread->task;
+		task_lock(task);
+		tss = task->machine.io_tss;
 
 		if (tss == 0) {
 		    int i;
 
+		    task_unlock(task);
+
 		    /*
 		     *	The thread has no ktss, so no IO permissions.
 		     */
@@ -698,6 +737,7 @@ kern_return_t thread_getstatus(thread, f
 		    memcpy(state->pm,
 			   tss->bitmap,
 			   sizeof state->pm);
+		    task_unlock(task);
 		}
 
 		*count = i386_ISA_PORT_MAP_STATE_COUNT;
Index: i386/i386/thread.h
===================================================================
--- i386/i386/thread.h.orig	2006-11-14 03:58:56.000000000 +0200
+++ i386/i386/thread.h	2006-11-14 04:21:24.000000000 +0200
@@ -158,13 +158,16 @@ struct i386_interrupt_state {
  */
 
 struct i386_machine_state {
-	iopb_tss_t		io_tss;
 	struct user_ldt	*	ldt;
 	struct i386_fpsave_state *ifps;
 	struct v86_assist_state	v86s;
 	struct real_descriptor user_gdt[USER_GDT_SLOTS];
 };
 
+typedef struct machine_task {
+	iopb_tss_t		io_tss;
+} machine_task_t;
+
 typedef struct pcb {
 	struct i386_interrupt_state iis[2];	/* interrupt and NMI */
 	struct i386_saved_state iss;
Index: i386/i386/user_ldt.c
===================================================================
--- i386/i386/user_ldt.c.orig	2006-11-14 03:58:56.000000000 +0200
+++ i386/i386/user_ldt.c	2006-11-14 04:16:29.000000000 +0200
@@ -249,7 +249,7 @@ i386_set_ldt(thread, first_selector, des
 	     * make sure it is properly set.
 	     */
 	    if (thread == current_thread())
-	        switch_ktss(pcb);
+	        switch_ktss(thread);
 	}
 
 	/*
Index: kern/task.c
===================================================================
--- kern/task.c.orig	2006-11-14 03:58:57.000000000 +0200
+++ kern/task.c	2006-11-14 04:16:29.000000000 +0200
@@ -67,6 +67,7 @@ void task_init(void)
 			0, "tasks");
 
 	eml_init();
+	machine_task_module_init ();
 
 	/*
 	 * Create the kernel task as the first task.
@@ -145,6 +146,7 @@ kern_return_t task_create(
 	eml_task_reference(new_task, parent_task);
 
 	ipc_task_init(new_task, parent_task);
+	machine_task_init(new_task);
 
 	new_task->total_user_time.seconds = 0;
 	new_task->total_user_time.microseconds = 0;
@@ -220,6 +222,8 @@ void task_deallocate(
 	if (c != 0)
 		return;
 
+	machine_task_terminate (task);
+
 	eml_task_deallocate(task);
 
 	pset = task->processor_set;
@@ -1092,6 +1096,7 @@ void task_collect_scan(void)
 			pset_unlock(pset);
 			simple_unlock(&all_psets_lock);
 
+			machine_task_collect (task);
 			pmap_collect(task->map->pmap);
 
 			if (prev_task != TASK_NULL)
Index: kern/task.h
===================================================================
--- kern/task.h.orig	2006-10-15 20:39:24.000000000 +0300
+++ kern/task.h	2006-11-14 04:16:29.000000000 +0200
@@ -45,6 +45,7 @@
 #include <kern/pc_sample.h>
 #include <kern/processor.h>
 #include <kern/syscall_emulation.h>
+#include <machine/thread.h>
 #include <vm/vm_map.h>
 
 struct task {
@@ -98,6 +99,9 @@ struct task {
 	vm_offset_t	fast_tas_base[TASK_FAST_TAS_NRAS];
 	vm_offset_t	fast_tas_end[TASK_FAST_TAS_NRAS];
 #endif	/* FAST_TAS */
+
+	/* Hardware specific data.  */
+	machine_task_t	machine;
 };
 
 #define task_lock(task)		simple_lock(&(task)->lock)