summaryrefslogtreecommitdiff
path: root/boot/boot.c
blob: a1bef66d5b5a3a9d381000ab1e1223ecc21af9f0 (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
/* Load a task using the single server, and then run it
   as if we were the kernel. 
   Copyright (C) 1993 Free Software Foundation

This file is part of the GNU Hurd.

The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU Hurd is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include <mach.h>
#include <mach/notify.h>
#include <errno.h>
#include <device/device.h>
#include <a.out.h>

#include "notify_S.h"
#include "exec_S.h"
#include "device_S.h"

mach_port_t privileged_host_port, master_device_port;
mach_port_t pseudo_master_device_port;
mach_port_t receive_set;
mach_port_t pseudo_console;

mach_port_t php_child_name, psmdp_child_name;

task_t child_task;
mach_port_t bootport;

/* These will prevent the Hurd-ish versions from being used */

int
task_by_pid (int pid)
{
  return syscall (-33, pid);
}

int
write (int fd,
       void *buf,
       int buflen)
{
  return syscall (4, fd, buf, buflen);
}

int
read (int fd,
      void *buf,
      int buflen)
{
  return syscall (3, fd, buf, buflen);
}

int
open (char *name,
      int flags,
      int mode)
{
  return syscall (5, name, flags, mode);
}

int
lseek (int fd,
       int off,
       int whence)
{
  return syscall (19, fd, off, whence);
}

int
_exit (int code)
{
  return syscall (1, code);
}


int
request_server (mach_msg_header_t *inp,
		mach_msg_header_t *outp)
{
  return (exec_server (inp, outp)
	  || device_server (inp, outp)
	  || notify_server (inp, outp));
}

vm_address_t
load_image (task_t t,
	    char *file)
{
  int fd;
  struct exec x;
  char *buf;
  int headercruft;
  vm_address_t base = 0x10000;
  int amount;
  vm_address_t bsspagestart, bssstart, stackaddr;
  int magic;

  fd = open (file, 0, 0);
  
  read (fd, &x, sizeof (struct exec));
  magic = N_MAGIC (x);

  headercruft = sizeof (struct exec) * (magic == ZMAGIC);
  
  amount = round_page (headercruft + x.a_text + x.a_data);
  vm_allocate (mach_task_self (), (u_int *)&buf, amount, 1);
  lseek (fd, -headercruft, 1);
  read (fd, buf, amount);
  vm_allocate (t, &base, amount, 0);
  vm_write (t, base, (u_int) buf, amount);
  if (magic != OMAGIC)
    vm_protect (t, base, trunc_page (headercruft + x.a_text),
		0, VM_PROT_READ | VM_PROT_EXECUTE);
  vm_deallocate (mach_task_self (), (u_int)buf, amount);

  bssstart = base + x.a_text + x.a_data + headercruft;
  bsspagestart = round_page (bssstart);
  vm_allocate (t, &bsspagestart, x.a_bss - (bsspagestart - bssstart), 0);
  
  return x.a_entry;
}


int
main (int argc, char **argv, char **envp)
{
  task_t newtask;
  thread_t newthread;
  mach_port_t foo;
  vm_address_t startpc;
  char msg[] = "Boot is here.\n";
  char c;

  write (1, msg, sizeof msg);

  privileged_host_port = task_by_pid (-1);
  master_device_port = task_by_pid (-2);

  task_create (mach_task_self (), 0, &newtask);
  
  startpc = load_image (newtask, argv[1]);
  
  mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_PORT_SET,
		      &receive_set);
  
  mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, 
		      &pseudo_master_device_port);
  mach_port_move_member (mach_task_self (), pseudo_master_device_port,
			 receive_set);

  mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
		      &pseudo_console);
  mach_port_move_member (mach_task_self (), pseudo_console, receive_set);
  mach_port_insert_right (mach_task_self (), pseudo_console, pseudo_console,
			  MACH_MSG_TYPE_MAKE_SEND);

  mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &bootport);
  mach_port_move_member (mach_task_self (), bootport, receive_set);

  mach_port_insert_right (mach_task_self (), bootport, bootport, 
			  MACH_MSG_TYPE_MAKE_SEND);
  task_set_bootstrap_port (newtask, bootport);
  mach_port_deallocate (mach_task_self (), bootport);

  mach_port_request_notification (mach_task_self (), newtask, 
				  MACH_NOTIFY_DEAD_NAME, 1, bootport, 
				  MACH_MSG_TYPE_MAKE_SEND_ONCE, &foo);
  if (foo)
    mach_port_deallocate (mach_task_self (), foo);
  child_task = newtask;

  php_child_name = 100;
  psmdp_child_name = 101;
  mach_port_insert_right (newtask, php_child_name, privileged_host_port,
			  MACH_MSG_TYPE_COPY_SEND);
  mach_port_insert_right (newtask, psmdp_child_name, pseudo_master_device_port,
			  MACH_MSG_TYPE_MAKE_SEND);
  
  thread_create (newtask, &newthread);
  start_thread (newtask, newthread, startpc);

  write (1, "pausing\n", 8);
  read (0, &c, 1);
  thread_resume (newthread);
  
  mach_msg_server (request_server, __vm_page_size * 2, receive_set);
}


/* Implementation of exec interface */


S_exec_exec (
	mach_port_t execserver,
	mach_port_t file,
	mach_port_t oldtask,
	int flags,
	data_t argv,
	mach_msg_type_number_t argvCnt,
	boolean_t argvSCopy,
	data_t envp,
	mach_msg_type_number_t envpCnt,
	boolean_t envpSCopy,
	portarray_t dtable,
	mach_msg_type_number_t dtableCnt,
	portarray_t portarray,
	mach_msg_type_number_t portarrayCnt,
	intarray_t intarray,
	mach_msg_type_number_t intarrayCnt,
	mach_port_array_t deallocnames,
	mach_msg_type_number_t deallocnamesCnt,
	mach_port_array_t destroynames,
	mach_msg_type_number_t destroynamesCnt)
{
  return EOPNOTSUPP;
}

S_exec_init (
	mach_port_t execserver,
	auth_t auth_handle,
	process_t proc_server)
{
  return EOPNOTSUPP;
}

S_exec_setexecdata (
	mach_port_t execserver,
	portarray_t ports,
	mach_msg_type_number_t portsCnt,
	intarray_t ints,
	mach_msg_type_number_t intsCnt)
{
  return EOPNOTSUPP;
}


error_t
S_exec_startup (mach_port_t port,
		u_int *base_addr,
		vm_size_t *stack_size,
		int *flags,
		char **argvP,
		u_int *argvlen,
		char **envpP,
		u_int *envplen,
		mach_port_t **dtableP,
		mach_msg_type_name_t *dtablepoly,
		u_int *dtablelen,
		mach_port_t **portarrayP,
		mach_msg_type_name_t *portarraypoly,
		u_int *portarraylen,
		int **intarrayP,
		u_int *intarraylen)
{
  mach_port_t *portarray, *dtable;
  int *intarray, nc;
  char argv[100];

  /* The argv string has nulls in it; so we use %c for the nulls
     and fill with constant zero. */
  nc = sprintf (argv, "[BOOTSTRAP]%c-x%c%d%c%d%c%s%c", '\0', '\0',
		php_child_name, '\0', psmdp_child_name, '\0', "hd0e", '\0');

  if (nc > *argvlen)
    vm_allocate (mach_task_self (), (vm_address_t *)argvP, nc, 1);
  bcopy (argv, *argvP, nc);
  *argvlen = nc;
  
  *base_addr = *stack_size = 0;

  *flags = 0;
  
  *envplen = 0;

  if (*portarraylen < INIT_PORT_MAX)
    vm_allocate (mach_task_self (), (u_int *)portarrayP,
		 (INIT_PORT_MAX * sizeof (mach_port_t)), 1);
  portarray = *portarrayP;
  *portarraylen = INIT_PORT_MAX;
  *portarraypoly = MACH_MSG_TYPE_COPY_SEND;

  *dtablelen = 0;
  *dtablepoly = MACH_MSG_TYPE_COPY_SEND;
  
  if (*intarraylen < INIT_INT_MAX)
    vm_allocate (mach_task_self (), (u_int *)intarrayP,
		 (INIT_INT_MAX * sizeof (mach_port_t)), 1);
  intarray = *intarrayP;
  *intarraylen = INIT_INT_MAX;
  
  bzero (portarray, INIT_PORT_MAX * sizeof (mach_port_t));
  bzero (intarray, INIT_INT_MAX * sizeof (int));
  
  return 0;
}



/* Implementation of device interface */

ds_device_open (mach_port_t master_port,
		mach_port_t reply_port,
		mach_msg_type_name_t reply_type,
		dev_mode_t mode,
		dev_name_t name,
		device_t *device)
{
  if (master_port != pseudo_master_device_port)
    return D_INVALID_OPERATION;
  
  if (!strcmp (name, "console"))
    {
      *device = pseudo_console;
      return 0;
    }
  
  return device_open (master_device_port, mode, name, device);
}

ds_device_close (device_t device)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return 0;
}

ds_device_write (device_t device,
		 mach_port_t reply_port,
		 mach_msg_type_name_t reply_type,
		 dev_mode_t mode,
		 recnum_t recnum,
		 io_buf_ptr_t data,
		 unsigned int datalen,
		 int *bytes_written)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;

  *bytes_written = write (1, (void *)*data, datalen);
  
  return (*bytes_written == -1 ? D_IO_ERROR : D_SUCCESS);
}

ds_device_write_inband (device_t device,
			mach_port_t reply_port,
			mach_msg_type_name_t reply_type,
			dev_mode_t mode,
			recnum_t recnum,
			io_buf_ptr_inband_t data,
			unsigned int datalen,
			int *bytes_written)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;

  *bytes_written = write (1, data, datalen);
  
  return (*bytes_written == -1 ? D_IO_ERROR : D_SUCCESS);
}

ds_device_read (device_t device,
		mach_port_t reply_port,
		mach_msg_type_name_t reply_type,
		dev_mode_t mode,
		recnum_t recnum,
		int bytes_wanted,
		io_buf_ptr_t *data,
		unsigned int *datalen)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  
  vm_allocate (mach_task_self (), (pointer_t *)data, bytes_wanted, 1);
  *datalen = read (0, *data, bytes_wanted);

  return (*datalen == -1 ? D_IO_ERROR : D_SUCCESS);
}

ds_device_read_inband (device_t device,
		       mach_port_t reply_port,
		       mach_msg_type_name_t reply_type,
		       dev_mode_t mode,
		       recnum_t recnum,
		       int bytes_wanted,
		       io_buf_ptr_inband_t data,
		       unsigned int *datalen)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  
  *datalen = read (0, data, bytes_wanted);
  
  return (*datalen == -1 ? D_IO_ERROR : D_SUCCESS);
}

ds_xxx_device_set_status (device_t device,
			  dev_flavor_t flavor,
			  dev_status_t status,
			  u_int statu_cnt)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_xxx_device_get_status (device_t device,
			  dev_flavor_t flavor,
			  dev_status_t status,
			  u_int *statuscnt)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_xxx_device_set_filter (device_t device,
			  mach_port_t rec,
			  int pri,
			  filter_array_t filt,
			  unsigned int len)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_device_map (device_t device,
	       vm_prot_t prot,
	       vm_offset_t offset,
	       vm_size_t size,
	       memory_object_t *pager,
	       int unmap)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_device_set_status (device_t device,
		      dev_flavor_t flavor,
		      dev_status_t status,
		      unsigned int statuslen)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_device_get_status (device_t device,
		      dev_flavor_t flavor,
		      dev_status_t status,
		      unsigned int *statuslen)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}

ds_device_set_filter (device_t device,
		      mach_port_t receive_port,
		      int priority,
		      filter_array_t filter,
		      unsigned int filterlen)
{
  if (device != pseudo_console)
    return D_NO_SUCH_DEVICE;
  return D_INVALID_OPERATION;
}


/* Implementation of notify interface */
do_mach_notify_port_deleted (mach_port_t notify,
			     mach_port_t name)
{
  return EOPNOTSUPP;
}

do_mach_notify_msg_accepted (mach_port_t notify,
			     mach_port_t name)
{
  return EOPNOTSUPP;
}

do_mach_notify_port_destroyed (mach_port_t notify,
			       mach_port_t port)
{
  return EOPNOTSUPP;
}

do_mach_notify_no_senders (mach_port_t notify,
			   mach_port_mscount_t mscount)
{
  return EOPNOTSUPP;
}

do_mach_notify_send_once (mach_port_t notify)
{
  return EOPNOTSUPP;
}

do_mach_notify_dead_name (mach_port_t notify,
			  mach_port_t name)
{
  if (name == child_task && notify == bootport)
    exit ();
}