summaryrefslogtreecommitdiff
path: root/viengoos/thread.c
blob: 6fb351774932a22c8afad39f106bd3eb198525cd (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
/* thread.c - Thread object implementation.
   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/>.  */

#include <viengoos/cap.h>
#include <hurd/stddef.h>
#include <viengoos/thread.h>
#include <bit-array.h>
#include <backtrace.h>

#include "cap.h"
#include "object.h"
#include "thread.h"
#include "activity.h"
#include "messenger.h"
#include "scheduler.h"
#include "debug.h"
#include <hurd/trace.h>

static void
thread_dump (int argc, char *argv[])
{
  struct thread *thread = current_thread;

  if (argc > 1)
    {
      vg_oid_t oid = strtol (argv[1], NULL, 16);
      struct vg_object *object
	= object_find (root_activity, oid, -1, VG_OBJECT_POLICY_VOID);
      if (! object)
	{
	  printf ("No such object: "VG_OID_FMT"\n", oid);
	  return;
	}

      if (object_type (object) != vg_cap_thread)
	{
	  printf (VG_OID_FMT" is a %s, not a thread\n",
		  oid, vg_cap_type_string (object_type (object)));
	  return;
	}

      thread = (struct thread *) object;
    }

  if (thread)
    printf (OBJECT_NAME_FMT ":\n",
	    OBJECT_NAME_PRINTF ((struct vg_object *) thread));
  else
    {
      printf ("NULL\n");
      return;
    }

  printf (REGS_FMT"\n"
	  "\n"
	  "address space: "VG_CAP_FMT"\n"
	  "activity: "VG_CAP_FMT"\n"
	  "exception messenger: "VG_CAP_FMT"\n"
	  "utcb: "VG_CAP_FMT"\n"
	  "\n"
	  "state: %d\n"
	  "time: %d ms (last payout at %ld.%09ld s)\n"
	  "total time: %ld.%09ld s\n"
	  "\n"
	  "last fault:\n",
	  REGS_PRINTF (thread),
	  VG_CAP_PRINTF (&thread->aspace),
	  VG_CAP_PRINTF (&thread->activity),
	  VG_CAP_PRINTF (&thread->exception_messenger),
	  VG_CAP_PRINTF (&thread->utcb),
	  thread->state,
	  thread->time,
	  thread->last_payout / 1000 / 1000 / 1000,
	  thread->last_payout % (1000 * 1000 * 1000),
	  thread->total_time / 1000 / 1000 / 1000,
	  thread->total_time % (1000 * 1000 * 1000));

  int cnt = sizeof (thread->last_fault) / sizeof (thread->last_fault[0]);
  int i;
  for (i = -1; i >= -cnt; i --)
    {
      int idx = (cnt + thread->last_fault_idx + i) % cnt;
      printf ("  %d: %"PRIxPTR" (%"PRIxPTR")\n",
	      i, thread->last_fault[idx], thread->last_fault_ip[idx]);
    }

  struct vg_object *object = vg_cap_to_object (root_activity, &thread->utcb);
  if (object)
    {
      struct vg_utcb *utcb = (struct vg_utcb *) object;
      printf ("\nUTCB:\n"
	      " activated mode: %d\n"
	      " pending_message: %d\n"
	      " interrupt in transition: %d\n"
	      " saved ip: %"PRIxPTR"\n"
	      " saved sp: %"PRIxPTR"\n"
	      " activation handler sp: %"PRIxPTR"\n"
	      " activation handler ip: %"PRIxPTR"\n"
	      " activation handler end: %"PRIxPTR"\n"
	      " messenger id: %"PRIxPTR"\n",
	      utcb->activated_mode,
	      utcb->pending_message,
	      utcb->interrupt_in_transition,
	      utcb->saved_ip,
	      utcb->saved_sp,
	      utcb->activation_handler_sp,
	      utcb->activation_handler_ip,
	      utcb->activation_handler_end,
	      utcb->messenger_id);
    }
}

static struct debug_command debug_command =
  {
    "thread",
    't',
    "Print a thread's state.",
    thread_dump,
  };

void
thread_bootstrap (void)
{
  debug_register (&debug_command);

  thread_arch_bootstrap ();
}

void
thread_init (struct thread *thread)
{
  thread_arch_init (thread);

  debug (1, DEBUG_BOLD ("Creating thread " OBJECT_NAME_FMT),
	 OBJECT_NAME_PRINTF ((struct vg_object *) thread));

  thread->init = true;
}

void
thread_deinit (struct activity *activity, struct thread *thread)
{
  debug (4, "Destroying thread " OBJECT_NAME_FMT,
	 OBJECT_NAME_PRINTF ((struct vg_object *) thread));

  if (! thread->init)
    return;

  thread_suspend (thread);
  thread_arch_deinit (thread);

  thread->init = false;
}

error_t
thread_exregs (struct activity *principal,
	       struct thread *thread, uintptr_t control,
	       struct vg_cap aspace,
	       uintptr_t aspace_flags, struct vg_cap_properties properties,
	       struct vg_cap activity,
	       struct vg_cap utcb,
	       struct vg_cap exception_messenger,
	       uintptr_t *sp, uintptr_t *ip,
	       uintptr_t *flags, uintptr_t *user_handle)
{
  if (! thread->init)
    thread_init (thread);

  if ((control & ~(VG_EXREGS_SET_REGS
		   | VG_EXREGS_GET_REGS
		   | VG_EXREGS_START
		   | VG_EXREGS_STOP)))
    {
      debug (1, "Control word contains invalid bits");
      return EINVAL;
    }

  if ((control & VG_EXREGS_SET_ASPACE))
    vg_cap_copy_x (principal,
		   VG_ADDR_VOID, &thread->aspace, VG_ADDR_VOID,
		   VG_ADDR_VOID, aspace, VG_ADDR_VOID,
		   aspace_flags, properties);

  if ((control & VG_EXREGS_SET_ACTIVITY))
    vg_cap_copy_simple
      (principal,
       VG_ADDR_VOID, &thread->activity, VG_ADDR_VOID,
       VG_ADDR_VOID, activity, VG_ADDR_VOID);

  if ((control & VG_EXREGS_SET_UTCB))
    vg_cap_copy_simple
      (principal,
       VG_ADDR_VOID, &thread->utcb, VG_ADDR_VOID,
       VG_ADDR_VOID, utcb, VG_ADDR_VOID);

  if ((control & VG_EXREGS_SET_EXCEPTION_MESSENGER))
    vg_cap_copy_simple
      (principal,
       VG_ADDR_VOID, &thread->exception_messenger, VG_ADDR_VOID,
       VG_ADDR_VOID, exception_messenger, VG_ADDR_VOID);

#define CONCAT(a, b) a##b
#define CONCAT3(a, b, c) a##b##c
#define ALTER(FIELD, field)				\
  {							\
    uintptr_t t = CONCAT (thread_, field) (thread);	\
    if ((control & CONCAT(VG_EXREGS_SET_, FIELD)))	\
      CONCAT3 (thread_, field, _set) (thread, *field);	\
    if ((control & VG_EXREGS_GET_REGS))			\
      *field = t;					\
  }
  ALTER(SP, sp)
  ALTER(IP, ip)
  ALTER(FLAGS, flags)
  ALTER(USER_HANDLE, user_handle)

  if ((control & VG_EXREGS_SET_RUNNABLE))
    {
      if ((control & VG_EXREGS_RUNNABLE))
	{
	  struct vg_object *a = vg_cap_to_object (principal, &thread->activity);
	  if (! a)
	    debug (0, "Thread not schedulable: no activity");
	  else
	    {
	      struct object_desc *desc = object_to_object_desc (a);
	      if (! vg_cap_types_compatible (desc->type, vg_cap_activity))
		debug (0, "Thread not schedulable: "
		       "activity slot contains a %s",
		       vg_cap_type_string (desc->type));
	      else
		thread_make_runnable (thread);
	    }
	}
      else
	thread_suspend (thread);
    }

  return 0;
}

bool
thread_activate (struct activity *activity,
		 struct thread *thread,
		 struct messenger *messenger,
		 bool may_block)
{
  assert (thread->init);
  assert (messenger);
  assert (object_type ((struct vg_object *) messenger) == vg_cap_messenger);


  uintptr_t ip = thread_ip (thread);
  uintptr_t sp = thread_sp (thread);

  struct vg_utcb *utcb
    = (struct vg_utcb *) vg_cap_to_object (activity, &thread->utcb);
  if (! utcb)
    {
#ifndef NDEBUG
      extern struct trace_buffer rpc_trace;
      trace_buffer_dump (&rpc_trace, 0);
#endif

      do_debug (4)
	as_dump_from (activity, &thread->aspace, "");
      debug (0, "Malformed thread ("OBJECT_NAME_FMT"): "
	     "no utcb (ip: %"PRIxPTR", sp: %"PRIxPTR")",
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread), ip, sp);
      return false;
    }

  if (object_type ((struct vg_object *) utcb) != vg_cap_page)
    {
      debug (0, "Malformed thread: utcb slot contains a %s, not a page",
	     vg_cap_type_string (object_type ((struct vg_object *) utcb)));
      return false;
    }

  if (utcb->activated_mode)
    {
      debug (0, "Deferring activation by "VG_OID_FMT": "
	     "thread "OBJECT_NAME_FMT" in activated mode! "
	     "(sp: %"PRIxPTR", ip: %"PRIxPTR")",
	     object_oid ((struct vg_object *) messenger),
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread), sp, ip);

      if (! may_block)
	return false;

      object_wait_queue_enqueue (activity,
				 (struct vg_object *) thread, messenger);
      messenger->wait_reason = MESSENGER_WAIT_TRANSFER_MESSAGE;

      utcb->pending_message = 1;

      return true;
    }

  debug (5, "Activating "OBJECT_NAME_FMT" (ip: %"PRIxPTR"; sp: %"PRIxPTR")"
	 REGS_FMT,
	 OBJECT_NAME_PRINTF ((struct vg_object *) thread), ip, sp,
	 REGS_PRINTF (thread));

  utcb->protected_payload = messenger->protected_payload;
  utcb->messenger_id = messenger->id;

  if (! messenger->out_of_band)
    {
      memcpy (utcb->inline_words, messenger->inline_words,
	      messenger->inline_word_count * sizeof (uintptr_t));
      memcpy (utcb->inline_caps, messenger->inline_caps,
	      messenger->inline_cap_count * sizeof (vg_addr_t));
      utcb->inline_word_count = messenger->inline_word_count;
      utcb->inline_cap_count = messenger->inline_cap_count;
    }

  utcb->activated_mode = 1;

  if (utcb->activation_handler_ip <= ip
      && ip < utcb->activation_handler_end)
    /* Thread is transitioning.  Don't save sp and ip.  */
    {
      debug (0, "Fault while interrupt in transition (ip: %"PRIxPTR")!",
	     ip);
      utcb->interrupt_in_transition = 1;
    }
  else
    {
      utcb->interrupt_in_transition = 0;
      utcb->saved_sp = sp;
      utcb->saved_ip = ip;
    }

  thread_ip_set (thread, utcb->activation_handler_ip);
  thread_sp_set (thread, utcb->activation_handler_sp);

  debug (5, "%p activated thread.  Setting ip to %"PRIxPTR", sp to %"PRIxPTR,
	 __builtin_return_address (0), thread_ip (thread), thread_sp (thread));

  /* Make it eligible for CPU time.  */
  thread_make_runnable (thread);

  return true;
}

void
thread_raise_exception (struct activity *activity,
			struct thread *thread,
			struct vg_message *message)
{
  struct messenger *handler
    = (struct messenger *) vg_cap_to_object (activity,
					     &thread->exception_messenger);
  if (! handler)
    {
      backtrace_print ();
      debug (0, "Thread "OBJECT_NAME_FMT" has no exception handler.",
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread));
    }
  else if (object_type ((struct vg_object *) handler) != vg_cap_messenger)
    debug (0, "%s is not a valid exception handler.",
	   vg_cap_type_string (object_type ((struct vg_object *) handler)));
  else
    {
      if (! messenger_message_load (activity, handler, message))
	debug (0, "Failed to deliver exception to thread's exception handler.");
      return;
    }
}

void
thread_deliver_pending (struct activity *activity,
			struct thread *thread)
{
  struct vg_utcb *utcb
    = (struct vg_utcb *) vg_cap_to_object (activity, &thread->utcb);
  if (! utcb)
    {
      debug (0, "Malformed thread ("OBJECT_NAME_FMT"): no utcb",
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread));
      return;
    }

  if (object_type ((struct vg_object *) utcb) != vg_cap_page)
    {
      debug (0, "Malformed thread: utcb slot contains a %s, not a page",
	     vg_cap_type_string (object_type ((struct vg_object *) utcb)));
      return;
    }

  if (utcb->activated_mode)
    {
      debug (0, "Deferring exception delivery: thread in activated mode!");
      return;
    }


  struct messenger *m;
  object_wait_queue_for_each (activity, (struct vg_object *) thread, m)
    if (m->wait_reason == MESSENGER_WAIT_TRANSFER_MESSAGE)
      {
	if (thread_activate (activity, thread, m, false))
	  object_wait_queue_unlink (activity, m);

	return;
      }

  utcb->pending_message = 0;
}