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
|
#include <error.h>
#include <signal.h>
#include <fcntl.h>
#include <pciaccess.h>
#include <cthreads.h>
#include <mach.h>
#include <hurd/trivfs.h>
#include <hurd/ports.h>
#include "device_U.h"
#include "irq.h"
#include "netdevice.h"
struct port_bucket *port_bucket;
struct port_bucket *irq_port_bucket;
struct port_class *dev_class;
struct port_class *intr_class;
/* Trivfs hooks. */
int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid = 0;
int trivfs_support_read = 0;
int trivfs_support_write = 0;
int trivfs_support_exec = 0;
int trivfs_allow_open = O_READ | O_WRITE;
struct port_class *trivfs_protid_portclasses[1];
struct port_class *trivfs_cntl_portclasses[1];
int trivfs_protid_nportclasses = 1;
int trivfs_cntl_nportclasses = 1;
/* Implementation of notify interface */
kern_return_t
do_mach_notify_port_deleted (mach_port_t notify,
mach_port_t name)
{
return EOPNOTSUPP;
}
kern_return_t
do_mach_notify_msg_accepted (mach_port_t notify,
mach_port_t name)
{
return EOPNOTSUPP;
}
kern_return_t
do_mach_notify_port_destroyed (mach_port_t notify,
mach_port_t port)
{
return EOPNOTSUPP;
}
kern_return_t
do_mach_notify_no_senders (mach_port_t notify,
mach_port_mscount_t mscount)
{
return ports_do_mach_notify_no_senders (notify, mscount);
}
kern_return_t
do_mach_notify_send_once (mach_port_t notify)
{
return EOPNOTSUPP;
}
kern_return_t
do_mach_notify_dead_name (mach_port_t notify,
mach_port_t name)
{
return EOPNOTSUPP;
}
void
trivfs_modify_stat (struct trivfs_protid *cred, io_statbuf_t *stat)
{
}
int notify_irq_server (mach_msg_header_t *inp, mach_msg_header_t *outp)
{
extern void pcnet32_interrupt(int irq);
extern struct device *ether_dev;
mach_irq_notification_t *irq_header = (mach_irq_notification_t *) inp;
if (inp->msgh_id != MACH_NOTIFY_IRQ)
return 0;
if (irq_header->irq == ether_dev->irq)
pcnet32_interrupt (irq_header->irq);
// ((mig_reply_header_t *) outp)->RetCode = MIG_NO_REPLY;
return 1;
}
mach_port_t master_device;
mach_port_t priv_host;
void int_handler (int sig)
{
error_t err = device_intr_notify (master_device, 2, 0, MACH_PORT_NULL,
MACH_MSG_TYPE_MAKE_SEND);
if (err)
error (2, err, "device_intr_notify");
exit (0);
}
static int
demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
{
extern int device_server (mach_msg_header_t *, mach_msg_header_t *);
extern int notify_server (mach_msg_header_t *, mach_msg_header_t *);
return device_server (inp, outp) || notify_server (inp, outp)
|| trivfs_demuxer (inp, outp) || notify_irq_server (inp, outp);
}
boolean_t
is_master_device (mach_port_t port)
{
struct port_info *pi = ports_lookup_port (port_bucket, port,
trivfs_protid_portclasses[0]);
if (pi == NULL)
return FALSE;
ports_port_deref (pi);
return TRUE;
}
error_t
trivfs_goaway (struct trivfs_control *fsys, int flags)
{
int count;
/* Stop new requests. */
ports_inhibit_class_rpcs (trivfs_cntl_portclasses[0]);
ports_inhibit_class_rpcs (trivfs_protid_portclasses[0]);
count = ports_count_class (trivfs_protid_portclasses[0]);
if (count && !(flags & FSYS_GOAWAY_FORCE))
{
/* We won't go away, so start things going again... */
ports_enable_class (trivfs_protid_portclasses[0]);
ports_resume_class_rpcs (trivfs_cntl_portclasses[0]);
ports_resume_class_rpcs (trivfs_protid_portclasses[0]);
return EBUSY;
}
mach_port_deallocate (mach_task_self (), master_device);
pci_system_cleanup ();
exit (0);
}
void dev_clean_routine (void *port)
{
error_t err = device_intr_notify (master_device, 2, 0, MACH_PORT_NULL,
MACH_MSG_TYPE_MAKE_SEND);
if (err)
error (2, err, "device_intr_notify");
}
any_t
irq_receive_thread(any_t unused)
{
int irq_demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
{
return notify_irq_server (inp, outp);
}
ports_manage_port_operations_one_thread (irq_port_bucket, irq_demuxer, 0);
return 0;
}
int
main ()
{
extern void mach_device_init();
extern any_t io_done_thread(any_t unused);
extern boolean_t ioperm_ports ();
extern int pcnet32_probe(struct device *dev);
mach_port_t bootstrap;
error_t err;
struct trivfs_control *fsys;
task_get_bootstrap_port (mach_task_self (), &bootstrap);
if (bootstrap == MACH_PORT_NULL)
error (1, 0, "must be started as a translator");
if (!ioperm_ports ())
error (2, errno, "cannot set the port access permission for the keyboard");
signal (SIGINT, int_handler);
err = get_privileged_ports (&priv_host, &master_device);
if (err)
error (1, err, "get_privileged_ports");
/* Initialize the port bucket and port classes. */
port_bucket = ports_create_bucket ();
irq_port_bucket = ports_create_bucket ();
dev_class = ports_create_class (dev_clean_routine, 0);
intr_class = ports_create_class (0, 0);
trivfs_cntl_portclasses[0] = ports_create_class (trivfs_clean_cntl, 0);
trivfs_protid_portclasses[0] = ports_create_class (trivfs_clean_protid, 0);
err = pci_system_init ();
if (err)
error (2, err, "pci_system_init");
err = pcnet32_probe (NULL);
if (err)
error (2, err, "pcnet32_probe");
mach_device_init ();
linux_net_emulation_init ();
linux_kmem_init ();
/* Reply to our parent. */
err = trivfs_startup (bootstrap, 0,
trivfs_cntl_portclasses[0], port_bucket,
trivfs_protid_portclasses[0], port_bucket, &fsys);
mach_port_deallocate (mach_task_self (), bootstrap);
if (err)
error (1, err, "Contacting parent");
cthread_detach (cthread_fork (io_done_thread, 0));
cthread_detach (cthread_fork (irq_receive_thread, 0));
/* Launch. */
do
{
ports_manage_port_operations_one_thread (port_bucket, demuxer, 0);
} while (trivfs_goaway (fsys, 0));
return 0;
}
|