forked from jordanbray/CECS-525-in-C-shell
/
main.c
92 lines (77 loc) · 1.56 KB
/
main.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
#include "assembly.h"
#include "screen.h"
#include "kmem.h"
#include "string.h"
extern unsigned char data_load;
extern unsigned char data_start;
extern unsigned char data_end;
int foo = 0xdeadbeef;
/* Copy the .data section from ROM to RAM */
static void copy_data(void)
{
unsigned char *rom_data = &data_load;
unsigned char *ram_data = &data_start;
if (foo == 0xdeadbeef)
putstr("Warning: .data appears initialized before copying\n");
/* Could be replaced with a call to memcpy */
while (ram_data < &data_end) {
*(ram_data++) = *(rom_data++);
}
if (foo != 0xdeadbeef)
putstr("Warning: .data not initialized as expected\n");
}
void *test_malloc(int bytes) {
void *ptr = kmalloc(bytes);
putstr("Memory address: ");
puthexint((int)ptr);
putch('\n');
return ptr;
}
void test_free(void *ptr) {
putstr("Freeing: ");
puthexint((int)ptr);
putch('\n');
kfree(ptr);
}
void memtest() {
void *p1 = test_malloc(124);
void __attribute__ ((unused)) *p2 = test_malloc(124);
test_free(p1);
void __attribute__ ((unused)) *p3 = test_malloc(124);
}
void shell() {
char ptr[64];
copy_data();
putstr("Welcome to the (unstable) C Kernel\n");
while(1)
{
putstr("> ");
getstr(ptr, 64);
if (strcmp(ptr, "STOP"))
{
putstr("Exiting...\n");
return;
}
else if (strcmp(ptr, "MEMTEST"))
{
putstr("Performing Memory Test\n");
memtest();
}
else if (strcmp(ptr, "ERRTEST"))
{
asm ("trap #0\n");
}
else
{
putstr("Echo: ");
putstr(ptr);
putch('\n');
}
}
}
void entry() {
__asm_initialize__();
kmeminit();
shell();
while(1);
}