OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 1:24 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 10:52 am 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
Hello everyone!

After many headaches, I managed to make the paging to work partially. I have only one problem, when I access uninitialized global variables I receive triple fault, but if I used only initialized global variables then everything is good. I think I could initialize all the global variables, but I want to know the cause of the problem. Partially, I know the problem, the global variables which are put into .data segment are loaded correct while those from the bss segment are not.
Note that the kernel is mapped in higher half (3GiB). I disassembled the .elf file and the code is:
Code:
mov    (%esp),%ecx
add    $0x6167,%ecx

lea    0x4dc4(%ecx),%eax // here it's loaded the initialized global variable
movl   $0x0,(%eax)

mov    -0x38(%ecx),%eax // here it's loaded the UNinitialized global variable
movl   $0x5,(%eax)

Note that the value from eax is exactly physical address of the variable (should be the virtual address...)

here it's the code where are accessing the global variables: https://github.com/EnachescuAlin/PhiOS/ ... main.c#L27
here it's entry function: https://github.com/EnachescuAlin/PhiOS/ ... ot32.s#L29
here it's linker.ld: https://github.com/EnachescuAlin/PhiOS/ ... inker32.ld


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 11:16 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
A bit more background would be useful, like who/what loaded the executable into memory, is this in kernel or userspace? etc..


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 11:17 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 10, 2016 7:35 am
Posts: 167
Location: Lancaster, England, Disunited Kingdom
TheLittleWho wrote:
Hello everyone!


Code:
mov    (%esp),%ecx
add    $0x6167,%ecx

lea    0x4dc4(%ecx),%eax // here it's loaded the initialized global variable
movl   $0x0,(%eax)

mov    -0x38(%ecx),%eax // here it's loaded the UNinitialized global variable
movl   $0x5,(%eax)




Why are you using a load effective address instruction (lea) for initialised and a move (mov) instruction for uninitialised?

EDIT: Ignore what I say below - I missed the brackets round the esp

It also looks odd to me that you are calculating an address relative to the stack pointer - that's (a) unusual and (b) likely to be impossibly unmaintainable.


Last edited by MichaelFarthing on Thu Aug 03, 2017 11:45 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 11:20 am 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
LtG wrote:
A bit more background would be useful, like who/what loaded the executable into memory, is this in kernel or userspace? etc..

I'm using grub and it's in kernel space. Let me know what else you want to know.


Last edited by TheLittleWho on Thu Aug 03, 2017 11:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 11:25 am 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
MichaelFarthing wrote:
TheLittleWho wrote:
Hello everyone!


Code:
mov    (%esp),%ecx
add    $0x6167,%ecx

lea    0x4dc4(%ecx),%eax // here it's loaded the initialized global variable
movl   $0x0,(%eax)

mov    -0x38(%ecx),%eax // here it's loaded the UNinitialized global variable
movl   $0x5,(%eax)



Why are you using a load effective address instruction (lea) for initialised and a move (mov) instruction for uninitialised?
It also looks odd to me that you are calculating an address relative to the stack pointer - that's (a) unusual and (b) likely to be impossibly unmaintainable.


That code is generated by the compiler, is not written by me. I'm using gcc cross compiler from here: http://wiki.osdev.org/GCC_Cross-Compiler
These are the flags used for compiler and linker:
set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -std=gnu11 -ffreestanding -O2 -Wall -Wextra -fpic")
set(CMAKE_LINKER_FLAGS "-T ${LINKER_FILE} -O2 -nostdlib -z max-page-size=0x1000")


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 11:42 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
TheLittleWho wrote:
LtG wrote:
A bit more background would be useful, like who/what loaded the executable into memory, is this in kernel or userspace? etc..

I'm using grub and it's in kernel space. Let me know what else you want to know.


I was hoping for a bit more than that. You did notice the "etc"?

Is the issue that:
- Grub loads your elf (kernel/bootstrap) and accessing the .bss in that causes a #TF
- Grub has loaded your kernel ages ago and now you have a userspace process running, but it does a syscall and that somehow causes a #TF
- etc

Explain the actual problem. Otherwise we're just guessing at what the problem is, let alone how to solve it. It would be useful to provide such a detailed description in the first place, not shifting the burden to "us" to ask you specific questions about 10 different things..


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 12:07 pm 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
So, grub call my entry function and I initialized the following "components": vga, gdt, tss, idt, pic, pit, pma
For each "component" I called an init function which (mostly) initialized the global variables used by that component. (in this moment everything is good).
Then I initialized the PD with the PTs and pages used in this moment (the kernel is mapped at 0xC0000000), I disabled the interrupts and enabled the paging then I did absolute jump in the higher half and I moved the virtual address of the kernel stack in esp and called kernel_main() which is a function written in C.
Here I can't used global variables which wasn't initialized in code (.bss segment), but I can use global variables which was initialized (.data segment) also I can use local variables.

I written an program (on linux) in C and I disassembled it and I saw that the global variables (both initialized and uninitialized) are accessed relative to rip register while in my .elf file they are accessed relative to esp.


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 2:14 pm 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
TheLittleWho wrote:
I written an program (on linux) in C and I disassembled it and I saw that the global variables (both initialized and uninitialized) are accessed relative to rip register while in my .elf file they are accessed relative to esp.
This esp approach is for x86, while the rip approach is for x64. There is no eip relative addressing in the 32-bit instruction set, so the return address is taken from the stack and used as base for code-relative accesses.

TheLittleWho wrote:
So, grub call my entry function and I initialized the following "components": vga, gdt, tss, idt, pic, pit, pma
For each "component" I called an init function which (mostly) initialized the global variables used by that component. (in this moment everything is good).
Then I initialized the PD with the PTs and pages used in this moment (the kernel is mapped at 0xC0000000), I disabled the interrupts and enabled the paging then I did absolute jump in the higher half and I moved the virtual address of the kernel stack in esp and called kernel_main() which is a function written in C.
Here I can't used global variables which wasn't initialized in code (.bss segment), but I can use global variables which was initialized (.data segment) also I can use local variables.
How much of the physical memory do you map at 0xC0000000. I am speaking slightly on a limb, but since the .bss is not part of the file, I suspect that you may have taken something related to the file size rather then the address space size when deciding how much memory to map.


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 6:36 pm 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
Initially you said: "when I access uninitialized global variables I receive triple fault".

What's the cause of the triple fault? Faulting #PF, which causes #DF which causes #TF?

Why does the initial #PF happen? Is it caused by using .bss and the .bss has invalid values instead of zeroes or is the access itself causing #PF which would mean your PD/PT's is at fault..?


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 10:39 pm 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
simeonz wrote:
How much of the physical memory do you map at 0xC0000000. I am speaking slightly on a limb, but since the .bss is not part of the file, I suspect that you may have taken something related to the file size rather then the address space size when deciding how much memory to map.


I have 2 variables in linker.ld for kernel begin and kernel end. Before enabling paging, if i get a pointer to a variable from .bss and after enabling paging add 0xBFF00000 to pointer and use it, it's ok. So, bss is mapped virtual, my problem is that when I access the global variable from bss, it's accessed physical address instead of virtual address...


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 10:46 pm 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
LtG wrote:
Initially you said: "when I access uninitialized global variables I receive triple fault".

What's the cause of the triple fault? Faulting #PF, which causes #DF which causes #TF?

Why does the initial #PF happen? Is it caused by using .bss and the .bss has invalid values instead of zeroes or is the access itself causing #PF which would mean your PD/PT's is at fault..?

It's a page fault -> double fault -> triple fault... I said in the first post, when access a variable from bss, her physical address is loaded, instead of the virtual address. I put in the first post 6 lines of asm code on which I took from .elf file. I want to know why for the variables from .data the address it's loaded some way and for the variables from .bss the address it's loaded differently. For variables from bss it's accessed physical address instead of virtual address, why?


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Thu Aug 03, 2017 10:56 pm 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
TheLittleWho wrote:
I have 2 variables in linker.ld for kernel begin and kernel end. Before enabling paging, if i get a pointer to a variable from .bss and after enabling paging add 0xBFF00000 to pointer and use it, it's ok. So, bss is mapped virtual, my problem is that when I access the global variable from bss, it's accessed physical address instead of virtual address...

In that case it would be helpful to see your linker script and the output of "your-target-objdump -x yourkernel" (replace "your-target" with your target triplet and "yourkernel" with your kernel file name), to see where symbols actually end up in the file.

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Fri Aug 04, 2017 2:16 am 
Offline
Member
Member

Joined: Sun Mar 01, 2015 7:58 am
Posts: 51
The linker script and the dump are shown below.

Code:
bin/x86_32/phios.elf:     file format elf32-i386
bin/x86_32/phios.elf
architecture: i386, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x00100018

Program Header:
    LOAD off    0x00001000 vaddr 0x00100000 paddr 0x00100000 align 2**12
         filesz 0x00005530 memsz 0x00005530 flags r-x
    LOAD off    0x00007000 vaddr 0x00106000 paddr 0x00106000 align 2**12
         filesz 0x00000248 memsz 0x0000a020 flags rw-

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         000049c8  00100000  00100000  00001000  2**12
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .text.__x86.get_pc_thunk.cx 00000004  001049c8  001049c8  000059c8  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .text.__x86.get_pc_thunk.bx 00000004  001049cc  001049cc  000059cc  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  3 .rodata       00000058  00105000  00105000  00006000  2**12
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .rodata.str1.1 00000330  00105058  00105058  00006058  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .rodata.str1.4 000001a8  00105388  00105388  00006388  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .data         00000159  00106000  00106000  00007000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  7 .got          000000e0  0010615c  0010615c  0000715c  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  8 .got.plt      0000000c  0010623c  0010623c  0000723c  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  9 .bss          00009020  00107000  00107000  00007248  2**12
                  ALLOC
SYMBOL TABLE:
00100000 l    d  .text   00000000 .text
001049c8 l    d  .text.__x86.get_pc_thunk.cx   00000000 .text.__x86.get_pc_thunk.cx
001049cc l    d  .text.__x86.get_pc_thunk.bx   00000000 .text.__x86.get_pc_thunk.bx
00105000 l    d  .rodata   00000000 .rodata
00105058 l    d  .rodata.str1.1   00000000 .rodata.str1.1
00105388 l    d  .rodata.str1.4   00000000 .rodata.str1.4
00106000 l    d  .data   00000000 .data
0010615c l    d  .got   00000000 .got
0010623c l    d  .got.plt   00000000 .got.plt
00107000 l    d  .bss   00000000 .bss
00000000 l    df *ABS*   00000000 CMakeFiles/phios.elf.dir/kernel/src/arch/x86/boot32.s.o
00100000 l       .text   00000000 multiboot_header_begin
00100018 l       .text   00000000 multiboot_header_end
0010b000 l       .bss   00000000 stack_top
00107000 l       .bss   00000000 stack_bottom
00000000 l    df *ABS*   00000000 phi_main.c
00000000 l    df *ABS*   00000000 paa.c
0010b004 l     O .bss   00000004 g_placementAddress
00000000 l    df *ABS*   00000000 bitmap_pma.c
00000000 l    df *ABS*   00000000 pmm.c
00000000 l    df *ABS*   00000000 asm_io.c
00000000 l    df *ABS*   00000000 gdt32.c
0010b080 l     O .bss   0000003c g_GDT32Struct
0010b040 l     O .bss   00000030 g_GDT32Entries
0010b020 l     O .bss   00000006 g_GDT32Pointer
00000000 l    df *ABS*   00000000 idt32.c
0010b0c0 l     O .bss   00000400 g_intHandlers
0010b500 l     O .bss   00000800 g_IDTEntries32
0010b4c0 l     O .bss   00000006 g_IDTPointer32
00000000 l    df *ABS*   00000000 handlers32.c
00000000 l    df *ABS*   00000000 pit.c
00102390 l     F .text   00000002 helper_pitHandler
0010bd00 l     O .bss   00000004 g_time
00000000 l    df *ABS*   00000000 ia32.c
00102430 l     F .text   00000093 helper_IA32_4KB_createPaging
001024d0 l     F .text   0000007d helper_IA32_4KB_deletePaging
00000000 l    df *ABS*   00000000 cpu.c
00000000 l    df *ABS*   00000000 pic.c
00000000 l    df *ABS*   00000000 tss32.c
00000000 l    df *ABS*   00000000 cpuid.c
0010bd20 l     O .bss   00000028 g_cpuInfo
00000000 l    df *ABS*   00000000 init32.c
00000000 l    df *ABS*   00000000 drivers_test.c
00000000 l    df *ABS*   00000000 util_test.c
00000000 l    df *ABS*   00000000 CMakeFiles/phios.elf.dir/kernel/src/arch/x86/descriptors32.s.o
00103360 l       .text   00000000 flush
00000000 l    df *ABS*   00000000 CMakeFiles/phios.elf.dir/kernel/src/arch/x86/interrupts32.s.o
0010353d l       .text   00000000 isr_common_stub_32
0010358c l       .text   00000000 irq_common_stub_32
00000000 l    df *ABS*   00000000 keyboard.c
00000000 l    df *ABS*   00000000 rtc.c
00103930 l     F .text   0000005f helper_getRegisterValue
0010bd48 l     O .bss   00000007 g_rtc
00000000 l    df *ABS*   00000000 text_mode.c
00000000 l    df *ABS*   00000000 kstring.c
00000000 l    df *ABS*   00000000 kstdio.c
00104750 l       .text   00000000 .L7
00104730 l       .text   00000000 .L8
00104700 l       .text   00000000 .L10
001046b0 l       .text   00000000 .L11
00104690 l       .text   00000000 .L12
00104640 l       .text   00000000 .L13
001045c0 l       .text   00000000 .L14
00000000 l    df *ABS*   00000000 kstdlib.c
00000000 l    df *ABS*   00000000
0010623c l     O .got.plt   00000000 _GLOBAL_OFFSET_TABLE_
001049c8 g     F .text.__x86.get_pc_thunk.cx   00000000 .hidden __x86.get_pc_thunk.cx
00100c10 g     F .text   00000026 GDT32_setStruct
00100e00 g     F .text   00000046 IDT32_registerHandler
00100e50 g     F .text   00000b1c IDT32_init
00106000 g       .data   00000000 linker_dataStart
0010bd80 g     O .bss   00002000 g_userStack
0010352f g     F .text   00000000 irq_32_14
00103d40 g     F .text   00000018 VGA_SetBackgroundColor
001035db g     F .text   00000000 CPUID_IsSuported
00103627 g     F .text   00000000 jumpToUserMode
001033fb g     F .text   00000000 isr_32_15
00103371 g     F .text   00000000 isr_32_0
001029e0 g     F .text   0000004a IA32_4KB_switchDirectory
001034d0 g     F .text   00000000 irq_32_3
0010fec0 g     O .bss   00000004 g_layout
0010351a g     F .text   00000000 irq_32_11
00103326 g     F .text   00000000 readCR0
00103cc0 g     F .text   0000007a VGA_MoveCursor
00102870 g     F .text   00000169 IA32_4KB_init
00103423 g     F .text   00000000 isr_32_19
001033ad g     F .text   00000000 isr_32_6
0010b000 g     O .bss   00000004 x
00100be0 g     F .text   00000007 io_inw
00100af0 g     F .text   000000a3 PMM_reserve
001034b2 g     F .text   00000000 irq_32_0
00103320 g     F .text   00000006 util_test_func
001042e0 g     F .text   00000047 kmemset
00100ba0 g     F .text   0000000a io_outb
00104010 g     F .text   0000006e VGA_Clear
001049c8 g       .text   00000000 linker_textEnd
001033e3 g     F .text   00000000 isr_32_12
0010b009 g     O .bss   00000001 g_index
001019f0 g     F .text   00000063 handlers32_default
00100a40 g     F .text   000000a3 PMM_free
0010347d g     F .text   00000000 isr_32_28
00100042 g     F .text   00000000 _higherHalf
001033db g     F .text   00000000 isr_32_11
0010338f g     F .text   00000000 isr_32_3
00101ad0 g     F .text   00000063 handlers32_debug
001039c0 g     F .text   000002f3 RTC_getDateAndTime
00104460 g     F .text   00000036 kstrlen
00103650 g     F .text   00000163 helper_keyboardReadScanCode
00103eb0 g     F .text   0000002f VGA_WriteChar
00103536 g     F .text   00000000 irq_32_15
00100bc0 g     F .text   0000000a io_outl
0010345f g     F .text   00000000 isr_32_25
00106000 g     O .data   00000059 g_USasciiCapsOn
00103310 g     F .text   00000006 drivers_test_func
0011000c g     O .bss   00000004 g_VGA_foregroundColor
00103335 g     F .text   00000000 readCR3
00103419 g     F .text   00000000 isr_32_18
001033c9 g     F .text   00000000 isr_32_9
001019a0 g     F .text   0000004b IDT32_irqHandler
00103d60 g     F .text   00000018 VGA_SetForegroundColor
00103840 g     F .text   00000086 keyboard_init
00100140 g     F .text   00000060 PAA_alloc
00103339 g     F .text   00000000 writeCR3
00102080 g     F .text   00000063 handlers32_pageFault
00100c90 g     F .text   0000009d GDT32_createEntries
001008b0 g     F .text   000000d7 PMM_addAllocator
00100750 g     F .text   000000c7 BitmapPMA_reserve
001001a0 g     F .text   00000012 PAA_getCurrentAddress
0010fec4 g     O .bss   00000004 g_keyboardBufferPos
00100d30 g     F .text   000000d0 GDT32_init
00103fd0 g     F .text   00000031 VGA_WriteBuffer
0010de40 g     O .bss   00002000 g_kernelStack
001034ee g     F .text   00000000 irq_32_6
001034e4 g     F .text   00000000 irq_32_5
001025b0 g     F .text   0000004c IA32_4KB_free
00103f40 g     F .text   0000002d VGA_WriteString
00101e50 g     F .text   00000063 handlers32_coprocessorSegmentOverrun
0010349b g     F .text   00000000 isr_32_31
00100000 g       .text   00000000 linker_kernelStart
00101de0 g     F .text   00000063 handlers32_doubleFault
0010350c g     F .text   00000000 irq_32_9
00102a30 g     F .text   00000044 IA32_4KB_enablePaging
00101f30 g     F .text   00000063 handlers32_segmentNotPresent
00104080 g     F .text   00000055 VGA_Init
00110010 g     O .bss   00000004 g_VGA_backgroundColor
00102dc0 g     F .text   0000016e CPUID_Init
00101970 g     F .text   00000023 IDT32_isrHandler
00102240 g     F .text   00000063 handlers32_SIMDFloatingPoint
00107000 g       .bss   00000000 linker_bssStart
00101ec0 g     F .text   00000063 handlers32_invalidTSS
001034c6 g     F .text   00000000 irq_32_2
001001c0 g     F .text   00000199 BitmapPMA_createAllocator
00101d00 g     F .text   00000063 handlers32_invaildOpcode
00110020 g       .bss   00000000 linker_kernelEnd
00102160 g     F .text   00000063 handlers32_alignmentCheck
0010fe40 g     O .bss   0000001c g_PMAVM
001020f0 g     F .text   00000063 handlers32_FPUx87Exception
00102f30 g     F .text   00000046 CPUID_GetVendorName
00100bb0 g     F .text   0000000b io_outw
00102b90 g     F .text   0000001d cpu_printState
001000d0 g     F .text   00000024 kernel_main
00100bf0 g     F .text   00000006 io_inl
001037c0 g     F .text   00000019 keyboard_intHandler
00103db0 g     F .text   000000f8 VGA_WriteColoredChar
0010337b g     F .text   00000000 isr_32_1
00101bb0 g     F .text   00000063 handlers32_breakpoint
0010fe5c g     O .bss   0000001c g_kernelPaging
001022b0 g     F .text   00000063 handlers32_virtualization
001037e0 g     F .text   0000005a keyboard_setLayout
00100100 g     F .text   0000003b PAA_init
0010344b g     F .text   00000000 isr_32_23
00103528 g     F .text   00000000 irq_32_13
001033b7 g     F .text   00000000 isr_32_7
00105058 g       .rodata   00000000 linker_rodataEnd
00103441 g     F .text   00000000 isr_32_22
00101a60 g     F .text   00000063 handlers32_zeroDivision
00103da0 g     F .text   00000002 VGA_Scroll
00101fa0 g     F .text   00000063 handlers32_stackSegmentFault
00105000 g       .rodata   00000000 linker_rodataStart
00100000 g       *ABS*   00000000 KERNEL_VMA
00104930 g     F .text   00000098 kitoa
00104250 g     F .text   00000084 kmemmove
001043d0 g     F .text   00000089 kstrcpy
00102ca0 g     F .text   0000004d PIC_clearMask
0010fec8 g     O .bss   00000001 g_capsOn
00102b60 g     F .text   0000002e cpu_printState64
00104150 g     F .text   0000008b kmemcmp
00102010 g     F .text   00000063 handlers32_generalProtectionFault
001038d0 g     F .text   00000051 keyboard_readKey
00103399 g     F .text   00000000 isr_32_4
00101b40 g     F .text   00000063 handlers32_NMI
0010ff00 g     O .bss   00000100 g_keyboardBuffer
00103f70 g     F .text   00000053 VGA_WriteColoredBuffer
001049cc g     F .text.__x86.get_pc_thunk.bx   00000000 .hidden __x86.get_pc_thunk.bx
00106100 g     O .data   00000059 g_USasciiNonShift
00106080 g     O .data   00000059 g_USasciiShift
00100990 g     F .text   000000a3 PMM_alloc
00104330 g     F .text   00000099 kstrcmp
00103ee0 g     F .text   00000051 VGA_WriteColoredString
00103513 g     F .text   00000000 irq_32_10
00100bd0 g     F .text   00000006 io_inb
00101c90 g     F .text   00000063 handlers32_boundRangeExceed
00110014 g     O .bss   00000004 g_VGA_row
00102da0 g     F .text   0000001b TSS32_setKernelStack
001023a0 g     F .text   00000090 PIT_init
00100820 g     F .text   00000087 PMM_init
001034da g     F .text   00000000 irq_32_4
0010fe80 g     O .bss   00000024 g_kernelArea
0010b00c g     O .bss   00000004 g_allocators
00102550 g     F .text   0000005f IA32_4KB_alloc
001005c0 g     F .text   00000184 BitmapPMA_free
00101c20 g     F .text   00000063 handlers32_overflow
00106159 g       .data   00000000 linker_dataEnd
0010dd80 g     O .bss   00000004 y
001034f8 g     F .text   00000000 irq_32_7
00103473 g     F .text   00000000 isr_32_27
00103344 g     F .text   00000000 GDT32_Load
001047c0 g     F .text   0000016c kutoa
00103d80 g     F .text   00000019 VGA_CreateEntry
00103990 g     F .text   00000026 RTC_init
001034a5 g     F .text   00000000 isr_32_128
001033eb g     F .text   00000000 isr_32_13
001044a0 g     F .text   0000007b kstrrev
00100c40 g     F .text   00000047 GDT32_getStruct
001033d3 g     F .text   00000000 isr_32_10
00110018 g     O .bss   00000004 g_VGA_column
00102c50 g     F .text   0000004d PIC_setMask
00102bb0 g     F .text   00000091 PIC_init
0010342d g     F .text   00000000 isr_32_20
00103455 g     F .text   00000000 isr_32_24
00100c00 g     F .text   00000005 io_wait
00101d70 g     F .text   00000063 handlers32_deviceNotAvailable
00103361 g     F .text   00000000 IDT32_Load
001034bc g     F .text   00000000 irq_32_1
00103369 g     F .text   00000000 TSS32_Load
00110020 g       .bss   00000000 linker_bssEnd
0010340f g     F .text   00000000 isr_32_17
00103385 g     F .text   00000000 isr_32_2
0010332a g     F .text   00000000 writeCR0
00102a80 g     F .text   000000df cpu_printState32
00104520 g     F .text   00000294 kprintf
001041e0 g     F .text   00000064 kmemcpy
001033c1 g     F .text   00000000 isr_32_8
00110000 g     O .bss   00000004 g_capsOnLayout
00100060 g     F .text   00000070 user_main
001040e0 g     F .text   00000066 kmemchr
0010b008 g     O .bss   00000001 g_allocatorsNumber
001033f3 g     F .text   00000000 isr_32_14
00110004 g     O .bss   00000001 g_shift
00103040 g     F .text   000002c9 init_init32
00103521 g     F .text   00000000 irq_32_12
0011001c g     O .bss   00000004 g_VGA_buffer
00102600 g     F .text   0000026e IA32_4KB_initKernelPaging
00102d30 g     F .text   00000065 TSS32_init
00103437 g     F .text   00000000 isr_32_21
001035ef g     F .text   00000000 CPUID_Call
001033a3 g     F .text   00000000 isr_32_5
00100360 g     F .text   0000025a BitmapPMA_alloc
00103487 g     F .text   00000000 isr_32_29
00102320 g     F .text   00000063 handlers32_security
001021d0 g     F .text   00000063 handlers32_machineCheck
0010ddc0 g     O .bss   00000068 g_TSSKernelEntry
00100018 g     F .text   0000003f _start32
00102f80 g     F .text   000000b7 CPUID_HasFeature
00110005 g     O .bss   00000001 g_special
00103405 g     F .text   00000000 isr_32_16
00103502 g     F .text   00000000 irq_32_8
00100000 g       *ABS*   00000000 KERNEL_PMA
00102cf0 g     F .text   0000003c PIC_maskUnusedIRQs
00100000 g       .text   00000000 linker_textStart
00103491 g     F .text   00000000 isr_32_30
00103469 g     F .text   00000000 isr_32_26
00110008 g     O .bss   00000004 g_shiftLayout



Code:
KERNEL_VMA = 0x100000;
KERNEL_PMA = 0x100000;

OUTPUT_FORMAT("elf32-i386")
ENTRY(_start32)
SECTIONS
{
    /* Place kernel at 1 MiB */
    . = KERNEL_PMA;

    linker_kernelStart = .;

    /* Read-Only sections */
    .text BLOCK(4K) : ALIGN(4K)
    {
        linker_textStart = .;

        CMakeFiles/phios.elf.dir/kernel/src/arch/x86/boot32.s.o (.text)
        *(EXCLUDE_FILE(CMakeFiles/phios.elf.dir/kernel/src/arch/x86/boot32.s.o) .text)

        linker_textEnd = .;
    }

    .rodata BLOCK(4K) : ALIGN(4K)
    {
        linker_rodataStart = .;

       *(.rodata)

        linker_rodataEnd = .;
    }

    /* Read-Write sections */
    .data BLOCK(4K) : ALIGN(4K)
    {
        linker_dataStart = .;

        *(.data)

        linker_dataEnd = .;
    }

    .bss BLOCK(4K) : ALIGN(4K)
    {
        linker_bssStart = .;

        *(.bss)
        *(COMMON)

        linker_bssEnd = .;
    }

    linker_kernelEnd = .;

    /* Discard useless sections */
    /DISCARD/ :
    {
        *(.note)
        *(.comment)
        *(.eh_frame) /* Add this if C++ support is needed */
    }
}


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Fri Aug 04, 2017 3:05 am 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
TheLittleWho wrote:
So, bss is mapped virtual, my problem is that when I access the global variable from bss, it's accessed physical address instead of virtual address...
Apparently, you have to pass "-pic" or "-pie" option to the final ld. I am confused by this, actually.

The linker can bypass GOT when the output is ET_EXEC. The original relocation, which is R_386_GOT32, is semantically converted to R_386_GOTOFF. Then it is preapplied and elided, which is possible, because the relative virtual layout is fixed in ET_EXEC and the offset of any variable from GOT is constant. Additionally a mov opcode in the relocated instruction has to be converted converted to lea opcode during link time. This is where the lea instruction in the assembly comes from. You can check the intermediate object file and see that it is still a mov instruction before the optimization.

The problem in your case is that the optimization is used only for .data, whilst for .bss the GOT entries are computed by the linker in terms of the fixed virtual addresses, thus causing the .bss variables to use GOT, but with fixed entries (and no corresponding R_386_GLOB_DAT relocations as with "-shared"). As a final resort you could have used "-q" to keep the relocations and processed them before switching paging on. Anyway, passing "-pic" to ld suddenly triggers GOT elision for both .data and .bss.

I also couldn't reproduce the relative addressing technique from your code. That is, with pic, normally the get_pc_thunk function is used to retrieve the current eip, because the return address is not a fixed quantity. I tried calling a static function and expected some kind of call graph analysis to show that it is called from one place, but the compiler still uses get_pc_thunk.


Top
 Profile  
 
 Post subject: Re: triple fault when use data from bss
PostPosted: Fri Aug 04, 2017 3:17 am 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
First of all, your KERNEL_VMA should rather be 0xC0000000, not 0x100000. Further, to have your sections at a different virtual and physical location, you will need to give the physical address using the AT directive. See this linker script for an example:
http://wiki.osdev.org/Higher_Half_x86_B ... #linker.ld

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 150 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group