关于《30天编写自己的操作系统》ipl 与linux0.01的关系

来源:互联网 发布:bootcamp助理下载 mac 编辑:程序博客网 时间:2024/04/27 23:39

最近一直在忙着考试所以很少写博客了

今天想写一点关于前面一段时间再看的《30天编写自己的操作系统》这本书,这本书目前看书看到了17天,大概340多页,自己按照作者代码的思路然后加上一点自己的思路也算是写了自己的一个版本的os,目前整个进度是到了这本书的16天的进度,其中写了很多关于自己的东西,因为我发觉其实作者在这本书中屏蔽了很多操作系统的知识,虽然还是可以学到很多知识,这个的话可能也是作者为了让本书成为操作系统编写的入门书籍吧。在跟着书籍的进度来编写os的同时,我自己也在看linux0.01的内核代码,发现了部分的相似之处,也有很多的不同,所以建议在看这本书的程序猿们可以同时看一看linux0.01的内核代码,代码不是特别的复杂,还是能看懂的。


今天和大家讨论一下其中的ipl.nas 和linux0.01 中的boot.s

boot.s
|
| boot.s is loaded at 0x7c00 by the bios-startup routines, and moves itself
| out of the way to address 0x90000, and jumps there.(boot.s将被BIOS启动例程自动加载到内存的0x07c00处,然后将自己移动到内存地址0x90000处,然后跳转该处的go标签继续运行。PS:看下面的jmpigo,INITSEG代码)
|
| It then loads the system at 0x10000, using BIOS interrupts. Thereafter
| it disables all interrupts, moves the system down to 0x0000, changes
| to protected mode, and calls the start of system. System then must
| RE-initialize the protected mode in it's own tables, and enable
| interrupts as needed.

{这个时候利用bios的中断,加载系统到0x10000,紧接着屏蔽所有的中断,移动系统到0x0000,然后切换到保护模式,然后调用系统的启动(main)函数,系统必须在自己的描述符表中重新初始化保护模式,需要的时候允许中断}
|
| NOTE! currently system is at most 8*65536 bytes long. This should be no
| problem, even in the future. I want to keep it simple. This 512 kB
| kernel size should be enough - in fact more would mean we'd have to move
| not just these start-up routines, but also do something about the cache-
| memory (block IO devices). The area left over in the lower 640 kB is meant
| for these. No other memory is assumed to be "physical", ie all memory
| over 1Mb is demand-paging. All addresses under 1Mb are guaranteed to match
| their physical addresses.

{注意:当前系统的大小最大是8*65536 字节,即使是在将来,这个应该没有什么问题。我希望能使它保持简单。512KB字节的内核应该足够了——实际上更大的内核意味着内核不仅仅只有这些启动例程,同时也包含了块设备的缓存相关的代码。余下的640KB可以包含像块设备缓存等类似的这些代码。其他的内存都没有被假设是物理意义上的(也就是说可以直接访问的),所有超过1M的内存都必须要映射。所有1M以下的内存地址都与物理地址一一对应。}
|
| NOTE1 abouve is no longer valid in it's entirety. cache-memory is allocated
| above the 1Mb mark as well as below. Otherwise it is mainly correct.
|
| NOTE 2! The boot disk type must be set at compile-time, by setting
| the following equ. Having the boot-up procedure hunt for the right
| disk type is severe brain-damage.
| The loader has been made as simple as possible (had to, to get it
| in 512 bytes with the code to move to protected mode), and continuos
| read errors will result in a unbreakable loop. Reboot by hand. It
| loads pretty fast by getting whole sectors at a time whenever possible.


| 1.44Mb disks:
sectors = 18
| 1.2Mb disks:
| sectors = 15
| 720kB disks:
| sectors = 9


.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text


BOOTSEG = 0x07c0 | BIOS启动例程加载boot.s所在的内存地址
INITSEG = 0x9000 | 移动后的boot.s的内存地址
SYSSEG  = 0x1000 | system loaded at 0x10000 (65536).系统被加载到0x10000(64KB处)
ENDSEG = SYSSEG + SYSSIZE | 系统在内存的末地址


entry start    |移动boot.s到0x90000处
start:
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,#256|循环256次
sub si,si                    |初始化源基地址寄存器
sub di,di|初始化目的基地址寄存器
rep
movw                  |  移动字(=2byte)也就是说256*2=512byte
jmpi go,INITSEG      | 跳转到0x90000:go执行(注意是已经复制到0x9000的程序的go)
go: mov ax,cs |程序移动到0x90000之后从0x90000:go标签开始执行
mov ds,ax
mov es,ax
mov ss,ax
mov sp,#0x400| arbitrary value >>512


mov ah,#0x03| read cursor pos
xor bh,bh
int 0x10

mov cx,#24
mov bx,#0x0007| page 0, attribute 7 (normal)
mov bp,#msg1| 打印msg1对应的信息"Loading system!"
mov ax,#0x1301| write string, move cursor
int 0x10


| ok, we've written the message, now  | 加载系统到0x10000处
| we want to load the system (at 0x10000)

mov ax,#SYSSEG
mov es,ax| segment of 0x010000
call read_it| 调用read_it函数标签,这个函数利用bios中断读取软盘上的系统
call kill_motor| 关闭软盘驱动


| if the read went well we get current cursor position ans save it for
| posterity.


mov ah,#0x03| read cursor pos
xor bh,bh
int 0x10| save it in known place, con_init fetches
mov [510],dx| it from 0x90510.

| now we want to move to protected mode ... 准备进入保护模式


cli | no interrupts allowed !(清除所有的中断)


| first we move the system to it's rightful place 首先将系统移动到正确的位置0x00000处


mov ax,#0x0000
cld | 'direction'=0, movs moves forward
do_move: | 移动系统到0x0000的内存处
mov es,ax| destination segment
add ax,#0x1000
cmp ax,#0x9000
jz end_move
mov ds,ax| source segment
sub di,di
sub si,si
mov cx,#0x8000
rep
movsw
j do_move


| then we load the segment descriptors 加载描述符表


end_move:


mov ax,cs| right, forgot this at first. didn't work :-)
mov ds,ax
lidt idt_48| load idt with 0,0 加载描述符表
lgdt gdt_48| load gdt with whatever appropriate


| that was painless, now we enable A20 运行20位地址访问


call empty_8042
mov al,#0xD1| command write
out #0x64,al
call empty_8042
mov al,#0xDF| A20 on
out #0x60,al
call empty_8042


| well, that went ok, I hope. Now we have to reprogram the interrupts :-(
| we put them right after the intel-reserved hardware interrupts, at
| int 0x20-0x2F(中断程序只能使用0x20-0x2F,前面的都是intel保留的
.

| There they won't mess up anything. Sadly IBM really
| messed this up with the original PC, and they haven't been able to
| rectify it afterwards. Thus the bios puts interrupts at 0x08-0x0f,
| which is used for the internal hardware interrupts as well. We just
| have to reprogram the 8259's, and it isn't fun.


mov al,#0x11| initialization sequence
out #0x20,al| send it to 8259A-1
.word 0x00eb,0x00eb| jmp $+2, jmp $+2
out #0xA0,al| and to 8259A-2
.word 0x00eb,0x00eb
mov al,#0x20| start of hardware int's (0x20)
out #0x21,al
.word 0x00eb,0x00eb
mov al,#0x28| start of hardware int's 2 (0x28)
out #0xA1,al
.word 0x00eb,0x00eb
mov al,#0x04| 8259-1 is master
out #0x21,al
.word 0x00eb,0x00eb
mov al,#0x02| 8259-2 is slave
out #0xA1,al
.word 0x00eb,0x00eb
mov al,#0x01| 8086 mode for both
out #0x21,al
.word 0x00eb,0x00eb
out #0xA1,al
.word 0x00eb,0x00eb
mov al,#0xFF| mask off all interrupts for now
out #0x21,al
.word 0x00eb,0x00eb
out #0xA1,al


| well, that certainly wasn't fun :-(. Hopefully it works, and we don't    进入保护模式之后就不能再继续使用BIOS了
| need no steenking BIOS anyway (except for the initial loading :-).
| The BIOS-routine wants lots of unnecessary data, and it's less
| "interesting" anyway. This is how REAL programmers do it.
|
| Well, now's the time to actually move into protected mode. To make   真正开始进入包含模式
| things as simple as possible, we do no register set-up or anything,
| we let the gnu-compiled 32-bit programs do that. We just jump to
| absolute address 0x00000, in 32-bit protected mode.


mov ax,#0x0001| protected mode (PE) bit
lmsw ax| This is it!

|jmp IP:CS 进入保护模式之后,CS 是选择子,GDT的第一个选择子没有用为空
jmpi 0,8| jmp offset 0 of segment 8 (cs)// 在这里跳转到系统运行


| This routine checks that the keyboard command queue is empty
| No timeout is used - if this hangs there is something wrong with
| the machine, and we probably couldn't proceed anyway.
empty_8042:
.word 0x00eb,0x00eb
in al,#0x64| 8042 status port
test al,#2| is input buffer full?
jnz empty_8042| yes - loop
ret


| This routine loads the system at address 0x10000, making sure
| no 64kB boundaries are crossed. We try to load it as fast as
| possible, loading whole tracks whenever we can.
|
| in: es - starting address segment (normally 0x1000)
|
| This routine has to be recompiled to fit another drive type,
| just change the "sectors" variable at the start of the file
| (originally 18, for a 1.44Mb drive)
|
sread: .word 1| sectors read of current track
head: .word 0 | current head
track: .word 0| current track
read_it:
mov ax,es
test ax,#0x0fff
die: jne die | es must be at 64kB boundary
xor bx,bx | bx is starting address within segment
rp_read:
mov ax,es
cmp ax,#ENDSEG| have we loaded all yet?
jb ok1_read
ret
ok1_read:
mov ax,#sectors
sub ax,sread
mov cx,ax
shl cx,#9
add cx,bx
jnc ok2_read
je ok2_read
xor ax,ax
sub ax,bx
shr ax,#9
ok2_read:
call read_track
mov cx,ax
add ax,sread
cmp ax,#sectors
jne ok3_read
mov ax,#1
sub ax,head
jne ok4_read
inc track
ok4_read:
mov head,ax
xor ax,ax
ok3_read:
mov sread,ax
shl cx,#9
add bx,cx
jnc rp_read
mov ax,es
add ax,#0x1000
mov es,ax
xor bx,bx
jmp rp_read


read_track:
push ax
push bx
push cx
push dx
mov dx,track
mov cx,sread
inc cx
mov ch,dl
mov dx,head
mov dh,dl
mov dl,#0
and dx,#0x0100
mov ah,#2
int 0x13
jc bad_rt
pop dx
pop cx
pop bx
pop ax
ret
bad_rt: mov ax,#0
mov dx,#0
int 0x13
pop dx
pop cx
pop bx
pop ax
jmp read_track


/*
 * This procedure turns off the floppy drive motor, so
 * that we enter the kernel in a known state, and
 * don't have to worry about it later.
 */
kill_motor:
push dx
mov dx,#0x3f2
mov al,#0
outb
pop dx
ret


gdt:
.word 0,0,0,0| dummy


.word 0x07FF| 8Mb - limit=2047 (2048*4096=8Mb)
.word 0x0000| base address=0
.word 0x9A00| code read/exec
.word 0x00C0| granularity=4096, 386


.word 0x07FF| 8Mb - limit=2047 (2048*4096=8Mb)
.word 0x0000| base address=0
.word 0x9200| data read/write
.word 0x00C0| granularity=4096, 386


idt_48:
.word 0| idt limit=0
.word 0,0| idt base=0L


gdt_48:
.word 0x800| gdt limit=2048, 256 GDT entries
.word gdt,0x9| gdt base = 0X9xxxx

msg1:
.byte 13,10
.ascii "Loading system ..."
.byte 13,10,13,10


.text
endtext:
.data
enddata:
.bss
endbss:


---------------------------------------------------------------

; haribote-ipl
; TAB=4
CYLS EQU 10 ; 十个柱面
ORG 0x7c00; MBR在内存中的位置为(DS=0,0x07c00=DS+0x7c00)


; 系统相关设置信息
JMP entry
DB 0x90
DB "HELLOIPL"; name of boot sector
DW 512; size of sector
DB 1; f
DW 1; FAT first sector
DB 2; FAT number
DW 224; root dir size
DW 2880; disk size in sector
DB 0xf0; disk type
DW 9; FAT length
DW 18; 18 sector each road
DW 2; disk up down 
DD 0; no partition
DD 2880; reclaim the disk size
DB 0,0,0x29; reserve
DD 0xffffffff; might be column 
DB "HELLO-OS   "; disk name
DB "FAT12   "; disk fs type
RESB 18; reserve 18 byte


; init程序入口


entry:
MOV AX,0
MOV SS,AX
MOV SP,0x7c00; heap stack init
MOV DS,AX; 初始化代码段的基地址在0x00000


; 读取磁盘到内存0x08200的位置


MOV AX,0x0820
MOV ES,AX
MOV CH,0; cylinder number
MOV DH,0; disk head number
MOV CL,2; sector number
readloop:
MOV SI,0; failed times
; retry to read disk if failed, read one sector
retry:
MOV AH,0x02; AH=0x02 : 读磁盘
MOV AL,1; 1扇区
MOV BX,0
MOV DL,0x00; A驱动器
;MOV DL,0x80
INT 0x13; 调用BIOS的函数例程
JNC next
ADD SI,1
CMP SI,5
JAE error
MOV AH,0x00
MOV DL,0x00
INT 0x13
JMP retry
next:
MOV AX,ES
ADD AX,0x0020 ;每读取一个扇区内存向后偏移(ES+=0x0020)<<4
MOV ES,AX
ADD CL,1
CMP CL,18;18个扇区
JBE readloop
MOV CL,1
ADD DH,1
CMP DH,2; 两个磁头
JB readloop
MOV DH,0
ADD CH,1
CMP CH,CYLS; 10个柱面
JB readloop
MOV [0x0ff0],CH
JMP 0xc200
error:
MOV SI,msg
putloop:
MOV AL,[SI]
ADD SI,1; SI设置为1
CMP AL,0
JE fin
MOV AH,0x0e; 在teletype模式下显示字符
MOV BX,15; 页码BH=0x00,前景色BL=0x0f
INT 0x10; BIOS显示服务
JMP putloop
fin:
HLT ; 暂停处理器
JMP fin
msg1:
DB 0x0a, 0x0a; 换行符
DB "READ OKKKK"
DB 0x0a; 换行符
DB 0
msg:
DB 0x0a, 0x0a; 换行符
DB "load error"
DB 0x0a; 换行符
DB 0


RESB 0x7dfe-$; (0x7dfe-当前位置)都填充0x00


DB 0x55, 0xaa

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机升级后支付宝打不开怎么办 qqq币充了没进帐怎么办 怎么办q币换成qq余额 支付宝qb冲多了怎么办 微信没钱怎么办怎么赚 忘记微信支付密码怎么办 手机设备注册达上限怎么办 在新手机上登微信需要验证怎么办 手机号被限制注册qq号怎么办 手机互换了微信怎么办 微信申诉只有一位好友怎么办 微信申诉没有好友怎么办 四川电信多余的话费怎么办 四川电信话费多了怎么办 固话冲q币要密码怎么办 手机卡怎么突然没了怎么办 联通话费冲错了怎么办 王卡高额半停机 怎么办 计算机报考在手机上网上支付怎么办 建行app充话费不到账怎么办 币乎账号被骗了怎么办? q币充了想返还怎么办 q币账号充值错了怎么办 淘宝乐充话费没到账怎么办 微信信用卡还款未到账怎么办 登不上qq怎么改qq密码怎么办 qq改不了以前的密码怎么办 qq微信密码都被改了怎么办 qq账号被盗一直改密码怎么办? 2018qq密码忘了怎么办 我qq密码忘记了怎么办 微信怎么办该改密码 微信改密码收不到验证码怎么办 微信不能改密码怎么办 qq钱包被限额了怎么办? 注册战网手机号被使用怎么办 电信充值卡密码刮花了怎么办 油卡充值卡密码刮花了怎么办 电费充值卡密码刮花了怎么办 手机充值卡密码刮坏了怎么办 办中石化油卡怎么办