feat: premier boot opérationnel avec la commande linux16 de GRUB, reste la gestion VESA

This commit is contained in:
Nicolas Hordé 2019-01-19 00:17:35 +01:00
parent 63f7e530cc
commit 4f7aae792e
17 changed files with 166 additions and 64 deletions

View File

@ -6,5 +6,5 @@ break *0x90200
cont cont
clear *0x90200 clear *0x90200
break main break main
cont ###cont
clear main clear main

View File

@ -3,6 +3,9 @@ set disassembly-flavor att
set architecture i386 set architecture i386
symbol-file ./system/system.sym symbol-file ./system/system.sym
break start break start
break system.c:50 break *0x100000
cont
clear *0x100000
si
break *0x100000
cont cont
clear system.c:50

Binary file not shown.

View File

@ -22,7 +22,7 @@ set timeout=4\n\
set default=0\n\ set default=0\n\
menuentry "cos2000" {\n\ menuentry "cos2000" {\n\
set root=(hd0,1)\n\ set root=(hd0,1)\n\
linux16 /boot/system.sys\n linux16 /boot/system.sys root=hd0,2\n
boot\n\ boot\n\
}" > /mnt/boot/grub/grub.cfg }" > /mnt/boot/grub/grub.cfg
umount /mnt umount /mnt

83
include/boot.h Normal file
View File

@ -0,0 +1,83 @@
/*******************************************************************************/
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
/* */
/* Sources modifiées du noyau Linux linux-source-4.15.0/arch/x86/include/uapi/asm/bootparam.h
/* Sources modifiées du noyau Linux linux-source-4.15.0/arch/x86/include/asm/e820/type.h
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _BOOT
#define _BOOT
#include "types.h"
#include "gdt.h"
#define E820_MAX_ENTRIES 128
typedef struct entrye820 {
u64 addr;
u64 size;
u32 type;
} entrye820 __attribute__((packed));
typedef struct bootparams {
entrye820 e820_table[E820_MAX_ENTRIES];
u32 e820_numbers;
u8* cmdline;
u8 kbflag;
} bootparams;
enum e820_type {
E820_TYPE_RAM = 1,
E820_TYPE_RESERVED = 2,
E820_TYPE_ACPI = 3,
E820_TYPE_NVS = 4,
E820_TYPE_UNUSABLE = 5,
E820_TYPE_PMEM = 7,
E820_TYPE_PRAM = 12,
E820_TYPE_RESERVED_KERN = 128,
};
typedef struct header {
u8 setup_sects;
u16 root_flags;
u32 syssize;
u16 ram_size;
u16 vid_mode;
u16 root_dev;
u16 boot_flag;
u16 jump;
u32 header;
u16 version;
u32 realmode_swtch;
u16 start_sys_seg;
u16 kernel_version;
u8 type_of_loader;
u8 loadflags;
u16 setup_move_size;
u32 code32_start;
u32 ramdisk_image;
u32 ramdisk_size;
u32 bootsect_kludge;
u16 heap_end_ptr;
u8 ext_loader_ver;
u8 ext_loader_type;
u32 cmd_line_ptr;
u32 initrd_addr_max;
u32 kernel_alignment;
u8 relocatable_kernel;
u8 min_alignment;
u16 xloadflags;
u32 cmdline_size;
u32 hardware_subarch;
u64 hardware_subarch_data;
u32 payload_offset;
u32 payload_length;
u64 setup_data;
u64 pref_address;
u32 init_size;
u32 handover_offset;
} __attribute__((packed)) header;
#endif

View File

@ -1,3 +1,6 @@
#ifndef _GDT
#define _GDT
/* Ordre imposé par SYSENTER */ /* Ordre imposé par SYSENTER */
#define SEL_KERNEL_CODE 0x8 /* Selecteur code du kernel */ #define SEL_KERNEL_CODE 0x8 /* Selecteur code du kernel */
#define SEL_KERNEL_STACK 0x10 /* Selecteur pile du kernel */ #define SEL_KERNEL_STACK 0x10 /* Selecteur pile du kernel */
@ -93,3 +96,4 @@ u16 getdesalign(u16 sel);
void setTSS(u32 ss, u32 sp); void setTSS(u32 ss, u32 sp);
#endif #endif
#endif

View File

@ -67,6 +67,7 @@
#include "types.h" #include "types.h"
#include "queue.h" #include "queue.h"
#include "boot.h"
/* Malloc, pour l'attribution de mémoire en heap */ /* Malloc, pour l'attribution de mémoire en heap */
typedef struct tmalloc typedef struct tmalloc
@ -114,8 +115,8 @@ typedef TAILQ_HEAD(page_s, page) page_t;
void physical_range_use(u64 addr, u64 len); void physical_range_use(u64 addr, u64 len);
void physical_range_free(u64 addr, u64 len); void physical_range_free(u64 addr, u64 len);
u8 *physical_page_getfree(void); u8 *physical_page_getfree(void);
void physical_init(void); void physical_init();
void initpaging(void); void initpaging();
void virtual_init(void); void virtual_init(void);
tmalloc *mallocpage(u64 size); tmalloc *mallocpage(u64 size);
void *vmalloc(u32 size); void *vmalloc(u32 size);

View File

@ -1,16 +1,14 @@
/*******************************************************************************/ /*******************************************************************************/
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */ /* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
/* */ /* */
/* Sources modifiées du noyau Linux linux-source-4.15.0/arch/x86/include/uapi/asm/bootparam.h
/* Sources modifiées du noyau Linux linux-source-4.15.0/arch/x86/include/asm/e820/type.h
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#include "types.h" #include "types.h"
#include "gdt.h" #include "gdt.h"
#include "boot.h"
#define E820_MAX_ENTRIES 128
typedef struct entrye820 {
u64 addr;
u64 size;
u32 type;
} entrye820 __attribute__((packed));
typedef struct miniregs { typedef struct miniregs {
union { union {
@ -70,8 +68,7 @@ typedef struct miniregs {
mov %[ediregs],%%edi\n\ mov %[ediregs],%%edi\n\
int %[numregs]\n\ int %[numregs]\n\
pushfl\n\ pushfl\n\
popl %%eax\n\ popl %[eflagsregs]\n\
mov %%eax,%[eflagsregs]\n\
mov %%eax,%[eaxregs]\n\ mov %%eax,%[eaxregs]\n\
mov %%ebx,%[ebxregs]\n\ mov %%ebx,%[ebxregs]\n\
mov %%ecx,%[ecxregs]\n\ mov %%ecx,%[ecxregs]\n\

View File

@ -1,5 +1,7 @@
/*******************************************************************************/ /*******************************************************************************/
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */ /* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
/* */ /* */
#include "boot.h";
extern restart; extern restart;
int main(u8* info);

View File

@ -71,10 +71,13 @@ void initselectors(u32 executingoffset)
movw %%ax, %%es \n \ movw %%ax, %%es \n \
movw %%ax, %%fs \n \ movw %%ax, %%fs \n \
movw %%ax, %%gs \n \ movw %%ax, %%gs \n \
movl %[offset], %%ebx \n \
movw %[stack], %%ax \n \ movw %[stack], %%ax \n \
movl %[offset], %%ebx \n \
movw %%ax, %%ss \n \ movw %%ax, %%ss \n \
movl %[stackoff], %%esp \n \ movl %[stackoff], %%esp \n \
pushl %%ebx \n \
ljmp %[code],$raz \n \
raz:\n \
xor %%eax,%%eax\n\ xor %%eax,%%eax\n\
xor %%ebx,%%ebx\n\ xor %%ebx,%%ebx\n\
xor %%ecx,%%ecx\n\ xor %%ecx,%%ecx\n\
@ -82,7 +85,7 @@ void initselectors(u32 executingoffset)
xor %%esi,%%esi\n\ xor %%esi,%%esi\n\
xor %%edi,%%edi\n\ xor %%edi,%%edi\n\
xor %%ebp,%%ebp\n\ xor %%ebp,%%ebp\n\
jmp %%ebx"::[data] "i"(SEL_KERNEL_DATA),[code] "i"(SEL_KERNEL_CODE),[stack] "i"(SEL_KERNEL_STACK),[stackoff] "i"(KERNEL_STACK_ADDR),[offset] "m"(executingoffset)); ret"::[data] "i"(SEL_KERNEL_DATA),[code] "i"(SEL_KERNEL_CODE),[stack] "i"(SEL_KERNEL_STACK),[stackoff] "i"(KERNEL_STACK_ADDR),[offset] "m"(executingoffset));
} }
/*******************************************************************************/ /*******************************************************************************/

View File

@ -5,10 +5,14 @@
#include "memory.h" #include "memory.h"
#include "queue.h" #include "queue.h"
#include "asm.h" #include "asm.h"
#include "boot.h"
static u8 *kernelcurrentheap = NULL; /* pointeur vers le heap noyau */ static u8 *kernelcurrentheap = NULL; /* pointeur vers le heap noyau */
static u8 bitmap[MAXMEMPAGE / 8]; /* bitmap */ static u8 bitmap[MAXMEMPAGE / 8]; /* bitmap */
static vrange_t vrange_head; static vrange_t vrange_head;
static u64 memorysize=0;
extern bootparams* allparams;
/*******************************************************************************/ /*******************************************************************************/
/* Erreur fatale */ /* Erreur fatale */
@ -154,20 +158,7 @@ void vfree(void *vaddr)
u64 physical_getmemorysize() u64 physical_getmemorysize()
{ {
/*u64 maxaddr = 0; return memorysize;
struct multiboot_tag_mmap *tag = getgrubinfo_mem();
multiboot_memory_map_t *mmap;
for (mmap = ((struct multiboot_tag_mmap *) tag)->entries;
(u8 *) mmap < (u8 *) tag + tag->size;
mmap =
(multiboot_memory_map_t *) ((unsigned long) mmap +
((struct multiboot_tag_mmap *)
tag)->entry_size))
if ((mmap->addr + mmap->len > maxaddr) && mmap->type == 1)
maxaddr = mmap->addr + mmap->len;
if (maxaddr >= MAXMEMSIZE)
maxaddr = MAXMEMSIZE - 1;
return maxaddr;*/
} }
/*******************************************************************************/ /*******************************************************************************/
@ -258,24 +249,22 @@ u64 getmemoryfree(void)
/*******************************************************************************/ /*******************************************************************************/
/* Initialisation du bitmap pour la gestion physique de la mémoire */ /* Initialisation du bitmap pour la gestion physique de la mémoire */
void physical_init(void) void physical_init()
{ {
/*u64 page; for (u64 page = 0; page < sizeof(bitmap); page++)
for (page = 0; page < sizeof(bitmap); page++)
bitmap[page] = 0xFF; bitmap[page] = 0xFF;
struct multiboot_tag_mmap *tag = getgrubinfo_mem(); for (u8 i=0;i<allparams->e820_numbers;i++)
multiboot_memory_map_t *mmap; {
for (mmap = ((struct multiboot_tag_mmap *) tag)->entries; if (allparams->e820_table[i].type == E820_TYPE_RAM)
(u8 *) mmap < (u8 *) tag + tag->size; physical_range_free(allparams->e820_table[i].addr, allparams->e820_table[i].size);
mmap =
(multiboot_memory_map_t *) ((unsigned long) mmap +
((struct multiboot_tag_mmap *)
tag)->entry_size))
if (mmap->type == 1)
physical_range_free(mmap->addr, mmap->len);
else else
physical_range_use(mmap->addr, mmap->len); physical_range_use(allparams->e820_table[i].addr, allparams->e820_table[i].size);
physical_range_use(0x0, KERNELSIZE);*/ if ((allparams->e820_table[i].addr + allparams->e820_table[i].size > memorysize) && allparams->e820_table[i].type == E820_TYPE_RAM)
memorysize = allparams->e820_table[i].addr + allparams->e820_table[i].size;
if (memorysize >= MAXMEMSIZE)
memorysize = MAXMEMSIZE - 1;
}
physical_range_use(0x0, KERNELSIZE);
} }
/*******************************************************************************/ /*******************************************************************************/
@ -668,7 +657,7 @@ void registry_init(void)
/*******************************************************************************/ /*******************************************************************************/
/* Initialisation de la mémoire paginée */ /* Initialisation de la mémoire paginée */
void initpaging(void) void initpaging()
{ {
identity_init(); identity_init();
registry_init(); registry_init();

View File

@ -139,7 +139,7 @@ qemu64: killer
qemu-system-x86_64 -m 5G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd --enable-kvm -cpu host -s & qemu-system-x86_64 -m 5G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd --enable-kvm -cpu host -s &
system/system.sys: system/system.sys:
make -C system VESA=$(VESA) make -C system
final/harddisk.img.final: final/harddisk.img.final:
make -C final harddisk.img.final make -C final harddisk.img.final

View File

@ -35,7 +35,7 @@ piggy.o: piggy.S
$(ASM) $@ $^ $(ASM) $@ $^
system: system.o system_asm.o ../lib/libs.o system: system.o system_asm.o ../lib/libs.o
$(LINK) -T system.ld system.o ../lib/libs.o $(LINK) -T system.ld system.o system_asm.o ../lib/libs.o
$(OBJDEBUG) system system.sym $(OBJDEBUG) system system.sym
$(NM) system > system.map $(NM) system > system.map

View File

@ -6,11 +6,10 @@
#include "setup.h" #include "setup.h"
#include "memory.h" #include "memory.h"
struct params { extern hdr;
entrye820 e820_table[E820_MAX_ENTRIES];
u32 e820_numbers; /* paramètres de démarrage */
u8 kbflag; static bootparams params;
} params;
/* registre gdt */ /* registre gdt */
static struct gdtr gdtreg; static struct gdtr gdtreg;
@ -273,17 +272,31 @@ void initpmode()
gotopmode(); gotopmode();
} }
void initparams()
{
//params.cmdline=((header*)hdr)->cmd_line_ptr;
asm("xorl %%eax,%%eax\n \
movw %%ds,%%ax\n \
shl $4,%%eax\n \
addw %[bootparams],%%ax \n \
movl %%eax,%%cr3"::[bootparams] "i" (&params));
}
void initvideo()
{
}
void main(void) void main(void)
{ {
showstr("*** Chargement de COS2000 - mode reel ***\r\n"); showstr("*** Chargement de COS2000 - mode reel ***\r\n");
/* initparams(); */ initparams();
showstr(" -Initialisation de la memoire\r\n"); showstr(" -Initialisation de la memoire\r\n");
initmemory(); initmemory();
showstr(" -Initialisation du clavier\r\n"); showstr(" -Initialisation du clavier\r\n");
initkeyboard(); initkeyboard();
/* initvideo(); */
showstr(" -Initialisation du coprocesseur\r\n"); showstr(" -Initialisation du coprocesseur\r\n");
initcoprocessor(); initcoprocessor();
showstr(" -Passage en mode protege\r\n"); showstr(" -Initialisation video & passage en mode protege\r\n");
initvideo();
initpmode(); initpmode();
} }

View File

@ -16,6 +16,7 @@
#include "syscall.h" #include "syscall.h"
#include "memory.h" #include "memory.h"
#include "system.h" #include "system.h"
#include "boot.h"
static u8 warnmsg[] = static u8 warnmsg[] =
"\033[150C\033[8D\033[37m\033[1m[ \033[36mNON\033[37m ]\033[0m"; "\033[150C\033[8D\033[37m\033[1m[ \033[36mNON\033[37m ]\033[0m";
@ -28,6 +29,8 @@ static u8 key = 0;
extern wrapper_timer; extern wrapper_timer;
extern wrapper_interruption20; extern wrapper_interruption20;
bootparams* allparams;
void ok() void ok()
{ {
print(okmsg); print(okmsg);
@ -46,9 +49,10 @@ void error()
return; return;
} }
int main(u8* info) void main(bootparams** params)
{ {
cli(); cli();
allparams=params;
initdriver(); initdriver();
registerdriver(&vgafonctions); registerdriver(&vgafonctions);
registerdriver(&vesafonctions); registerdriver(&vesafonctions);
@ -62,7 +66,7 @@ int main(u8* info)
print("\033[37m\033[0m -Initilisation de la memoire virtuelle"); print("\033[37m\033[0m -Initilisation de la memoire virtuelle");
initgdt(&&next); initgdt(&&next);
next: next:
initpaging(); initpaging(*allparams);
remap_memory(VESA_FBMEM); remap_memory(VESA_FBMEM);
ok(); ok();

View File

@ -9,6 +9,7 @@ SECTIONS
. = 0x100000; . = 0x100000;
.text ALIGN(16): { .text ALIGN(16): {
_text = .; _text = .;
*(.inittext)
*(.text) *(.text)
} }
.data ALIGN(16): { .data ALIGN(16): {

View File

@ -6,7 +6,7 @@
#include "gdt.h" #include "gdt.h"
.code32 .code32
.section ".text" .section ".inittext"
.global start .global start
.extern main .extern main
start: start:
@ -17,7 +17,9 @@ start:
movw %ax, %gs movw %ax, %gs
movw $SEL_KERNEL_STACK, %ax movw $SEL_KERNEL_STACK, %ax
movw %ax, %ss movw %ax, %ss
movl $KERNEL_STACK_ADDR, %esp movl %cr3, %eax
mov $KERNEL_STACK_ADDR,%esp
pushl %eax
xor %eax,%eax xor %eax,%eax
xor %ebx,%ebx xor %ebx,%ebx
xor %ecx,%ecx xor %ecx,%ecx
@ -25,4 +27,4 @@ start:
xor %esi,%esi xor %esi,%esi
xor %edi,%edi xor %edi,%edi
xor %ebp,%ebp xor %ebp,%ebp
call main calll main