1// See LICENSE for license details.
2
3#include "pk.h"
4#include "file.h"
5#include "frontend.h"
6#include <stdint.h>
7#include <stdarg.h>
8
9static void vprintk(const char* s, va_list vl)
10{
11  char out[256]; // XXX
12  int res = vsnprintf(out, sizeof(out), s, vl);
13  file_write(stderr, out, res < sizeof(out) ? res : sizeof(out));
14}
15
16void printk(const char* s, ...)
17{
18  va_list vl;
19  va_start(vl, s);
20
21  vprintk(s, vl);
22
23  va_end(vl);
24}
25
26void dump_tf(trapframe_t* tf)
27{
28  static const char* regnames[] = {
29    "z ", "ra", "sp", "gp", "tp", "t0",  "t1",  "t2",
30    "s0", "s1", "a0", "a1", "a2", "a3",  "a4",  "a5",
31    "a6", "a7", "s2", "s3", "s4", "s5",  "s6",  "s7",
32    "s8", "s9", "sA", "sB", "t3", "t4",  "t5",  "t6"
33  };
34
35  tf->gpr[0] = 0;
36
37  for(int i = 0; i < 32; i+=4)
38  {
39    for(int j = 0; j < 4; j++)
40      printk("%s %lx%c",regnames[i+j],tf->gpr[i+j],j < 3 ? ' ' : '\n');
41  }
42  printk("pc %lx va %lx insn       %x sr %lx\n", tf->epc, tf->badvaddr,
43         (uint32_t)tf->insn, tf->status);
44}
45
46void do_panic(const char* s, ...)
47{
48  va_list vl;
49  va_start(vl, s);
50
51  vprintk(s, vl);
52  shutdown(-1);
53
54  va_end(vl);
55}
56
57void kassert_fail(const char* s)
58{
59  register uintptr_t ra asm ("ra");
60  do_panic("assertion failed @ %p: %s\n", ra, s);
61}
62