aboutsummaryrefslogtreecommitdiff
path: root/pk/console.c
blob: 3d3edaee61db86cabcf268c1106474d126a12aa8 (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
// See LICENSE for license details.

#include "pk.h"
#include "mmap.h"
#include "file.h"
#include "frontend.h"
#include <stdint.h>
#include <stdarg.h>

static void vprintk(const char* s, va_list vl)
{
  char out[256]; // XXX
  int res = vsnprintf(out, sizeof(out), s, vl);
  file_write(stderr, out, res < sizeof(out) ? res : sizeof(out));
}

void printk(const char* s, ...)
{
  va_list vl;
  va_start(vl, s);

  vprintk(s, vl);

  va_end(vl);
}

static const char* get_regname(int r)
{
  static const char regnames[] = {
    "z \0" "ra\0" "sp\0" "gp\0" "tp\0" "t0\0"  "t1\0"  "t2\0"
    "s0\0" "s1\0" "a0\0" "a1\0" "a2\0" "a3\0"  "a4\0"  "a5\0"
    "a6\0" "a7\0" "s2\0" "s3\0" "s4\0" "s5\0"  "s6\0"  "s7\0"
    "s8\0" "s9\0" "sA\0" "sB\0" "t3\0" "t4\0"  "t5\0"  "t6"
  };

  return &regnames[r * 3];
}

void dump_tf(trapframe_t* tf)
{

  tf->gpr[0] = 0;

  for(int i = 0; i < 32; i+=4)
  {
    for(int j = 0; j < 4; j++)
      printk("%s %lx%c", get_regname(i+j), tf->gpr[i+j], j < 3 ? ' ' : '\n');
  }
  printk("pc %lx va %lx insn       %x sr %lx\n", tf->epc, tf->badvaddr,
         (uint32_t)tf->insn, tf->status);
}

void do_panic(const char* s, ...)
{
  va_list vl;
  va_start(vl, s);

  vprintk(s, vl);
  shutdown(-1);

  va_end(vl);
}

void kassert_fail(const char* s)
{
  register uintptr_t ra asm ("ra");
  do_panic("assertion failed @ %p: %s\n", ra, s);
}