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
|
#include "demo.h"
struct cpu all_cpus[NUM_CPUS];
struct cpu *__cpus[] = { &all_cpus[0], &all_cpus[1], &all_cpus[2], &all_cpus[3] };
static volatile unsigned boot_done;
static void unknown_command(const char *cmd)
{
print("unknown command '%s'", cmd);
}
static void cmd_run(char **tokens)
{
unsigned mask;
if (parse_cpu_mask(tokens, &mask) < 0)
return;
run_cpus(mask);
}
static void cmd_halt(char **tokens)
{
unsigned mask;
if (parse_cpu_mask(tokens, &mask) < 0)
return;
halt_cpus(mask);
}
static void cmd_read(char **tokens)
{
void *ptr;
if (parse_aligned(tokens, &ptr) < 0 || expect_end(tokens) < 0)
return;
do_read(ptr);
}
static void cmd_write(char **tokens)
{
void *ptr;
unsigned val;
if (parse_aligned(tokens, &ptr) < 0 || parse_hex(tokens, &val) < 0 || expect_end(tokens) < 0)
return;
do_write(ptr, val);
}
static void cmd_cache(char **tokens)
{
void *ptr;
unsigned cpu;
if (parse_cpu(tokens, &cpu) < 0 || parse_ptr(tokens, &ptr) < 0)
return;
cache_debug(cpu, ptr);
}
static void cmd_perf(char **tokens)
{
unsigned cpu;
const char *cmd;
if (parse_cpu(tokens, &cpu) < 0)
return;
else if (!(cmd = strtok_input(tokens))) {
unexpected_eof();
return;
} else if (expect_end(tokens) < 0)
return;
if (!strcmp(cmd, "clear"))
perf_clear(cpu);
else if (!strcmp(cmd, "show"))
perf_show(cpu);
else
unknown_command(cmd);
}
static void cmd_remote_read(char **tokens, unsigned cpu)
{
void *ptr;
if (parse_aligned(tokens, &ptr) < 0 || expect_end(tokens) < 0)
return;
remote_send(cpu, ptr, 0, 0);
}
static void cmd_remote_write(char **tokens, unsigned cpu)
{
void *ptr;
unsigned val;
if (parse_aligned(tokens, &ptr) < 0 || parse_hex(tokens, &val) < 0 || expect_end(tokens) < 0)
return;
remote_send(cpu, ptr, 1, val);
}
static void cmd_remote(char **tokens)
{
unsigned cpu;
const char *cmd;
if (parse_cpu(tokens, &cpu) < 0)
return;
else if (cpu == 0) {
print("cannot send remote cmd to bsp");
return;
} else if (!(cmd = strtok_input(tokens))) {
unexpected_eof();
return;
}
if (!strcmp(cmd, "read"))
cmd_remote_read(tokens, cpu);
else if (!strcmp(cmd, "write"))
cmd_remote_write(tokens, cpu);
else
unknown_command(cmd);
}
static void kick_cpus(void)
{
for (unsigned i = this_cpu->num + 1; i < NUM_CPUS; ++i) {
if (cpu_is_alive(i)) {
run_cpu(i);
return;
}
print("cpu%u is dead", i);
}
boot_done = 1;
}
static void bsp_main(void)
{
for (struct cpu *cpu = all_cpus; cpu < all_cpus + NUM_CPUS; ++cpu)
cpu->mailbox = 0;
boot_done = 0;
kick_cpus();
while (!boot_done);
extern volatile unsigned _boot_num;
print("booted %u cpus", _boot_num);
_boot_num = 0;
while (1) {
char input[64];
read_line(input, sizeof input);
char *tokens = input;
char *cmd = strtok_input(&tokens);
if (!cmd)
continue;
if (!strcmp(cmd, "run"))
cmd_run(&tokens);
else if (!strcmp(cmd, "halt"))
cmd_halt(&tokens);
else if (!strcmp(cmd, "read"))
cmd_read(&tokens);
else if (!strcmp(cmd, "write"))
cmd_write(&tokens);
else if (!strcmp(cmd, "cache"))
cmd_cache(&tokens);
else if (!strcmp(cmd, "perf"))
cmd_perf(&tokens);
else if (!strcmp(cmd, "remote"))
cmd_remote(&tokens);
else
unknown_command(cmd);
}
}
static void ap_main(void)
{
kick_cpus();
halt_cpu(this_cpu->num);
while (1) {
int write;
void *ptr;
unsigned val;
remote_recv(&ptr, &write, &val);
if (write)
do_write(ptr, val);
else
do_read(ptr);
}
}
void reset(void)
{
if (this_cpu->num == 0)
console_init();
print("exited reset");
if (this_cpu->num == 0)
bsp_main();
else
ap_main();
}
|