Page 1 of 2

Custom bootloader output to terminal not working

Posted: Sat Mar 31, 2012 2:39 pm
by BetaWar
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:

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
Loader.asm (first part of the kernel):

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
main.cpp:

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;
}
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:

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

Re: Custom bootloader output to terminal not working

Posted: Sat Mar 31, 2012 2:59 pm
by bluemoon
I have not read the full source but this may give a hint:
00102628000i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
If the compiler generated stos* for writing to B8000 it will be out of limit.

Re: Custom bootloader output to terminal not working

Posted: Sat Mar 31, 2012 3:11 pm
by BetaWar
Yes, it is intended to write a byte to a short value, that is the the backspace case, in which we want to leave a blank at the previous character unless we are already at the column head (x == 0). Lower down we have this code:

Code: Select all

where = (unsigned short*)0xb8000 + (y * 80 + x);
    *where = c | att;
    x++;
which is used to setting the color and character to be written in a space.
bluemoon wrote:I have not read the full source but this may give a hint:
00102628000i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
If the compiler generated stos* for writing to B8000 it will be out of limit.
I am not sure if this is an error or not honestly. My bootloader sets es to 0 and then I never reset it to anything else, so I believe it is probably correct (unless there is something I haven't read yet that says it should have a different value... quite possible.).

Re: Custom bootloader output to terminal not working

Posted: Sat Mar 31, 2012 3:18 pm
by turdus

Code: Select all

[ORG 0x7c00]
...and...
  mov bx, 0x1000 ; destination address = 0000:1000
As soon as your kernel became bigger than 6C00h, you'll be in big trouble. Also, I'm not sure you're using raw binary for your kernel, and the first byte is at label loader...

Code: Select all

jmp 0x08:0x01000
You don't have to use serial to debug to bochs' terminal, see E9h hack.

Re: Custom bootloader output to terminal not working

Posted: Sat Mar 31, 2012 3:53 pm
by Rudster816
1. The BIOS assumes you have a valid stack setup, so you NEED to have one setup before calling the floppy functions. The reason it's working is probably dumb luck.
2. You're not putting the color in to the video memory.

Try this

Code: Select all

  for(i = 0; s[i] != 0; i++){
    *vidmem = s[i];
    *(vidmem + 1) = 0x7; // White text on black background. 
    vidmem += 2;
  
http://wiki.osdev.org/Drawing_In_Protected_Mode

Explains everything you need to know.

turdus wrote: You don't have to use serial to debug to bochs' terminal, see E9h hack.
It literally took me 2 minutes to setup printing strings to a file\terminal over the serial port, and it will work on all VM's. It really helps because you can dump as much information as you want without having to worry about it all fitting on the screen\kernel panics\etc.

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 2:13 am
by Combuster
Are you even sure loader is (even) called? You haven't defined it's location in the binary but are merely assuming it comes first.

Also:
//kputs("Hello world\n");
Does not print anything.

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 4:51 am
by turdus
Rudster816 wrote:It literally took me 2 minutes to setup printing strings to a file\terminal over the serial port, and it will work on all VM's.
1. Using E9h hack is so simple that you simply cannot mess it up ("out 0E9h, al", that's all) as opposite to serial where you need to specify protocol parameters and must check whether it's ready to send the byte.
2. Considering the difficulty of the problem OP has, I assume he/she is not an experienced hardware programmer, so I would choose the simplier way if I were the OP.
3. No need to work on all VMs, OP is using bochs.
It really helps because you can dump as much information as you want without having to worry about it all fitting on the screen\kernel panics\etc.
Yes, but it also applies to E9h, so your argument is invalid.

To OP. Use this, and tell us what's bochs output says:

Code: Select all

reset_drive:
mov al, '1'
out 0E9h, al

Code: Select all

clear_pipe:
mov al, '2'
out 0E9h, al

Code: Select all

mov al, '3'
out 0E9h, al
  jmp 0x08:0x01000

Code: Select all

loader:
mov al, '4'
out 0E9h, al
I'm pretty sure it won't make it to '4'. (Although you haven't posted your linker script, but starting kernel with code could cause problems later. You should need an aout header at a minimum).

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 5:09 am
by Rudster816
turdus wrote: Yes, but it also applies to E9h, so your argument is invalid.
I was no longer making a case for using a real serial port vs the Bochs E9 'hack' at that point.
turdus wrote: 1. Using E9h hack is so simple that you simply cannot mess it up ("out 0E9h, al", that's all) as opposite to serial where you need to specify protocol parameters and must check whether it's ready to send the byte.
2. Considering the difficulty of the problem OP has, I assume he/she is not an experienced hardware programmer, so I would choose the simplier way if I were the OP.
3. No need to work on all VMs, OP is using bochs.

Yes, but it also applies to E9h, so your argument is invalid.
1. All you have to do is copy the initialization code
, and you're at the point where outputing to the serial port is as easy as "out 0E9h, al". It might not be robust, but it doesn't have to be seeing as it's just a temporary hack. All I did was copied and pasted that initialization code, copied my printf function and changed the putc to a function that's essentially the same as the write_serial function on there. It's worked on all three VM's ive tested (Bochs, Qemu, and Vmware).
2. If you're going to start hardware programming, doing bare bone serial output is probably an excellent starting point, and (to a certain extent) applies to physical hardware too.
3. You need to test on multiple VM's when testing anything hardware or even CPU related. It's quite common to have bugs in another VM that do not occur in Bochs (or vice versa), so debugging those cases would just get that more difficult without the benefit of being able to see the data your kernel is outputting.

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 12:19 pm
by BetaWar
Rudster816 wrote:1. The BIOS assumes you have a valid stack setup, so you NEED to have one setup before calling the floppy functions. The reason it's working is probably dumb luck.
2. You're not putting the color in to the video memory.

Try this

Code: Select all

  for(i = 0; s[i] != 0; i++){
    *vidmem = s[i];
    *(vidmem + 1) = 0x7; // White text on black background. 
    vidmem += 2;
  
http://wiki.osdev.org/Drawing_In_Protected_Mode

Explains everything you need to know.

turdus wrote: You don't have to use serial to debug to bochs' terminal, see E9h hack.
It literally took me 2 minutes to setup printing strings to a file\terminal over the serial port, and it will work on all VM's. It really helps because you can dump as much information as you want without having to worry about it all fitting on the screen\kernel panics\etc.
I have since set up a simple stack in real mode, and that didn't change anything. I also went ahead and set the output colors as you said and it didn't seem to change anything either (still getting blank output).

I appreciate the pointer towards serial ports, however I seem to be getting something wrong with it as it outputs "ELF 4 " and nothing more... I used this page: http://wiki.osdev.org/Serial_Port as a reference when setting the serial port up. One thing I am not 100% sure on is whether I actually have to read anything from the serial port or not? I wouldn't think I would need to read anything unless I was trying to read something (which I am not), so I didn't worry about that part of the code.

Combuster wrote:Are you even sure loader is (even) called? You haven't defined it's location in the binary but are merely assuming it comes first.

Also:
//kputs("Hello world\n");
Does not print anything.
Yeah, I know, it is commented out because I was trying to use the "print" function instead, but I left it in there so you could see that I was trying multiple things and not getting either of them to work. I wasn't expecting commented out code to run :)


@turdus
Referring to point 2, you are correct in thinking that I am not an experienced hardware programmer. I am trying to learn, it is a slow process, but hopefully I will get there eventually.
Pt 3 - I am actually wanting to deploy to VMWare at the moment, but bochs is easier to use in this case so I am using it (it gives you more information off the bat for one thing). I am however running into an issue with the e9 hack, it appears that you have to do a custom build for the e9 hack to be enabled, however when I attempt to build it from source I get the error
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
ERROR: pkg-config was not found, or unable to access the gtk+-2.0 package.
Install pkg-config and the gtk+ development package,
or disable the gui debugger, or the wxWidgets display library (whichever is being used).
I have tried disabling the gui debugger, and that doesn't seem to work (same error), so I am not sure what is up with that. Given that my bochs isn't compiled to work with the e9 hack, the code you suggested to change doesn't output anything new.

Here is the current code modifications:
Bootloader:

Code: Select all

eset_drive:
  mov al, '1'
  out 0x0e9, al

  mov ah, 0 ; reset command
  int 0x13 ; call interrupt 13
  or ah, ah ; check for error
  jnz reset_drive ; try again if ah != 0

  xor ax, ax
  mov ss, ax
  mov sp, 0x600

  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 al, '2'
  out 0x0e9, al

  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

  mov al, '3'
  out 0x0e9, al

  jmp 0x08:0x01000
Added the e9 hack outputs, and a simple stack to the real mode.

main.cpp:

Code: Select all

#define PORT 0x3f8   /* COM1 */

void init_serial() {
  outportb(PORT + 1, 0x00);    // Disable all interrupts
  outportb(PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
  outportb(PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
  outportb(PORT + 1, 0x00);    //                  (hi byte)
  outportb(PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
  outportb(PORT + 2, 0xC7);    // Enable FIFO, clear them, with 14-byte threshold
  outportb(PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
}

bool isTransmitEmpty(void){
  return inportb(PORT + 5) & 0x20;
}

void write_serial(const char* s){
  for(int i = 0; s[i] != '\0'; i++){
    while(!isTransmitEmpty());
    outportb(PORT, s[i]);
  }
  while(!isTransmitEmpty());
  outportb(PORT, '\0');
}

//TextMonitorDriver* monitor;
int main(){
  init_serial();
  write_serial("Entered main\n");
  //clear();
  //attrib = 0x0f;
  kputs("Hello world\n");
  write_serial("Should have output 'Hello World'\n");
  //kputs("Hello world\n");
  //kputs("Hello world\n");
  print("THIS IS A TEST\n");
  write_serial("Should have output 'THIS IS A TEST'\n");
Added serial port stuff, and uncommented one of the kputs.

loader:

Code: Select all

loader:
  mov al, '4'
  out 0x0e9, al
Added the e9 hack output.

And the only difference in the bochs output seems to be:
00015211423i[FDD ] partial read() on floppy image returns 467/512
00015256378i[FDD ] read() on floppy image returns 0
00015303859i[SER ] com1: FIFO enabled
[/code]

Though I can post the entire file if you would like.

Thanks for the patients guys.

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 12:42 pm
by turdus
BetaWar wrote:I am however running into an issue with the e9 hack, it appears that you have to do a custom build for the e9 hack to be enabled, however when I attempt to build it from source I get the error
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
ERROR: pkg-config was not found, or unable to access the gtk+-2.0 package.
Install pkg-config and the gtk+ development package,
or disable the gui debugger, or the wxWidgets display library (whichever is being used).
I have tried disabling the gui debugger, and that doesn't seem to work (same error), so I am not sure what is up with that.
And what about wxwidgets? Are you using it? Try to disable that too. Alternatively you should try to install gtk, http://www.gtk.org/. On linux it's called libgtk-2.0-dev or something similar. Sorry, never used stock binary bochs, I always compile it, didn't realize e9h is not part of it by default.

Btw when it freeze, start bochs debugger, and tell me what does

Code: Select all

<bochs:2> u /32 0x1000
say? (I hope debugger is built in by default, is it?)

Re: Custom bootloader output to terminal not working

Posted: Sun Apr 01, 2012 12:55 pm
by BetaWar
Yeah, I was being dumb an just installed gtk+2 instead of the dev libraries for it. However, I am also having a problem with the e9 hack, if I attempt to add the configure line "--port-e9-hack" I get the error "configure: error: unrecognized option: `--port-e9-hack'". I am trying to compile bochs version 2.5.1

Do I simply need to tell the bochsrc.txt file to enable e9?

Re: Custom bootloader output to terminal not working

Posted: Mon Apr 02, 2012 2:56 am
by turdus
BetaWar wrote:Yeah, I was being dumb an just installed gtk+2 instead of the dev libraries for it. However, I am also having a problem with the e9 hack, if I attempt to add the configure line "--port-e9-hack" I get the error "configure: error: unrecognized option: `--port-e9-hack'". I am trying to compile bochs version 2.5.1

Do I simply need to tell the bochsrc.txt file to enable e9?
Wel, well well. Yes, I've checked my config.log and it seems that 2.5.1 does not need port-e9-hack flag anymore. And yes, in my bochsrc I have

Code: Select all

port_e9_hack: enabled=1
If debugger is also included (or you use gdb stub), you may not need to compile bochs after all.

Re: Custom bootloader output to terminal not working

Posted: Mon Apr 02, 2012 8:58 am
by BetaWar
Alright, I enabled the e9 hack through the bochsrc.txt file and it didn't seem to do anything (at least I couldn't find the output). However, that was just me being stupid again (or not reading the output carefully enough -- Yay!).

I added a quick check the force output to the screen (using int 0x10) at the top of my bootloader script:

Code: Select all

checkENine:
  ; check if e9 hack is available
  in al, 0xe9
  cmp al, 0xe9
  jne .noENine
  jmp .ENine
.noENine:
  mov ah, 0x0e
  mov al, 'N'
  mov bh, 0x0f
  mov bl, 0
  int 0x10
  jmp reset_drive
.ENine:
  mov ah, 0x0e
  mov al, 'Y'
  mov bh, 0x0f
  mov bl, 0
  int 0x10
Basically, all it does is check that the e9 port returns '0xe9' when read from (which it should based on the documentation: http://bochs.sourceforge.net/doc/docboo ... chsrc.html), and then outputs 'Y' or 'N' depending on whether or not e9 is enabled.

After that I went ahead and output the stderr to one file and the stdout to another:

stderr:

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 0xa9ccc10
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 0xb505f008. after alignment, vector=0xb5060000
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 0xaa086e0
00000000000i[PLGIN] loaded plugin libbx_cmos.so
00000000000i[     ] lt_dlhandle is 0xaa08c10
00000000000i[PLGIN] loaded plugin libbx_dma.so
00000000000i[     ] lt_dlhandle is 0xaa09178
00000000000i[PLGIN] loaded plugin libbx_pic.so
00000000000i[     ] lt_dlhandle is 0xaa09728
00000000000i[PLGIN] loaded plugin libbx_pit.so
00000000000i[     ] lt_dlhandle is 0xaa09c38
00000000000i[PLGIN] loaded plugin libbx_vga.so
00000000000i[     ] lt_dlhandle is 0xaa0a220
00000000000i[PLGIN] loaded plugin libbx_floppy.so
00000000000i[     ] lt_dlhandle is 0xaa0aa28
00000000000i[PLGIN] loaded plugin libbx_pci.so
00000000000i[     ] lt_dlhandle is 0xaa0b158
00000000000i[PLGIN] loaded plugin libbx_pci2isa.so
00000000000i[     ] lt_dlhandle is 0xaa0b660
00000000000i[PLGIN] loaded plugin libbx_unmapped.so
00000000000i[     ] lt_dlhandle is 0xaa0bc78
00000000000i[PLGIN] loaded plugin libbx_biosdev.so
00000000000i[     ] lt_dlhandle is 0xaa0c148
00000000000i[PLGIN] loaded plugin libbx_speaker.so
00000000000i[     ] lt_dlhandle is 0xaa0c758
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.so
00000000000i[     ] lt_dlhandle is 0xaa0cc30
00000000000i[PLGIN] loaded plugin libbx_gameport.so
00000000000i[     ] lt_dlhandle is 0xaa0d110
00000000000i[PLGIN] loaded plugin libbx_pci_ide.so
00000000000i[     ] lt_dlhandle is 0xaa0d620
00000000000i[PLGIN] loaded plugin libbx_acpi.so
00000000000i[     ] lt_dlhandle is 0xaa0dc48
00000000000i[PLGIN] loaded plugin libbx_ioapic.so
00000000000i[     ] lt_dlhandle is 0xaa0e260
00000000000i[PLGIN] loaded plugin libbx_keyboard.so
00000000000i[     ] lt_dlhandle is 0xaa0e858
00000000000i[PLGIN] loaded plugin libbx_harddrv.so
00000000000i[     ] lt_dlhandle is 0xaa20618
00000000000i[PLGIN] loaded plugin libbx_serial.so
00000000000i[     ] lt_dlhandle is 0xaa21148
00000000000i[PLGIN] loaded plugin libbx_parallel.so
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Fri Mar 30 07:01:00 2012 (time0=1333116060)
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
00015212128i[FDD  ] partial read() on floppy image returns 467/512
00015257083i[FDD  ] read() on floppy image returns 0
00015304564i[SER  ] com1: FIFO enabled
00089128000p[XGUI ] >>PANIC<< POWER button turned off.
00089128000i[CPU0 ] CPU is in protected mode (active)
00089128000i[CPU0 ] CS.d_b = 32 bit
00089128000i[CPU0 ] SS.d_b = 32 bit
00089128000i[CPU0 ] EFER   = 0x00000000
00089128000i[CPU0 ] | RAX=0000000000000000  RBX=0000000000101000
00089128000i[CPU0 ] | RCX=0000000000090002  RDX=00000000000003f8
00089128000i[CPU0 ] | RSP=0000000000105fe0  RBP=0000000000105ff4
00089128000i[CPU0 ] | RSI=00000000000e478c  RDI=000000000000ffac
00089128000i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00089128000i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00089128000i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00089128000i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00089128000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf zf af PF cf
00089128000i[CPU0 ] | SEG selector     base    limit G D
00089128000i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00089128000i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 ffffffff 1 1
00089128000i[CPU0 ] |  DS:0010( 0002| 0|  0) 00000000 ffffffff 1 1
00089128000i[CPU0 ] |  SS:0010( 0002| 0|  0) 00000000 ffffffff 1 1
00089128000i[CPU0 ] |  ES:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00089128000i[CPU0 ] |  FS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00089128000i[CPU0 ] |  GS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00089128000i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00089128000i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00089128000i[CPU0 ] | RIP=00000000000026af (00000000000026af)
00089128000i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00089128000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00089128000i[CPU0 ] 0x00000000000026af>> jmp .-2 (0x000026af) : EBFE
00089128000i[CMOS ] Last time is 1333116082 (Fri Mar 30 07:01:22 2012)
00089128000i[XGUI ] Exit
00089128000i[     ] restoring default signal behavior
00089128000i[CTRL ] quit_sim called with exit code 1
stdout:

Code: Select all

========================================================================
                       Bochs x86 Emulator 2.4.5
              Build from CVS snapshot, on April 25, 2010
========================================================================
1234
As you can see it is outputting 1 through 4, so it is making it to the loader script... I went ahead and added another e9 output at the beginning of the main program:

Code: Select all

#define BOCHSOUT(c) outportb(0xe9, c)

int main(){
  BOCHSOUT('5');
  //...
To see if it was making it to the main function ever and it output as well:

Code: Select all

========================================================================
                       Bochs x86 Emulator 2.4.5
              Build from CVS snapshot, on April 25, 2010
========================================================================
12345
So it is making it to my code, just not outputting to the terminal... Odd.




I also just noticed that you asked for the linker script back a few posts, so here it is:

Code: Select all

ENTRY (loader)

SECTIONS
{
    . = 0x00100000;

    .text ALIGN (0x1000) :
    {
        *(.text)
        *(.gnu.linkonce.t*)
    }

    .rodata ALIGN (0x1000) :
    {
        start_ctors = .;
        *(.ctor*)
        end_ctors = .;

        start_dtors = .;
        *(.dtor*)
        end_dtors = .;

        *(.rodata*)
        *(.gnu.linkonce.r*)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
        *(.gnu.linkonce.d*)
    }

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        *(.gnu.linkonce.b*)
        ebss = .;
    }

    /DISCARD/ :
    {
        *(.comment)
        *(.eh_frame) /* discard this, unless you are implementing runtime support for C++ exceptions. */
    }
}
Thanks for the continued help.

Re: Custom bootloader output to terminal not working

Posted: Mon Apr 02, 2012 9:27 am
by turdus
Great! Now as Rudster816 wrote,
It really helps because you can dump as much information as you want without having to worry about it
I suggest to implement now BOCHSOUTSTR and BOCHSOUTHEX to display more complex information. You'll need them sooner or later.

I asked for the linker script to make sure label "loader" is at offset 0 in your binary, but since it displays output at main() we can assume the jump is correct and executes what you expect. Have you applied modifications that others wrote (gdt limit, pointer cast, attribute byte etc.)? It would be great to see your kputc() routine in it's current state. Should work by now, so should print() as well.

Some blind tips: because of color attribute, you should have "x*2" everywhere, and the width is 160 not 80. Try to dump 0xB8000 in debugger, without clear() it may have attributes of 0, which is black on black. No other guesses for now.

Re: Custom bootloader output to terminal not working

Posted: Mon Apr 02, 2012 9:51 am
by BetaWar
Alright, I'll begin writing them next chance I get to work on things.

I haven't yet applied the all of the other modifications (mainly the gdt limit), however I believe I have the pointer cast and attribute byte updated properly.

Here is my current code for kputc and print:

Code: Select all

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 = att | ' ';
  }
  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++){
    BOCHSOUT(s[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 + 1) = 0x7;
    vidmem += 2;
  }
  offset += i;
  outportb(0x3d5, (unsigned char)offset);
  outportb(0x3d4, 14);
  outportb(0x3d5, (unsigned char)(offset >> 8));
}
And it doesn't seem to work... In fact it doesn't seem that kputs or print are ever actually being called... at least I am not getting the output expected from the BOCHSOUT call I am making in the kputs function... I will also try adding a BOCHSOUT to the top of kputs to see if it is at least getting that far.

Potentially dumb question: How do I go about running the bochs debugger? I had thought it was running from the get-go (the console output), but I am thinking that may not be the case any more.

<quick update>

After doing a bit more prodding I figured out that I am having null passed into the functions (instead of the const char* that it is supposed to be). So, I am thinking that I have somehow set up my stack incorrectly (which would explain why void functions work). I'll post my loader and bootloader assembly (as it currently stands) next time I boot the linux box I am working on and hopefully we can figure out what is up with it.

</quick update>