jump to
@@ -10,13 +10,13 @@ *(display++) = c;
*(display++) = 0x0F; } -void print_string(char *str) { +void print_string(const char *str) { while (*str != 0) { printc(*(str++)); } } -void assert_failed(const char * message) { +void assert_failed(const char *message) { print_string(message); while (1) {}
@@ -82,7 +82,7 @@
return buffer += fat->cluster_size; } -int FAT12_root_dir_next(FAT12_descriptor_t *fat, int index, char *fname_buffer, uint16_t *first_cluster, uint32_t *file_size) { +int FAT12_root_dir_next(FAT12_descriptor_t *fat, unsigned int index, char *fname_buffer, uint16_t *first_cluster, uint32_t *file_size) { fname_buffer[0] = 0; while (1) {
@@ -67,7 +67,7 @@ * @param file_size Pointer to a uint32_t to be filled with the file size.
* * @return -1 if the search has reached an end; Index within the directory table otherwise. */ -int FAT12_root_dir_next(FAT12_descriptor_t *fat, int index, char *fname_buffer, uint16_t *first_cluster, uint32_t *file_size); +int FAT12_root_dir_next(FAT12_descriptor_t *fat, unsigned int index, char *fname_buffer, uint16_t *first_cluster, uint32_t *file_size); /** * @brief Given a cluster index, return the value of the FAT table.@@ -79,4 +79,4 @@ * @return Value of the FAT at the given cluster index.
*/ uint16_t FAT12_next_cluster(FAT12_descriptor_t *fat, uint16_t cluster); -#endif+#endif
@@ -9,14 +9,14 @@ struct memblock *next;
size_t size; }; -volatile struct memblock *malloc_first, *malloc_last, *malloc_next_free; +static struct memblock *malloc_first, *malloc_last, *malloc_next_free; int malloc_init(void *start, void *end) { uint32_t mem_start = (uint32_t)(start); uint32_t mem_end = (uint32_t)(end); - malloc_first = (volatile struct memblock*)(mem_start); - malloc_last = (volatile struct memblock*)(mem_end - sizeof(struct memblock)); + malloc_first = (struct memblock*)(mem_start); + malloc_last = (struct memblock*)(mem_end - sizeof(struct memblock)); malloc_next_free = malloc_first; malloc_first->size = 0;@@ -66,8 +66,8 @@
// TODO: in some cases, one might rather link to the // next block after that instead of creating a new block - struct memblock *new_block = (volatile struct memblock*)(addr); - + struct memblock *new_block = (struct memblock*)(addr); + new_block->next = malloc_next_free->next; new_block->size = 0;@@ -104,5 +104,5 @@ * Frees a previously allocated block of memory. (KERNEL MODE)
* \param ptr Pointer to the memory block to be freed. */ void free(void* ptr) { - + (void)ptr; }
@@ -169,6 +169,8 @@ while (1) {}
} void kfault(const char* string, INTERRUPT_FRAME *frame, uint16_t err_code) { + (void)err_code; + cli(); printk("%s\n", string); printk("EIP: %p\n", frame->eip);
@@ -70,6 +70,7 @@ return (buffer_head == buffer_tail);
} __attribute__((interrupt)) void keyboard_int_handler(INTERRUPT_FRAME *frame) { + (void)frame; while (inb(PS2_COMMAND) & 0x01) { nop(); nop(); nop(); nop(); buffer_enqueue(inb(PS2_DATA));@@ -116,13 +117,16 @@ return (int)(buffer_dequeue());
} } -int ps2_kb_read(int fd, char *buffer, uint32_t size) { - int res = 0; - for (int i = 0; i < size; i++) { +size_t ps2_kb_read(int fd, char *buffer, size_t size) { + size_t res; + + (void)fd; + + for (res = 0; res < size; res++) { if (buffer_empty()) { break; } - buffer[i] = buffer_dequeue(); - res++; + buffer[res] = buffer_dequeue(); } + return res; -}+}
@@ -6,26 +6,38 @@ #include "drivers/console.h"
#include "drivers/keyboard.h" file_operations_t tty_fops = { - tty_open, /* open */ - tty_openat, /* openat */ - tty_read, /* read */ - tty_write, /* write */ - NULL, /* dir_next */ - NULL /* lseek */ -}; + tty_open, /* open */ + tty_openat, /* openat */ + tty_read, /* read */ + tty_write, /* write */ + NULL, /* dir_next */ + NULL, /* lseek */ + NULL /* tell */ +}; int tty_open(const char *pathname, int flags) { + (void)pathname; + (void)flags; + return 0; } -int tty_openat(int fd, const char *fname, int flags) { +int tty_openat(file_t *file, file_t *handle, const char *fname, int flags) { + (void)file; + (void)handle; + (void)fname; + (void)flags; + return 0; } -int tty_read(int fd, char *buffer, uint32_t size) { - uint32_t i = 0; - static int state = 0; - static char next[2]; +ssize_t tty_read(file_t *file, char *buffer, size_t size) { + ssize_t i = 0; + static int state = 0; + static char next[2]; + + (void)file; + while (i < size) { if (state > 0) { buffer[i++] = next[--state];@@ -53,10 +65,14 @@ next[0] = table[scancode];
state = 2; } } - return i; + + return i; } -int tty_write(int fd, char *buffer, uint32_t size) { - vga_con.write_n(buffer, size); - return size; +ssize_t tty_write(file_t *file, const char *buffer, size_t size) { + (void)file; + + vga_con.write_n(buffer, size); + + return size; }
@@ -2,14 +2,12 @@ #ifndef TTY_H
#define TTY_H #include "file.h" -#include "drivers/keyboard.h" -#include "drivers/console.h" int tty_open(const char *pathname, int flags); -int tty_openat(int fd, const char *fname, int flags); -int tty_read(int fd, char *buffer, uint32_t size); -int tty_write(int fd, char *buffer, uint32_t size); +int tty_openat(file_t *file, file_t *handle, const char *fname, int flags); +ssize_t tty_read(file_t *file, char *buffer, size_t size); +ssize_t tty_write(file_t *file, const char *buffer, size_t size); extern file_operations_t tty_fops; -#endif+#endif
@@ -32,6 +32,7 @@ void vga_con_write_c(const char c);
void vga_con_write_n(const char *string, uint32_t num); void vga_con_write_s(const char *string); void vga_con_clear(int mode); +void vga_con_clear_all(void); void vga_con_clear_line(int mode); void vga_con_newline(void); void vga_con_backspace(void);@@ -42,7 +43,7 @@ vga_con_init,
vga_con_write_c, vga_con_write_n, vga_con_write_s, - vga_con_clear, + vga_con_clear_all, vga_con_newline, vga_con_backspace };@@ -255,13 +256,17 @@ end = VGA_TEXTMODE_CELLS;
break; } - for (int i = 0; i < VGA_TEXTMODE_CELLS; i++) { + for (int i = start; i < end; i++) { VGA_TEXTMODE_MEM[2 * i] = 0; VGA_TEXTMODE_MEM[2 * i + 1] = color; } - + line = 0; column = 0; +} + +void vga_con_clear_all(void) { + vga_con_clear(2); } void vga_con_newline(void) {@@ -278,4 +283,4 @@ }
set_char(0); set_cursor(line, column); -}+}
@@ -3,6 +3,7 @@ #include "elf.h"
#include "core.h" #include "file.h" +#include "mm/paging.h" #include "sched/process.h" #include "memory.h"@@ -66,44 +67,50 @@ uint32_t entsize;
} SECT_HEADER; void elf_infodump(VIRT_ADDR elf_pointer, uint32_t size) { +#ifndef DEBUG + (void)elf_pointer; + (void)size; +#else ELF_HEADER *header = (ELF_HEADER*)elf_pointer; - - PRINT_DBG("Reading ELF file from location %p of length %i\n", elf_pointer, size); - PRINT_DBG("\n"); - PRINT_DBG("ELF header size: %i\n", sizeof(ELF_HEADER)); + (void)size; + + printk("Reading ELF file from location %p of length %i\n", elf_pointer, size); + printk("\n"); + + printk("ELF header size: %i\n", sizeof(ELF_HEADER)); assert(sizeof(ELF_HEADER) == 52); - PRINT_DBG("\n"); + printk("\n"); - PRINT_DBG("ELF header hexdump:\n"); + printk("ELF header hexdump:\n"); memdump(elf_pointer, 52); - PRINT_DBG("\n"); + printk("\n"); - PRINT_DBG("ELF header magic number:\n"); - PRINT_DBG("%s\n", &(header->e_ident)); - PRINT_DBG("\n"); + printk("ELF header magic number:\n"); + printk("%s\n", &(header->e_ident)); + printk("\n"); - PRINT_DBG("ELF header infodump:\n"); - PRINT_DBG("- machine: %i\n", header->machine); - PRINT_DBG("- version: %i\n", header->version); - PRINT_DBG("- entry: %p\n", header->entry); - PRINT_DBG("\n"); - PRINT_DBG("- program headers:\n"); - PRINT_DBG(" - offset: %i\n", header->proghead_offset); - PRINT_DBG(" - entries: %i\n", header->ph_num); - PRINT_DBG(" - entry size: %i\n", header->ph_entry_size); - PRINT_DBG("\n"); - PRINT_DBG("- section headers:\n"); - PRINT_DBG(" - offset: %i\n", header->secthead_offset); - PRINT_DBG(" - entries: %i\n", header->sh_num); - PRINT_DBG(" - entry size: %i\n", header->sh_entry_size); - PRINT_DBG("\n"); + printk("ELF header infodump:\n"); + printk("- machine: %i\n", header->machine); + printk("- version: %i\n", header->version); + printk("- entry: %p\n", header->entry); + printk("\n"); + printk("- program headers:\n"); + printk(" - offset: %i\n", header->proghead_offset); + printk(" - entries: %i\n", header->ph_num); + printk(" - entry size: %i\n", header->ph_entry_size); + printk("\n"); + printk("- section headers:\n"); + printk(" - offset: %i\n", header->secthead_offset); + printk(" - entries: %i\n", header->sh_num); + printk(" - entry size: %i\n", header->sh_entry_size); + printk("\n"); - PRINT_DBG("Enumerating sections:\n"); + printk("Enumerating sections:\n"); int sh_offset = header->secthead_offset; SECT_HEADER *sect_headers = (SECT_HEADER*)(elf_pointer + sh_offset); - + int num_sections = header->sh_num; int section_size = header->sh_entry_size;@@ -114,15 +121,16 @@ assert(sizeof(SECT_HEADER) == section_size);
for (int i = 0; i < num_sections; i++) { SECT_HEADER sh = sect_headers[i]; - - PRINT_DBG("Section: %s\n", (char*)(sect_names_addr + sh.name)); - PRINT_DBG("- type: %i\n", sh.type); - PRINT_DBG("- offset: %i\n", sh.offset); - PRINT_DBG("- size: %i\n", sh.size); - PRINT_DBG("- addr: %i\n", sh.addr); - PRINT_DBG("- addr_align: %i\n", sh.addr_align); - PRINT_DBG("\n"); + + printk("Section: %s\n", (char*)(sect_names_addr + sh.name)); + printk("- type: %i\n", sh.type); + printk("- offset: %i\n", sh.offset); + printk("- size: %i\n", sh.size); + printk("- addr: %i\n", sh.addr); + printk("- addr_align: %i\n", sh.addr_align); + printk("\n"); } +#endif } PROCESS_ID elf_exec(const char *fname, char *args) {@@ -131,7 +139,7 @@ PRINT_DBG("Loading ELF executable \"%s\".\n", fname);
// TODO: needs to change when we have other file systems int fd = file_open(fname, 0); PRINT_DBG("File handle: %i\n", fd); - + if (fd == -1) { printk("Executable file not found: %s\n", fname); return -1;@@ -161,9 +169,10 @@ int sh_num = header.sh_num;
assert(header.sh_entry_size == sizeof(SECT_HEADER)); - SECT_HEADER sect_headers[16]; - file_lseek(fd, sh_offset, SEEK_SET); - int sect_headers_size = file_read(fd, (void*)(§_headers), sizeof(SECT_HEADER) * sh_num); + SECT_HEADER sect_headers[16]; + file_lseek(fd, sh_offset, SEEK_SET); + size_t sect_headers_size = file_read(fd, (void*)(§_headers), + sizeof(SECT_HEADER) * sh_num); if (sect_headers_size != sizeof(SECT_HEADER) * sh_num) { printk("Error while reading executable.\n");@@ -174,7 +183,7 @@ int num_sections = header.sh_num;
int section_size = header.sh_entry_size; SECT_HEADER *sect_names_sh = §_headers[header.sh_strndx]; - + char *sect_names = malloc(sect_names_sh->size); if (sect_names == NULL) {@@ -183,7 +192,7 @@ return -1;
} file_lseek(fd, sect_names_sh->offset, SEEK_SET); - int sect_names_size = file_read(fd, sect_names, sect_names_sh->size); + size_t sect_names_size = file_read(fd, sect_names, sect_names_sh->size); if (sect_names_size != sect_names_sh->size) { printk("Error while reading executable.\n");@@ -195,27 +204,27 @@
// go through all sections and copy/allocate memory as necessary PRINT_DBG("Enumerating %i sections:\n", num_sections); for (int i = 0; i < num_sections; i++) { - SECT_HEADER *sh = §_headers[i]; - char *name = (char*)(sect_names + sh->name); + SECT_HEADER *sh = §_headers[i]; +#ifdef DEBUG + char *name = (char*)(sect_names + sh->name); + uint32_t lma = sh->offset; +#endif + VIRT_ADDR vma = (VIRT_ADDR)(sh->addr); if ((sh->flags & SHF_ALLOC) && (sh->flags & SHF_EXECINSTR)) { - uint32_t lma = sh->offset; - uint32_t vma = sh->addr; uint32_t sect_size = sh->size; PRINT_DBG("%p\n", sh->flags); PRINT_DBG("Copying code section %s to its destination ", name); PRINT_DBG("(LMA: %p, VMA: %p)\n", lma, vma); file_lseek(fd, sh->offset, SEEK_SET); - int read_size = file_read(fd, vma, sect_size); - + size_t read_size = file_read(fd, vma, sect_size); + if (read_size != sect_size) { printk("Error while reading executable.\n"); return -1; } } else if (sh->flags & SHF_ALLOC) { - uint32_t lma = sh->offset; - uint32_t vma = sh->addr; PRINT_DBG("Allocating space for section %s ", name); PRINT_DBG("(LMA: %p, VMA: %p)\n", lma, vma);@@ -224,7 +233,7 @@ } else {
PRINT_DBG("Skipping section %s\n", name); } } - + PRINT_DBG("\n"); PRINT_DBG("Entry point: %p\n", header.entry);@@ -235,4 +244,4 @@ PROCESS_MAIN *entry = (PROCESS_MAIN*)(header.entry);
entry(args); return 0; -}+}
@@ -52,12 +52,17 @@ int FAT_dir_next(file_t *file, int index, char *fname_buffer) {
uint16_t first_cluster; uint32_t file_size; + (void)file; + // TODO: subdirectories return FAT12_root_dir_next(fat_desc, index, fname_buffer, &first_cluster, &file_size); } int FAT_openat(file_t *root, file_t *handle, const char *fname, int flags) { - int i = 0; + int i = 0; + + (void)root; + (void)flags; // TODO: take fd into consideration (open file in that subdirectory) uint16_t first_cluster;@@ -131,6 +136,7 @@ // to be implemented
} else { kpanic("Wrong whence!"); } + return file->pos; } off_t FAT_tell(file_t *file) {
@@ -59,7 +59,7 @@ }
file_t *get_process_local_file(int fd) { if (fd > 1) { return &file_table[fd]; } - + PROCESS_ID pid = get_current_process(); PRINT_DBG("pid: %i\n", pid); PROCESS *p = get_process(pid);@@ -115,7 +115,7 @@ file_t *file = get_process_local_file(fd);
if (file->fops->dir_next == NULL) { return -1; } - file->fops->dir_next(file, index, fname_buffer); + return file->fops->dir_next(file, index, fname_buffer); } off_t file_lseek(int fd, off_t offset, int whence) {
@@ -30,7 +30,7 @@ struct file_operations {
int (*open)(const char *pathname, int flags); int (*openat)(file_t *root, file_t *handle, const char *fname, int flags); ssize_t (*read)(file_t *file, char *buffer, size_t size); - ssize_t (*write)(file_t *file, char *buffer, size_t size); + ssize_t (*write)(file_t *file, const char *buffer, size_t size); int (*dir_next)(file_t *file, int index, char *fname_buffer); off_t (*lseek)(file_t *file, off_t offset, int whence); off_t (*tell)(file_t *file);
@@ -18,14 +18,18 @@ (uint16_t)(0xC000) \
} INTERRUPT(default_isr, frame) { + (void)frame; printk("interrupt was issued\n"); } INTERRUPT(breakpoint_isr, frame) { + (void)frame; printk("BREAKPOINT WAS HIT\n"); } EXCEPTION(double_fault_isr, frame, error_code) { + (void)frame; + (void)error_code; kpanic("CRITICAL: DOUBLE FAULT"); }@@ -34,11 +38,13 @@ kfault("CRITICAL: GENERAL PROTECTION FAULT", frame, error_code);
} INTERRUPT(pic1_handler, frame) { + (void)frame; printk("PIC1 INTERRUPT\n"); pic1_eoi(); } INTERRUPT(pic2_handler, frame) { + (void)frame; printk("PIC2 INTERRUPT\n"); pic2_eoi(); }@@ -69,9 +75,9 @@ } else if (i == 0x08) {
install_interrupt(i, double_fault_isr, 0x18, INT_GATE); } else if (i == 0x0d) { install_interrupt(i, gpf_isr, 0x18, INT_GATE); - } else if (i >= 0x21 || i < 0x28) { + } else if (i >= 0x21 && i < 0x28) { install_interrupt(i, pic1_handler, 0x18, INT_GATE); - } else if (i >= 0x28 || i < 0x30) { + } else if (i >= 0x28 && i < 0x30) { install_interrupt(i, pic2_handler, 0x18, INT_GATE); } else { install_interrupt(i, default_isr, 0x18, INT_GATE);
@@ -61,7 +61,7 @@ sti();
printk("done.\n"); printk("Initiallizing malloc..."); - malloc_init(0xC0400000u, 0xC0800000u); + malloc_init((void*)(0xC0400000u), (void*)(0xC0800000u)); printk("done.\n"); printk("Installing syscalls...");
@@ -15,5 +15,7 @@ return (uint32_t)(first_free);
} void mark_as_free(void* page_addr) { - -}+ (void)page_addr; + + /* TODO: implement */ +}
@@ -116,29 +116,31 @@ }
} size_t copy_to_pdir(VIRT_ADDR src, size_t length, PHYS_ADDR pdir, VIRT_ADDR dest) { - VIRT_ADDR mount_dest = (VIRT_ADDR)(0xe0000000); - mount_page_dir(pdir); - PHYS_ADDR page; + VIRT_ADDR mount_dest = (VIRT_ADDR)(0xe0000000); + mount_page_dir(pdir); + PHYS_ADDR page; + size_t res = 0; - while (length != 0) { - page = get_free_page(); - force_map_page_to_this(page, PAGE_DIR_INDEX(mount_dest), PAGE_TABLE_INDEX(mount_dest), PAGE_TABLE_FLAGS); - force_map_page_to(page, PAGE_DIR_INDEX(dest), PAGE_TABLE_INDEX(dest), PAGE_TABLE_FLAGS); + while (length != 0) { + page = get_free_page(); + force_map_page_to_this(page, PAGE_DIR_INDEX(mount_dest), PAGE_TABLE_INDEX(mount_dest), PAGE_TABLE_FLAGS); + force_map_page_to(page, PAGE_DIR_INDEX(dest), PAGE_TABLE_INDEX(dest), PAGE_TABLE_FLAGS); - uint32_t offset = ((uint32_t)dest % PAGE_SIZE); - uint32_t part_length = (offset + length <= PAGE_SIZE) ? length : PAGE_SIZE - offset; + uint32_t offset = ((uint32_t)dest % PAGE_SIZE); + uint32_t part_length = (offset + length <= PAGE_SIZE) ? length : PAGE_SIZE - offset; - PRINT_DBG("src=%p dest=%p length=%i offset=%i plen=%i\n", src, dest, length, offset, part_length); + PRINT_DBG("src=%p dest=%p length=%i offset=%i plen=%i\n", src, dest, length, offset, part_length); - memset(mount_dest, 0, PAGE_SIZE); - memcpy((uint8_t*)(mount_dest) + offset, src, part_length); + memset(mount_dest, 0, PAGE_SIZE); + memcpy((uint8_t*)(mount_dest) + offset, src, part_length); - dest += part_length; - src += part_length; - length -= part_length; - } + dest += part_length; + src += part_length; + length -= part_length; + res += part_length; + } - return dest; + return res; } int map_range_to(PHYS_ADDR page_dir, VIRT_ADDR dest, PHYS_ADDR src, uint32_t page_count, uint32_t flags) {@@ -206,6 +208,9 @@ #define PAGE_FAULT_FLAGS_PRESENT (1 << 0)
EXCEPTION(page_fault_isr, frame, error_code) { volatile VIRT_ADDR faulty_addr; + + (void)frame; + __asm__ volatile ("mov %%cr2, %0" : "=a" (faulty_addr)); uint32_t pdir_index = PAGE_DIR_INDEX(faulty_addr); uint32_t ptbl_index = PAGE_TABLE_INDEX(faulty_addr);@@ -222,4 +227,4 @@
int paging_init(void) { install_interrupt(0x0e, page_fault_isr, 0x18, TRAP_GATE); return 0; -}+}
@@ -1,14 +1,14 @@
#include "file.h" #include "pipe.h" -#include "sched/sched.h" file_operations_t pipe_fops = { - NULL, /* open */ - NULL, /* openat */ - pipe_read, /* read */ - pipe_write, /* write */ - NULL, /* dir_next */ - NULL /* lseek */ + NULL, /* open */ + NULL, /* openat */ + pipe_read, /* read */ + pipe_write, /* write */ + NULL, /* dir_next */ + NULL, /* lseek */ + NULL /* tell */ }; #define PIPE_BUFFER_SIZE 512@@ -38,21 +38,29 @@ write_head = wh_next;
return c; } -int pipe_read(int fd, char *buffer, uint32_t size) { - int i = 0; +ssize_t pipe_read(file_t *file, char *buffer, size_t size) { + size_t i = 0; + + (void)file; + while (i < size) { int res = pipe_readc(); if (res == -1) { break; } buffer[i++] = (char)(res); } + return i; } -int pipe_write(int fd, char *buffer, uint32_t size) { - int i = 0; +ssize_t pipe_write(file_t *file, const char *buffer, size_t size) { + size_t i = 0; + + (void)file; + while (i < size) { int res = pipe_writec(buffer[i++]); if (res == -1) { break; } } + return i; -}+}
@@ -3,9 +3,9 @@ #define PIPE_H
#include "file.h" -int pipe_read(int fd, char *buffer, uint32_t size); -int pipe_write(int fd, char *buffer, uint32_t size); +ssize_t pipe_read(file_t *file, char *buffer, size_t size); +ssize_t pipe_write(file_t *file, const char *buffer, size_t size); extern file_operations_t pipe_fops; -#endif+#endif
@@ -45,11 +45,12 @@ }
int sched_dispatcher(void); +void sched_idle(char *args) { + (void)args; -void sched_idle(char *args) { - while (1) { - hlt(); - } + while (1) { + hlt(); + } } /*!@@ -83,7 +84,7 @@ } else {
p->stdin = 0; p->stdout = 1; } - + // TODO: implement with malloc strcpy(p->name_buf, name);@@ -93,8 +94,8 @@ } else {
strcpy(p->args_buf, args); } - p->name = (const char*)&(p->name_buf); - p->args = (const char*)&(p->args_buf); + p->name = (const char*)&(p->name_buf); + p->args = (char*)&(p->args_buf); PROCESS_ID pid = add_process(p, current_pid); p->id = pid;@@ -110,7 +111,8 @@ frame.cs = 0x18;
frame.eip = (uint32_t)(p->eip); // load stack - copy_to_pdir(&frame, sizeof(frame), p->page_dir, p->esp); + size_t copylen = copy_to_pdir(&frame, sizeof(frame), p->page_dir, p->esp); + assert(copylen == sizeof(frame)); // save stack checksum stack_compute_checksum(&(p->checksum), &frame, &(&frame)[1]);@@ -160,29 +162,29 @@
STACK_CHECKSUM checksum; stack_compute_checksum(&(checksum), next->esp, next->ebp); - // check stack - if (stack_compare_checksum(&(next->checksum), &(checksum))) { - printk("STACK DAMAGED: PROCESS %i (%s), ESP %X, EBP %X\n", current_pid, get_process(current_pid)->name, next->esp, next->ebp); - memdump((void*)(next->esp), (void*)((uint32_t)(next->ebp) - (uint32_t)(next->esp))); - kpanic("CRITICAL STACK DAMAGE"); - } + // check stack + if (stack_compare_checksum(&(next->checksum), &(checksum))) { + printk("STACK DAMAGED: PROCESS %i (%s), ESP %X, EBP %X\n", current_pid, get_process(current_pid)->name, next->esp, next->ebp); + memdump((void*)(next->esp), (uint32_t)(next->ebp - next->esp)); + kpanic("CRITICAL STACK DAMAGE"); + } - // prepare stack - frame = (volatile SCHED_FRAME*)(next->esp); - ebp = next->ebp; + // prepare stack + frame = (SCHED_FRAME*)(next->esp); + ebp = (uint32_t)(next->ebp); - frame->cs = 0x18; - frame->eip = next->eip; - frame->eflags = (uint32_t)(next->eflags); - frame->esp = (uint32_t)(next->esp); - frame->ebp = (uint32_t)(next->ebp); + frame->cs = 0x18; + frame->eip = (uint32_t)(next->eip); + frame->eflags = (uint32_t)(next->eflags); + frame->esp = (uint32_t)(next->esp); + frame->ebp = (uint32_t)(next->ebp); PRINT_DBG("esp: %p, ebp: %p, eip: %p, eflags: %p\n", frame->esp, frame->ebp, frame->eip, frame->eflags); // reset the timer pit_setup_channel(PIT_CHANNEL_0, PIT_MODE_0, SCHED_INTERVAL); - + pic1_eoi(); }@@ -198,7 +200,7 @@ // create idle process
PROCESS *idle = get_slot(); assert(idle != NULL); - PROCESS_ID idle_pid = add_process(idle, NULL); + PROCESS_ID idle_pid = add_process(idle, 0); assert(idle_pid == 0); idle->page_dir = get_current_page_dir();@@ -211,7 +213,6 @@ }
void sched_yield(void) { crit_enter(); - PROCESS *current = get_process(current_pid); uint32_t csc = crit_stash(); INT(0x20);@@ -241,7 +242,7 @@ }
void sched_unblock(int pid) { PROCESS *process = get_process(pid); - + // only unblock previously blocked processes if (process->state == PSTATE_BLOCKED) { process->state = PSTATE_READY;@@ -251,7 +252,7 @@
int sched_kill(PROCESS_ID pid) { int success = 1; crit_enter(); - + PROCESS *process = get_process(pid); if (process != NULL) { // kill all children
@@ -24,4 +24,5 @@ // restore process stack and state of registers
mov %eax, %esp mov %ebx, %ebp popa - iret+ iret +
@@ -4,8 +4,9 @@ #include "sched/process.h"
PROCESS_ID next_schedule(PROCESS_ID current) { PROCESS* next = get_first_process(); + PROCESS* choice = NULL; - PROCESS* choice = NULL; + (void)current; while (next) { // skip idle process
@@ -1,13 +1,13 @@
#include "sched/stack_check.h" void stack_compute_checksum(STACK_CHECKSUM* checksum, const void *esp, const void *ebp) { - *checksum = 0; - - for (uint32_t *p = (uint32_t)(esp); p < (uint32_t)(ebp); p = &p[1]) { - *checksum ^= *p; - } + *checksum = 0; + + for (uint32_t *p = (uint32_t*)(esp); p < (uint32_t*)(ebp); p = &p[1]) { + *checksum ^= *p; + } } int stack_compare_checksum(STACK_CHECKSUM* a, STACK_CHECKSUM* b) { return (a == b); -}+}
@@ -52,7 +52,7 @@ static uint8_t from_BCD(uint8_t bcd_value) {
return (bcd_value >> 4) * 10 + (bcd_value & 0x0F); } -static uint8_t rtc_set(uint8_t reg, uint8_t value) { +static void rtc_set(uint8_t reg, uint8_t value) { outb(reg, RTC_COMMAND); /*nop(); nop();@@ -74,6 +74,8 @@ return 0;
} INTERRUPT(rtc_interrupt, frame) { + (void)frame; + time_tick(); alarm_tick();@@ -102,4 +104,6 @@ // enable interrupts again
sti(); pic_unmask_interrupt(8); + + return 0; }
@@ -2,10 +2,8 @@ #include "cedos.h"
#include "stdio.h" void assert_failed(const char * message) { - printf(message); - - int pid = get_pid(); + printf(message); - /* TODO: Kill this process */ - while (1) {} + int pid = get_pid(); + process_kill(pid); }
@@ -20,13 +20,13 @@ interrupt(0x30, res, 3, 0, 0, 0);
return res; } -int sc_file_read(int fd, char *buffer, uint32_t size) { +ssize_t sc_file_read(int fd, char *buffer, size_t size) { volatile uint32_t res = 0; interrupt(0x30, res, 0, fd, buffer, size); return res; } -int sc_file_write(int fd, char *buffer, uint32_t size) { +ssize_t sc_file_write(int fd, const char *buffer, size_t size) { volatile uint32_t res = 0; interrupt(0x30, res, 1, fd, buffer, size); return res;@@ -61,7 +61,7 @@ volatile uint32_t res = 0;
interrupt(0x30, res, 5, pid, 0, 0); } -int sc_file_open(char *pathname, uint32_t flags) { +int sc_file_open(const char *pathname, uint32_t flags) { volatile uint32_t res = 0; interrupt(0x30, res, 6, pathname, flags, 0); return res;@@ -78,7 +78,6 @@ interrupt(0x30, res, 8, 0, 0, 0);
} int dir_next(int fd, int index, char *fname_buffer) { - void *pointer = (void*)(0); volatile uint32_t res = 0; interrupt(0x30, res, 9, fd, index, fname_buffer); return res;@@ -109,4 +108,5 @@
int time_now(datetime_t *buffer) { volatile uint32_t res = 0; interrupt(0x30, res, 16, buffer, 0, 0); + return 0; }
@@ -15,6 +15,9 @@ int minute;
int second; } datetime_t; +typedef uint32_t size_t; +typedef int64_t ssize_t; + int sysprint(const char *fmt, int arg1, int arg2); int yield(); void sleep(int msec);@@ -26,9 +29,9 @@ void process_kill(int pid);
void graphics_set_mode(int mode); -int sc_file_read(int fd, char *buffer, uint32_t size); -int sc_file_write(int fd, char *buffer, uint32_t size); -int sc_file_open(char *buffer, uint32_t flags); +ssize_t sc_file_read(int fd, char *buffer, size_t size); +ssize_t sc_file_write(int fd, const char *buffer, size_t size); +int sc_file_open(const char *buffer, uint32_t flags); int sc_file_lseek(int fd, uint32_t offset, int whence); int sc_file_tell(int fd);@@ -42,4 +45,4 @@
int time_now(datetime_t *buffer); -#endif+#endif
@@ -7,7 +7,8 @@
extern void main(char *args); int _start(char *args) { - malloc_init(0x20000000, 0x30000000); - - main(args); -}+ malloc_init((void*)0x20000000, (void*)0x30000000); + + main(args); + return 0; +}
@@ -8,7 +8,8 @@ char user_numeric[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char user_hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; FILE* fopen(const char* filename, const char* mode) { - return (FILE*)(sc_file_open(filename, NULL)); + (void)mode; + return (FILE*)(sc_file_open(filename, 0)); } size_t fread(void* buffer, size_t size, size_t count, FILE* file) {@@ -66,15 +67,20 @@ return fputs(str, stdout);
} char * fgets ( char * str, int num, FILE * stream ) { - int i = 0; + int i = 0; - while (i < num) { - str[i] = fgetc(stream); - if (str[i] == '\n' || str[i] == EOF) { break; } - i++; - } + while (i < num) { + int res = fgetc(stream); + + if (res == EOF) { return NULL; } + + str[i] = res; + + if (str[i] == '\n') { break; } + i++; + } - return i + 1; + return str; } char * gets ( char * str, int num ) {@@ -160,57 +166,18 @@ return rek_sprint_uint((unsigned int)value, buffer);
} } -int sprint_string(char *str, char *buffer) { - int length = 0; +static size_t sprint_string(const char *str, char *buffer) { + size_t length = 0; while (str[length]) { buffer[length] = str[length]; length++; } return length; } -int sprint_char(char c, char *buffer) { +static size_t sprint_char(char c, char *buffer) { buffer[0] = c; return 1; } -/*void rek_print_uint(unsigned int value) { - if (value > 0) { - rek_print_uint(value / 10); - print_char(user_numeric[value % 10]); - } -}*/ - -void print_uint(unsigned int value) { - char buffer[16]; - int size = sprint_uint(value, buffer); - print(buffer, size); -} - -void print_int(int value) { - char buffer[16]; - int size = sprint_int(value, buffer); - print(buffer, size); -} - -void print_uint32(uint32_t value) { - char buffer[16]; - int size = sprint_uint32(value, buffer); - print(buffer, size); -} - -void print_string(char *str) { - int length = 0; - while (str[length]) { length++; } - print(str, length); -} - -int print_hex_char(uint8_t c) { - char buffer[2]; - int size = sprint_hex_char(c, buffer); - print(buffer, size); -} - int vsprintf(char *str, const char *fmt, va_list args) { - uint32_t index = 0; - int offset = 0; while (*fmt) {@@ -250,7 +217,7 @@ return offset;
} int vfprintf(FILE *file, const char *fmt, va_list args) { - uint8_t buffer[512]; + char buffer[512]; int res = vsprintf(buffer, fmt, args); res = fwrite(buffer, sizeof(uint8_t), res, file); return res;@@ -291,9 +258,9 @@ return res;
} int fseek(FILE* file, long offset, int whence) { - return sc_file_lseek(file, offset, whence); + return sc_file_lseek((int)file, offset, whence); } long ftell(FILE* file) { - return sc_file_tell(file); + return sc_file_tell((int)file); }
@@ -9,7 +9,7 @@ if (str[0] == '+') {
sign = 1; i = 1; } else if (str[0] == '-') { - sign == -1; + sign = -1; i = 1; } else if (str[0] < '0' || str[0] > '9') { return 0;@@ -19,5 +19,5 @@ while (str[i] >= '0' && str[i] <= '9') {
res = 10 * res + (str[i++] - '0'); } - return res; -}+ return sign * res; +}
@@ -8,7 +8,7 @@
void main(char *args) { FILE* file = fopen(args, "r"); - if (file == -1) { + if (file == NULL) { printf("Could not find file: %s\n", args); return; }@@ -20,4 +20,4 @@ int size = fread(buffer, 1, 1024, file);
if (size == 0) { break; } fwrite(buffer, 1, size, stdout); } -}+}
@@ -12,8 +12,8 @@ printf("Could not find file: %s\n", args);
return; } - uint8_t in_buffer[16]; - uint8_t out_buffer[64]; + char in_buffer[16]; + char out_buffer[64]; while (1) { int in_offset = ftell(file);@@ -40,4 +40,4 @@ }
out_offset += sprintf(out_buffer + out_offset, "|\n"); fwrite(out_buffer, 1, out_offset, stdout); } -}+}
@@ -59,11 +59,11 @@ return res;
} typedef struct { - uint8_t* buffer; + char* buffer; int width; int height; - + int x; int y; } image_object;@@ -87,7 +87,7 @@ imgobj->x += xoff;
imgobj->y += yoff; } -void decode_rle(uint8_t* rle_data, int rle_size, uint8_t* img_buf, int width, int height) { +static void decode_rle(char* rle_data, int rle_size, char* img_buf, int width, int height) { image_object imgobj = { .buffer = img_buf, .width = width,@@ -102,7 +102,7 @@ while (1) {
if (data_i >= rle_size) { break; } - + const int count = rle_data[data_i++]; const int value = rle_data[data_i++];@@ -114,18 +114,18 @@ case 0:
/* end of line */ imgbuf_newline(&imgobj); break; - + case 1: /* end of bitmap; ignore for now */ break; - + case 2: /* delta */ uint8_t xoff = rle_data[data_i++]; uint8_t yoff = rle_data[data_i++]; imgbuf_offset(&imgobj, xoff, yoff); break; - + default: /* non-RLE subsequence */ for (int i = 0; i < value; i++) {@@ -145,17 +145,15 @@ }
void main(char *args) { // open image file - const int fd = fopen(args, "r"); + FILE *fd = fopen(args, "r"); - if (fd < 0) { + if (fd == NULL) { printf("Could not find file: %s\n", args); return; } - const int limit = VGA_MODE_13_WIDTH * VGA_MODE_13_HEIGHT; - - const BMP_FILE_HEADER bmp_header; - const DIB_HEADER dib_header; + BMP_FILE_HEADER bmp_header; + DIB_HEADER dib_header; int size = 0;@@ -173,14 +171,14 @@ assert(dib_header.bits_per_pixel == 8);
assert(dib_header.num_color_planes == 1); const int data_size = width * height * (dib_header.bits_per_pixel / 8); - char const* imgbuf = (char const*)malloc(data_size); + char *imgbuf = malloc(data_size); memset(imgbuf, 0, data_size); - + fseek(fd, offset, SEEK_SET); if (dib_header.compression_method == BI_RLE8) { const int rle_size = dib_header.raw_data_size; - char const* databuf = (char const*)malloc(rle_size); + char *databuf = malloc(rle_size); size = fread(databuf, sizeof(uint8_t), rle_size, fd); /* TODO: check returned size */@@ -191,10 +189,6 @@ } else if (dib_header.compression_method == BI_RGB) {
size = fread(imgbuf, sizeof(uint8_t), data_size, fd); /* TODO: check returned size */ } - - int off_x = 0; - int off_y = 0; - // switch video mode and display image graphics_set_mode(GMODE_VIDEO);@@ -217,4 +211,4 @@ if (c == 0x1B || c == 'q') { break; }
} graphics_set_mode(GMODE_TEXT); -}+}
@@ -5,8 +5,9 @@ #include <stdint.h>
void main(char *args) { char buffer[256]; + int index = 0; - int index = 0; + (void)args; while (1) { int next = dir_next(2, index, buffer);@@ -16,4 +17,4 @@
printf("%s\n", buffer); index = next; } -}+}
@@ -50,7 +50,7 @@ buffer[size] = 0x80;
run = 0; } - uint32_t *M = buffer; + uint32_t *M = (uint32_t*)buffer; uint32_t chunk_hash[4] = { hash[0], hash[1], hash[2], hash[3] }; uint32_t F, g;@@ -90,4 +90,4 @@ for (int i = 0; i < 16; i++) {
printf("%x", digest[i]); } printf("\n"); -}+}
@@ -4,6 +4,8 @@
#include <stdint.h> void main(char *args) { + (void)args; + uint32_t memusage = sc_mem_usage(); printf("%i KB\n", memusage / 1000); -}+}
@@ -2,7 +2,8 @@ #include "stdio.h"
#include "stdlib.h" #include "string.h" -int main(char *args) { +void main(char *args) { + (void)args; printf("Malloc test\n"); void* a = malloc(1024);@@ -26,4 +27,4 @@ b = realloc(b, 2048);
printf("Value of a at addr %p: %s\n", a, a); printf("Value of b at addr %p: %s\n", b, b); -}+}
@@ -4,7 +4,9 @@
#include <stdint.h> void main(char *args) { + (void)args; + printf("Thank you for using CeDOS!\n"); hard_reset(); -}+}
@@ -20,7 +20,7 @@ char* buffer = malloc(512);
buffer[0] = 0; struct hist_item* history = hist_first; - + while (1) { c = getchar();@@ -68,7 +68,8 @@ int async[32];
int async_index; void main(char *args) { - uint32_t a = 0, b = 1, i = 0; + (void)args; + printf("\n"); printf("\e[94mShELF shell interface for CeDOS\e[97m\n"); printf("Version: " VERSION "\n");@@ -76,18 +77,21 @@
while (1) { printf("/> "); - char* buffer = read_line(buffer, hist_first); + char* buffer = read_line(); if (strlen(buffer) == 0) { continue; } char *file = buffer; - char *args = strchr(file, ' '); - char *amp = strchr(file, ','); - - if (args != NULL) { - args[0] = 0; - args++; - } + const char *first_space = strchr(file, ' '); + const char *amp = strchr(file, ','); + + char *args = NULL; + + if (first_space != NULL) { + size_t offset = first_space - file; + file[offset] = 0; + args = &(file[offset + 1]); + } if (strcmp(file, "exit") == 0) { printf("Thank you for using ShELF!\n");@@ -119,7 +123,5 @@ async[async_index++] = pid;
} else { process_wait(pid); } - - //printf("Child process %i terminated.\n", pid); } -}+}