Skip to content

Commit ca4a624

Browse files
committedOct 16, 2024
Improve documentation and output
1 parent 8df676d commit ca4a624

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed
 

‎kernels1/kernel.cc

+7-14
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ uintptr_t syscall(regstate* regs) {
311311

312312
// It can be useful to log events using `log_printf`.
313313
// Events logged this way are stored in the host's `log.txt` file.
314-
/* log_printf("proc %d: syscall %d at rip %p\n",
315-
current->pid, regs->reg_rax, regs->reg_rip); */
314+
// log_printf("p%d: %s\n", current->pid, syscall_name(regs->reg_rax));
316315

317316
// Show the current cursor location and memory state.
318317
console_show_cursor(cursorpos);
@@ -394,17 +393,14 @@ pid_t syscall_spawn(const char* command) {
394393
char pipebuf[1];
395394
size_t pipebuf_len = 0;
396395

397-
// syscall_pipewrite(buf, sz)
398-
// Handles the SYSCALL_PIPEWRITE system call; see `sys_pipewrite`
399-
// in `u-lib.hh`.
400-
401396
ssize_t syscall_pipewrite(const char* buf, size_t sz) {
397+
// See `sys_pipewrite` in `u-lib.cc` for specification.
402398
if (sz == 0) {
403399
// nothing to write
404400
return 0;
405401
} else if (pipebuf_len == 1) {
406-
// kernel buffer full, try again
407-
return -1;
402+
// kernel buffer full, process should try again
403+
return E_AGAIN;
408404
} else {
409405
// write one character
410406
pipebuf[0] = buf[0];
@@ -413,17 +409,14 @@ ssize_t syscall_pipewrite(const char* buf, size_t sz) {
413409
}
414410
}
415411

416-
// syscall_piperead(buf, sz)
417-
// Handles the SYSCALL_PIPEREAD system call; see `sys_piperead`
418-
// in `u-lib.hh`.
419-
420412
ssize_t syscall_piperead(char* buf, size_t sz) {
413+
// See `sys_piperead` in `u-lib.cc` for specification.
421414
if (sz == 0) {
422415
// no room to read
423416
return 0;
424417
} else if (pipebuf_len == 0) {
425-
// kernel buffer empty, try again
426-
return -1;
418+
// kernel buffer empty, process should try again
419+
return E_AGAIN;
427420
} else {
428421
// read one character
429422
buf[0] = pipebuf[0];

‎kernels1/lib.cc

+26
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,32 @@ void assert_memeq_fail(const char* file, int line, const char* msg,
787787
}
788788

789789

790+
// system call debugging
791+
792+
const char* syscall_name(int syscall) {
793+
switch (syscall) {
794+
case SYSCALL_GETPID:
795+
return "getpid";
796+
case SYSCALL_YIELD:
797+
return "yield";
798+
case SYSCALL_PANIC:
799+
return "panic";
800+
case SYSCALL_PAGE_ALLOC:
801+
return "page_alloc";
802+
case SYSCALL_GETSYSNAME:
803+
return "getsysname";
804+
case SYSCALL_SPAWN:
805+
return "spawn";
806+
case SYSCALL_PIPEWRITE:
807+
return "pipewrite";
808+
case SYSCALL_PIPEREAD:
809+
return "piperead";
810+
default:
811+
return "(unknown)";
812+
}
813+
}
814+
815+
790816
// Some static tests of our arithmetic functions
791817

792818
static_assert(msb(0) == 0, "msb failure");

‎kernels1/lib.hh

+3
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ inline uint32_t crc32c(const void* buf, size_t sz) {
225225
#define SYSCALL_PIPEWRITE 7
226226
#define SYSCALL_PIPEREAD 8
227227

228+
const char* syscall_name(int syscall);
229+
228230

229231
// System call error return values
230232

233+
#define E_AGAIN -11 // Try again
231234
#define E_INVAL -22 // Invalid argument
232235
#define E_RANGE -34 // Out of range
233236

‎kernels1/p-pipereader.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
void process_main() {
44
char buf[200];
55
size_t buflen = 0;
6-
size_t nreads = 0;
76

87
while (true) {
98
// Read a message
9+
size_t nreads = 0;
1010
while (memchr(buf, '\n', buflen) == NULL) {
1111
++nreads;
1212
ssize_t r = sys_piperead(&buf[buflen], sizeof(buf) - buflen);
@@ -19,9 +19,9 @@ void process_main() {
1919

2020
// Print the message that was read
2121
char* newline = (char*) memchr(buf, '\n', buflen);
22-
size_t messagelen = (newline + 1) - buf;
23-
console_printf(0x0E00, "%zu sys_piperead calls: read %.*s",
24-
nreads, messagelen, buf);
22+
size_t messagelen = newline - buf + 1;
23+
console_printf(0x0E00, "%zu sys_piperead calls for %zuB: read %.*s",
24+
nreads, messagelen, messagelen, buf);
2525

2626
// Shift down rest of buffer
2727
memmove(buf, &buf[messagelen], buflen - messagelen);

‎kernels1/p-pipewriter.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ const char* messages[] = {
77
};
88

99
void process_main() {
10-
size_t nwrites = 0;
11-
1210
while (true) {
1311
// First, write a message.
1412
const char* message = messages[rand(0, arraysize(messages) - 1)];
1513
size_t pos = 0;
1614
size_t len = strlen(message);
15+
size_t nwrites = 0;
1716
while (pos < len) {
1817
++nwrites;
1918
ssize_t w = sys_pipewrite(&message[pos], len - pos);
@@ -25,8 +24,8 @@ void process_main() {
2524
}
2625

2726
// Print that message was written.
28-
console_printf(0x0F00, "%zu sys_pipewrite calls: wrote %s",
29-
nwrites, message);
27+
console_printf(0x0F00, "%zu sys_pipewrite calls for %zuB: wrote %.*s",
28+
nwrites, len, len, message);
3029

3130
// Wait 1-3 seconds.
3231
unsigned long wait_until = ticks + rand(HZ, 3 * HZ - 1);

‎kernels1/u-lib.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,19 @@ pid_t sys_spawn(const char* command) {
4343
}
4444

4545
// sys_pipewrite(buf, sz)
46-
// Write data to pipe from `buf`. Writes at most `sz` bytes.
47-
// Returns number of bytes written or -1 on error.
46+
// Copy up to `sz` bytes of data from `buf` into the system pipe.
47+
// Returns number of bytes written or a negative error code (if, for
48+
// instance, the system pipe’s data transfer buffer is full).
4849
[[gnu::noinline]]
4950
ssize_t sys_pipewrite(const void* buf, size_t sz) {
5051
return make_syscall(SYSCALL_PIPEWRITE, (uintptr_t) buf, sz);
5152
}
5253

5354
// sys_piperead(buf, sz)
54-
// Read data from pipe into `buf`. Reads at most `sz` bytes.
55-
// Returns number of bytes read or -1 on error.
55+
// Read up to `sz` bytes of data from the system pipe into `buf`.
56+
// Bytes read are removed from the system pipe.
57+
// Returns number of bytes read or a negative error code (if, for
58+
// instance, the system pipe’s data transfer buffer is empty).
5659
[[gnu::noinline]]
5760
ssize_t sys_piperead(void* buf, size_t sz) {
5861
return make_syscall(SYSCALL_PIPEREAD, (uintptr_t) buf, sz);

0 commit comments

Comments
 (0)
Please sign in to comment.