Bad adress for data access

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
astro01
Posts: 3
Joined: Sat Jul 15, 2017 10:16 am

Bad adress for data access

Post by astro01 »

Hello,

I'm newbie in os development, and i try to learn this.

I read a french tutoriel (http://michelizza.developpez.com/realis ... me/#LV-B-1).

But when i try to access at data when my processor is in protected mode, i have problems.

This is my code :

my boot sector :

Code: Select all

[BITS 16]  ; indique a nasm que l'on travaille en 16 bits
[ORG 0x0]
 
jmp start
 
%include "display.asm"
 
msgDebut: db "Chargement du kernel", 13, 10, 0
 
gdt:
  db 0, 0, 0, 0, 0, 0, 0, 0
gdt_cs:
  db 0xFF, 0xFF, 0x0, 0x0, 0x0, 10011011b, 11011111b, 0x0
gdt_ds:
  db 0xFF, 0xFF, 0x0, 0x0, 0x0, 10010011b, 11011111b, 0x0
gdtptr:
  dw 0  ; limite
  dd 0  ; base
 
start:
 
; initialisation des segments en 0x07C00
mov ax, 0x07C0
mov ds, ax
mov es, ax
mov ax, 0x8000
mov ss, ax
mov sp, 0xf000 ; stack de 0x8F000 -> 0x80000
 
; affiche un msg
mov si, msgDebut
call afficher
 
xor ah, ah
xor dl, dl
int 0x13
 
push es
mov ax, 0x100
mov es, ax
mov bx, 0
 
mov ah, 2
mov al, 50
mov ch, 0
mov cl, 2
mov dh, 0
mov dl, 0
int 0x13
pop es
 
mov ax, gdtptr
mov bx, gdt
sub ax, bx
mov word [gdtptr], ax
 
xor eax, eax
xor ebx, ebx
mov ax, ds
mov ecx, eax
shl ecx, 4
mov bx, gdt
add ecx, ebx
mov dword [gdtptr+2], ecx
 
cli
lgdt [gdtptr]
 
mov si, msgDebut
mov eax, cr0
or ax, 1
mov cr0, eax
mov si, msgDebut
 
jmp next
next:
 
mov ax, 0x10
mov ds, ax
mov fs, ax
mov gs, ax
mov es, ax
mov ss, ax
mov esp, 0x9F000 
 
jmp dword 0x8:0x1000
 
times 510-($-$$) db 144
dw 0xAA55
My kernel :

Code: Select all

[BITS 32]
 
EXTERN print
 
GLOBAL _start
 
_start:
 
jmp start
 
msg1 db "init kernel", 0
 
start:
 
mov byte [0xB8A00], 'H'
mov byte [0xB8A01], 0x56
 
mov eax, msg1
push eax
call print
pop eax
 
end:
jmp end
My function print in C :

Code: Select all

void print(char *string) {
  unsigned char *ptr = (unsigned char *) (0xB8A00);
 
  while (*string != 0) {
    *ptr = *string;
    *(ptr + 1) = 0x57;
    ++string;
    ptr += 2;
  }
}
The problem is located at line 18 of my kernel code.

The address moved in eax is 0x10:0x1022.
But my datas is located at 0x10:0x1002.

This is my debug traces (with Bochs) :
(0) [0x000000000000100e] 0008:000000000000100e (unk. ctxt): mov byte ptr ds:0xb8a00, 0x48 ; c605008a0b0048
<bochs:54>
Next at t=271759839
(0) [0x0000000000001015] 0008:0000000000001015 (unk. ctxt): mov byte ptr ds:0xb8a01, 0x56 ; c605018a0b0056
<bochs:55>
Next at t=271759840
(0) [0x000000000000101c] 0008:000000000000101c (unk. ctxt): mov eax, 0x00001022 ; b822100000
<bochs:56> x/100 0x10:0x1002
[bochs]:
0x0000000000001002 <bogus+ 0>: 0x74696e69 0x72656b20 0x006c656e 0x8a0005c6
0x0000000000001012 <bogus+ 16>: 0xc648000b 0x0b8a0105 0x22b85600 0x50000010
0x0000000000001022 <bogus+ 32>: 0x000003e8 0xfeeb5800 0x83e58955 0x45c710ec
0x0000000000001032 <bogus+ 48>: 0x0b8a00fc 0x8b1eeb00 0xb60f0845 0x8bc28900
0x0000000000001042 <bogus+ 64>: 0x1088fc45 0x83fc458b 0x00c601c0 0x08458357
0x0000000000001052 <bogus+ 80>: 0xfc458301 0x08458b02 0x8400b60f 0x90d875c0
0x0000000000001062 <bogus+ 96>: 0x0000c3c9 0x00000000 0x00000000 0x00000000
I have put in blue my datas, and in red, the bad address moved in eax.

I don't understand why i have 0x1022 when i execute mov eax, msg1 ?
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Bad adress for data access

Post by simeonz »

I built your code like this:

Code: Select all

nasm -f elf -o kernel.o kernel.asm
gcc -m32 -c screen.c
ld -m elf_i386 --oformat binary -Ttext 1000 kernel.o screen.o -o kernel
The "-m elf_i386" is due to the host being x86-64.

The result is (only the relevant snippet):

Code: Select all

ndisasm -b32 -o0x1000 -a -s0x100e kernel
(...)
0000100E  C605008A0B0048    mov byte [dword 0xb8a00],0x48
00001015  C605018A0B0056    mov byte [dword 0xb8a01],0x56
0000101C  B802100000        mov eax,0x1002
00001021  50                push eax
00001022  E805000000        call dword 0x102c
00001027  58                pop eax
00001028  EBFE              jmp short 0x1028
(...)
The address points at the string as it should. May be something went wrong with the linking in your build, because what you have appears like misdirected relocation. (Relocatable references are self-referencing before they are applied, which is what you have.)
M2004
Member
Member
Posts: 65
Joined: Sun Mar 07, 2010 2:12 am

Re: Bad adress for data access

Post by M2004 »

astro01: Are you sure your boot code actually works at all? It looks little fishy to me.
Have you tested that boot code gets executed properly from the beginning?

Just a quick look:
1 ) [ORG 0x0] should be [ORG 0x7C00]
2) Are you sure that stack pointer (sp) setup does not overwrite something in the memory?
Best way to be sure is to set ss:sp 0:0x7C00. There are free space just above the "boot spot".

Regards
M2004
astro01
Posts: 3
Joined: Sat Jul 15, 2017 10:16 am

Re: Bad adress for data access

Post by astro01 »

Thank you for your answers. :D

Now it's work with your command.

I have used ld with -m i386linux option with -m elf_i386, it's work.
astro01
Posts: 3
Joined: Sat Jul 15, 2017 10:16 am

Re: Bad adress for data access

Post by astro01 »

M2004 wrote:astro01: Are you sure your boot code actually works at all? It looks little fishy to me.
Have you tested that boot code gets executed properly from the beginning?

Just a quick look:
1 ) [ORG 0x0] should be [ORG 0x7C00]
2) Are you sure that stack pointer (sp) setup does not overwrite something in the memory?
Best way to be sure is to set ss:sp 0:0x7C00. There are free space just above the "boot spot".

Regards
M2004
Yes my code work, for this moment, my stack pointer not overwrite something in the memory, but you're probably right, I'll replace that.
Post Reply