summaryrefslogtreecommitdiff
path: root/viengoos/viengoos.c
blob: 786f22f6c53f85a12bd465d6cea1251401e1262c (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
/* viengoos.c - Main file for viengoos.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   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 3 of the
   License, 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 this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include "mutex.h"

#ifndef NDEBUG
struct ss_lock_trace ss_lock_trace[SS_LOCK_TRACE_COUNT];
int ss_lock_trace_count;
#endif

#include <assert.h>
#include <unistd.h>
#include <alloca.h>
#include <string.h>

#include <l4/thread-start.h>
#include <l4/pagefault.h>

#include <hurd/startup.h>
#include <hurd/stddef.h>
#include <hurd/thread.h>

#include <process-spawn.h>

#include "viengoos.h"
#include "sigma0.h"
#include "memory.h"
#include "boot-modules.h"
#include "cap.h"
#include "object.h"
#include "activity.h"
#include "thread.h"
#include "as.h"
#include "server.h"
#include "shutdown.h"
#include "output.h"
#include "zalloc.h"
#include "ager.h"


#define BUG_ADDRESS	"<bug-hurd@gnu.org>"

/* The program name.  */
char *program_name = "viengoos";

/* The following must be defined and are used to calculate the extents
   of the laden binary itself.  _END is one more than the address of
   the last byte.  */
extern char _start;
extern char _end;

static struct output_driver output_device;

static void
parse_args (int argc, char *argv[])
{
  int i = 1;

  while (i < argc)
    {
      if (!strcmp (argv[i], "--usage"))
	{
	  i++;
	  printf ("Usage %s [OPTION...]\n", argv[0]);
	  printf ("Try `%s --help' for more information\n", program_name);
	  shutdown_machine ();	  
	}
      else if (!strcmp (argv[i], "--help"))
	{
	  struct output_driver **drv = output_drivers;

	  i++;
	  printf ("Usage: %s [OPTION...]\n"
		  "\n"
		  "Boot the Hurd system and wrap the L4 privileged system "
		  "calls.\n"
		  "\n"
		  "  -o, --output DRV  use output driver DRV\n"
		  "  -D, --debug LEVEL enable debug output (1-5)"
#ifdef DEBUG_ELIDE
		  " (disabled)"
#endif
		  "\n"
		  "  -h, --halt        halt the system at error (default)\n"
		  "  -r, --reboot      reboot the system at error\n"
		  "\n"
		  "      --usage       print out some usage information and "
		  "exit\n"
		  "      --help        display this help and exit\n"
		  "      --version     output version information and exit\n"
		  "\n", argv[0]);

	  printf ("Valid output drivers are: ");
	  while (*drv)
	    {
	      printf ("%s", (*drv)->name);
	      if (drv == output_drivers)
		printf (" (default)");
	      drv++;
	      if (*drv && (*drv)->name)
		printf (", ");
	      else
		printf (".\n\n");
	    }

	  printf ("Report bugs to " BUG_ADDRESS ".\n");
	  shutdown_machine ();	  
	}
      else if (!strcmp (argv[i], "--version"))
	{
	  i++;
	  printf ("%s " PACKAGE_VERSION "\n", program_name);
	  shutdown_machine ();	  
	}
      else if (!strcmp (argv[i], "-o") || !strcmp (argv[i], "--output"))
	{
	  i++;
	  if (!output_init (&output_device, argv[i], true))
	    panic ("Unknown output driver %s", argv[i]);
	  i++;
	}
      else if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--halt"))
	{
	  i++;
	  shutdown_reset = 0;
	}
      else if (!strcmp (argv[i], "-r") || !strcmp (argv[i], "--reset"))
	{
	  i++;
	  shutdown_reset = 1;
	}
      else if (!strcmp (argv[i], "-D") || !strcmp (argv[i], "--debug"))
	{
	  i++;
	  if (! ('0' <= argv[i][0] && argv[i][0] <= '9'))
	    panic ("Option -D expects an integer argument");
#ifndef DEBUG_ELIDE
	  output_debug = atoi (argv[i]);
#endif
	  i++;
	}
      else if (argv[i][0] == '-')
	panic ("Unsupported option %s", argv[i]);
      else
	panic ("Invalid non-option argument %s", argv[i]);
    }
}

static void
memory_configure (void)
{
  /* Reserve their memory.  */
  int i;
  for (i = 0; i < boot_module_count; i ++)
    {
      if (! memory_reserve (boot_modules[i].start, boot_modules[i].end,
			    i == 0 ? memory_reservation_system_executable
			    : memory_reservation_modules))
	panic ("Failed to reserve memory for boot module %d (%s).",
	       i, boot_modules[i].command_line);
      if (boot_modules[i].command_line
	  && ! memory_reserve ((l4_word_t) boot_modules[i].command_line,
			       (l4_word_t) boot_modules[i].command_line
			       + strlen (boot_modules[i].command_line),
			       i == 0 ? memory_reservation_system_executable
			       : memory_reservation_modules))
	panic ("Failed to reserve memory for boot module %d's "
	       "command line (%s).",
	       i, boot_modules[i].command_line);
    }

  /* Grab all available physical memory.  */
  do_debug (3)
    memory_reserve_dump ();
  memory_grab ();

  printf ("memory: %x-%x\n", first_frame, last_frame);
}

void
system_task_load (void)
{
  const char *const argv[] = { boot_modules[0].command_line, NULL };
  struct thread *thread;
  thread = process_spawn (ADDR_VOID,
			  (void *) boot_modules[0].start,
			  (void *) boot_modules[0].end,
			  argv, NULL,
			  true);
}

void
ager_start (void)
{
  /* 16k stack.  */
  const int stack_size = PAGESIZE * 4;
  void *stack = (void *) zalloc (stack_size);
  /* XXX: We assume the stack grows down.  */
  void *sp = stack + stack_size;

  /* Push the argument and return address onto the stack.  */
  /* XXX: We assume the stack grows down and we assume that the stack
     has the normal x86 layout.  */
  sp -= sizeof (l4_word_t);
  * (l4_word_t *) sp = l4_myself ();
  sp -= sizeof (l4_word_t);
  * (l4_word_t *) sp = 0;

  l4_thread_id_t tid = l4_global_id (l4_thread_no (l4_myself ()) + 1,
				     l4_version (l4_myself ()));

  int ret = l4_thread_control (tid, l4_myself (),
			       l4_myself (),
			       l4_myself (),
			       (void *) _L4_utcb_base () + l4_utcb_size ());
  if (! ret)
    panic ("Could not create ager thread (id=%x.%x): %s",
	   l4_thread_no (tid), l4_version (tid),
	   l4_strerror (l4_error_code ()));

  
  debug (1, "Created ager: %x", tid);

  l4_thread_id_t targ = tid;
  l4_word_t control = _L4_XCHG_REGS_CANCEL_IPC
    | _L4_XCHG_REGS_SET_SP | _L4_XCHG_REGS_SET_IP;
  l4_word_t dummy = 0;
  l4_word_t sp_arg = (l4_word_t) sp;
  l4_word_t ip = (l4_word_t) ager_loop;
  _L4_exchange_registers (&targ, &control, &sp_arg, &ip,
			  &dummy, &dummy, &dummy);
  if (targ == l4_nilthread)
    panic ("Failed to start ager thread (id=%x.%x): %s",
	   l4_thread_no (tid), l4_version (tid),
	   l4_strerror (l4_error_code ()));
}


int
main (int argc, char *argv[])
{
  parse_args (argc, argv);

  debug (1, "If the following fails, you failed to patch L4 or libl4.  "
	 "Reread the README.");
  debug (1, "%s " PACKAGE_VERSION " (%x)", program_name, l4_my_global_id ());

  /* Assert that the size of a cap is a power of 2.  */
  assert ((sizeof (struct cap) & (sizeof (struct cap) - 1)) == 0);

  /* Reserve the rm binary.  */
  if (! memory_reserve ((l4_word_t) &_start, (l4_word_t) &_end,
			memory_reservation_self))
    panic ("Failed to reserve memory for self.");

  /* Find the modules.  */
  find_components ();

  memory_configure ();

  /* We need to ensure that the whole binary is faulted in.  sigma0 is
     only willing to page the first thread.  Since memory_configure
     only grabs otherwise unreserved memory and the binary is
     reserved, we either have to implement a pager for additional
     threads (e.g., the ager) or we just fault the binary in now.  The
     latter is the easiest solution.  */
  l4_word_t p;
  for (p = (l4_word_t) &_start; p < (l4_word_t) &_end; p += PAGESIZE)
    * (volatile l4_word_t *) p = *(l4_word_t *)p;

  object_init ();

  /* Load the system task.  */
  system_task_load ();

  ager_start ();

  /* And, start serving requests.  */
  server_loop ();
}