Custom bootloader output to terminal not working
Posted: Sat Mar 31, 2012 2:39 pm
The title may be a little misleading. The problem I am running into is outputting to a terminal in my operating system after rolling a custom bootloader (just sets up the GDT, loads the kernel, and jumps to it -- after placing us in protected mode). The kernel then sets up things up for C++ objects (figuring out how to handle ctors and dtors), then calls the main function which just tries to output a message to the terminal and loop forever.
The problem that I am having is the message is never output, and bochs doesn't ever display the message (though it does loop like it should).
Here is the relevant code:
Bootloader:
Loader.asm (first part of the kernel):
main.cpp:
Now, neither function print or kputs work in outputting a string to the screen, but clear does correctly clear the screen when called. Any ideas in what is happening? Because I have been bashing my head on this one for a few hours now and haven't managed to figure it out.
For anyone who is interested in the bochs debug output:
The problem that I am having is the message is never output, and bochs doesn't ever display the message (though it does loop like it should).
Here is the relevant code:
Bootloader:
Code: Select all
[BITS 16]
[ORG 0x7c00]
reset_drive:
mov ah, 0 ; reset command
int 0x13 ; call interrupt 13
or ah, ah ; check for error
jnz reset_drive ; try again if ah != 0
mov ax, 0
mov es, ax
mov bx, 0x1000 ; destination address = 0000:1000
mov ah, 0x02 ; read sector command
mov al, 0x1c; 28 ; number of 512 byte sectors to read
mov ch, 0 ; cylinder = 0
mov cl, 0x02 ; sector = 2
mov dh, 0 ; head = 0
int 0x13 ; call int 13
or ah, ah ; check for error
jnz reset_drive ; try again
cli ; disable interrupts
xor ax, ax
mov ds, ax
lgdt [gdt_desc] ; load the initial gdt descriptor - we will redo
; this in the kernel
mov eax, cr0
or eax, 1 ; set bit 0 to get to protected mode
mov cr0, eax ; set the values
jmp 0x08:clear_pipe
[BITS 32]
clear_pipe:
mov ax, 0x10 ; save data segment identifier
mov ds, ax ; move a valid data segment into the data segment register
mov ss, ax ; move a valid data segment into the stack segment register
mov esp, 0x090000 ; move the stack pointer to 90000h
jmp 0x08:0x01000
gdt:
gdt_null:
dd 0
dd 0
gdt_code:
dw 0x0ffff
dw 0
db 0
db 10011010b
db 11001111b
db 0
gdt_data:
dw 0x0ffff
dw 0
db 0
db 10010010b
db 11001111b
db 0
gdt_end:
gdt_desc:
dw gdt_end - gdt - 1 ; limit (size)
dd gdt ; address of the gdt
times 510-($-$$) db 0
dw 0x0aa55
Code: Select all
global loader ; making entry point visible to linker
extern main ; kmain is defined in kmain.cpp
extern start_ctors ; beginning and end
extern end_ctors ; of the respective
extern start_dtors ; ctors and dtors section,
extern end_dtors ; declared by the linker script
section .text
; reserve initial kernel stack space
STACKSIZE equ 0x4000 ; that's 16k.
loader:
mov esp, stack + STACKSIZE ; set up the stack
push eax ; Multiboot magic number
push ebx ; Multiboot info structure
mov ebx, start_ctors ; call the constructors
jmp .ctors_until_end
.call_constructor:
call [ebx]
add ebx,4
.ctors_until_end:
cmp ebx, end_ctors
jb .call_constructor
call main ; call kernel proper
mov ebx, end_dtors ; call the destructors
jmp $
; jmp .text
.call_destructor:
sub ebx, 4
call [ebx]
.dtors_until_end:
cmp ebx, start_dtors
jb .call_destructor
cli
.hang:
hlt ; halt machine should kernel return
jmp .hang
Code: Select all
#include "system.h"
int x = 0;
int y = 0;
int attrib = 0x0f;
void scroll(void){
unsigned blank = 0x20 | (attrib << 8);
unsigned tmp;
if(y >= 25){
tmp = y - 24;
memcpy((unsigned char*)0xb8000, (unsigned char*)(0xb8000 + tmp * 80), (25 - tmp) * 80 * 2);
memsetw((unsigned short*)0xb8000 + (25 - tmp) * 80, blank, 80);
y = 24;
}
}
void move(void){
unsigned tmp = y * 80 + x;
outportb(0x3D4, 14);
outportb(0x3D5, tmp >> 8);
outportb(0x3D4, 15);
outportb(0x3D5, tmp);
}
void clear(void){
unsigned short blank = 0x20 | (attrib << 8);
int i;
for(i = 0; i < 25; i++){
memsetw((unsigned short*)0xb8000 + i * 80, blank, 80);
}
x = y = 0;
move();
}
void kputc(char c){
unsigned short* where;
unsigned short att = attrib << 8;
if(c == '\b' && x != 0){
x--;
where = (unsigned short*)0xb8000 + (y * 80 + x);
*where = ' ';
}
else if(c == 0x09){
x = (x + 8) & !7;
}
else if(c == '\r'){
x = 0;
}
else if(c == '\n'){
x = 0;
y++;
}
else if(c >= ' '){
where = (unsigned short*)0xb8000 + (y * 80 + x);
*where = c | att;
x++;
}
if(x >= 80){
x = 0;
y++;
}
scroll();
move();
}
void kputs(const char* s){
for(int i = 0, len = strlen(s); i < len; i++){
kputc(s[i]);
}
}
void print(const char* s){
unsigned char* vidmem = (unsigned char*)0xb8000;
unsigned short offset;
unsigned int i;
outportb(0x3d4, 14);
offset = inportb(0x3d5) << 8;
outportb(0x3d4, 15);
offset |= inportb(0x3d5);
for(i = 0; s[i] != 0; i++){
*vidmem = s[i];
vidmem += 2;
}
offset += i;
outportb(0x3d5, (unsigned char)offset);
outportb(0x3d4, 14);
outportb(0x3d5, (unsigned char)(offset >> 8));
}
int main(){
//clear();
//attrib = 0x0f;
//kputs("Hello world\n");
//kputs("Hello world\n");
//kputs("Hello world\n");
print("THIS IS A TEST\n");
for(;;);
return 0;
}
For anyone who is interested in the bochs debug output:
Code: Select all
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins'
00000000000i[ ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[ ] reading configuration from bochsrc.txt
00000000000i[ ] lt_dlhandle is 0xa219c10
00000000000i[PLGIN] loaded plugin libbx_x.so
00000000000i[ ] installing x module as the Bochs GUI
00000000000i[ ] Bochs x86 Emulator 2.4.5
00000000000i[ ] Build from CVS snapshot, on April 25, 2010
00000000000i[ ] System configuration
00000000000i[ ] processors: 1 (cores=1, HT threads=1)
00000000000i[ ] A20 line support: yes
00000000000i[ ] CPU configuration
00000000000i[ ] level: 6
00000000000i[ ] SMP support: no
00000000000i[ ] APIC support: yes
00000000000i[ ] FPU support: yes
00000000000i[ ] MMX support: yes
00000000000i[ ] 3dnow! support: no
00000000000i[ ] SEP support: yes
00000000000i[ ] SSE support: sse2
00000000000i[ ] XSAVE support: no
00000000000i[ ] AES support: no
00000000000i[ ] MOVBE support: no
00000000000i[ ] x86-64 support: yes
00000000000i[ ] 1G paging support: no
00000000000i[ ] MWAIT support: no
00000000000i[ ] VMX support: no
00000000000i[ ] Optimization configuration
00000000000i[ ] RepeatSpeedups support: yes
00000000000i[ ] Trace cache support: yes
00000000000i[ ] Fast function calls: yes
00000000000i[ ] Devices configuration
00000000000i[ ] ACPI support: yes
00000000000i[ ] NE2000 support: yes
00000000000i[ ] PCI support: yes, enabled=yes
00000000000i[ ] SB16 support: yes
00000000000i[ ] USB support: yes
00000000000i[ ] VGA extension support: vbe
00000000000i[MEM0 ] allocated memory at 0xb5072008. after alignment, vector=0xb5073000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] mem block size = 0x00100000, blocks=32
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('/usr/share/bochs/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/38400 ('/usr/share/bochs/VGABIOS-lgpl-latest')
00000000000i[ ] lt_dlhandle is 0xa2556e0
00000000000i[PLGIN] loaded plugin libbx_cmos.so
00000000000i[ ] lt_dlhandle is 0xa255c10
00000000000i[PLGIN] loaded plugin libbx_dma.so
00000000000i[ ] lt_dlhandle is 0xa256178
00000000000i[PLGIN] loaded plugin libbx_pic.so
00000000000i[ ] lt_dlhandle is 0xa256728
00000000000i[PLGIN] loaded plugin libbx_pit.so
00000000000i[ ] lt_dlhandle is 0xa256c38
00000000000i[PLGIN] loaded plugin libbx_vga.so
00000000000i[ ] lt_dlhandle is 0xa257220
00000000000i[PLGIN] loaded plugin libbx_floppy.so
00000000000i[ ] lt_dlhandle is 0xa257a28
00000000000i[PLGIN] loaded plugin libbx_pci.so
00000000000i[ ] lt_dlhandle is 0xa258158
00000000000i[PLGIN] loaded plugin libbx_pci2isa.so
00000000000i[ ] lt_dlhandle is 0xa258660
00000000000i[PLGIN] loaded plugin libbx_unmapped.so
00000000000i[ ] lt_dlhandle is 0xa258c78
00000000000i[PLGIN] loaded plugin libbx_biosdev.so
00000000000i[ ] lt_dlhandle is 0xa259148
00000000000i[PLGIN] loaded plugin libbx_speaker.so
00000000000i[ ] lt_dlhandle is 0xa259758
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.so
00000000000i[ ] lt_dlhandle is 0xa259c30
00000000000i[PLGIN] loaded plugin libbx_gameport.so
00000000000i[ ] lt_dlhandle is 0xa25a110
00000000000i[PLGIN] loaded plugin libbx_pci_ide.so
00000000000i[ ] lt_dlhandle is 0xa25a620
00000000000i[PLGIN] loaded plugin libbx_acpi.so
00000000000i[ ] lt_dlhandle is 0xa25ac48
00000000000i[PLGIN] loaded plugin libbx_ioapic.so
00000000000i[ ] lt_dlhandle is 0xa25b260
00000000000i[PLGIN] loaded plugin libbx_keyboard.so
00000000000i[ ] lt_dlhandle is 0xa25b858
00000000000i[PLGIN] loaded plugin libbx_harddrv.so
00000000000i[ ] lt_dlhandle is 0xa26d618
00000000000i[PLGIN] loaded plugin libbx_serial.so
00000000000i[ ] lt_dlhandle is 0xa26e148
00000000000i[PLGIN] loaded plugin libbx_parallel.so
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Fri Mar 30 02:09:22 2012 (time0=1333098562)
00000000000i[DMA ] channel 4 used by cascade
00000000000i[DMA ] channel 2 used by Floppy Drive
00000000000i[FDD ] fd0: 'floppy.img' ro=0, h=2,t=80,spt=18
00000000000i[PCI ] 440FX Host bridge present at device 0, function 0
00000000000i[PCI ] PIIX3 PCI-to-ISA bridge present at device 1, function 0
00000000000i[VGA ] interval=50000
00000000000i[MEM0 ] Register memory access handlers: 0x000a0000 - 0x000bffff
00000000000i[XGUI ] test_alloc_colors: 16 colors available out of 16 colors tried
00000000000i[XGUI ] font 8 wide x 16 high, display depth = 24
00000000000i[MEM0 ] Register memory access handlers: 0xe0000000 - 0xe0ffffff
00000000000i[VGA ] VBE Bochs Display Extension Enabled
00000000000i[PLGIN] init_dev of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'speaker' plugin device by virtual method
00000000000i[SPEAK] Failed to open /dev/console: Resource temporarily unavailable
00000000000i[SPEAK] Deactivating beep on console
00000000000i[PLGIN] init_dev of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'pci_ide' plugin device by virtual method
00000000000i[PCI ] PIIX3 PCI IDE controller present at device 1, function 1
00000000000i[PLGIN] init_dev of 'acpi' plugin device by virtual method
00000000000i[PCI ] ACPI Controller present at device 1, function 3
00000000000i[PLGIN] init_dev of 'ioapic' plugin device by virtual method
00000000000i[IOAP ] initializing I/O APIC
00000000000i[MEM0 ] Register memory access handlers: 0xfec00000 - 0xfec00fff
00000000000i[PLGIN] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD ] will paste characters every 1000 keyboard ticks
00000000000i[PLGIN] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD ] Using boot sequence floppy, none, none
00000000000i[HD ] Floppy boot signature check is enabled
00000000000i[PLGIN] init_dev of 'serial' plugin device by virtual method
00000000000i[SER ] com1 at 0x03f8 irq 4
00000000000i[PLGIN] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR ] parallel port 1 at 0x0378 irq 7
00000000000i[PLGIN] register state of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] register state of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] register state of 'speaker' plugin device by virtual method
00000000000i[PLGIN] register state of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] register state of 'gameport' plugin device by virtual method
00000000000i[PLGIN] register state of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] register state of 'acpi' plugin device by virtual method
00000000000i[PLGIN] register state of 'ioapic' plugin device by virtual method
00000000000i[PLGIN] register state of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] register state of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] register state of 'serial' plugin device by virtual method
00000000000i[PLGIN] register state of 'parallel' plugin device by virtual method
00000000000i[SYS ] bx_pc_system_c::Reset(HARDWARE) called
00000000000i[CPU0 ] cpu hardware reset
00000000000i[APIC0] allocate APIC id=0 (MMIO enabled) to 0xfee00000
00000000000i[CPU0 ] CPUID[0x00000000]: 00000003 756e6547 6c65746e 49656e69
00000000000i[CPU0 ] CPUID[0x00000001]: 00000f20 00000800 00002000 078bfbff
00000000000i[CPU0 ] CPUID[0x00000002]: 00410601 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x00000003]: 00000000 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x00000004]: 00000000 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000000]: 80000008 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000001]: 00000000 00000000 00000101 2a100800
00000000000i[CPU0 ] CPUID[0x80000002]: 20202020 20202020 20202020 6e492020
00000000000i[CPU0 ] CPUID[0x80000003]: 286c6574 50202952 69746e65 52286d75
00000000000i[CPU0 ] CPUID[0x80000004]: 20342029 20555043 20202020 00202020
00000000000i[CPU0 ] CPUID[0x80000006]: 00000000 42004200 02008140 00000000
00000000000i[CPU0 ] CPUID[0x80000007]: 00000000 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000008]: 00003020 00000000 00000000 00000000
00000000000i[PLGIN] reset of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] reset of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] reset of 'speaker' plugin device by virtual method
00000000000i[PLGIN] reset of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] reset of 'gameport' plugin device by virtual method
00000000000i[PLGIN] reset of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] reset of 'acpi' plugin device by virtual method
00000000000i[PLGIN] reset of 'ioapic' plugin device by virtual method
00000000000i[PLGIN] reset of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] reset of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] reset of 'serial' plugin device by virtual method
00000000000i[PLGIN] reset of 'parallel' plugin device by virtual method
00000000000i[XGUI ] [x] Mouse off
00000003305i[BIOS ] $Revision: 1.247 $ $Date: 2010/04/04 19:33:50 $
00000318042i[KBD ] reset-disable command received
00000436212i[VBIOS] VGABios $Id$
00000436283i[VGA ] VBE known Display Interface b0c0
00000436315i[VGA ] VBE known Display Interface b0c4
00000436984i[VBIOS] VBE Bios $Id$
00000600000i[XGUI ] charmap update. Font Height is 16
00000754500i[BIOS ] Starting rombios32
00000754979i[BIOS ] Shutdown flag 0
00000755618i[BIOS ] ram_size=0x02000000
00000756080i[BIOS ] ram_end=32MB
00000796681i[BIOS ] Found 1 cpu(s)
00000812857i[BIOS ] bios_table_addr: 0x000fbc18 end=0x000fcc00
00000812960i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001140654i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001468584i[P2I ] PCI IRQ routing: PIRQA# set to 0x0b
00001468605i[P2I ] PCI IRQ routing: PIRQB# set to 0x09
00001468626i[P2I ] PCI IRQ routing: PIRQC# set to 0x0b
00001468647i[P2I ] PCI IRQ routing: PIRQD# set to 0x09
00001468657i[P2I ] write: ELCR2 = 0x0a
00001469504i[BIOS ] PIIX3/PIIX4 init: elcr=00 0a
00001477348i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237 class=0x0600
00001479801i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000 class=0x0601
00001482093i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010 class=0x0101
00001482328i[PIDE ] new BM-DMA address: 0xc000
00001482994i[BIOS ] region 4: 0x0000c000
00001485195i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113 class=0x0680
00001485438i[ACPI ] new irq line = 11
00001485450i[ACPI ] new irq line = 9
00001485481i[ACPI ] new PM base address: 0xb000
00001485495i[ACPI ] new SM base address: 0xb100
00001485523i[PCI ] setting SMRAM control register to 0x4a
00001649614i[CPU0 ] Enter to System Management Mode
00001649624i[CPU0 ] RSM: Resuming from System Management Mode
00001813642i[PCI ] setting SMRAM control register to 0x0a
00001822511i[BIOS ] MP table addr=0x000fbcf0 MPC table addr=0x000fbc20 size=0xd0
00001824388i[BIOS ] SMBIOS table addr=0x000fbd00
00001827474i[BIOS ] Firmware waking vector 0x1ff00cc
00001868456i[BIOS ] ACPI tables: RSDP addr=0x000fbe20 ACPI DATA addr=0x01ff0000 size=0x1f18
00001868493i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001869285i[BIOS ] bios_table_cur_addr: 0x000fbe44
00014041544i[BIOS ] Booting from 0000:7c00
00015211418i[FDD ] partial read() on floppy image returns 361/512
00015256373i[FDD ] read() on floppy image returns 0
00102628000p[XGUI ] >>PANIC<< POWER button turned off.
00102628000i[CPU0 ] CPU is in protected mode (active)
00102628000i[CPU0 ] CS.d_b = 32 bit
00102628000i[CPU0 ] SS.d_b = 32 bit
00102628000i[CPU0 ] EFER = 0x00000000
00102628000i[CPU0 ] | RAX=0000000000000005 RBX=0000000000101000
00102628000i[CPU0 ] | RCX=0000000000090002 RDX=00000000000003d5
00102628000i[CPU0 ] | RSP=0000000000105fe0 RBP=0000000000105ff4
00102628000i[CPU0 ] | RSI=00000000000e478c RDI=000000000000ffac
00102628000i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00102628000i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00102628000i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00102628000i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00102628000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf zf af PF cf
00102628000i[CPU0 ] | SEG selector base limit G D
00102628000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00102628000i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 ffffffff 1 1
00102628000i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00102628000i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00102628000i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00102628000i[CPU0 ] | FS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00102628000i[CPU0 ] | GS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00102628000i[CPU0 ] | MSR_FS_BASE:0000000000000000
00102628000i[CPU0 ] | MSR_GS_BASE:0000000000000000
00102628000i[CPU0 ] | RIP=000000000000254b (000000000000254b)
00102628000i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00102628000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00102628000i[CPU0 ] 0x000000000000254b>> jmp .-2 (0x0000254b) : EBFE
00102628000i[CMOS ] Last time is 1333098587 (Fri Mar 30 02:09:47 2012)
00102628000i[XGUI ] Exit
00102628000i[ ] restoring default signal behavior
00102628000i[CTRL ] quit_sim called with exit code 1