diff --git a/src/Makefile b/src/Makefile index 9d4f0d3..43e5fd7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,6 +14,7 @@ OBJS = \ userland.o \ launch_process.o \ paging.o \ + myfile.o \ DEPS = $(OBJS:.o=.d) diff --git a/src/gdt.c b/src/gdt.c index aa54e17..1a72ff3 100644 --- a/src/gdt.c +++ b/src/gdt.c @@ -69,8 +69,6 @@ void init_gdt(void) kgdtr.limit = sizeof(gdt) - 1; kgdtr.base = (u32) gdt; - memcpy((char*) 0x30000, &userland, 1000); - gdt[0] = init_descriptor((struct segment_desc_param) { .Limit_1 = 0, .Base = 0, .Type = 0, .S = 0, .DPL = 0, .P = 0, .Limit_2 = 0, .AVL = 0, .L = 0, .D_B = 0, .G = 0 }); diff --git a/src/kernel.c b/src/kernel.c index 9460275..711c1b4 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -8,9 +8,138 @@ #include "tss.h" #include "launch_process.h" +struct elf_header +{ + u32 magic_number; + u8 bus_size; + u8 endianess; + u8 elf_header_version; + u8 os_abi; + u32 unused[2]; + u16 type; + u16 isa; + u32 elf_version; + u32 entry_point; + u32 program_header_table_offset; + u32 section_header_table_offset; + u32 flags; + u16 header_size; + u16 program_header_table_entry_size; + u16 number_of_program_header_table_entries; + u16 size_of_program_header_table_entries; + u16 number_of_section_header_table_entries; + u16 index_section_header_table_with_section_names; +} __attribute__((packed)); + +struct elf_header_table +{ + u32 segment_type; + u32 p_offset; + u32 p_vaddr; + u32 undefined; + u32 p_filesz; + u32 p_memsz; + u32 flags; + u32 alignment; +} __attribute__((packed)); + +int read_cdrom(u16 port, bool slave, u32 lba, u32 sectors, u16 *buffer); + +extern char _binary_a_out_start[]; +extern char _binary_a_out_end[]; +extern char _binary_a_out_size[]; + void main(void) { - DEBUG_INFO("Entering Main Function"); + char *data_start = &_binary_a_out_start; + char *data_end = _binary_a_out_end; + size_t data_size = (size_t)_binary_a_out_size; + + DEBUG_INFO("data_start: %d", _binary_a_out_start); + DEBUG_INFO("data_start: %d", _binary_a_out_end); + DEBUG_INFO("data_start: %d", _binary_a_out_size); + + // write_serial("\n"); + // write_serial("mem "); + // write_serial_nb(data_start, false); + // write_serial(": "); + // for (char * j = data_start; j < data_start + 0x1005; j++) + // { + // if ((u32) j % 25 == 0) + // { + // write_serial("\n"); + // write_serial("mem "); + // write_serial_nb(j, false); + // write_serial(": "); + // write_serial_nb((u8) *j, false); + // } + // else + // { + // write_serial(": "); + // write_serial_nb((u8) *j, false); + // } + // } + + struct elf_header *elf_header = (struct elf_header *) data_start; + + write_serial("ELF: "); + char *magic_number = (char *) &elf_header->magic_number; + DEBUG_INFO("ELF: %c%c%c%c", magic_number[0], magic_number[1], magic_number[2], magic_number[3]); + DEBUG_INFO("ELF ENTRY POINT: %d", elf_header->entry_point); + DEBUG_INFO("PROGRAM HEADER ENTRY POINT: %d", elf_header->program_header_table_offset); + DEBUG_INFO("NUMBER PROGRAM HEADER ENTRY: %d", elf_header->number_of_program_header_table_entries); + + for (int i = 0; i < elf_header->number_of_program_header_table_entries; i++) + { + struct elf_header_table *elf_header_table = (struct elf_header_table *) (data_start + elf_header->program_header_table_offset + sizeof(struct elf_header_table) * i); + DEBUG_INFO("ELF HEADER TABLE OFFSET: %d, %d", elf_header_table, (u32) elf_header_table - (u32) data_start); + char *segment_type = (char *) &elf_header_table->segment_type; + DEBUG_INFO("ELF HEADER TYPE: %d%d%d%d", segment_type[0], segment_type[1], segment_type[2], segment_type[3]); + if (segment_type[0] == 1 && elf_header_table->p_vaddr < 0x100000) + { + DEBUG_INFO("-------------------"); + DEBUG_INFO("CODE DATA: %d", elf_header_table->p_offset); + DEBUG_INFO("CODE START: %d", elf_header_table->p_vaddr); + DEBUG_INFO("CODE SIZE: %d", elf_header_table->p_filesz); + DEBUG_INFO("ALLOCATION SIZE: %d", elf_header_table->p_memsz); + DEBUG_INFO("CODE START SECTOR: %d", data_start + elf_header_table->p_offset); + memcpy(elf_header_table->p_vaddr, data_start + elf_header_table->p_offset, elf_header_table->p_filesz); + // for (int j = 0; j < 0x22; j++) + // { + // write_serial_nb(*(data_start + 0x1000 + j), false); + // write_serial(" "); + // } + DEBUG_INFO("CODE: %d", *(data_start + elf_header_table->p_offset)); + if (elf_header_table->p_filesz < elf_header_table->p_memsz) + { + for (int i = elf_header_table->p_filesz; i < elf_header_table->p_memsz; i++) + { + elf_header_table->p_vaddr[data_start + elf_header_table->p_offset + i] = 0; + } + } + DEBUG_INFO("-------------------"); + } + } + + u32 *add = 0x30000; + DEBUG_INFO("data_start: %d", *add); + + + // for (int i = 0; i < 100; i++) + // { + // DEBUG_INFO("bin(%d) %d", i, data_start[i]); + // } + + DEBUG_INFO("Entering Main Function"); + + // for (int i = 0; i < 100000; i++) + // { + // u16 bufffer[2048]; + // int read_cdrom_ret = read_cdrom(0x0172, 0, i, 1, bufffer); + + // // DEBUG_INFO("read_cdrom returned %d", read_cdrom_ret); + // DEBUG_INFO("read_cdrom returned %s", bufffer); + // } launch_process(0x28, 0x30000, 0x20, 0x18, 0x20); @@ -23,7 +152,6 @@ void main(void) void kernel_main(void) { - init_serial(); DEBUG_INFO("Starting kernel"); diff --git a/src/myfile.o b/src/myfile.o new file mode 100644 index 0000000..644e412 Binary files /dev/null and b/src/myfile.o differ diff --git a/src/paging.c b/src/paging.c index 0a4118e..98098c9 100644 --- a/src/paging.c +++ b/src/paging.c @@ -3,8 +3,8 @@ #include "debug.h" #include "serial.h" -struct page_directory_entry *page_dir = 0x2000; //__attribute__((aligned(4096))); -struct page_table_entry *page_table = 0x3000; //__attribute__((aligned(4096))); +struct page_directory_entry *page_dir = 0x5000; //__attribute__((aligned(4096))); +struct page_table_entry *page_table = 0x6000; //__attribute__((aligned(4096))); struct page_directory_param { u8 P; diff --git a/src/userland.c b/src/userland.c index 51821fb..8555bd6 100644 --- a/src/userland.c +++ b/src/userland.c @@ -1,26 +1,26 @@ #include "userland.h" -void userland(void) -{ - int res = 0; - // asm ("mov $1, %0" : "=r" (res)); - // asm volatile ("movl $2, %eax; int $0x30"); - // asm("movl %%eax,%0" : "=r"(res)); - // asm volatile ("int $0x30"); - char *str = (void *) 0x30100; - str[0] = 'H'; - str[1] = 'e'; - str[2] = 'l'; - str[3] = 'l'; - str[4] = 'o'; - str[5] = '\0'; - asm volatile ("mov $1, %%eax; movl $0x30100, %%ebx; int $0x30; movl %%eax, %1" : "=m" (str), "=r" (res)); - // asm volatile ("mov $1, %%eax; movl %0, %%ebx; int $0x30" : "=m" (str)); - // asm ("mov $1, %eax; int $0x30"); - // asm ("movl %0, %eax; int $0x30" : "=m" (res)); - // asm ("movl %eax, %eax; int $0x30"); - // asm ("movl $28, %eax; movl $5, %ebx; int $0x30"); - // asm ("movl $43, %eax; movl $7, %ebx; int $0x30"); - while (1); - return; /* never go there */ -} \ No newline at end of file +// void userland(void) +// { +// int res = 0; +// // asm ("mov $1, %0" : "=r" (res)); +// // asm volatile ("movl $2, %eax; int $0x30"); +// // asm("movl %%eax,%0" : "=r"(res)); +// // asm volatile ("int $0x30"); +// char *str = (void *) 0x30100; +// str[0] = 'H'; +// str[1] = 'e'; +// str[2] = 'l'; +// str[3] = 'l'; +// str[4] = 'o'; +// str[5] = '\0'; +// asm volatile ("mov $1, %%eax; movl $0x30100, %%ebx; int $0x30; movl %%eax, %1" : "=m" (str), "=r" (res)); +// // asm volatile ("mov $1, %%eax; movl %0, %%ebx; int $0x30" : "=m" (str)); +// // asm ("mov $1, %eax; int $0x30"); +// // asm ("movl %0, %eax; int $0x30" : "=m" (res)); +// // asm ("movl %eax, %eax; int $0x30"); +// // asm ("movl $28, %eax; movl $5, %ebx; int $0x30"); +// // asm ("movl $43, %eax; movl $7, %ebx; int $0x30"); +// while (1); +// return; /* never go there */ +// } \ No newline at end of file