Error: Trying to execute code outside RAM
Posted: Sun Feb 12, 2017 2:59 am
I've written a very simple kernel based in part on the Bare Bones tutorial. My working directory looks like
I am able to boot and run everything as expected until I include a static variable in either kernel.c or terminal.c, which causes the 'Trying to execute code outside RAM' error. My initial guess was that that static variable was messing up / overwriting the EIP somehow but I'm not sure how that would be possible. If I understand correctly static variables should get put on .bss which shouldn't mess with eip in any way at all.
**Edit**
I oversimplified things yesterday trying to reduce the problem down to a forum post friendly format. I failed to notice that including "terminal.h" and attempting to call a function from it is required to reproduce the error. The nature of the function called does not look to effect the error. Currently terminal.h declares a single function and terminal.c consists of two lines, one including terminal.h and an empty function definition for the function declared in terminal.h.
A static variable declared in either terminal.c or kernel.c causes the error to occur.
My next guess was that a compile flag might be causing some weird behavior but after looking everything up I'm not so sure.
**Double Edit - I've also tried using a cross compiler but the issue is not resolved.**
Compiling with
and
****
Anything to help me understand what is going on is greatly appreciated.
crash dump
kernel.c
boot.s
link.ld
Code: Select all
boot.s link.ld kernel.c terminal.h terminal.c Makefile
**Edit**
I oversimplified things yesterday trying to reduce the problem down to a forum post friendly format. I failed to notice that including "terminal.h" and attempting to call a function from it is required to reproduce the error. The nature of the function called does not look to effect the error. Currently terminal.h declares a single function and terminal.c consists of two lines, one including terminal.h and an empty function definition for the function declared in terminal.h.
A static variable declared in either terminal.c or kernel.c causes the error to occur.
Code: Select all
//terminal.h
#ifndef TERM_HEADER
#define TERM_HEADER
void some_function_from_terminal_h();
#endif
Code: Select all
//terminal.c
#include "terminal.h"
void some_function_from_terminal_h(){}
**Double Edit - I've also tried using a cross compiler but the issue is not resolved.**
Compiling with
Code: Select all
gcc -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
-nostartfiles -nodefaultlibs -c
Code: Select all
as --32
Anything to help me understand what is going on is greatly appreciated.
crash dump
Code: Select all
qemu: fatal: Trying to execute code outside RAM or ROM at 0xc36620e6
EAX=2badb002 EBX=00010000 ECX=00000000 EDX=00000000
ESI=00000000 EDI=00000000 EBP=20b0c366 ESP=fffffff0
EIP=c36620e6 EFL=00200086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0010 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00100380 00000020
IDT= 00000000 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000008 CCD=fffffff0 CCO=SUBL
EFER=0000000000000000
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
Code: Select all
#include "terminal.h"
static int test; // Without this everything behaves as expected
int kmain() {
test = 0;
/* This function runs just fine unless a static variable is declared in terminal.c or here in kernel.c */
some_function_from_terminal_h();
for(;;);
return 0;
}
boot.s
Code: Select all
.intel_syntax
.extern kmain
.set MAGIC_NUMBER, 0x1BADB002
.set FLAGS, 0X0
.set CHECKSUM, -(MAGIC_NUMBER + FLAGS)
.set KERNEL_STACK_SIZE, 16384 # 16 KiB
.section .multiboot
.align 4
.long MAGIC_NUMBER
.long FLAGS
.long CHECKSUM
# Reserve stack space for kernel
.section .bss
.align 16
stack_bottom:
.skip KERNEL_STACK_SIZE
stack_top:
.section .text
.global loader
loader:
mov %esp, stack_top
call kmain
loop:
jmp loop
Code: Select all
ENTRY(loader)
SECTIONS {
. = 1M;
.text BLOCK(4K) : ALIGN (4K)
{
*(.multiboot)
*(.text)
}
.rodata BLOCK(4K) : ALIGN (4K)
{
*(.rodata)
}
.data BLOCK(4K) : ALIGN (4K)
{
*(.data)
}
.bss BLOCK(4K) : ALIGN (4K)
{
*(COMMON)
*(.bss)
}
}