Skip to content

Commit 58b1320

Browse files
committed
Bring kernel exercises to partiy
1 parent 3df5b29 commit 58b1320

20 files changed

+48
-90
lines changed

kernel2/.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

kernel2/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

kernel2/lib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ 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;
@@ -218,7 +219,6 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
218219
numbuf[0] = va_arg(val, int);
219220
numbuf[1] = '\0';
220221
break;
221-
normal:
222222
default:
223223
data = numbuf;
224224
numbuf[0] = (*format ? *format : '%');
@@ -254,7 +254,7 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
254254
zeros = precision > len ? precision - len : 0;
255255
else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ZERO)
256256
&& !(flags & FLAG_LEFTJUSTIFY)
257-
&& len + strlen(prefix) < width)
257+
&& len + (int) strlen(prefix) < width)
258258
zeros = width - len - strlen(prefix);
259259
else
260260
zeros = 0;

kernel2/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);

kernel2/process.c

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

kernel2/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) {

kernel3/.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

kernel3/BRANCHES.md

-23
This file was deleted.

kernel3/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

kernel3/lib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ 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;
@@ -218,7 +219,6 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
218219
numbuf[0] = va_arg(val, int);
219220
numbuf[1] = '\0';
220221
break;
221-
normal:
222222
default:
223223
data = numbuf;
224224
numbuf[0] = (*format ? *format : '%');
@@ -254,7 +254,7 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
254254
zeros = precision > len ? precision - len : 0;
255255
else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ZERO)
256256
&& !(flags & FLAG_LEFTJUSTIFY)
257-
&& len + strlen(prefix) < width)
257+
&& len + (int) strlen(prefix) < width)
258258
zeros = width - len - strlen(prefix);
259259
else
260260
zeros = 0;

kernel3/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);

kernel3/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) {

kernel3x/.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-reader.full 0x100000
88
add-symbol-file obj/p-writer.full 0x140000
99
add-symbol-file obj/p-mapreader.full 0x180000
1010
add-symbol-file obj/p-mapwriter.full 0x1c0000
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

kernel3x/BRANCHES.md

-23
This file was deleted.

kernel3x/GNUmakefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ run-gdb-%: run-gdb-$(QEMUDISPLAY)-%
9898
@:
9999

100100
run-gdb-graphic-%: %.img check-qemu
101-
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -S -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
102-
$(call run,gdb -x .gdbinit,GDB)
101+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
102+
$(call run,sleep 0.5; gdb -x .gdbinit,GDB)
103103

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

107107
run: run-qemu-$(basename $(IMAGE))
108108
run-qemu: run-qemu-$(basename $(IMAGE))

kernel3x/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

kernel3x/k-hardware.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -684,13 +684,14 @@ int error_printf(int cpos, int color, const char* format, ...) {
684684

685685

686686
// check_keyboard
687-
// Check for the user typing a control key. Control-C or 'q' exit the
688-
// virtual machine.
687+
// Check for the user typing a control key. Control-C or 'q' exit
688+
// the virtual machine. Returns key typed or -1 for no key.
689689

690-
void check_keyboard(void) {
690+
int check_keyboard(void) {
691691
int c = keyboard_readc();
692692
if (c == 0x03 || c == 'q')
693693
poweroff();
694+
return c;
694695
}
695696

696697

kernel3x/kernel.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ int keyboard_readc(void);
142142

143143
// check_keyboard
144144
// Check for the user typing a control key. Control-C or 'q' exit
145-
// the virtual machine.
146-
void check_keyboard(void);
145+
// the virtual machine. Returns key typed or -1 for no key.
146+
int check_keyboard(void);
147147

148148

149149
// process_init(p, flags)

kernel3x/lib.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
219219
numbuf[0] = va_arg(val, int);
220220
numbuf[1] = '\0';
221221
break;
222-
normal:
223222
default:
224223
data = numbuf;
225224
numbuf[0] = (*format ? *format : '%');
@@ -255,7 +254,7 @@ void printer_vprintf(printer* p, int color, const char* format, va_list val) {
255254
zeros = precision > len ? precision - len : 0;
256255
else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ZERO)
257256
&& !(flags & FLAG_LEFTJUSTIFY)
258-
&& len + strlen(prefix) < width)
257+
&& len + (int) strlen(prefix) < width)
259258
zeros = width - len - strlen(prefix);
260259
else
261260
zeros = 0;

kernel3x/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) {

0 commit comments

Comments
 (0)