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

+6-4
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

+1-3
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

-18
This file was deleted.

kernel4x/COPYRIGHT

+1-1
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

+3-3
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

+1-1
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

+31-16
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

+20-19
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

+11-2
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

+4-3
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;

kernel4x/lib.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ typedef unsigned long uint64_t;
3030
typedef long intptr_t; // ints big enough to hold pointers
3131
typedef unsigned long uintptr_t;
3232

33-
typedef uint64_t size_t; // sizes and offsets
34-
typedef int64_t ssize_t;
35-
typedef int64_t off_t;
33+
typedef unsigned long size_t; // sizes and offsets
34+
typedef long ssize_t;
35+
typedef long off_t;
3636

37-
typedef int32_t pid_t; // process IDs
37+
typedef int pid_t; // process IDs
3838

3939
void* memcpy(void* dst, const void* src, size_t n);
4040
void* memmove(void* dst, const void* src, size_t n);

kernel4x/process.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ void app_printf(int colorid, const char* format, ...) {
1616
va_start(val, format);
1717
cursorpos = console_vprintf(cursorpos, color, format, val);
1818
va_end(val);
19+
20+
if (CROW(cursorpos) >= 23)
21+
cursorpos = CPOS(0, 0);
1922
}
2023

2124

@@ -26,20 +29,14 @@ void panic(const char* format, ...) {
2629
va_list val;
2730
va_start(val, format);
2831
char buf[160];
29-
memcpy(buf, "PANIC: ", 7);
30-
int len = vsnprintf(&buf[7], sizeof(buf) - 7, format, val) + 7;
32+
int len = vsnprintf(buf, sizeof(buf), format, val);
3133
va_end(val);
3234
if (len > 0 && buf[len - 1] != '\n')
3335
strcpy(buf + len - (len == (int) sizeof(buf) - 1), "\n");
34-
(void) console_printf(CPOS(23, 0), 0xC000, "%s", buf);
35-
sys_panic(NULL);
36+
sys_panic(buf);
3637
spinloop: goto spinloop; // should never get here
3738
}
3839

3940
void assert_fail(const char* file, int line, const char* msg) {
40-
(void) console_printf(CPOS(23, 0), 0xC000,
41-
"PANIC: %s:%d: assertion '%s' failed\n",
42-
file, line, msg);
43-
sys_panic(NULL);
44-
spinloop: goto spinloop; // should never get here
41+
panic("%s:%d: assertion '%s' failed\n", file, line, msg);
4542
}

kernel4x/x86-64.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,14 @@ static inline void tlbflush(void) {
339339
}
340340

341341
static inline uint32_t read_eflags(void) {
342-
uint32_t eflags;
343-
asm volatile("pushfl; popl %0" : "=r" (eflags));
342+
uint64_t eflags;
343+
asm volatile("pushfq; popq %0" : "=r" (eflags));
344344
return eflags;
345345
}
346346

347347
static inline void write_eflags(uint32_t eflags) {
348-
asm volatile("pushl %0; popfl" : : "r" (eflags));
348+
uint64_t rflags = eflags; // really only lower 32 bits are used
349+
asm volatile("pushq %0; popfq" : : "r" (rflags));
349350
}
350351

351352
static inline uintptr_t read_rbp(void) {

shell1/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
filtermultiples
2+
forkmix
3+
forkmix2
4+
manyfork
5+
pipedemo
6+
pipesizer
7+
primesieve

shell1/GNUmakefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
PROGRAMS = forkmix forkmix2 manyfork \
2+
pipedemo pipesizer \
3+
filtermultiples primesieve
4+
5+
all: $(PROGRAMS)
6+
7+
include ../common/rules.mk
8+
9+
%.o: %.c $(BUILDSTAMP)
10+
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
11+
12+
%-profgen: %-profgen.o
13+
$(CC) -fprofile-generate $^ -o $@
14+
15+
$(PROGRAMS): %: %.o
16+
$(CC) $(CFLAGS) $(O) -o $@ $^
17+
18+
19+
clean:
20+
rm -f *.o $(PROGRAMS)
21+
rm -rf $(DEPSDIR) *.dSYM
22+
23+
.PHONY: all clean

shell1/filtermultiples.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "helpers.h"
2+
3+
int main(int argc, char** argv) {
4+
assert(argc >= 2);
5+
int p = strtol(argv[1], NULL, 10);
6+
assert(p > 1);
7+
8+
char buf[BUFSIZ];
9+
while (fgets(buf, BUFSIZ, stdin))
10+
if (isdigit((unsigned char) buf[0])) {
11+
int i = strtol(buf, NULL, 10);
12+
if (i % p != 0)
13+
printf("%d\n", i);
14+
}
15+
}

0 commit comments

Comments
 (0)