- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过编写自己的引导加载程序将内核加载到内存中。我已经能够成功地将内核加载到内存中 - 我知道因为我使用了 bochs 调试器,所以将断点设置为 0x7c00 并单步执行,系统确实跳转到内核中。问题是,跳转到内核后,所有打印语句(在 kernel.s 中)都不起作用。这在终端上表明内核已加载到内存中。
这是 bootblock.s 文件(大部分相关代码位于标签 booter
处:
# bootblock.s
# Empty boot block file
# .equ symbol, expression
# These directive set the value of the symbol to the expression
.equ BOOT_SEGMENT,0x07c0
.equ DISPLAY_SEGMENT,0xb800
.equ KERNEL_LOCATION, 0x1000
.equ STACK_SP, 0xffff
.equ STACK_SS, 0x0
.text # Code segment
.globl _start # The entry point must be global
.code16 # Real mode
_start:
###MAKE BOOTABLE###
#. = _start + 510
#.byte = 0x55
#.byte = 0xaa
jmp booter
os_size:
#Place where createimage writes the OS size
.word 0
.word 0
print:
movw $BOOT_SEGMENT,%ax
movw %ax,%ds
print_loop:
lodsb
cmpb $0,%al
je print_done
movb $14,%ah
movl $0x0002,%ebx
int $0x10
jmp print_loop
print_done:
retw
booter:
###SET UP STACK###
#Allocating the stack
movw $STACK_SS, %ax
movw %ax, %ss
movw $STACK_SP, %sp
movl $allocating, %esi
call print
movl $done, %esi
call print
#Resetting the disk drive, setting %dl and calling int 0x13
#movb $0x0, %ah
#movb $0x0, %dl
#int $0x13
movl $bootblock_test, %esi
call print
movl $hellostring, %esi
call print
###LOAD KERNEL###
movl $loadingkernel, %esi
call print
#Number of sectors to read
#movb $0x24, %al
#movb $0x80, %al
movb $0x08, %al
movb $0x02, %ah
#track number
#movb $0x00, %ch
#which sector to read from (sector 2 where kernel should be)
movb $0x02, %cl
#set up head number
movb $0x0, %dh
#Set the drive number to 0x0 (floppy)
movb $0x0, %dl
#Time to set es:bx to read from the correct place (0:1000)
movw $0x0100, %bx
movw %bx, %es
movw $0x0, %bx
#movw $0x0, %ax
#Setting %ah = 2 and calling int 0x13 (read sector)
int $0x13
movl $done, %esi
call print
#Booting up at 0x07c0
#movw $BOOT_SEGMENT, %ax
#movw %ax, %ds
#movl $bootmessage, %esi
#call print
#%dh/%ch control head numbers, setting them to 0
#movb $0x0, %dh
#movb $0x0, %ch
#movw %ds,
###INVOKE KERNEL###
#Kernel jump
movl $readymessage, %esi
call print
#Setting %ds = 0x7c0
movw $0x0100, %ax
movw %ax, %ds
#Time to set es:bx to read from the correct place (0:1000)
movw $0x0100, %bx
movw %bx, %es
movw $0x0, %bx
movl $0x1000, %ax
jmp %ax
mov $0x0, %ax
#If any errors, message will be displayed here
movl $errormessage, %esi
call print
forever:
jmp forever
#Error handling
error:
movl $errormessage, %esi
call print
# messages
mystring:
.asciz "test.\n\r"
bootblock_test:
.asciz "\nBootblock Test\n\r"
hellostring:
.asciz "How are you today?\n\r"
myname:
.asciz "Welcome\n\r"
loadingkernel:
.asciz "Loading Kernel...\n\r"
done:
.asciz "Done!\n\r"
bootmessage:
.asciz "Booting up...\n\r"
readymessage:
.asciz "Sliding into yo Kernel like... \n\r"
errormessage:
.asciz "Something went terribly wrong...\n\r"
rebootmessage:
.asciz "Press any key to reboot the OS!\n\r"
allocating:
.asciz "Allocating Stack...\n\r"
这是 kernel.s 文件:
.data # Data segment
# Some strings
kernel:
.asciz "[Kernel]-> "
testing:
.asciz "Running a trivial test... "
works:
.asciz "Seems Ok. Now go get some sleep :)."
not:
.asciz "*Failed*"
# 'Newline' string ('carriage return', 'linefeed', '\0')
newline:
.byte 10
.byte 13
.byte 0
# An integer
result:
.word 1000
.text # Code segment
.code16 # Real mode
.globl _start # The entry point must be global
#
# The first instruction to execute in a program is called the entry
# point. The linker expects to find the entry point in the "symbol" _start
# (with underscore).
#
_start:
pushw %bp # Setup stack frame
movw %sp,%bp
pushw $newline
call displayString # Print messages
pushw $kernel
call displayString
pushw $testing
call displayString
pushw $1000
call trivialTest # trivialTest(1000)
addw $8,%sp # Pop newline, kernel, testing, and '1000'
cmpw %ax,result
jne .L6 # If (trivialTest(1000) != 1000) goto L6
pushw $works
jmp .L12
.L6: # Test failed
pushw $not
.L12:
call displayString # Print ok/failed message
addw $2,%sp
pushw $newline
call displayString
addw $2,%sp
.L8: # Loop forever
jmp .L8
#
# int trivialTest(n)
# {
# if (n > 0) {
# trivialTest(n-1);
# }
# return n;
# }
trivialTest:
pushw %bp # Setup stack frame
movw %sp,%bp
movw 4(%bp),%ax # Move argument to ax
testw %ax,%ax # Logical compare (sets SF, ZF and PF)
jg .L2 # if (argument > 0) goto L2
xorw %ax,%ax # else return 0
popw %bp
retw
.L2:
decw %ax
pushw %ax
call trivialTest # trivialTest(argument - 1)
# (Recursive calls until argument == 0)
addw $2,%sp # Pop argument
incw %ax
popw %bp
retw # Return (argument in ax)
displayString:
pushw %bp # Setup stack frame
movw %sp,%bp
pushw %ax # Save ax, bx, cx, si, es
pushw %bx
pushw %cx
pushw %si
pushw %es
movw %ds, %ax # Make sure ES points to the right
movw %ax, %es # segment
movw 4(%bp),%cx # Move string adr to cx
movw %cx, %si
loop:
lodsb # Load character to write (c) into al,
# and increment si
cmpb $0, %al
jz done # if (c == '\0') exit loop
movb $14,%ah # else print c
movw $0x0002,%bx
# int 0x10 sends a character to the display
# ah = 0xe (14)
# al = character to write
# bh = active page number (we use 0x00)
# bl = foreground color (we use 0x02)
int $0x10
jmp loop
done:
popw %es # Restore saved registers
popw %si
popw %cx
popw %bx
popw %ax
popw %bp
retw # Return to caller
我再次在调试器中检查内核是否已加载到内存中(0x1000)。我相信问题在于我如何设置/使用 bootblock.s 中的某些寄存器(主要是: ds
,ax
),但我不确定它是什么。
最佳答案
代码存在许多问题。大多数似乎适用于大多数 16 位操作系统引导加载程序的基本内容可以在我最近的 Stackoverflow answer 中找到。 (类似类型的问题)。它涵盖了 SS/SP/ES/DS/CS 寄存器需要注意的事项、8086/8088 上的堆栈性能问题以及旧有缺陷的 8086/8088 上需要注意的一些事项。
代码的一个具体问题 - 如果您将在 8086/8088 系统或模拟器(不是 286、386 等)上运行代码,那么您应该坚持使用 16 位寄存器,因为 32 位寄存器不是可用的。您的代码使用ESI和EBX寄存器(32位)。您应该使用SI和BX。
此代码中的主要问题是,在大多数情况下,内核都是从磁盘读取的,但它读取的扇区数量恰好少于内核镜像实际占用的扇区数。这恰好导致内核要打印的变量没有被加载。
我最初假设内核显得如此小,从磁盘读取 8 个扇区对于所提供的示例代码来说已经足够了:
#Number of sectors to read
movb $0x08, %al
我发现原来的发帖者正在做一项作业,并且有人发布了一些关键信息来解决问题。我已经 fork 了git project仅供引用。一些关键信息是所使用的 Makefile 的类型(略有差异)以及为作业提供的名为 createimage
的程序。
Makefile 足够接近类似于:
# Makefile for the OS projects.
# Best viewed with tabs set to 4 spaces.
CC = gcc -Wall -Wextra -std=c99 -g
LD = ld
# Where to locate the kernel in memory
KERNEL_ADDR = 0x1000
# Compiler flags
#-fno-builtin: Don't recognize builtin functions that do not begin with
# '__builtin_' as prefix.
#
#-fomit-frame-pointer: Don't keep the frame pointer in a register for
# functions that don't need one.
#
#-make-program-do-what-i-want-it-to-do:
# Turn on all friendly compiler flags.
#
#-O2: Turn on all optional optimizations except for loop unrolling
# and function inlining.
#
#-c: Compile or assemble the source files, but do not link.
#
#-Wall: All of the `-W' options combined (all warnings on)
CCOPTS = -c -fomit-frame-pointer -O2 -fno-builtin
# Linker flags
#-nostartfiles: Do not use the standard system startup files when linking.
#
#-nostdlib: Don't use the standard system libraries and startup files when
# linking. Only the files you specify will be passed to the linker.
#
#-Ttext X: Use X as the starting address for the text segment of the output
# file.
LDOPTS = -nostartfiles -nostdlib -Ttext
# Makefile targets
all: bootblock createimage kernel image boch_image
kernel: kernel.o
$(LD) $(LDOPTS) $(KERNEL_ADDR) -o kernel $<
bootblock: bootblock.o
$(LD) $(LDOPTS) 0x0 -o bootblock $<
createimage: createimage.o
$(CC) -o createimage $<
# Create an image to put on the floppy
image: bootblock createimage kernel
./createimage ./bootblock ./kernel
# Put the image on the floppy (these two stages are independent, as both
# vmware and bochs can run using only the image file stored on the harddisk)
#boot: image
# cat ./image > /dev/sda
#write image to boch disk image
boch_image: image
dd if=image of=bochs.img conv=notrunc
# Clean up!
clean:
rm -f *.o
rm -f createimage image bootblock kernel
# No, really, clean up!
distclean: clean
rm -f *~
rm -f \#*
rm -f *.bak
rm -f bochsout.txt
# How to compile a C file
%.o:%.c
$(CC) $(CCOPTS) $<
# How to assemble
%.o:%.s
$(CC) $(CCOPTS) $<
# How to produce assembler input from a C file
%.s:%.c
$(CC) $(CCOPTS) -S $<
根据原始发布者的后续评论,他们正在构建 elf_i386 二进制文件(32 位的 Linux ELF 格式)。上面的 Makefile 表明内核被构建为 ELF 镜像,然后放置在磁盘上。 ELF 格式为每个部分添加了大量的填充,因此假设从磁盘读取 8 个扇区 可能还不够。 Makefile 中没有引用 objcopy 或转换为平面二进制格式。我发现createimage 他们提供的程序从 32 位 ELF 格式中提取内核镜像,并生成输出,说明引导加载程序需要读取多少个扇区才能获取整个内核。 createimage
代码是:
/* createimage.c -- create a bootable image in 16 real mode from several elf file
*/
#include <stdio.h>
#include <stdlib.h>
#include "createimage.h"
int file_process(FILE *elf_file, FILE *image, char *elf_filename);
long byte_get (unsigned char *field, int size);
int main (int argc, char ** argv)
{
//here hasn't check the magic numbers of elf
if (argc != 3) {
printf("USAGE:%s bootblock kernel\n", argv[0]);
return -1;
}
FILE *bootblock, *kernel, *image;
if ((bootblock = fopen (argv[1], "rb")) == NULL) {
printf("can't open %s\n", argv[1]);
return -1;
}
if ((image = fopen ("image", "wb")) == NULL) {
printf("can't open image!\n");
return -1;
}
if (file_process(bootblock, image, argv[1])) {
printf("process bootblock failed\n");
return -1;
}
if ((kernel = fopen (argv[2], "rb")) == NULL) {
printf("can't open %s\n", argv[2]);
return -1;
}
if (file_process(kernel, image, argv[2])) {
printf("process kernel failed\n");
return -1;
}
fclose(bootblock);
fclose(kernel);
fclose(image);
return 0;
}
long byte_get (unsigned char *field, int size)
{
switch (size)
{
case 1:
return *field;
case 2:
return ((unsigned int) (field[0])) | (((unsigned int) (field[1])) << 8);
case 4:
return ((unsigned long) (field[0]))
| (((unsigned long) (field[1])) << 8)
| (((unsigned long) (field[2])) << 16)
| (((unsigned long) (field[3])) << 24);
default:
printf("byte_get error\n");
return -1;
}
}
/* read information from elf file, and write LOAD segment to image file
*
* note: the structure in file is not aligned, we just read it from file byte
* by byte
*/
int file_process(FILE *elf_file, FILE *image, char *elf_filename)
{
unsigned int header_sz, pheader_sz;
unsigned long phoff;
unsigned int p_offset;
unsigned int p_filesz;
unsigned int p_memsz;
elf_header header;
elf_program_header pheader;
header_sz = sizeof (elf_header);
pheader_sz = sizeof(elf_program_header);
printf("processing %s:\n", elf_filename);
printf("header size is: %d\n", header_sz);
printf("program header size is: %d\n", pheader_sz);
if (header_sz != fread(&header, 1, header_sz, elf_file)) {
printf("read error!\n");
return -1;
}
//get program header's offset
phoff = byte_get(header.e_phoff, sizeof(header.e_phoff));
printf("Program header table offset in file is :\t %u\n", phoff);
if (fseek(elf_file, phoff, SEEK_SET)) {
printf("fseek %s failed! at line %d\n", elf_filename, __LINE__);
return -1;
}
//printf("the current position: %d\n", ftell(elf_file));
if (pheader_sz != fread(&pheader, 1, pheader_sz, elf_file)) {
printf("read error at line %d!\n", __LINE__);
return -1;
}
//get the LOAD segment's offset, filesz, mensz
p_offset = byte_get(pheader.p_offset, sizeof(pheader.p_offset));
p_filesz = byte_get(pheader.p_filesz, sizeof(pheader.p_filesz));
p_memsz = byte_get(pheader.p_memsz, sizeof(pheader.p_memsz));
printf("p_offset: 0x%x\tp_filesz: 0x%x\tp_memsz: 0x%x\t\n", p_offset, p_filesz, p_memsz);
//write elf's LOAD segment to image, and pad to 512 bytes(1 sector)
char *buffer;
const unsigned int sector_sz = 512;
const char MBR_signature[] = {0x55, 0xaa};
unsigned int n_sector;
unsigned int n_byte;
if (p_memsz % sector_sz != 0)
n_sector = p_memsz / sector_sz + 1;
else
n_sector = p_memsz / sector_sz;
n_byte = n_sector * sector_sz;
if (!(buffer = (char *)calloc(n_byte, sizeof(char)))) {
printf("malloc buffer failed! at line %d\n", __LINE__);
return -1;
}
if (fseek(elf_file, p_offset, SEEK_SET)) {
printf("fseek %s failed! at line %d\n", elf_filename, __LINE__);
return -1;
}
if (p_filesz != fread(buffer, 1, p_filesz, elf_file)) {
printf("read error at line %d!\n", __LINE__);
return -1;
}
if (n_byte != fwrite(buffer, 1, n_byte, image)) {
printf("write error at line %d!\n", __LINE__);
return -1;
}
//write MBR signature to image, which is 2 bytes
if (fseek(image, 510, SEEK_SET)) {
printf("fseek %s failed! at line %d\n", elf_filename, __LINE__);
return -1;
}
if (2 != fwrite(MBR_signature, 1, 2, image)) {
printf("write error at line %d!\n", __LINE__);
return -1;
}
printf("write image:\n%d sectors,\t%d bytes\n", n_sector, n_byte);
return 0;
}
重要的是,在提取代码和数据部分(使用 dd
放入磁盘镜像中)后,它会在底部提供与此类似的输出:
processing ./kernel:
header size is: 52
program header size is: 32
Program header table offset in file is : 52
p_offset: 0x54 p_filesz: 0x10db p_memsz: 0x10db
write image:
9 sectors, 4608 bytes
dd if=image of=bochs.img conv=notrunc
这里有重要信息9 个扇区,4608 字节
。这表示内核在扇区中的大小。原始发布者的代码必须确保它们在加载内核时读取那么多扇区。因此,简单的解决方法是将代码更改为:
#Number of sectors to read should match output from createimage
movb $0x09, %al
关于assembly - 无法从 kernel.s 打印,即使它已加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683438/
我需要从 kernel/system/do_kill.c (Minix) 将一些信息写入我的特殊日志文件(例如,/home/log.txt) . 我已经尝试过: int filedesc; filed
我正在经历 Uboot 和内核启动过程。 FDT(平面设备树)到底有什么用? 我读过的许多链接都指出,uboot 以 FDT 的形式将板和 SOC 配置信息传递给内核 https://wiki.fre
我的操作系统是Fedora 17。最近,出现内核污染警告“kernel bug at kernel/auditsc.c:1772!-abrt”:不应报告此问题(这可能是一个已知问题)。发生内核问题,但
为 Linux 编译内核模块的教程,使用不同的 Makefile 语法。 Example 1 obj-m += rpi-pwm.o Example 2 obj-m := nothing.o 有什么区别
我正在浏览 Linux 网络设备驱动程序代码,想知道是否可以从驱动程序代码中调用设备层代码。 --- a/drivers/net/ethernet/realtek/8139too.c +++ b/dr
出于工具目的,我尝试附加到 kprobe 事件,但我对 kprobe 事件不太熟悉。我读到注册的 kprobes 列表可以在 /sys/kernel/debug/kprobes/list 中找到,但是
我在其他地方使用 LinK+ 来开发 linux 内核模块。我的开发机器安装了 Linux Mint 18 操作系统,内核版本为 4.4.xx。为了进行测试,我想将内核模块部署到内核版本为 3.16.
我正在玩弄 Android Linux 内核。内核是P970 V30B内核。可用here .我想找出特定的 cmdline 参数到底做了什么。 我知道它是命令行参数,其中有一个参数我找不到它到底做了什
是否可以将 PTE 指向不同的物理页面? 假设我目前在某个进程 A 的上下文中处于内核模式,该进程当前将地址 400k 映射到物理页号。 5. 我可以将该地址 (400k) 映射到物理页号吗? 6 ?
我正在开发一个要在路由器上运行的内核模块。路由器型号为 Netgear 的 DGN2200v2。它在 MIPS 上运行 Linux 2.6.30。我的问题是,当我加载我的模块时,似乎我的 module
关闭。这个问题不符合 Stack Overflow guidelines 。它目前不接受答案。 想改进这个问题?更新问题,使其成为 Stack Overflow 的 on-topic。 6年前关闭。
在大型网格中执行的任务调用两个内核有区别吗 1. for(int i=0;i>>(MatrixA,MatrixB) } 2. dim3 dimBlock(16, 16); dim3 dimGrid(1
在 Linux 中,我生成了一个猜测 VM 并加载了另一个 Linux 实例。 VM 是通过 KVM/libvirt/qemu 生成的。 guest VM 被主机内核视为一个进程。让我们说由于某种原因
我想知道如何设置正确 MACH_TYPE或 arch_id对于内核。我搜索并找到了至少 2 个引用,其中内核会卡在“启动内核...”处。这些都给出了相同的答案。正确设置您的机器类型。但后来都没有提到如
kernel data inpage error蓝屏是一个不常见的问题,一般都是更新失败或者第三方软件冲突导致的,解决方法也非常简单,需要先安全模式进入系统,下面来看看详细的教程吧。 kern
当我用 app/console cache:clear 清除缓存时我收到错误: [Symfony\Component\DependencyInjection\Exception\ParameterNo
我正在玩 Raspberry 3,并尝试使用 U-Boot 启动 Linux 内核。 我构建了一个 Linux 内核(来自 github.com/raspberrypi)和 Busbox-Userla
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
长话短说,我一直在做一个项目,当我使用时我注意到: 1.9.3p392 :001 > `gzip` IRB::Abort: abort then interrupt! from (irb):1
我有一个服务,我在其中注入(inject) TokenStorage 并想要获取当前用户。 /** * * @Service("liip_theme.theme_request_listener"
我是一名优秀的程序员,十分优秀!