Skip to content

Commit

Permalink
Set up local fix for es segment register.
Browse files Browse the repository at this point in the history
  • Loading branch information
dthain committed Dec 15, 2023
1 parent 401e578 commit 0d470b3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions user/sysstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ See the file LICENSE for details.
#include "library/string.h"
#include "library/stdio.h"

struct system_stats s = {0};

int main(int argc, char *argv[])
{
printf("a");
flush();
/*
This demonstrates (and fixes) a kernel bug.
GCC implements the initialization of the structure on the stack like this:
rep stos %eax,%es:(%edi)
However, basekernel does not automatically set up (or save) the es segment register,
and so the operation crashes. The es register should be set up correctly in kernelcore,
saved and restored when processing interrupts/system calls, and also initialized correctly
using process_kstack_init.
*/

/* The workaround here is to explicitly set up the es register prior to using it.*/

printf("b");
flush();
asm("movl %ds, %ax");
asm("movl %ax, %es");

struct system_stats s = {0};

if (syscall_system_stats(&s)) {
return 1;
}
printf("c");
flush();


printf("System uptime: %u:%u:%u\n", s.time / (3600), (s.time % 3600) / 60, s.time % 60);
printf("Disk 0: %d blocks read, %d blocks written\n", s.blocks_read[0], s.blocks_written[0]);
Expand Down

0 comments on commit 0d470b3

Please sign in to comment.