summaryrefslogtreecommitdiff
path: root/wortel/wortel.h
blob: b7645862e39bca2b78a1074061a57044edd43c04 (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
/* wortel.h - RPC client stubs for wortel users.
   Copyright (C) 2004 Free Software Foundation, Inc.
   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 Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#ifndef _WORTEL_USER_H
#define _WORTEL_USER_H	1

#include <stdbool.h>

#include <l4/types.h>
#include <l4/space.h>
#include <l4/ipc.h>

#include <hurd/types.h>


/* The wortel capability ID is used to authenticate a message.  */
typedef l4_word_t wortel_cap_id_t;

/* The user must define these variables.  */
extern l4_thread_id_t wortel_thread_id;
extern wortel_cap_id_t wortel_cap_id;


/* Message labels.  */

/* A wortel message label consists of 16 bits.  The lower 8 bits
   specify the cap ID.  The next 7 bits specify the message ID.  The
   highest bit is always 0.  */
#define WORTEL_MSG_CAP_ID_BITS		8

#define WORTEL_MSG_THREAD_CONTROL	1
#define WORTEL_MSG_SPACE_CONTROL	2
#define WORTEL_MSG_PROCESSOR_CONTROL	3
#define WORTEL_MSG_MEMORY_CONTROL	4

#define WORTEL_MSG_PUTCHAR		32
#define WORTEL_MSG_SHUTDOWN		33

#define WORTEL_MSG_GET_MEM		64
#define WORTEL_MSG_GET_CAP_REQUEST	65
#define WORTEL_MSG_GET_CAP_REPLY	66
#define WORTEL_MSG_GET_THREADS		67
#define WORTEL_MSG_BOOTSTRAP_FINAL	68
#define WORTEL_MSG_GET_FIRST_FREE_THREAD_NO 69
#define WORTEL_MSG_GET_TASK_CAP_REQUEST	70
#define WORTEL_MSG_GET_TASK_CAP_REPLY	71
#define WORTEL_MSG_GET_DEVA_CAP_REQUEST	72
#define WORTEL_MSG_GET_DEVA_CAP_REPLY	73

#define _WORTEL_LABEL(id)					\
  (((id) << WORTEL_MSG_CAP_ID_BITS)				\
   | (wortel_cap_id & ((1 << WORTEL_MSG_CAP_ID_BITS) - 1)))


/* Privileged system calls.  */

/* Like l4_thread_control.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_thread_control (l4_thread_id_t dest, l4_thread_id_t space,
		       l4_thread_id_t scheduler, l4_thread_id_t pager,
		       void *utcb)
{
  l4_msg_tag_t tag;
  l4_word_t result;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_THREAD_CONTROL));
  l4_msg_tag_set_untyped_words (&tag, 5);
  l4_set_msg_tag (tag);
  l4_load_mr (1, dest);
  l4_load_mr (2, space);
  l4_load_mr (3, scheduler);
  l4_load_mr (4, pager);
  l4_load_mr (5, (l4_word_t) utcb);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &result);

  return result;
}


/* Like l4_space_control.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_space_control (l4_thread_id_t space, l4_word_t control,
		      l4_fpage_t kip, l4_fpage_t utcb,
		      l4_thread_id_t redirector, l4_word_t *old_control)
{
  l4_msg_tag_t tag;
  l4_word_t result;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_SPACE_CONTROL));
  l4_msg_tag_set_untyped_words (&tag, 5);
  l4_set_msg_tag (tag);
  l4_load_mr (1, space);
  l4_load_mr (2, control);
  l4_load_mr (3, kip);
  l4_load_mr (4, utcb);
  l4_load_mr (5, redirector);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &result);
  l4_store_mr (2, old_control);

  return result;
}


/* Like l4_processor_control.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_processor_control (l4_word_t proc, l4_word_t control,
			  l4_word_t internal_freq, l4_word_t external_freq,
			  l4_word_t voltage)
{
  l4_msg_tag_t tag;
  l4_word_t result;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_PROCESSOR_CONTROL));
  l4_msg_tag_set_untyped_words (&tag, 5);
  l4_set_msg_tag (tag);
  l4_load_mr (1, proc);
  l4_load_mr (2, control);
  l4_load_mr (3, internal_freq);
  l4_load_mr (4, external_freq);
  l4_load_mr (5, voltage);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &result);

  return result;
}


/* Like l4_set_pages_attributes, except that only up to 59 fpages can
   be provided.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_memory_control (l4_word_t nr, l4_fpage_t *fpages, l4_word_t *attributes)
{
  l4_msg_tag_t tag;
  unsigned int i;
  l4_word_t result;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_MEMORY_CONTROL));
  l4_msg_tag_set_untyped_words (&tag, nr + 4);
  l4_set_msg_tag (tag);
  for (i = 1; i <= nr; i++)
    l4_load_mr (i, fpages[i - 1]);
  l4_load_mr (i++, attributes[0]);
  l4_load_mr (i++, attributes[1]);
  l4_load_mr (i++, attributes[2]);
  l4_load_mr (i++, attributes[3]);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &result);

  return result;
}


/* Manager interface.  */

/* Echo the character CHR on the manager console.  */
static inline void
__attribute__((always_inline))
wortel_putchar (int chr)
{
  l4_msg_tag_t tag;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_PUTCHAR));
  l4_msg_tag_set_untyped_words (&tag, 1);
  l4_set_msg_tag (tag);
  l4_load_mr (1, (l4_word_t) chr);
  tag = l4_call (wortel_thread_id);
}


/* Shutdown the system.  */
static inline void
__attribute__((always_inline))
wortel_shutdown (void)
{
  l4_msg_tag_t tag;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_SHUTDOWN));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  /* Never reached.  */
}


/* Boot interface.  */

/* Get some physical memory.  */
static inline l4_fpage_t
__attribute__((always_inline))
wortel_get_mem (void)
{
  l4_msg_tag_t tag;
  l4_word_t mrs[2];
  l4_map_item_t map_item;
  
  l4_accept (l4_map_grant_items (L4_COMPLETE_ADDRESS_SPACE));

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_MEM));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &mrs[0]);
  l4_store_mr (2, &mrs[1]);
  map_item = *((l4_map_item_t *) mrs);

  return l4_map_item_snd_fpage (map_item);
}


/* Get the number of extra threads for physmem.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_get_threads (void)
{
  l4_msg_tag_t tag;
  l4_word_t nr_threads;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_THREADS));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &nr_threads);
  return nr_threads;
}


/* Get the next physmem capability request.  */
static inline hurd_task_id_t
__attribute__((always_inline))
wortel_get_cap_request (unsigned int *nr_fpages, l4_fpage_t *fpages)
{
  l4_msg_tag_t tag;
  hurd_task_id_t task_id;
  unsigned int nr_items;
  unsigned int mr;
  l4_word_t mrs[2];

  l4_accept (l4_map_grant_items (L4_COMPLETE_ADDRESS_SPACE));

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_CAP_REQUEST));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  *nr_fpages = 0;
  mr = 1;
  l4_store_mr (mr++, &task_id);
  nr_items = l4_typed_words (tag) / 2;
  while (nr_items--)
    {
      /* FIX this code.  Do not break alias rule.  */
      l4_map_item_t *map_itemp = (l4_map_item_t *) mrs;

      l4_store_mrs (mr, 2, &mrs[0]);

      fpages[*nr_fpages] = l4_map_item_snd_fpage (*map_itemp);
      mr += 2;
      (*nr_fpages)++;
    }

  return task_id;
}


/* Reply to a capability request.  */
static inline void
__attribute__((always_inline))
wortel_get_cap_reply (hurd_cap_handle_t handle)
{
  l4_msg_tag_t tag;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_CAP_REPLY));
  l4_msg_tag_set_untyped_words (&tag, 1);
  l4_set_msg_tag (tag);
  l4_load_mr (1, handle);
  tag = l4_call (wortel_thread_id);
}


/* Get the task and deva cap handle.  */
static inline void
__attribute__((always_inline))
wortel_bootstrap_final (l4_thread_id_t *task_server,
			hurd_cap_handle_t *task_cap_handle,
			l4_thread_id_t *deva_server,
			hurd_cap_handle_t *deva_cap_handle)
{
  l4_msg_tag_t tag;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_BOOTSTRAP_FINAL));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, task_server);
  l4_store_mr (2, task_cap_handle);
  l4_store_mr (3, deva_server);
  l4_store_mr (4, deva_cap_handle);
}


/* Get the first free thread number for task.  */
static inline l4_word_t
__attribute__((always_inline))
wortel_get_first_free_thread_no (void)
{
  l4_msg_tag_t tag;
  l4_word_t first_free_thread_no;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag,
			_WORTEL_LABEL (WORTEL_MSG_GET_FIRST_FREE_THREAD_NO));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &first_free_thread_no);
  return first_free_thread_no;
}


/* Get the next task capability request.  */
static inline hurd_task_id_t
__attribute__((always_inline))
wortel_get_task_cap_request (unsigned int *nr_threads, l4_thread_id_t *threads)
{
  l4_msg_tag_t tag;
  hurd_task_id_t task_id;
  unsigned int nr_items;
  unsigned int mr;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_TASK_CAP_REQUEST));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  *nr_threads = 0;
  mr = 1;
  l4_store_mr (mr++, &task_id);
  nr_items = l4_untyped_words (tag) - 1;
  while (nr_items--)
    l4_store_mr (mr++, &threads[(*nr_threads)++]);

  return task_id;
}


/* Reply to a task capability request.  */
static inline void
__attribute__((always_inline))
wortel_get_task_cap_reply (hurd_cap_handle_t handle)
{
  l4_msg_tag_t tag;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_TASK_CAP_REPLY));
  l4_msg_tag_set_untyped_words (&tag, 1);
  l4_set_msg_tag (tag);
  l4_load_mr (1, handle);
  tag = l4_call (wortel_thread_id);
}


/* Get the next task capability request.  */
static inline hurd_task_id_t
__attribute__((always_inline))
wortel_get_deva_cap_request (bool *master)
{
  l4_msg_tag_t tag;
  hurd_task_id_t task_id;
  l4_word_t is_master;

  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_DEVA_CAP_REQUEST));
  l4_set_msg_tag (tag);
  tag = l4_call (wortel_thread_id);

  l4_store_mr (1, &task_id);
  l4_store_mr (2, &is_master);

  *master = is_master ? true : false;

  return task_id;
}


/* Reply to a task capability request.  */
static inline void
__attribute__((always_inline))
wortel_get_deva_cap_reply (hurd_cap_handle_t handle)
{
  l4_msg_tag_t tag;
  
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);

  tag = l4_niltag;
  l4_msg_tag_set_label (&tag, _WORTEL_LABEL (WORTEL_MSG_GET_DEVA_CAP_REPLY));
  l4_msg_tag_set_untyped_words (&tag, 1);
  l4_set_msg_tag (tag);
  l4_load_mr (1, handle);
  tag = l4_call (wortel_thread_id);
}


#endif	/* _WORTEL_USER_H */