操作系统内核Hack:(一)实验环境搭建
操作系统内核Hack:(一)实验环境搭建
三四年前,心血来潮,入手《Orange’s:一个操作系统的实现》学习操作系统内核,还配套买了王爽的《汇编语言(第二版)》和《80X86汇编语言程序设计教程》,虽然Orang’s只看了不到三分之一,但当时还是很认真的,练习也做了不少。唯一遗憾的就是没有留下文字记录,导致现在忘得差不多一干二净了,后悔不已!如今想再捡起来,弥补当时的懒惰,虽然困难重重,但这么优秀的国产书怎么能看完就算了呢!而且当年还是在Windows下练习的,现在终于用上了原汁原味的Linux。再也不用在虚拟机鼓捣了,更应该重新品味一番!
1.开发工具安装
1.1 编译器:NASM和GCC
操作系统内核的引导部分是要用汇编语言来写的,但后面大部分都是用C语言,所以编译器方面我们需要NASM和GCC。
cdai@cdai ~ $ sudo apt-get install build-essential nasm
1.2 编辑器:SublimeText
Sublime Text提供了NASM插件,通过Package Control直接就能安装,很方便。虽然没有什么智能提示,但是有关键字染色还是不错的!
1.3 构建:Make
构建工具方面,当然是用GNU Make。具体内容先不做详述,等到需要时再系统学习一下。
1.4 模拟器:Bochs
Linux当然选择Bochs,此外还要安装bximage来生成映像文件。
cdai@cdai ~ $ sudo apt-get install bochs bximage
要注意的是,像上面那样通过apt安装的确方便,但是却没有调试功能,所以一般建议通过Bochs源码安装,打开调试功能。如果出现错误请参考第四部分“Bochs常见问题”和网上资料。
cdai@cdai ~/Software $ tar xzvf bochs-2.4.6.tar.gz
cdai@cdai ~/Software $ cd bochs-2.4.6
cdai@cdai ~/Software/bochs-2.4.6 $ ./configure --enable-debugger --enable-disasm
cdai@cdai ~/Software/bochs-2.4.6 $ make
cdai@cdai ~/Software/bochs-2.4.6 $ sudo make install
2.操作系统实验
2.1 Hello, OS!
这是我们的第一个汇编程序,只有20多行很短,但是“五脏俱全”。因为涉及到汇编语言的基础知识,所以先不做解释。待整理完汇编语言的一篇文章之后,再简单解释一下这个NASM汇编程序的含义。
org 07c00h
mov ax, cs
mov ds, ax
mov es, ax
call DispStr
jmp $
DispStr:
mov ax, BootMessage
mov bp, ax
mov cx, 16
mov ax, 01301h
mov bx, 000ch
mov dl, 0
int 10h
ret
BootMessage: db "Hello, OS world!"
times 510-($-$$) db 0
dw 0xaa55
2.2 编译汇编程序
用NASM编译我们写好的汇编代码。因为汇编语言是机器语言的“助记符”,与二进制代码几乎是一一对应的,所以可以看到编译出的二进制文件boot.bin正好就是我们想要的512字节。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's $ nasm boot.asm -o boot.bin
cdai@cdai ~/Workspace/syspace/1-assembly/orange's $ ls -l
total 16
-rw-r--r-- 1 cdai cdai 266 Sep 12 09:58 boot.asm
-rw-r--r-- 1 cdai cdai 512 Sep 12 08:50 boot.bin
2.3 创建虚拟软盘
安装Bochs时我们还安装了一个叫bximage的软件,它可以帮我们生成一个虚拟软盘或硬盘。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's $ bximage
========================================================================
bximage
Disk Image Creation Tool for Bochs
$Id: bximage.c,v 1.34 2009/04/14 09:45:22 sshwarts Exp $
========================================================================
Do you want to create a floppy disk image or a hard disk image?
Please type hd or fd. [hd] fd
Choose the size of floppy disk image to create, in megabytes.
Please type 0.16, 0.18, 0.32, 0.36, 0.72, 1.2, 1.44, 1.68, 1.72, or 2.88.
[1.44]
I will create a floppy image with
cyl=80
heads=2
sectors per track=18
total sectors=2880
total bytes=1474560
What should I name the image?
[a.img]
Writing: [] Done.
I wrote 1474560 bytes to a.img.
The following line should appear in your bochsrc:
floppya: image="a.img", status=inserted
于是在当前目录我们就得到了一个a.img,大小是1440K,可以将它想象成在Windows中经常碰到的ISO镜像文件。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's $ ls -l --block-size=k
total 20K
-rw-r--r-- 1 cdai cdai 1440K Sep 12 09:57 a.img
-rw-r--r-- 1 cdai cdai 1K Sep 12 09:58 boot.asm
-rw-r--r-- 1 cdai cdai 1K Sep 12 08:50 boot.bin
2.4 烧录程序
有了虚拟软盘,我们就可以将刚才编译好的汇编程序“烧录”到软盘中了。注意:一定要加conv=notrunc参数,因为a.img比boot.bin大得多,如果不加这个参数则a.img会按照boot.bin的大小被截断。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's $ dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000741281 s, 691 kB/s
2.5 配置Bochs
现在还不能急着启动我们的操作系统,因为我们还没告诉Bochs我们的虚拟机是什么样子,比如内存多大、软盘/硬盘映像文件在哪等等。所以我们需要在当前文件夹下创建一个bochsrc配置文件。
###########################################################
# Configuration file for Bochs
###########################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: /usr/share/bochs/BIOS-bochs-latest
vgaromimage: /usr/share/vgabios/vgabios.bin
# what disk images will be used
floppya: 1_44=a.img, status=inserted
# choose the boot disk
boot: floppy
# where do we send log messages
log: bochsout.txt
# disable the mouse
mouse: enabled=0
# enable key mapping, using US layout as default
keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
2.6 启动Bochs
一切准备就绪,执行bochs -f bochsrc就可以启动我们的操作系统了!如果发现Bochs出错崩溃了,看一下提示的错误信息,参照本文第四部分的“Bochs常见问题”寻找解决办法。如果没有包含在其中,就上网搜索一下,关于Bochs常见问题的资料还是挺多的。
具有调试功能的Bochs启动时不会立即执行我们的操作系统,而是出现选项,给了我们加断点和调试的机会。这里我们直接选6开始模拟,然后输出”c”回车继续执行。
========================================================================
Bochs x86 Emulator 2.4.6
Build from CVS snapshot, on February 22, 2011
Compiled at Sep 12 2015, 12:05:24
========================================================================
00000000000i[ ] reading configuration from bochsrc
------------------------------
Bochs Configuration: Main Menu
------------------------------
This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate. Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found. When you are satisfied with the configuration, go
ahead and start the simulation.
You can also start bochs with the -q option to skip these menus.
1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now
Please choose one: [6]
00000000000i[ ] installing x module as the Bochs GUI
00000000000i[ ] using log file bochsout.txt
Next at t=0
(0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b ; ea5be000f0
<bochs:1> c
最终我们能看到下面这样的画面:
Hello, OS world!�ios (PCI) current-cvs 19 Sep 2012
This VGA/VBE Bios is released under the GNU LGPL
Please visit :
. http://bochs.sourceforge.net
. http://www.nongnu.org/vgabios
Bochs VBE Display Adapter enabled
Bochs BIOS - build: 06/08/13
$Revision: 1.257 $ $Date: 2011/01/26 09:52:02 $
Options: apmbios pcibios pnpbios eltorito rombios32
Press F12 for boot menu.
Booting from Floppy...
3.实验环境升级
通过上面的例子,可以总结一下未来我们开发的一般步骤:
- 用Sublime Text编辑代码
- 用Make调用GCC/NASM编译并写入映像文件
- 启动Bochs运行和调试
但这种方式存在一个潜在的问题,就是引导扇区空间有限,只有512字节。随着我们的程序越来越大,很快512字节就会不够用了。写一个能够加载我们程序的引导扇区可以解决这个问题,但现阶段来说有点难。于是我们可以求助于现成的操作系统内核,如DOS,加载我们的程序。而且很多教程也都是基于DOS来讲解的。
3.1 可引导FreeDos
下载FreeDos镜像,并修改bochsrc配置文件中的软驱设置。注意:Sourceforge已基本瘫痪,我是从CSDN用积分下载下来的。而且我从freedos官网上下的freedos可引导软盘版也不好用,所以还是乖乖从Bochs上下载了。
floppya: 1_44=freedos.img, status=inserted
floppyb: 1_44=a.img, status=inserted
3.2 用FreeDos格式化a.img
启动Bochs,格式化B盘,清除掉之前写入到a.img的内容。
Type INSTALL to start the FreeDOS installation
If you need to create a partition on your hard disk for FreeDOS, you
will need to do that yourself. Use FDISK to create a partition, and
use FORMAT to make the partition writable by FreeDOS. You can run
both programs from this Install Boot Floppy.
_________________________________________________________________________
A:\>format B:
Reading boot sector...
Cylinder: 0 Head: 0
Saving UNFORMAT information...
Cylinder: 77 Head: 1
Creating file system...
Cylinder: 0 Head: 0
Format operation complete.
3.3 重编译boot.asm
首先,修改boot.asm文件第一行的07c00h,改为0100h。因为DOS与Linux的引导扇区加载到内存中的位置不同。改好后重新用NASM编译,后缀名是COM。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's/ch03 $ nasm boot.asm -o boot.com
3.4 拷贝程序到软盘映像
4)将编译出的COM文件复制到软盘映像中。如果没有/mnt/floppy文件夹就用sudo创建一个,没有什么特殊的。有一点要特别注意:一定要停掉Bochs,否则a.img会损坏。
cdai@cdai ~/Workspace/syspace/1-assembly/orange's/ch03 $ sudo mkdir /mnt/floppy
cdai@cdai ~/Workspace/syspace/1-assembly/orange's/ch03 $ sudo mount -o loop a.img /mnt/floppy
cdai@cdai ~/Workspace/syspace/1-assembly/orange's/ch03 $ sudo cp boot.com /mnt/floppy/
cdai@cdai ~/Workspace/syspace/1-assembly/orange's/ch03 $ sudo umount /mnt/floppy
3.5 通过FreeDos运行
重新启动Bochs,执行软驱B中的boot.com就能在屏幕第一行看到红色的”Hello, OS world!”了!
Hello, OS world!�________________________________________________________
WELCOME TO THE FREEDOS BETA4 RELEASE!
Type INSTALL to start the FreeDOS installation
If you need to create a partition on your hard disk for FreeDOS, you
will need to do that yourself. Use FDISK to create a partition, and
use FORMAT to make the partition writable by FreeDOS. You can run
both programs from this Install Boot Floppy.
_________________________________________________________________________
A:\>dir B:
Volume in drive B has no label
Directory of B:\*.*
BOOT COM 512 09-13-115 11:37a
1 file 512 bytes
0 dirs 1,457,664 bytes free
A:\>B:\boot.com
4.Bochs常见问题
4.1 [安装] error adding symbols: DSO missing from command line
修改Makefile的LIBS中加上”-lpthread”。
4.2 [运行] romimage directive malformed
romimage语法错误,例如没写”file=”。
4.3 [运行] dlopen failed for module ‘x’
除了bochs,还要安装bochs-x。
4.4 [运行] bochs-bin: symbol lookup error: /usr/lib/bochs/plugins/libbx_x.so: undefined symbol: XpmCreatePixmapFromData
我是Linux Mint系统(Ubuntu),网上资料说还要安装bochs-sdl,并且在bochsrc配置文件的开头加上”display_library: sdl”。
4.5 [运行] unknown host key name ‘XK_0’ (wrong keymap ?)
注释掉#keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
4.6 我最终的环境和配置
apt安装Bochs时需要设置”display_library: sdl”并注释掉”keyboard_mapping”,但源码安装却没有碰到类似问题。
cdai@cdai ~ $ sudo apt-get install bximage bochs bochs-x bochs-sdl
cdai@cdai ~ $ cat bochsrc
###########################################################
# Configuration file for Bochs
###########################################################
#display_library: sdl
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/vgabios/vgabios.bin
# what disk images will be used
floppya: 1_44=a.img, status=inserted
# choose the boot disk
boot: floppy
# where do we send log messages
log: bochsout.txt
# disable the mouse
mouse: enabled=0
# enable key mapping, using US layout as default
keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
操作系统内核Hack:(一)实验环境搭建的更多相关文章
- 操作系统内核Hack:(二)底层编程基础
操作系统内核Hack:(二)底层编程基础 在<操作系统内核Hack:(一)实验环境搭建>中,我们看到了一个迷你操作系统引导程序.尽管只有不到二十行,然而要完全看懂还是需要不少底层软硬件知识 ...
- 操作系统内核Hack:(三)引导程序制作
操作系统内核Hack:(三)引导程序制作 关于本文涉及到的完整源码请参考MiniOS的v1_bootloader分支. 1.制作方法 现在我们已经了解了关于BootLoader的一切知识,让我们开始动 ...
- Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试
标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9 ...
- 操作系统内核Hack:(四)内核雏形
操作系统内核Hack:(四)内核雏形 在本系列的前一篇文章<操作系统内核Hack:(三)BootLoader制作>中,我们制作出了一个两阶段引导BootLoader,并进入了一个内核的空壳 ...
- mininet(一)实验环境搭建
mininet(一)实验环境搭建 mininet(二)简单的路由实验 mininet(三)简单的NAT实验 最近学习(https://github.com/mininet/openflow-tutor ...
- 【MySQL】MySQL无基础学习和入门之一:数据库基础概述和实验环境搭建
数据库基础概述 大部分互联网公司都选择MySQL作为业务数据存储数据库,除了MySQL目前还有很多公司使用Oracle(甲骨文).SQLserver(微软).MongoDB等. 从使用成本来区分可以 ...
- Linux下的ssh实验环境搭建与管理
实验环境[size=10.5000pt]1:网桥模式[size=10.5000pt]2:安装好vmtoos[size=10.5000pt]3:安装好yum[size=10.5000pt]4:安装好ss ...
- TensorFlow实验环境搭建
初衷: 由于系统.平台的原因,网上有各种版本的tensorflow安装教程,基于linux的.mac的.windows的,各有不同,tensorflow的官网也给出了具体的安装命令.但实际上,即使te ...
- hadoop_spark伪分布式实验环境搭建和运行实例详细教程
hadoop+spark伪分布式环境搭建 安装须知 单机模式(standalone): 该模式是Hadoop的默认模式.这种模式在一台单机上运行,没有分布式文件系统,而是直接读写本地操作系统的文件系统 ...
随机推荐
- Spring-cloud(五) 使用Ribbon进行Restful请求
写在前面 本文由markdown格式写成,为本人第一次这么写,排版可能会有点乱,还望各位海涵. 主要写的是使用Ribbon进行Restful请求,测试各个方法的使用,代码冗余较高,比较适合初学者,介意 ...
- [LeetCode] Delete and Earn 删除与赚取
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- python内置方法
1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...
- python 简单实现淘宝关键字商品爬取
本文有2个文件 1:taobao_re_xpath 2:taobao_re_xpath_setting # 1:taobao_re_xpath # -*- coding:utf-8 -*- # aut ...
- Python super使用
一 基础使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: #!/usr ...
- Docker入门之--定制镜像
1. 首先定制一个Web 服务器为例 1.1 启动镜像 执行下面命令 docker run --name webserver -d -p 80:80 nginx 1.2 查看容器和镜像状态 然后执行下 ...
- [SCOI 2010]字符串
Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...
- POJ1743 Musical Theme(二分+后缀数组)
题目大概是给n个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 这题是传说中楼教主男人八题之一,虽然已经是用后缀数组解决不可重叠最 ...
- [USACO Jan09] 安全路径
Gremlins最近在农场上泛滥,它们经常会阻止牛们从农庄(牛棚_1)走到别的牛棚(牛_i的目的 地是牛棚_i).每一个gremlin只认识牛_i并且知道牛_i一般走到牛棚_i的最短路经.所以它 们在 ...
- 凸包(BZOJ1069)
顶点一定在凸包上,我们枚举对角线,观察到固定一个点后,随着另一个点的增加,剩下两个点的最优位置一定是单调的,于是就得到了一个优秀的O(n^2)做法. #include <cstdio> # ...