Skip to content

Commit cfd5d34

Browse files
author
Margo
committed
Merge branch 'master' of github.com:cs61/cs61-exercises
2 parents d3b44f4 + d967b5e commit cfd5d34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4215
-86
lines changed

kernel4x/.gdbinit

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
init-if-undefined $target_set = 0
2-
if $target_set == 0
1+
init-if-undefined $loaded = 0
2+
if $loaded == 0
3+
set $loaded = 1
34
set arch i386:x86-64
4-
target remote localhost:1234
55
file obj/kernel.full
66
add-symbol-file obj/bootsector.full 0x7c00
77
add-symbol-file obj/p-alloc.full 0x100000
88
add-symbol-file obj/p-recurse.full 0x100000
99
add-symbol-file obj/p-fork.full 0x100000
1010
add-symbol-file obj/p-panic.full 0x100000
11+
target remote localhost:1234
1112
source build/functions.gdb
1213
display/5i $pc
13-
set $target_set = 1
14+
15+
# Your initialization commands here (if you want)
1416
end

kernel4x/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
*~
22
core*
33
log.txt
4-
os[0-9]*.img
4+
*.img
55
obj
6-
weensyos1
7-
weensyos1.tar.gz

kernel4x/BRANCHES.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

kernel4x/COPYRIGHT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The files in this directory are:
22

33
/*
4-
* Copyright (C) 2006-2012 Eddie Kohler
4+
* Copyright (C) 2006-2016 Eddie Kohler
55
* Copyright (C) 2005 Regents of the University of California
66
* Copyright (C) 1997 Massachusetts Institute of Technology
77
*

kernel4x/GNUmakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ run-gdb-%: run-gdb-$(QEMUDISPLAY)-%
108108
@:
109109

110110
run-gdb-graphic-%: %.img check-qemu
111-
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -S -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
112-
$(call run,gdb -x .gdbinit,GDB)
111+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
112+
$(call run,sleep 0.5; gdb -x .gdbinit,GDB)
113113

114114
run-gdb-console-%: %.img check-qemu
115-
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses -S -gdb tcp::1234 $(QEMUIMG),QEMU $<)
115+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses -gdb tcp::1234 $(QEMUIMG),QEMU $<)
116116

117117
run: run-qemu-$(basename $(IMAGE))
118118
run-qemu: run-qemu-$(basename $(IMAGE))

kernel4x/build/rules.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CFLAGS := $(CFLAGS) \
2626
-std=gnu11 -m64 -mcmodel=large \
2727
-mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow \
2828
-ffreestanding -fno-omit-frame-pointer \
29-
-Wall -Wno-format -Wno-unused -Werror -gdwarf-2
29+
-Wall -W -Wshadow -Wno-format -Wno-unused -Werror -gdwarf-2
3030
# Include -fno-stack-protector if the option exists.
3131
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
3232
DEPCFLAGS = -MD -MF $(DEPSDIR)/$*.d -MP

kernel4x/k-hardware.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,34 @@ void log_printf(const char* format, ...) {
663663
}
664664

665665

666+
// error_printf, error_vprintf
667+
// Print debugging messages to the console and to the host's
668+
// `log.txt` file via `log_printf`.
669+
670+
int error_vprintf(int cpos, int color, const char* format, va_list val) {
671+
va_list val2;
672+
__builtin_va_copy(val2, val);
673+
log_vprintf(format, val2);
674+
va_end(val2);
675+
return console_vprintf(cpos, color, format, val);
676+
}
677+
678+
int error_printf(int cpos, int color, const char* format, ...) {
679+
va_list val;
680+
va_start(val, format);
681+
cpos = error_vprintf(cpos, color, format, val);
682+
va_end(val);
683+
return cpos;
684+
}
685+
686+
666687
// check_keyboard
667688
// Check for the user typing a control key. 'a' through 'd' cause a soft
668689
// reboot where the kernel runs a different program.
669-
// Control-C or 'q' exit the virtual machine.
690+
// Control-C or 'q' exit the virtual machine. Returns key typed or -1
691+
// for no key.
670692

671-
void check_keyboard(void) {
693+
int check_keyboard(void) {
672694
int c = keyboard_readc();
673695
if (c >= 'a' && c < 'a' + WEENSYOS_NBINARIES) {
674696
// Install a temporary page table to carry us through the
@@ -695,6 +717,7 @@ void check_keyboard(void) {
695717
: : "b" (multiboot_info) : "memory");
696718
} else if (c == 0x03 || c == 'q')
697719
poweroff();
720+
return c;
698721
}
699722

700723

@@ -718,20 +741,12 @@ void panic(const char* format, ...) {
718741

719742
if (format) {
720743
// Print panic message to both the screen and the log
721-
int cpos = console_printf(CPOS(23, 0), 0xC000, "PANIC: ");
722-
log_printf("PANIC: ");
723-
724-
cpos = console_vprintf(cpos, 0xC000, format, val);
725-
log_vprintf(format, val);
726-
727-
if (CCOL(cpos)) {
728-
cpos = console_printf(cpos, 0xC000, "\n");
729-
log_printf("\n");
730-
}
731-
} else {
732-
(void) console_printf(CPOS(23, 0), 0xC000, "PANIC");
733-
log_printf("PANIC\n");
734-
}
744+
int cpos = error_printf(CPOS(24, 0), 0xC000, "PANIC: ");
745+
cpos = error_vprintf(cpos, 0xC000, format, val);
746+
if (CCOL(cpos))
747+
error_printf(cpos, 0xC000, "\n");
748+
} else
749+
error_printf(CPOS(24, 0), 0xC000, "PANIC");
735750

736751
va_end(val);
737752
fail();

kernel4x/kernel.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,35 +196,34 @@ void exception(x86_64_registers* reg) {
196196
schedule();
197197

198198
case INT_GPF:
199-
console_printf(CPOS(24, 0), 0xC000, "Process %d GPF (rip=%p)!\n",
200-
current->p_pid, current->p_registers.reg_rip);
199+
error_printf(CPOS(23, 0), 0xC000, "Process %d GPF!\n", current->p_pid);
201200
current->p_state = P_BLOCKED;
202201
break;
203202

204203
case INT_PAGEFAULT: {
205-
uintptr_t faultaddr = rcr2();
206-
const char* who = reg->reg_err & PFERR_USER
207-
? "user" : "kernel";
204+
uintptr_t addr = rcr2();
208205
const char* operation = reg->reg_err & PFERR_WRITE
209-
? "write" : "read";
206+
? "write" : "read";
210207
const char* problem = reg->reg_err & PFERR_PRESENT
211-
? "protection problem" : "missing page";
212-
213-
if (reg->reg_err & PFERR_USER) {
214-
console_printf(CPOS(23, 0), 0xC000,
215-
"process %d page fault for %p (%s %s, rip=%p)!\n",
216-
current->p_pid, faultaddr, operation, problem,
217-
reg->reg_rip);
218-
current->p_state = P_BLOCKED;
219-
} else
220-
panic("%s page fault for %p (%s %s, rip=%p)!\n",
221-
who, faultaddr, operation, problem, reg->reg_rip);
208+
? "protection problem" : "missing page";
209+
char name[20];
210+
if (reg->reg_err & PFERR_USER)
211+
snprintf(name, sizeof(name), "Process %d", current->p_pid);
212+
else
213+
strcpy(name, "Kernel");
214+
215+
error_printf(CPOS(23, 0), 0xC000,
216+
"%s page fault for %p (%s %s, rip=%p)!\n",
217+
name, addr, operation, problem, reg->reg_rip);
218+
if (!(reg->reg_err & PFERR_USER))
219+
panic("Kernel page fault");
220+
current->p_state = P_BLOCKED;
222221
break;
223222
}
224223

225224
default:
226-
panic("Unexpected exception %d!\n", current->p_registers.reg_intno);
227-
break; /* will not be reached */
225+
panic("Unexpected exception %d!\n", reg->reg_intno);
226+
break; /* will not be reached */
228227

229228
}
230229

@@ -273,7 +272,9 @@ void run(proc* p) {
273272
assert(p->p_state == P_RUNNABLE);
274273
current = p;
275274

275+
// Load the process's current pagetable
276276
lcr3((uintptr_t) p->p_pagetable);
277+
277278
// This function is defined in k-exception.S. It restores the process's
278279
// registers then jumps back to user mode.
279280
exception_return(&p->p_registers);

kernel4x/kernel.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ int keyboard_readc(void);
148148

149149
// check_keyboard
150150
// Check for the user typing a control key. Control-C or 'q' exit
151-
// the virtual machine.
152-
void check_keyboard(void);
151+
// the virtual machine. Returns key typed or -1 for no key.
152+
int check_keyboard(void);
153153

154154

155155
// process_init(p, flags)
@@ -186,4 +186,13 @@ void page_unalloc(uintptr_t addr);
186186
void log_printf(const char* format, ...) __attribute__((noinline));
187187
void log_vprintf(const char* format, va_list val) __attribute__((noinline));
188188

189+
190+
// error_printf, error_vprintf
191+
// Print debugging messages to the console and to the host's
192+
// `log.txt` file via `log_printf`.
193+
int error_printf(int cpos, int color, const char* format, ...)
194+
__attribute__((noinline));
195+
int error_vprintf(int cpos, int color, const char* format, va_list val)
196+
__attribute__((noinline));
197+
189198
#endif

kernel4x/lib.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
181181
again:
182182
switch (*format) {
183183
case 'l':
184+
case 'z':
184185
length = 1;
185186
++format;
186187
goto again;
187-
case 'd': {
188+
case 'd':
189+
case 'i': {
188190
long x = length ? va_arg(val, long) : va_arg(val, int);
189191
int negative = x < 0 ? FLAG_NEGATIVE : 0;
190192
num = negative ? -x : x;
@@ -218,7 +220,6 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
218220
numbuf[0] = va_arg(val, int);
219221
numbuf[1] = '\0';
220222
break;
221-
normal:
222223
default:
223224
data = numbuf;
224225
numbuf[0] = (*format ? *format : '%');
@@ -254,7 +255,7 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
254255
zeros = precision > len ? precision - len : 0;
255256
else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ZERO)
256257
&& !(flags & FLAG_LEFTJUSTIFY)
257-
&& len + strlen(prefix) < width)
258+
&& len + (int) strlen(prefix) < width)
258259
zeros = width - len - strlen(prefix);
259260
else
260261
zeros = 0;

0 commit comments

Comments
 (0)