forked from tock/libtock-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtock.c
792 lines (733 loc) · 25.7 KB
/
tock.c
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "tock.h"
typedef struct {
subscribe_upcall* cb;
int arg0;
int arg1;
int arg2;
void* ud;
} tock_task_t;
#define TASK_QUEUE_SIZE 16
static tock_task_t task_queue[TASK_QUEUE_SIZE];
static int task_cur = 0;
static int task_last = 0;
int tock_enqueue(subscribe_upcall cb, int arg0, int arg1, int arg2, void* ud) {
int next_task_last = (task_last + 1) % TASK_QUEUE_SIZE;
if (next_task_last == task_cur) {
return -1;
}
task_queue[task_last].cb = cb;
task_queue[task_last].arg0 = arg0;
task_queue[task_last].arg1 = arg1;
task_queue[task_last].arg2 = arg2;
task_queue[task_last].ud = ud;
task_last = next_task_last;
return task_last;
}
int tock_status_to_returncode(statuscode_t status) {
// Conversion is easy. Since ReturnCode numeric mappings are -1*ErrorCode,
// and success is 0 in both cases, we can just multiply by -1.
return -1 * status;
}
int tock_command_return_novalue_to_returncode(syscall_return_t command_return) {
if (command_return.type == TOCK_SYSCALL_SUCCESS) {
return RETURNCODE_SUCCESS;
} else if (command_return.type == TOCK_SYSCALL_FAILURE) {
return tock_status_to_returncode(command_return.data[0]);
} else {
// The remaining SyscallReturn variants must never happen if using this
// function. We return `EBADRVAL` to signal an unexpected return variant.
return RETURNCODE_EBADRVAL;
}
}
int tock_command_return_u32_to_returncode(syscall_return_t command_return, uint32_t* val) {
if (command_return.type == TOCK_SYSCALL_SUCCESS_U32) {
*val = command_return.data[0];
return RETURNCODE_SUCCESS;
} else if (command_return.type == TOCK_SYSCALL_FAILURE) {
return tock_status_to_returncode(command_return.data[0]);
} else {
// The remaining SyscallReturn variants must never happen if using this
// function. We return `EBADRVAL` to signal an unexpected return variant.
return RETURNCODE_EBADRVAL;
}
}
int tock_command_return_u64_to_returncode(syscall_return_t command_return, uint64_t* val) {
uint32_t lsb;
uint32_t msb;
if (command_return.type == TOCK_SYSCALL_SUCCESS_U64) {
lsb = command_return.data[0];
msb = command_return.data[1];
*val = ((uint64_t)msb << 32) | lsb;
return RETURNCODE_SUCCESS;
} else if (command_return.type == TOCK_SYSCALL_FAILURE) {
return tock_status_to_returncode(command_return.data[0]);
} else {
// The remaining SyscallReturn variants must never happen if using this
// function. We return `EBADRVAL` to signal an unexpected return variant.
return RETURNCODE_EBADRVAL;
}
}
int tock_command_return_u32_u32_to_returncode(syscall_return_t command_return, uint32_t* val1, uint32_t* val2) {
if (command_return.type == TOCK_SYSCALL_SUCCESS_U32_U32) {
*val1 = command_return.data[0];
*val2 = command_return.data[1];
return RETURNCODE_SUCCESS;
} else if (command_return.type == TOCK_SYSCALL_FAILURE) {
return tock_status_to_returncode(command_return.data[0]);
} else {
// The remaining SyscallReturn variants must never happen if using this
// function. We return `EBADRVAL` to signal an unexpected return variant.
return RETURNCODE_EBADRVAL;
}
}
int tock_subscribe_return_to_returncode(subscribe_return_t subscribe_return) {
// If the subscribe was successful, easily return SUCCESS.
if (subscribe_return.success) {
return RETURNCODE_SUCCESS;
} else {
// Not success, so return the proper returncode.
return tock_status_to_returncode(subscribe_return.status);
}
}
int tock_allow_rw_return_to_returncode(allow_rw_return_t allow_return) {
// If the allow was successful, easily return SUCCESS.
if (allow_return.success) {
return RETURNCODE_SUCCESS;
} else {
// Not success, so return the proper returncode.
return tock_status_to_returncode(allow_return.status);
}
}
int tock_allow_ro_return_to_returncode(allow_ro_return_t allow_return) {
// If the allow was successful, easily return SUCCESS.
if (allow_return.success) {
return RETURNCODE_SUCCESS;
} else {
// Not success, so return the proper returncode.
return tock_status_to_returncode(allow_return.status);
}
}
int tock_allow_userspace_r_return_to_returncode(allow_userspace_r_return_t allow_return) {
// If the allow was successful, easily return SUCCESS.
if (allow_return.success) {
return RETURNCODE_SUCCESS;
} else {
// Not success, so return the proper returncode.
return tock_status_to_returncode(allow_return.status);
}
}
void yield_for(bool* cond) {
while (!*cond) {
yield();
}
}
// Returns 1 if a task is processed, 0 otherwise
int yield_check_tasks(void) {
if (task_cur != task_last) {
tock_task_t task = task_queue[task_cur];
task_cur = (task_cur + 1) % TASK_QUEUE_SIZE;
task.cb(task.arg0, task.arg1, task.arg2, task.ud);
return 1;
} else {
return 0;
}
}
#if defined(__thumb__)
void yield(void) {
if (yield_check_tasks()) {
return;
} else {
// Note: A process stops yielding when there is a callback ready to run,
// which the kernel executes by modifying the stack frame pushed by the
// hardware. The kernel copies the PC value from the stack frame to the LR
// field, and sets the PC value to callback to run. When this frame is
// unstacked during the interrupt return, the effectively clobbers the LR
// register.
//
// At this point, the callback function is now executing, which may itself
// clobber any of the other caller-saved registers. Thus we mark this
// inline assembly as conservatively clobbering all caller-saved registers,
// forcing yield to save any live registers.
//
// Upon direct observation of this function, the LR is the only register
// that is live across the SVC invocation, however, if the yield call is
// inlined, it is possible that the LR won't be live at all (commonly seen
// for the `while (1) { yield(); }` idiom) or that other registers are
// live, thus it is important to let the compiler do the work here.
//
// According to the AAPCS: A subroutine must preserve the contents of the
// registers r4-r8, r10, r11 and SP (and r9 in PCS variants that designate
// r9 as v6) As our compilation flags mark r9 as the PIC base register, it
// does not need to be saved. Thus we must clobber r0-3, r12, and LR
register uint32_t wait __asm__ ("r0") = 1; // yield-wait
register uint32_t wait_field __asm__ ("r1") = 0; // yield result ptr
__asm__ volatile (
"svc 0 \n"
:
: "r" (wait), "r" (wait_field)
: "memory", "r2", "r3", "r12", "lr"
);
}
}
int yield_no_wait(void) {
if (yield_check_tasks()) {
return 1;
} else {
// Note: A process stops yielding when there is a callback ready to run,
// which the kernel executes by modifying the stack frame pushed by the
// hardware. The kernel copies the PC value from the stack frame to the LR
// field, and sets the PC value to callback to run. When this frame is
// unstacked during the interrupt return, the effectively clobbers the LR
// register.
//
// At this point, the callback function is now executing, which may itself
// clobber any of the other caller-saved registers. Thus we mark this
// inline assembly as conservatively clobbering all caller-saved registers,
// forcing yield to save any live registers.
//
// Upon direct observation of this function, the LR is the only register
// that is live across the SVC invocation, however, if the yield call is
// inlined, it is possible that the LR won't be live at all (commonly seen
// for the `while (1) { yield(); }` idiom) or that other registers are
// live, thus it is important to let the compiler do the work here.
//
// According to the AAPCS: A subroutine must preserve the contents of the
// registers r4-r8, r10, r11 and SP (and r9 in PCS variants that designate
// r9 as v6) As our compilation flags mark r9 as the PIC base register, it
// does not need to be saved. Thus we must clobber r0-3, r12, and LR
uint8_t result = 0;
register uint32_t wait __asm__ ("r0") = 0; // yield-no-wait
register uint8_t* wait_field __asm__ ("r1") = &result; // yield result ptr
__asm__ volatile (
"svc 0 \n"
:
: "r" (wait), "r" (wait_field)
: "memory", "r2", "r3", "r12", "lr"
);
return (int)result;
}
}
void tock_exit(uint32_t completion_code) {
register uint32_t r0 __asm__ ("r0") = 0; // Terminate
register uint32_t r1 __asm__ ("r1") = completion_code;
__asm__ volatile (
"svc 6"
:
: "r" (r0), "r" (r1)
: "memory");
__builtin_unreachable();
}
void tock_restart(uint32_t completion_code) {
register uint32_t r0 __asm__ ("r0") = 1; // Restart
register uint32_t r1 __asm__ ("r1") = completion_code;
__asm__ volatile (
"svc 6"
:
: "r" (r0), "r" (r1)
: "memory");
__builtin_unreachable();
}
subscribe_return_t subscribe(uint32_t driver, uint32_t subscribe,
subscribe_upcall cb, void* userdata) {
register uint32_t r0 __asm__ ("r0") = driver;
register uint32_t r1 __asm__ ("r1") = subscribe;
register void* r2 __asm__ ("r2") = cb;
register void* r3 __asm__ ("r3") = userdata;
register int rtype __asm__ ("r0");
register int rv1 __asm__ ("r1");
register int rv2 __asm__ ("r2");
register int rv3 __asm__ ("r3");
__asm__ volatile (
"svc 1"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (r0), "r" (r1), "r" (r2), "r" (r3)
: "memory");
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
subscribe_return_t rval = {true, (subscribe_upcall*)rv1, (void*)rv2, 0};
return rval;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
subscribe_return_t rval = {false, (subscribe_upcall*)rv2, (void*)rv3, (statuscode_t)rv1};
return rval;
} else {
exit(1);
}
}
syscall_return_t command(uint32_t driver, uint32_t command,
int arg1, int arg2) {
register uint32_t r0 __asm__ ("r0") = driver;
register uint32_t r1 __asm__ ("r1") = command;
register uint32_t r2 __asm__ ("r2") = arg1;
register uint32_t r3 __asm__ ("r3") = arg2;
register uint32_t rtype __asm__ ("r0");
register uint32_t rv1 __asm__ ("r1");
register uint32_t rv2 __asm__ ("r2");
register uint32_t rv3 __asm__ ("r3");
__asm__ volatile (
"svc 2"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (r0), "r" (r1), "r" (r2), "r" (r3)
: "memory"
);
syscall_return_t rval = {rtype, {rv1, rv2, rv3}};
return rval;
}
allow_ro_return_t allow_readonly(uint32_t driver, uint32_t allow, const void* ptr, size_t size) {
register uint32_t r0 __asm__ ("r0") = driver;
register uint32_t r1 __asm__ ("r1") = allow;
register const void* r2 __asm__ ("r2") = ptr;
register size_t r3 __asm__ ("r3") = size;
register int rtype __asm__ ("r0");
register int rv1 __asm__ ("r1");
register int rv2 __asm__ ("r2");
register int rv3 __asm__ ("r3");
__asm__ volatile (
"svc 4"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (r0), "r" (r1), "r" (r2), "r" (r3)
: "memory"
);
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_ro_return_t rv = {true, (const void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_ro_return_t rv = {false, (const void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(1);
}
}
allow_rw_return_t allow_readwrite(uint32_t driver, uint32_t allow, void* ptr, size_t size) {
register uint32_t r0 __asm__ ("r0") = driver;
register uint32_t r1 __asm__ ("r1") = allow;
register const void* r2 __asm__ ("r2") = ptr;
register size_t r3 __asm__ ("r3") = size;
register int rtype __asm__ ("r0");
register int rv1 __asm__ ("r1");
register int rv2 __asm__ ("r2");
register int rv3 __asm__ ("r3");
__asm__ volatile (
"svc 3"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (r0), "r" (r1), "r" (r2), "r" (r3)
: "memory"
);
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_rw_return_t rv = {true, (void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_rw_return_t rv = {false, (void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(1);
}
}
allow_userspace_r_return_t allow_userspace_read(uint32_t driver,
uint32_t allow, void* ptr,
size_t size) {
register uint32_t r0 __asm__ ("r0") = driver;
register uint32_t r1 __asm__ ("r1") = allow;
register const void* r2 __asm__ ("r2") = ptr;
register size_t r3 __asm__ ("r3") = size;
register int rtype __asm__ ("r0");
register int rv1 __asm__ ("r1");
register int rv2 __asm__ ("r2");
register int rv3 __asm__ ("r3");
__asm__ volatile (
"svc 7"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (r0), "r" (r1), "r" (r2), "r" (r3)
: "memory"
);
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_userspace_r_return_t rv = {true, (void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_userspace_r_return_t rv = {false, (void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(-1);
}
}
memop_return_t memop(uint32_t op_type, int arg1) {
register uint32_t r0 __asm__ ("r0") = op_type;
register int r1 __asm__ ("r1") = arg1;
register uint32_t val __asm__ ("r1");
register uint32_t code __asm__ ("r0");
__asm__ volatile (
"svc 5"
: "=r" (code), "=r" (val)
: "r" (r0), "r" (r1)
: "memory"
);
if (code == TOCK_SYSCALL_SUCCESS) {
memop_return_t rv = {TOCK_STATUSCODE_SUCCESS, 0};
return rv;
} else if (code == TOCK_SYSCALL_SUCCESS_U32) {
memop_return_t rv = {TOCK_STATUSCODE_SUCCESS, val};
return rv;
} else if (code == TOCK_SYSCALL_FAILURE) {
memop_return_t rv = {(statuscode_t) val, 0};
return rv;
} else {
// Invalid return type
exit(1);
}
}
#elif defined(__riscv)
// Implementation of the syscalls for generic RISC-V platforms.
//
// For RISC-V, the arguments are passed through registers a0-a4. Generally,
// the syscall number is put in a4, and the required arguments are specified in
// a0-a3. Nothing specifically syscall related is pushed to the process stack.
void yield(void) {
if (yield_check_tasks()) {
return;
} else {
register uint32_t a0 __asm__ ("a0") = 1; // yield-wait
register uint32_t wait_field __asm__ ("a1") = 0; // yield result ptr
__asm__ volatile (
"li a4, 0\n"
"ecall\n"
:
: "r" (a0), "r" (wait_field)
: "memory", "a2", "a3", "a4", "a5", "a6", "a7",
"t0", "t1", "t2", "t3", "t4", "t5", "t6", "ra"
);
}
}
int yield_no_wait(void) {
if (yield_check_tasks()) {
return 1;
} else {
uint8_t result = 0;
register uint32_t a0 __asm__ ("a0") = 0; // yield-no-wait
register uint8_t* a1 __asm__ ("a1") = &result;
__asm__ volatile (
"li a4, 0\n"
"ecall\n"
:
: "r" (a0), "r" (a1)
: "memory", "a2", "a3", "a4", "a5", "a6", "a7",
"t0", "t1", "t2", "t3", "t4", "t5", "t6", "ra"
);
return (int)result;
}
}
void tock_restart(uint32_t completion_code) {
register uint32_t a0 __asm__ ("a0") = 1; // exit-restart
register uint32_t a1 __asm__ ("a1") = completion_code;
register uint32_t a4 __asm__ ("a4") = 6;
__asm__ volatile (
"ecall\n"
:
: "r" (a0), "r" (a1), "r" (a4)
: "memory");
__builtin_unreachable();
}
void tock_exit(uint32_t completion_code) {
register uint32_t a0 __asm__ ("a0") = 0; // exit-terminate
register uint32_t a1 __asm__ ("a1") = completion_code;
register uint32_t a4 __asm__ ("a4") = 6;
__asm__ volatile (
"ecall\n"
:
: "r" (a0), "r" (a1), "r" (a4)
: "memory");
__builtin_unreachable();
}
subscribe_return_t subscribe(uint32_t driver, uint32_t subscribe,
subscribe_upcall uc, void* userdata) {
register uint32_t a0 __asm__ ("a0") = driver;
register uint32_t a1 __asm__ ("a1") = subscribe;
register void* a2 __asm__ ("a2") = uc;
register void* a3 __asm__ ("a3") = userdata;
register uint32_t a4 __asm__ ("a4") = 1;
register int rtype __asm__ ("a0");
register int rv1 __asm__ ("a1");
register int rv2 __asm__ ("a2");
register int rv3 __asm__ ("a3");
__asm__ volatile (
"ecall\n"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (a4)
: "memory");
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
subscribe_return_t rval = {true, (subscribe_upcall*)rv1, (void*)rv2, 0};
return rval;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
subscribe_return_t rval = {false, (subscribe_upcall*)rv2, (void*)rv3, (statuscode_t)rv1};
return rval;
} else {
exit(1);
}
}
syscall_return_t command(uint32_t driver, uint32_t command,
int arg1, int arg2) {
register uint32_t a0 __asm__ ("a0") = driver;
register uint32_t a1 __asm__ ("a1") = command;
register uint32_t a2 __asm__ ("a2") = arg1;
register uint32_t a3 __asm__ ("a3") = arg2;
register uint32_t a4 __asm__ ("a4") = 2;
register int rtype __asm__ ("a0");
register int rv1 __asm__ ("a1");
register int rv2 __asm__ ("a2");
register int rv3 __asm__ ("a3");
__asm__ volatile (
"ecall\n"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (a4)
: "memory");
syscall_return_t rval = {rtype, {rv1, rv2, rv3}};
return rval;
}
allow_rw_return_t allow_readwrite(uint32_t driver, uint32_t allow,
void* ptr, size_t size) {
register uint32_t a0 __asm__ ("a0") = driver;
register uint32_t a1 __asm__ ("a1") = allow;
register void* a2 __asm__ ("a2") = ptr;
register size_t a3 __asm__ ("a3") = size;
register uint32_t a4 __asm__ ("a4") = 3;
register int rtype __asm__ ("a0");
register int rv1 __asm__ ("a1");
register int rv2 __asm__ ("a2");
register int rv3 __asm__ ("a3");
__asm__ volatile (
"ecall\n"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (a4)
: "memory");
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_rw_return_t rv = {true, (void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_rw_return_t rv = {false, (void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(1);
}
}
allow_userspace_r_return_t allow_userspace_read(uint32_t driver,
uint32_t allow, void* ptr,
size_t size) {
register uint32_t a0 __asm__ ("a0") = driver;
register uint32_t a1 __asm__ ("a1") = allow;
register void* a2 __asm__ ("a2") = ptr;
register size_t a3 __asm__ ("a3") = size;
register int rtype __asm__ ("a0");
register int rv1 __asm__ ("a1");
register int rv2 __asm__ ("a2");
register int rv3 __asm__ ("a3");
__asm__ volatile (
"li a4, 7\n"
"ecall\n"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (a0), "r" (a1), "r" (a2), "r" (a3)
: "memory");
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_userspace_r_return_t rv = {true, (void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_userspace_r_return_t rv = {false, (void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(-1);
}
}
allow_ro_return_t allow_readonly(uint32_t driver, uint32_t allow,
const void* ptr, size_t size) {
register uint32_t a0 __asm__ ("a0") = driver;
register uint32_t a1 __asm__ ("a1") = allow;
register const void* a2 __asm__ ("a2") = ptr;
register size_t a3 __asm__ ("a3") = size;
register uint32_t a4 __asm__ ("a4") = 4;
register int rtype __asm__ ("a0");
register int rv1 __asm__ ("a1");
register int rv2 __asm__ ("a2");
register int rv3 __asm__ ("a3");
__asm__ volatile (
"ecall\n"
: "=r" (rtype), "=r" (rv1), "=r" (rv2), "=r" (rv3)
: "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (a4)
: "memory");
if (rtype == TOCK_SYSCALL_SUCCESS_U32_U32) {
allow_ro_return_t rv = {true, (const void*)rv1, (size_t)rv2, 0};
return rv;
} else if (rtype == TOCK_SYSCALL_FAILURE_U32_U32) {
allow_ro_return_t rv = {false, (const void*)rv2, (size_t)rv3, (statuscode_t)rv1};
return rv;
} else {
// Invalid return type
exit(1);
}
}
memop_return_t memop(uint32_t op_type, int arg1) {
register uint32_t a0 __asm__ ("a0") = op_type;
register int a1 __asm__ ("a1") = arg1;
register uint32_t a4 __asm__ ("a4") = 5;
register uint32_t val __asm__ ("a1");
register uint32_t code __asm__ ("a0");
__asm__ volatile (
"ecall\n"
: "=r" (code), "=r" (val)
: "r" (a0), "r" (a1), "r" (a4)
: "memory"
);
if (code == TOCK_SYSCALL_SUCCESS) {
memop_return_t rv = {TOCK_STATUSCODE_SUCCESS, 0};
return rv;
} else if (code == TOCK_SYSCALL_SUCCESS_U32) {
memop_return_t rv = {TOCK_STATUSCODE_SUCCESS, val};
return rv;
} else if (code == TOCK_SYSCALL_FAILURE) {
memop_return_t rv = {(statuscode_t) val, 0};
return rv;
} else {
// Invalid return type
exit(1);
}
}
#endif
// Returns the address where the process's RAM region starts.
void* tock_app_memory_begins_at(void) {
memop_return_t ret = memop(2, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the address immediately after the end of the process's RAM region.
void* tock_app_memory_ends_at(void) {
memop_return_t ret = memop(3, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the address where the process's flash region starts.
void* tock_app_flash_begins_at(void) {
memop_return_t ret = memop(4, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the address immediately after the end of the process's flash region.
void* tock_app_flash_ends_at(void) {
memop_return_t ret = memop(5, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the address where the process's grant region (which is memory owned
// by the kernel) begins.
void* tock_app_grant_begins_at(void) {
memop_return_t ret = memop(6, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the number of writeable flash regions defined in the process's
// header.
int tock_app_number_writeable_flash_regions(void) {
memop_return_t ret = memop(7, 0);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (int) ret.data;
} else {
return 0;
}
}
// Returns the address where the writeable flash region specified by
// `region_index` starts. Returns NULL if the specified writeable flash region
// does not exist.
void* tock_app_writeable_flash_region_begins_at(int region_index) {
memop_return_t ret = memop(8, region_index);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
// Returns the address immediately after the writeable flash region specified by
// `region_index` ends. Returns NULL if the specified writeable flash region
// does not exist.
void* tock_app_writeable_flash_region_ends_at(int region_index) {
memop_return_t ret = memop(9, region_index);
if (ret.status == TOCK_STATUSCODE_SUCCESS) {
return (void*) ret.data;
} else {
return NULL;
}
}
bool driver_exists(uint32_t driver) {
syscall_return_t sval = command(driver, 0, 0, 0);
// Any success type says the driver exists.
if (sval.type >= TOCK_SYSCALL_SUCCESS) {
return true;
} else {
return false;
}
}
const char* tock_strerr(statuscode_t status) {
return tock_strrcode(tock_status_to_returncode(status));
}
// Convert a ReturnCode to a string.
const char* tock_strrcode(returncode_t returncode) {
switch (returncode) {
case RETURNCODE_SUCCESS:
return "Success";
case RETURNCODE_FAIL:
return "Unknown Error";
case RETURNCODE_EBUSY:
return "Underlying system is busy; retry";
case RETURNCODE_EALREADY:
return "The state requested is already set";
case RETURNCODE_EOFF:
return "The component is powered down";
case RETURNCODE_ERESERVE:
return "Reservation required before use";
case RETURNCODE_EINVAL:
return "An invalid parameter was passed";
case RETURNCODE_ESIZE:
return "Parameter passed was too large";
case RETURNCODE_ECANCEL:
return "Operation cancelled by a call";
case RETURNCODE_ENOMEM:
return "Memory required not available";
case RETURNCODE_ENOSUPPORT:
return "Operation or command is unsupported";
case RETURNCODE_ENODEVICE:
return "Device does not exist";
case RETURNCODE_EUNINSTALLED:
return "Device is not physically installed";
case RETURNCODE_ENOACK:
return "Packet transmission not acknowledged";
case RETURNCODE_EBADRVAL:
return "Invalid SyscallReturn variant";
}
return "Invalid error number";
}
void tock_expect(int expected, int actual, const char* file, unsigned line) {
if (expected != actual) {
printf("Expectation failure in \"%s\" at line %u\n", file, line);
printf("Expected value: %d\n", expected);
printf(" But got value: %d (possible error: %s)\n", actual, tock_strrcode(actual));
exit(1);
}
}