-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkernel.c
172 lines (142 loc) · 2.78 KB
/
kernel.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
/*
* Raspberry PI Remote Serial Protocol.
* Copyright 2012 Jamie Iles, jamie@jamieiles.com.
* Licensed under GPLv2.
*/
#include "gdbstub.h"
#include "io.h"
#include "gpio.h"
#include "kernel.h"
#include "uart.h"
#include "debug.h"
#include "regs.h"
#include "printk.h"
#include "types.h"
static void pinmux_cfg(void)
{
struct pinmux_cfg cfg[] = {
PINMUX(14, PUD_UP, FN_0),
PINMUX(15, PUD_UP, FN_0),
PINMUX(16, PUD_OFF, FN_OUT),
};
int err = pinmux_cfg_many(cfg, ARRAY_SIZE(cfg));
BUG_ON(err, "failed to configure pinmux");
}
static void platform_init(void)
{
uart_disable();
pinmux_cfg();
}
static void __used init_bss(void)
{
extern char __bss_start, __bss_end;
char *p;
for (p = &__bss_start; p < &__bss_end; ++p)
*p = 0;
}
void dump_regs(struct arm_regs *regs)
{
int i;
const char * const reg_names[] = {
"r0 ", "r1 ", "r2 ", "r3 ",
"r4 ", "r5 ", "r6 ", "r7 ",
"r8 ", "r9 ", "r10", "r11",
"r12", "sp ", "lr ", "pc ",
};
for (i = 0; i < 16; ++i) {
puts(reg_names[i]);
puts(": ");
print_hex(regs->r[i]);
puts("\n");
}
puts("cpsr: ");
print_hex(regs->r[CPSR]);
puts("\n");
}
void panic(void)
{
BUG("panic!, entering infinite loop...");
}
static const struct bug_entry *find_bug(unsigned long addr)
{
extern const struct bug_entry __bug_start, __bug_end;
const struct bug_entry *b;
for (b = &__bug_start; b < &__bug_end; ++b)
if (b->addr == addr)
return b;
return NULL;
}
static void show_bug(const struct bug_entry *b,
struct arm_regs *regs)
{
puts("\nBUG: ");
puts(b->filename);
puts(":");
print_hex(b->line);
puts(" ");
puts(b->msg);
puts("\n");
dump_regs(regs);
}
static void handle_bugs(struct arm_regs *regs)
{
const struct bug_entry *b = find_bug(regs->r[PC]);
if (!b)
return;
show_bug(b, regs);
for (;;)
continue;
}
void do_undef(struct arm_regs *regs)
{
handle_bugs(regs);
dump_regs(regs);
panic();
regs->r[PC] += 4;
}
void do_prefetch(struct arm_regs *regs)
{
BUG("prefetch abort\n");
regs->r[PC] += 4;
}
void do_dabort(struct arm_regs *regs)
{
BUG("prefetch abort\n");
regs->r[PC] += 4;
}
void do_reserved(struct arm_regs *regs)
{
BUG("unhandled exception\n");
regs->r[PC] += 4;
}
void do_swi(struct arm_regs *regs)
{
puts("in monitor mode!\n");
regs->r[PC] += 4;
}
void do_irq(struct arm_regs *regs)
{
puts("in irq handler\n");
}
void do_fiq(struct arm_regs *regs)
{
puts("in fiq handler\n");
}
static inline void delay_n_cycles(unsigned long n)
{
n /= 2;
asm volatile("1: subs %0, %0, #1\n"
" bne 1b" : "+r"(n) : "r"(n) : "memory");
}
void start_kernel(void)
{
platform_init();
gdbstub_init();
asm volatile("cpsie if");
for (;;) {
writel(PERIPH_BASE + 0x00200000 + 0x1c, 1 << 16);
delay_n_cycles(50000000);
writel(PERIPH_BASE + 0x00200000 + 0x28, 1 << 16);
delay_n_cycles(50000000);
}
}