You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

裸机环境下32位内核意外内存损坏问题求助

内核.text段覆盖引发随机异常问题

我有一个可被下述Bootloader成功加载的32位内核,当前遇到的问题是内核的.text段会覆盖其他内存区域,进而引发随机异常行为。当仅存在3个func函数时无异常,但添加第4个func后问题触发,因此推测是超出了某种内存限制。曾尝试修改链接脚本添加对齐设置,但并未解决问题(可能操作有误)。相关代码及状态如下:


Bootloader代码

[org 0x7c00]
; we can also alter the data segment register (ds) accordingly:
; mov ds, 0x7c0 (it will be 0x7c00 because address bus is 20-bit)

KERNEL_LOCATION equ 0x1000
BOOT_DISK equ 0

; setting up the stack and tiny memory model
xor ax, ax
mov es, ax
mov ds, ax
mov bp, 0x8000
mov sp, bp


; loading the kernel in memory location 0x1000
call read_disk

; ; Set up VESA graphics mode (example: mode 0x101 - 640x480, 16 colors)
; mov ax, 0x4F02     ; VESA function to set video mode
; mov bx, 0x101      ; Mode number for desired resolution/color depth
; int 0x10           ; Call BIOS interrupt to set the video mode

; initialize protected mode
init_pm:

    ; clear the screen using bios before disabling it
    mov ax, 0x0003 
    int 10h

    ; clear interrupts
    mov bx,suc
    ;call print_string
    
    ; some delay just for looking cool :)
    ;call delay
    ;mov ax, 0x0003
    ;int 10h
 
    cli
    ; this instruction loads the gdt location and size into the gdtr (gdt register) in the cpu
    lgdt [GDT_descriptor]

    ; changing the first bit of cr0 register (0th control register) which corresponds to [ Protected Mode Enable ]
    mov eax, cr0
    or eax, 1
    mov cr0, eax

    ; far jump!!
    jmp CODE_SEG:start_protected_mode

%include "gdt.asm"
%include "read_disk.asm"
;%include "print_string.asm"
%include "delay.asm"

; this tells the assembeler (in this case nasm) to compile the
; code to 32 bit machine code now on
[bits 32]
start_protected_mode:

    ; changing stack and memory segments model
    mov eax, DATA_SEG
    mov ds, eax
    mov ss, eax
    mov es, eax
    mov fs, eax
    mov gs, eax

    mov ebp, 0x90000        ; 32 bit stack base pointer
    mov esp, ebp

    jmp KERNEL_LOCATION

suc:
    db "entering 32-bit mode...",0
jmp $



times 510-($-$$) db 0
dw 0xaa55

内核C代码

#include "std/stdout.h"
#include "terminal/terminal.h"



void func1(){
        for (int i = 0; i < 33; i++)
    {
        /* code */
    }
}
void func2(){
        for (int i = 0; i < 33; i++)
    {
        /* code */
    }
}
void func3(){
        for (int i = 0; i < 33; i++)
    {
        /* code */
    }
}
void func4(){
    for (int i = 0; i < 33; i++)
    {
        /* code */
    }
    
}


void kernel_start(){
    prepare_terminal();
    background_color = 32;
}

当前链接脚本

ENTRY(_start)

SECTIONS {
    . = 0x1000;

    .text : {
        *(.text)
    }
    
    .data : {
        *(.data)
    }

    .bss : {
        *(.bss)
    }
}

状态截图

  • 异常状态:异常状态
  • 正常状态:正常状态

内容的提问来源于stack exchange,提问作者alireza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 10:13:25