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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
|
/*
* Mach Operating System
* Copyright (c) 1993,1991,1990,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
*/
/*
* Mach device server routines (i386at version).
*
* Copyright (c) 1996 The University of Utah and
* the Computer Systems Laboratory at the University of Utah (CSL).
* All rights reserved.
*
* Permission to use, copy, modify and distribute this software is hereby
* granted provided that (1) source code retains these copyright, permission,
* and disclaimer notices, and (2) redistributions including binaries
* reproduce the notices in supporting documentation, and (3) all advertising
* materials mentioning features or use of this software display the following
* acknowledgement: ``This product includes software developed by the
* Computer Systems Laboratory at the University of Utah.''
*
* THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
* IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
* ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* CSL requests users of this software to return to csl-dist@cs.utah.edu any
* improvements that they make and grant CSL redistribution rights.
*
* Author: Shantanu Goel, University of Utah CSL
*/
#include <stdio.h>
#include <string.h>
#include <error.h>
#include <hurd.h>
#include <mach.h>
#include <cthreads.h>
#include "vm_param.h"
#include "device_reply_U.h"
#include "io_req.h"
#include "dev_hdr.h"
#include "util.h"
#include "queue.h"
#include "spl.h"
extern struct port_bucket *port_bucket;
extern struct port_class *dev_class;
extern struct device_emulation_ops linux_net_emulation_ops;
extern struct device_emulation_ops mach_device_emulation_ops;
#define NUM_EMULATION (sizeof (emulation_list) / sizeof (emulation_list[0]))
/* List of emulations. */
static struct device_emulation_ops *emulation_list[] =
{
&linux_net_emulation_ops,
&mach_device_emulation_ops,
};
extern kern_return_t device_intr_notify (mach_port_t master_port,
int irq, int id,
mach_port_t receive_port,
mach_msg_type_name_t receive_portPoly);
mach_device_t
device_lookup (char *name)
{
// if (strcmp (kbd_device->dev_ops->d_name, name) == 0)
// {
// mach_device_reference (kbd_device);
// return kbd_device;
// }
return NULL;
}
void
mach_device_deallocate (void *device)
{
ports_port_deref (device);
}
void
mach_device_reference (mach_device_t device)
{
ports_port_ref (device);
}
emul_device_t
mach_convert_port_to_device (device_t device)
{
mach_device_t dev = ports_lookup_port (port_bucket, device, dev_class);
if (dev == NULL)
return NULL;
return &dev->dev;
}
void *
device_to_pi (emul_device_t device)
{
return ((void *) device) - (int) &((mach_device_t) 0)->dev;
}
/*
* What follows is the interface for the native Mach devices.
*/
mach_port_t
mach_convert_device_to_port (mach_device_t device)
{
if (device == NULL)
return MACH_PORT_NULL;
// TODO I have to somehow dereference it when it is called at the first time.
return ports_get_right (device);
}
/* Implementation of device interface */
kern_return_t
ds_xxx_device_set_status (device_t device, dev_flavor_t flavor,
dev_status_t status, size_t statu_cnt)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_xxx_device_get_status (device_t device, dev_flavor_t flavor,
dev_status_t status, size_t *statuscnt)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_xxx_device_set_filter (device_t device, mach_port_t rec,
int pri, filter_array_t filt, size_t len)
{
return D_INVALID_OPERATION;
}
io_return_t
ds_device_intr_notify (mach_port_t master_port, int irq,
int id, mach_port_t receive_port)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_pci_write_config (mach_port_t master_port, char bus, char device_fn,
char where, pci_config_data_t data,
mach_msg_type_number_t dataCnt)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_pci_read_config (mach_port_t master_port, char bus, char device_fn,
char where, int bytes_wanted, pci_config_data_t result,
mach_msg_type_number_t *resultCnt)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_pci_find_device (mach_port_t master_port, short vendor, short device_id,
short index, short *bus, char *device_fn)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_pci_present (mach_port_t master_port)
{
return D_INVALID_OPERATION;
}
kern_return_t
ds_device_irq_enable (mach_port_t master_port,
int irq, char status)
{
return D_INVALID_OPERATION;
}
io_return_t
ds_device_open (mach_port_t open_port, mach_port_t reply_port,
mach_msg_type_name_t reply_port_type, dev_mode_t mode,
char *name, device_t *devp)
{
int i;
io_return_t err;
extern boolean_t is_master_device (mach_port_t port);
/* Open must be called on the master device port. */
if (!is_master_device (open_port))
return D_INVALID_OPERATION;
/* There must be a reply port. */
if (! MACH_PORT_VALID (reply_port))
{
fprintf (stderr, "ds_* invalid reply port\n");
return MIG_NO_REPLY;
}
/* Call each emulation's open routine to find the device. */
for (i = 0; i < NUM_EMULATION; i++)
{
err = (*emulation_list[i]->open) (reply_port, reply_port_type,
mode, name, devp);
if (err != D_NO_SUCH_DEVICE)
break;
}
return err;
}
io_return_t
ds_device_close (device_t dev)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
ret = (device->emul_ops->close
? (*device->emul_ops->close) (device->emul_data)
: D_SUCCESS);
mach_device_deallocate (device_to_pi (device));
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_write (device_t dev, mach_port_t reply_port,
mach_msg_type_name_t reply_port_type, dev_mode_t mode,
recnum_t recnum, io_buf_ptr_t data, unsigned int count,
int *bytes_written)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
if (data == 0)
return D_INVALID_SIZE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->write)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->write) (device->emul_data, reply_port,
reply_port_type, mode, recnum,
data, count, bytes_written);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_write_inband (device_t dev, mach_port_t reply_port,
mach_msg_type_name_t reply_port_type,
dev_mode_t mode, recnum_t recnum,
io_buf_ptr_inband_t data, unsigned count,
int *bytes_written)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
if (data == 0)
return D_INVALID_SIZE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->write_inband)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->write_inband) (device->emul_data, reply_port,
reply_port_type, mode, recnum,
data, count, bytes_written);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_read (device_t dev, mach_port_t reply_port,
mach_msg_type_name_t reply_port_type, dev_mode_t mode,
recnum_t recnum, int count, io_buf_ptr_t *data,
unsigned *bytes_read)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->read)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->read) (device->emul_data, reply_port,
reply_port_type, mode, recnum,
count, data, bytes_read);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_read_inband (device_t dev, mach_port_t reply_port,
mach_msg_type_name_t reply_port_type, dev_mode_t mode,
recnum_t recnum, int count, char *data,
unsigned *bytes_read)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->read_inband)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->read_inband) (device->emul_data, reply_port,
reply_port_type, mode, recnum,
count, data, bytes_read);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_set_status (device_t dev, dev_flavor_t flavor,
dev_status_t status, mach_msg_type_number_t status_count)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->set_status)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->set_status) (device->emul_data, flavor,
status, status_count);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_get_status (device_t dev, dev_flavor_t flavor, dev_status_t status,
mach_msg_type_number_t *status_count)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->get_status)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->get_status) (device->emul_data, flavor,
status, status_count);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_set_filter (device_t dev, mach_port_t receive_port, int priority,
filter_t *filter, unsigned filter_count)
{
emul_device_t device;
io_return_t ret;
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
device = mach_convert_port_to_device (dev);
if (! device->emul_ops->set_filter)
return D_INVALID_OPERATION;
ret = (*device->emul_ops->set_filter) (device->emul_data, receive_port,
priority, filter, filter_count);
ports_port_deref (device_to_pi (device));
return ret;
}
io_return_t
ds_device_map (device_t dev, vm_prot_t prot, vm_offset_t offset,
vm_size_t size, mach_port_t *pager, boolean_t unmap)
{
/* Refuse if device is dead or not completely open. */
if (dev == MACH_PORT_NULL)
return D_NO_SUCH_DEVICE;
return D_INVALID_OPERATION;
}
boolean_t
ds_open_done(ior)
register io_req_t ior;
{
kern_return_t result;
register mach_device_t device;
device = ior->io_device;
result = ior->io_error;
if (result != D_SUCCESS) {
/*
* Open failed. Deallocate port and device.
*/
// dev_port_remove(device);
// ipc_port_dealloc_kernel(device->port);
device_lock(device);
device->state = DEV_STATE_INIT;
if (device->io_wait) {
device->io_wait = FALSE;
// thread_wakeup((event_t)device);
}
device_unlock(device);
// mach_device_deallocate(device);
device = MACH_DEVICE_NULL;
}
else {
/*
* Open succeeded.
*/
device_lock(device);
device->state = DEV_STATE_OPEN;
device->open_count = 1;
if (device->io_wait) {
device->io_wait = FALSE;
// thread_wakeup((event_t)device);
}
device_unlock(device);
/* donate device reference to get port */
}
/*
* Must explicitly convert device to port, since
* device_reply interface is built as 'user' side
* (thus cannot get translation).
*/
if (MACH_PORT_VALID(ior->io_reply_port)) {
(void) ds_device_open_reply(ior->io_reply_port,
ior->io_reply_port_type,
result,
mach_convert_device_to_port(device));
}
// else
// mach_device_deallocate(device);
mach_device_deallocate (device);
return (TRUE);
}
static io_return_t
device_open(reply_port, reply_port_type, mode, name, device_p)
mach_port_t reply_port;
mach_msg_type_name_t reply_port_type;
dev_mode_t mode;
char * name;
device_t *device_p; /* out */
{
register mach_device_t device;
register kern_return_t result;
register io_req_t ior;
device = device_lookup (name);
if (device == NULL)
return D_NO_SUCH_DEVICE;
/*
* If the device is being opened or closed,
* wait for that operation to finish.
*/
device_lock(device);
while (device->state == DEV_STATE_OPENING ||
device->state == DEV_STATE_CLOSING) {
// device->io_wait = TRUE;
// thread_sleep((event_t)device, simple_lock_addr(device->lock), TRUE);
// device_lock(device);
device_unlock (device);
mach_device_deallocate (device);
return D_INVALID_OPERATION;
}
/*
* If the device is already open, increment the open count
* and return.
*/
if (device->state == DEV_STATE_OPEN) {
if (device->flag & D_EXCL_OPEN) {
/*
* Cannot open a second time.
*/
device_unlock(device);
mach_device_deallocate(device);
return (D_ALREADY_OPEN);
}
device->open_count++;
device_unlock(device);
// TODO I have to dereference it at the first time.
*device_p = ports_get_send_right (device);
return (D_SUCCESS);
/*
* Return deallocates device reference while acquiring
* port.
*/
}
/*
* Allocate the device port and register the device before
* opening it.
*/
device->state = DEV_STATE_OPENING;
device_unlock(device);
// dev_port_enter(device);
/*
* Open the device.
*/
io_req_alloc(ior, 0);
ior->io_device = device;
ior->io_unit = device->dev_number;
ior->io_op = IO_OPEN | IO_CALL;
ior->io_mode = mode;
ior->io_error = 0;
ior->io_done = ds_open_done;
ior->io_reply_port = reply_port;
ior->io_reply_port_type = reply_port_type;
mach_device_reference (device);
result = (*device->dev_ops->d_open)(device->dev_number, (int)mode, ior);
if (result == D_IO_QUEUED)
return (MIG_NO_REPLY);
/*
* Return result via ds_open_done.
*/
ior->io_error = result;
(void) ds_open_done(ior);
io_req_free(ior);
return (MIG_NO_REPLY); /* reply already sent */
}
static io_return_t
device_close(device)
register mach_device_t device;
{
device_lock(device);
/*
* If device will remain open, do nothing.
*/
if (--device->open_count > 0) {
device_unlock(device);
return (D_SUCCESS);
}
/*
* If device is being closed, do nothing.
*/
if (device->state == DEV_STATE_CLOSING) {
device_unlock(device);
return (D_SUCCESS);
}
/*
* Mark device as closing, to prevent new IO.
* Outstanding IO will still be in progress.
*/
device->state = DEV_STATE_CLOSING;
device_unlock(device);
/*
* ? wait for IO to end ?
* only if device wants to
*/
/*
* Remove the device-port association.
*/
// dev_port_remove(device);
// ipc_port_dealloc_kernel(device->port);
/*
* Close the device
*/
(*device->dev_ops->d_close)(device->dev_number);
/*
* Finally mark it closed. If someone else is trying
* to open it, the open can now proceed.
*/
device_lock(device);
device->state = DEV_STATE_INIT;
if (device->io_wait) {
device->io_wait = FALSE;
// thread_wakeup((event_t)device);
}
device_unlock(device);
return (D_SUCCESS);
}
boolean_t ds_read_done(ior)
io_req_t ior;
{
vm_offset_t start_data, end_data;
vm_offset_t start_sent, end_sent;
register vm_size_t size_read;
if (ior->io_error)
size_read = 0;
else
size_read = ior->io_count - ior->io_residual;
start_data = (vm_offset_t)ior->io_data;
end_data = start_data + size_read;
start_sent = (ior->io_op & IO_INBAND) ? start_data :
trunc_page(start_data);
end_sent = (ior->io_op & IO_INBAND) ?
start_data + ior->io_alloc_size : round_page(end_data);
/*
* Zero memory that the device did not fill.
*/
if (start_sent < start_data)
memset((char *)start_sent, 0, start_data - start_sent);
if (end_sent > end_data)
memset((char *)end_data, 0, end_sent - end_data);
/*
* Touch the data being returned, to mark it dirty.
* If the pages were filled by DMA, the pmap module
* may think that they are clean.
*/
{
register vm_offset_t touch;
register int c;
for (touch = start_sent; touch < end_sent; touch += PAGE_SIZE) {
c = *(volatile char *)touch;
*(volatile char *)touch = c;
}
}
/*
* Send the data to the reply port - this
* unwires and deallocates it.
*/
if (ior->io_op & IO_INBAND) {
(void)ds_device_read_reply_inband(ior->io_reply_port,
ior->io_reply_port_type,
ior->io_error,
(char *) start_data,
size_read);
} else {
// vm_map_copy_t copy;
// kern_return_t kr;
//
// kr = vm_map_copyin_page_list(kernel_map, start_data,
// size_read, TRUE, TRUE,
// ©, FALSE);
//
// if (kr != KERN_SUCCESS)
// panic("read_done: vm_map_copyin_page_list failed");
(void)ds_device_read_reply(ior->io_reply_port,
ior->io_reply_port_type,
ior->io_error,
(char *) start_data,
size_read);
}
/*
* Free any memory that was allocated but not sent.
*/
if (ior->io_count != 0) {
if (ior->io_op & IO_INBAND) {
if (ior->io_alloc_size > 0)
free (ior->io_data);
// zfree(io_inband_zone, (vm_offset_t)ior->io_data);
} else {
register vm_offset_t end_alloc;
end_alloc = start_sent + round_page(ior->io_alloc_size);
if (end_alloc > end_sent)
vm_deallocate(mach_task_self (),
end_sent,
end_alloc - end_sent);
}
}
mach_device_deallocate(ior->io_device);
return (TRUE);
}
/*
* Read from a device.
*/
static io_return_t
device_read(device, reply_port, reply_port_type, mode, recnum,
bytes_wanted, data, data_count)
mach_device_t device;
mach_port_t reply_port;
mach_msg_type_name_t reply_port_type;
dev_mode_t mode;
recnum_t recnum;
int bytes_wanted;
io_buf_ptr_t *data; /* out */
unsigned int *data_count; /* out */
{
register io_req_t ior;
register io_return_t result;
#ifdef lint
*data = *data;
*data_count = *data_count;
#endif /* lint */
if (device->state != DEV_STATE_OPEN)
return (D_NO_SUCH_DEVICE);
/* XXX note that a CLOSE may proceed at any point */
/*
* There must be a reply port.
*/
if (!MACH_PORT_VALID(reply_port)) {
printf("ds_* invalid reply port\n");
return (MIG_NO_REPLY); /* no sense in doing anything */
}
/*
* Package the read request 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 = mode;
ior->io_recnum = recnum;
ior->io_data = 0; /* driver must allocate data */
ior->io_count = bytes_wanted;
ior->io_alloc_size = 0; /* no data allocated yet */
ior->io_residual = 0;
ior->io_error = 0;
ior->io_done = ds_read_done;
ior->io_reply_port = reply_port;
ior->io_reply_port_type = reply_port_type;
/*
* The ior keeps an extra reference for the device.
*/
mach_device_reference(device);
/*
* And do the read.
*/
result = (*device->dev_ops->d_read)(device->dev_number, ior);
/*
* If the IO was queued, delay reply until it is finished.
*/
if (result == D_IO_QUEUED)
return (MIG_NO_REPLY);
/*
* Return result via ds_read_done.
*/
ior->io_error = result;
(void) ds_read_done(ior);
io_req_free(ior);
return (MIG_NO_REPLY); /* reply has already been sent. */
}
/*
* Read from a device, but return the data 'inband.'
*/
static io_return_t
device_read_inband(device, reply_port, reply_port_type, mode, recnum,
bytes_wanted, data, data_count)
mach_device_t device;
mach_port_t reply_port;
mach_msg_type_name_t reply_port_type;
dev_mode_t mode;
recnum_t recnum;
int bytes_wanted;
char *data; /* pointer to OUT array */
unsigned int *data_count; /* out */
{
register io_req_t ior;
register io_return_t result;
#ifdef lint
*data = *data;
*data_count = *data_count;
#endif /* lint */
if (device->state != DEV_STATE_OPEN)
return (D_NO_SUCH_DEVICE);
/* XXX note that a CLOSE may proceed at any point */
/*
* There must be a reply port.
*/
if (!MACH_PORT_VALID(reply_port)) {
printf("ds_* invalid reply port\n");
return (MIG_NO_REPLY); /* no sense in doing anything */
}
/*
* 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 | IO_INBAND;
ior->io_mode = mode;
ior->io_recnum = recnum;
ior->io_data = 0; /* driver must allocate data */
ior->io_count =
((bytes_wanted < sizeof(io_buf_ptr_inband_t)) ?
bytes_wanted : sizeof(io_buf_ptr_inband_t));
ior->io_alloc_size = 0; /* no data allocated yet */
ior->io_residual = 0;
ior->io_error = 0;
ior->io_done = ds_read_done;
ior->io_reply_port = reply_port;
ior->io_reply_port_type = reply_port_type;
/*
* The ior keeps an extra reference for the device.
*/
mach_device_reference(device);
/*
* Do the read.
*/
result = (*device->dev_ops->d_read)(device->dev_number, ior);
/*
* If the io was queued, delay reply until it is finished.
*/
if (result == D_IO_QUEUED)
return (MIG_NO_REPLY);
/*
* Return result, via ds_read_done.
*/
ior->io_error = result;
(void) ds_read_done(ior);
io_req_free(ior);
return (MIG_NO_REPLY); /* reply has already been sent. */
}
static io_return_t
device_set_status(device, flavor, status, status_count)
mach_device_t device;
dev_flavor_t flavor;
dev_status_t status;
mach_msg_type_number_t status_count;
{
if (device->state != DEV_STATE_OPEN)
return (D_NO_SUCH_DEVICE);
/* XXX note that a CLOSE may proceed at any point */
return ((*device->dev_ops->d_setstat)(device->dev_number,
flavor,
status,
status_count));
}
static io_return_t
device_get_status(device, flavor, status, status_count)
mach_device_t device;
dev_flavor_t flavor;
dev_status_t status; /* pointer to OUT array */
mach_msg_type_number_t *status_count; /* out */
{
if (device->state != DEV_STATE_OPEN)
return (D_NO_SUCH_DEVICE);
/* XXX note that a CLOSE may proceed at any point */
return ((*device->dev_ops->d_getstat)(device->dev_number,
flavor,
status,
status_count));
}
/*
* Allocate wired-down memory for device read.
*/
kern_return_t device_read_alloc(ior, size)
register io_req_t ior;
register vm_size_t size;
{
vm_offset_t addr;
kern_return_t kr;
/*
* Nothing to do if no data.
*/
if (ior->io_count == 0)
return (KERN_SUCCESS);
if (ior->io_op & IO_INBAND) {
ior->io_data = (io_buf_ptr_t) malloc(sizeof(io_buf_ptr_inband_t));
ior->io_alloc_size = sizeof(io_buf_ptr_inband_t);
} else {
size = round_page(size);
kr = vm_allocate (mach_task_self (), &addr, size, TRUE);
// kr = kmem_alloc(kernel_map, &addr, size);
if (kr != KERN_SUCCESS)
return (kr);
ior->io_data = (io_buf_ptr_t) addr;
ior->io_alloc_size = size;
}
return (KERN_SUCCESS);
}
struct thread_wait
{
struct condition cond;
struct mutex mutex;
int v;
};
static struct thread_wait io_done_wait;
void thread_wait_init (struct thread_wait *t)
{
mutex_init (&t->mutex);
condition_init (&t->cond);
t->v = 0;
}
void thread_block (struct thread_wait *t)
{
mutex_lock (&t->mutex);
t->v = 1;
while (t->v)
hurd_condition_wait (&t->cond, &t->mutex);
mutex_unlock (&t->mutex);
}
void thread_wakeup (struct thread_wait *t)
{
mutex_lock (&t->mutex);
t->v = 0;
condition_signal (&t->cond);
mutex_unlock (&t->mutex);
}
queue_head_t io_done_list;
struct mutex io_done_list_lock;
#define splio splsched /* XXX must block ALL io devices */
void iodone(ior)
register io_req_t ior;
{
register spl_t s;
/*
* If this ior was loaned to us, return it directly.
*/
if (ior->io_op & IO_LOANED) {
(*ior->io_done)(ior);
return;
}
/*
* If !IO_CALL, some thread is waiting for this. Must lock
* structure to interlock correctly with iowait(). Else can
* toss on queue for io_done thread to call completion.
*/
s = splio();
if ((ior->io_op & IO_CALL) == 0) {
ior_lock(ior);
ior->io_op |= IO_DONE;
ior->io_op &= ~IO_WANTED;
ior_unlock(ior);
// thread_wakeup((event_t)ior);
} else {
ior->io_op |= IO_DONE;
mutex_lock (&io_done_list_lock);
enqueue_tail(&io_done_list, (queue_entry_t)ior);
thread_wakeup (&io_done_wait);
// thread_wakeup((event_t)&io_done_list);
mutex_unlock (&io_done_list_lock);
}
splx(s);
}
void wakeup_io_done_thread ()
{
thread_wakeup (&io_done_wait);
}
void io_done_thread_continue()
{
for (;;) {
extern void free_skbuffs ();
register spl_t s;
register io_req_t ior;
free_skbuffs ();
s = splio();
mutex_lock(&io_done_list_lock);
while ((ior = (io_req_t)dequeue_head(&io_done_list)) != 0) {
mutex_unlock(&io_done_list_lock);
splx(s);
if ((*ior->io_done)(ior)) {
/*
* IO done - free io_req_elt
*/
io_req_free(ior);
}
/* else routine has re-queued it somewhere */
s = splio();
mutex_lock(&io_done_list_lock);
}
// assert_wait(&io_done_list, FALSE);
mutex_unlock(&io_done_list_lock);
splx(s);
// counter(c_io_done_thread_block++);
// thread_block(io_done_thread_continue);
thread_block (&io_done_wait);
}
}
void
wire_thread()
{
kern_return_t kr;
mach_port_t priv_host_port;
kr = get_privileged_ports (&priv_host_port, NULL);
if (kr != KERN_SUCCESS)
panic("get privileged port: %d", kr);
kr = thread_wire(priv_host_port,
mach_thread_self(),
TRUE);
if (kr != KERN_SUCCESS)
panic("wire_thread: %d", kr);
}
void
thread_set_own_priority (int priority)
{
kern_return_t kr;
mach_port_t priv_host_port;
mach_port_t pset, psetcntl;
kr = get_privileged_ports (&priv_host_port, NULL);
if (kr != KERN_SUCCESS)
panic("get privileged port: %d", kr);
kr = thread_get_assignment (mach_thread_self (), &pset);
if (kr != KERN_SUCCESS)
panic("thread get assignment: %d", kr);
kr = host_processor_set_priv (priv_host_port, pset, &psetcntl);
if (kr != KERN_SUCCESS)
panic("processor set priv: %d", kr);
kr = thread_max_priority (mach_thread_self (), psetcntl, 0);
if (kr != KERN_SUCCESS)
panic("set thread max priority: %d", kr);
kr = thread_priority (mach_thread_self (), 0, FALSE);
if (kr != KERN_SUCCESS)
panic("set thread priority: %d", kr);
}
any_t io_done_thread(any_t unused)
{
/*
* Set thread privileges and highest priority.
*/
// current_thread()->vm_privilege = TRUE;
// stack_privilege(current_thread());
wire_thread ();
thread_set_own_priority(0);
io_done_thread_continue();
/*NOTREACHED*/
return 0;
}
void mach_device_init()
{
// vm_offset_t device_io_min, device_io_max;
queue_init(&io_done_list);
mutex_init (&io_done_list_lock);
thread_wait_init (&io_done_wait);
// device_io_map = kmem_suballoc(kernel_map,
// &device_io_min,
// &device_io_max,
// DEVICE_IO_MAP_SIZE,
// FALSE);
/*
* If the kernel receives many device_write requests, the
* device_io_map might run out of space. To prevent
* device_write_get from failing in this case, we enable
* wait_for_space on the map. This causes kmem_io_map_copyout
* to block until there is sufficient space.
* (XXX Large writes may be starved by small writes.)
*
* There is a potential deadlock problem with this solution,
* if a device_write from the default pager has to wait
* for the completion of a device_write which needs to wait
* for memory allocation. Hence, once device_write_get
* allocates space in device_io_map, no blocking memory
* allocations should happen until device_write_dealloc
* frees the space. (XXX A large write might starve
* a small write from the default pager.)
*/
// device_io_map->wait_for_space = TRUE;
// io_inband_zone = zinit(sizeof(io_buf_ptr_inband_t), 0,
// 1000 * sizeof(io_buf_ptr_inband_t),
// 10 * sizeof(io_buf_ptr_inband_t),
// FALSE,
// "io inband read buffers");
}
struct device_emulation_ops mach_device_emulation_ops =
{
(void*) mach_device_reference,
(void*) mach_device_deallocate,
(void*) mach_convert_device_to_port,
device_open,
device_close,
NULL,
NULL,
device_read,
device_read_inband,
device_set_status,
device_get_status,
NULL,
NULL,
NULL,
NULL,
NULL
};
|