Commit b4706cfd authored by Yury Popov's avatar Yury Popov

Bootloader data show

Showing bootloader data, such as:
addresses
flags
command line
bootloader name (if present)
parent c882d917
......@@ -5,7 +5,7 @@ CFLAGS=-c -nostdlib -s -m64 -O2
BIN=bin/pxkrnl
all: $(BIN) clean
$(BIN): obj/x32to64.o obj/boot.o obj/memory.o obj/pxlib.o obj/interrupts.o
$(BIN): obj/x32to64.o obj/boot.o obj/memory.o obj/pxlib.o obj/interrupts.o obj/multiboot_info.o
$(LD) -belf64-x86-64 -o $(BIN) -s --nostdlib $?
$(OBJCOPY) $(BIN) -Felf32-i386
obj/boot.o : src/boot.cpp obj
......@@ -16,6 +16,8 @@ obj/memory.o : src/memory.cpp obj
$(CC) $(CFLAGS) src/memory.cpp -o $@
obj/interrupts.o : src/interrupts.cpp obj
$(CC) $(CFLAGS) src/interrupts.cpp -o $@
obj/multiboot_info.o : src/multiboot_info.cpp obj
$(CC) $(CFLAGS) src/multiboot_info.cpp -o $@
obj/x32to64.o : src/x32to64.s obj
fasm src/x32to64.s obj/x32to64.o
clean:
......
No preview for this file type
#include "pxlib.h"
_uint64 rdtsc(){
unsigned long eax, edx;
asm("rdtsc":"=a"(eax),"=d"(edx));
_uint64 ret;
ret = edx; ret <<= 32; ret |= eax;
return ret;
}
int main()
{
clrscr();
memory_init();
interrupts_init();
_uint64 start, end;
short c=0;
for(;;){
c++;
print("Stage ");prints(c);print(": 255 allocs of 16 bytes");
start = rdtsc();
for(int i=0; i<255; i++) malloc(0x10);
end = rdtsc();
print(" in 0x");printq(end-start);print(" tacts\n");
}
kernel_info();
}
#include "interrupts.h"
PIDT idt = 0;
PHCODE handlers = 0;
INTERRUPT32 interrupts32[256];
asm volatile("\
_interrupt_handler:\n\
\
......@@ -97,7 +98,7 @@ void interrupts_init()
outportb(0xA1, 0x02);
outportb(0x21, 0x01);
outportb(0xA1, 0x01);
outportb(0x21, 0xFC);
outportb(0xA1, 0xFF);
outportb(0x21, 0);
outportb(0xA1, 0);
asm volatile( "lidtq %0\nsti"::"m"(idt->rec));
}
\ No newline at end of file
......@@ -8,6 +8,13 @@ typedef struct {
long addr;
char ret;
} __attribute__ ((packed)) HCODE, *PHCODE;
typedef struct {
short offset_low;
short selector;
char zero;
char type;
short offset_middle;
} INTERRUPT32, *PINTERRUPT32;
typedef struct {
short offset_low;
short selector;
......@@ -16,14 +23,15 @@ typedef struct {
short offset_middle;
long offset_high;
long reserved;
} INTERRUPT, *PINTERRUPT;
} INTERRUPT64, *PINTERRUPT64;
typedef struct {
short limit;
void* addr;
} __attribute__ ((packed)) IDTR, *PIDTR;
typedef struct {
INTERRUPT ints[256];
INTERRUPT64 ints[256];
IDTR rec;
} IDT, *PIDT;
extern INTERRUPT32 interrupts32[256];
extern void interrupts_init();
#endif
......@@ -30,12 +30,33 @@ PPML4E get_page(void* base_addr)
void memory_init()
{
_int64 kernel = 0, stack = 0, stack_top = 0, data = 0, data_top = 0, bss = 0, bss_top = 0;
asm("movq $_start, %q0":"=a"(kernel)); stack = kernel - 0x4000; stack_top = kernel;
asm("movq $_start, %q0":"=a"(kernel)); stack = kernel - 0x10000; stack_top = kernel;
asm("movq $__data_start__, %q0":"=a"(data));
asm("movq $__rt_psrelocs_end, %q0":"=a"(data_top)); data_top = (data_top & 0xFFFFF000) + 0x1000;
asm("movq $__bss_start__, %q0":"=a"(bss));
asm("movq $__bss_end__, %q0":"=a"(bss_top)); bss_top = (bss_top & 0xFFFFF000) + 0x1000;
// Buffering BIOS interrupts
for(int i=0; i<256; i++){
PINTERRUPT32 intr = (PINTERRUPT32)(((_int64)i & 0xFF)*sizeof(INTERRUPT32));
interrupts32[i] = *intr;
}
// Buffering grub data
GRUB gd = *grub_data;
char cmdline[256], bl_name[256]; long cmdlinel = 0, blnamel = 0;
if(((gd.flags & 4) == 4) && (gd.pcmdline != 0)){
char* c = (char*)(((_int64)gd.pcmdline) & 0xFFFFFFFF);
int i = 0;
while((c[i] != 0)&&(i<255)) cmdline[i++] = c[i];
cmdline[i] = 0;
cmdlinel = i+1;
}
if(((gd.flags & 512) == 512) && (gd.pboot_loader_name != 0)){
char* c = (char*)(((_int64)gd.pboot_loader_name) & 0xFFFFFFFF);
int i = 0;
while((c[i] != 0)&&(i<255)) bl_name[i++] = c[i];
bl_name[i] = 0;
blnamel = i+1;
}
// Initialization of pagetables
(*(_int64*)(get_page((void*)0x00000000))) &= 0xFFFFFFFFFFFFFFF0; // BIOS Data
for(_int64 addr = 0x0A0000; addr < 0x0C8000; addr += 0x1000) // Video data & VGA BIOS
......@@ -91,6 +112,26 @@ void memory_init()
}
}
}
grub_data = (PGRUB)malloc(sizeof(GRUB));
*grub_data = gd;
if(cmdlinel > 0) {
char* c = (char*)malloc(cmdlinel);
grub_data->pcmdline = (long)((_int64)c & 0xFFFFFFFF);
for(int i=0; i<cmdlinel; i++){
c[i] = cmdline[i];
}
} else {
grub_data->pcmdline = 0;
}
if(blnamel > 0) {
char* c = (char*)malloc(blnamel);
grub_data->pboot_loader_name = (long)((_int64)c & 0xFFFFFFFF);
for(int i=0; i<blnamel; i++){
c[i] = bl_name[i];
}
} else {
grub_data->pboot_loader_name = 0;
}
}
void memmap()
{
......
#ifndef MEMORY_H
#define MEMORY_H
#include "pxlib.h"
#include "interrupts.h"
typedef void* PML4E, **PPML4E;
typedef PPML4E PDPE, *PPDPE;
typedef PPDPE PDE, *PPDE;
......@@ -8,16 +10,17 @@ typedef struct {
long flags;
long mem_lower, mem_upper;
long boot_device;
char *cmdline;
long pcmdline;
long mods_count; void *mods_addr;
long syms[3];
long mmap_length; void *mmap_addr;
long drives_length; void *drives_addr;
void *config_table;
char *boot_loader_name;
void *apm_table;
void *vbe_control_info, *vbe_mode_info, *vbe_mode, *vbe_interface_seg, *vbe_interface_off, *vbe_interface_len;
long pconfig_table;
long pboot_loader_name;
long papm_table;
long pvbe_control_info, pvbe_mode_info, pvbe_mode, pvbe_interface_seg, pvbe_interface_off, pvbe_interface_len;
} GRUB, *PGRUB;
typedef struct {
void* addr;
_int64 size;
......@@ -32,5 +35,4 @@ extern void memmap();
extern void memory_init();
extern void* palloc(char sys = 0);
extern void* malloc(_int64 size, int align = 4);
#include "pxlib.h"
#endif
#include "multiboot_info.h"
void kernel_info()
{
_int64 kernel = 0, stack = 0, stack_top = 0, data = 0, data_top = 0, bss = 0, bss_top = 0;
asm("movq $_start, %q0":"=a"(kernel)); stack = kernel - 0x10000; stack_top = kernel;
asm("movq $__data_start__, %q0":"=a"(data));
asm("movq $__rt_psrelocs_end, %q0":"=a"(data_top)); data_top = (data_top & 0xFFFFF000) + 0x1000;
asm("movq $__bss_start__, %q0":"=a"(bss));
asm("movq $__bss_end__, %q0":"=a"(bss_top)); bss_top = (bss_top & 0xFFFFF000) + 0x1000;
print(" - Kernel info:\n");
print("Kernel:\t"); printq(kernel); print("\n");
print("Stack:\t"); printq(stack); print(" - "); printq(stack_top-1); print("\n");
print("Data:\t"); printq(data); print(" - "); printq(data_top-1); print("\n");
print("BSS:\t"); printq(bss); print(" - "); printq(bss_top-1); print("\n");
print(" - Grub data:\n");
print("Flags:\t"); printl(grub_data->flags); print("\n");
if((grub_data->flags & 1) == 1) {
print("Mem:\t"); printl(grub_data->mem_lower); print(" - "); printl(grub_data->mem_upper); print("\n");
}
if((grub_data->flags & 2) == 2) {
print("Boot device:\t"); printl(grub_data->boot_device); print("\n");
}
if(((grub_data->flags & 4) == 4) && (grub_data->pcmdline != 0)) {
print("Command line:\t"); print((char*)((_int64)grub_data->pcmdline & 0xFFFFFFFF)); print("\n");
}
if(((grub_data->flags & 512) == 512) && (grub_data->pboot_loader_name != 0)) {
print("Loader name:\t"); print((char*)((_int64)grub_data->pboot_loader_name & 0xFFFFFFFF)); print("\n");
}
}
\ No newline at end of file
#ifndef MULTIBOOT_INFO_H
#define MULTIBOOT_INFO_H
#include "pxlib.h"
#include "memory.h"
extern void kernel_info();
#endif
......@@ -15,6 +15,8 @@ uint __inline inportl(short port){ uint c; asm("inl %w1, %d0":"=a"(c):"d"(port))
void __inline outportb(short port, char c){ asm("outb %b0, %w1"::"a"(c),"d"(port)); }
void __inline outports(short port, short c){ asm("outw %w0, %w1"::"a"(c),"d"(port)); }
void __inline outportl(short port, uint c){ asm("outl %d0, %w1"::"a"(c),"d"(port)); }
_uint64 __inline rdtsc() { unsigned long eax, edx; asm("rdtsc":"=a"(eax),"=d"(edx)); _uint64 ret; ret = edx; ret <<= 32; ret |= eax; return ret; }
#include "memory.h"
#include "interrupts.h"
#include "multiboot_info.h"
#endif
......@@ -21,6 +21,28 @@ multiboot_entry:
jne .NoMultiboot
mov [grub_data], ebx
; Moving command line to 0x100000
mov eax, ebx
mov eax, [eax]
test eax, 4 ; Test for cmdline flag
jz .nocmdline
mov eax, ebx
mov eax, [eax+16]
test eax, eax ; Cmdline pointer is 0
jz .nocmdline
mov dword [ebx+16], 0x100000
mov ebx, 0x100000
.cmdloop:
mov cl, [eax]
mov [ebx], cl
cmp cl, 0
jz .nocmdline
inc eax
inc ebx
jmp .cmdloop
.nocmdline:
mov dx, 0x3D4
mov al, 0xA
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment