create account

从头写一个操作系统 07 (create an OS from scratch 07) by geyu

View this thread on: hive.blogpeakd.comecency.com
· @geyu · (edited)
$0.38
从头写一个操作系统 07 (create an OS from scratch 07)
[lesson 9](https://github.com/cfenollosa/os-tutorial/tree/master/09-32bit-gdt)

*你需要自己去查: GDT*

*本节目标: 编写 GDT*


还记得lesson 6的segmentation吗?段地址左移一位,只有16位的寻址总线却达到了20位的寻址能力。


在32位模式下,段地址的使用方法有了变化。现在,偏移量是GDT中段描述符的索引。段描述符定义了基础地址(32位)、地址范围(20位)和其他一些标志位(只读,权限等)。更让人容易迷惑的是GDT的数据结构有点奇怪,打开os-dev.pdf看第34页,或者GDT的维基。


编写GDT最简单的方式是定义两个段,一个是代码段,另一是数据段。让它们重叠在一起放弃对内存的保护,今后我们会用c语言来修复这个缺陷。


第一个GDT入口必须是`0x00`,确保程序员管理内存是没有出错



然后,CPU不能直接加载GDT地址,它需要先取得一个GDT的大小(16bit)、地址(32bit)的数据结构(GDT descriptor)。用`lgdt`操作符加载GDT descriptor即可。



让我们直接跳转到GDT的汇编代码。这一节的理论比较复杂,最后仔细看看os-dev上的内容。
```
gdt_start: ; 不要更改这个标签,后面计算gdt大小会用到
    ; GDT以8个字节的空字节开始
    dd 0x0 ; 4 byte
    dd 0x0 ; 4 byte

; code段的GDT. base = 0x00000000, length = 0xfffff
;  os-dev.pdf 的36页有flags的详细介绍
gdt_code: 
    dw 0xffff    ; segment length, bits 0-15
    dw 0x0       ; segment base, bits 0-15
    db 0x0       ; segment base, bits 16-23
    db 10011010b ; flags (8 bits)
    db 11001111b ; flags (4 bits) + segment length, bits 16-19
    db 0x0       ; segment base, bits 24-31

; GDT for data segment. base and length identical to code segment 翻译同上
; some flags changed, again, refer to os-dev.pdf
gdt_data:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10010010b
    db 11001111b
    db 0x0

gdt_end:

; GDT descriptor
gdt_descriptor:
    ;size的取值范围为1到65536,但内存地址0xFFFF == 65535 ,所以1-65536  ->   0 - 65535 这样作为对应,所以减去1。
    dw gdt_end - gdt_start - 1 ; size (16 bit), always one less of its true size
    
    dd gdt_start ; address (32 bit)

; define some constants for later use
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
```

下一节课,我们会完成32位保护模式的跳转,并且测试今天的代码。


[THE ORIGIN ARTICALE IN GITHUB:](https://github.com/cfenollosa/os-tutorial/tree/master/09-32bit-gdt)
-----------------------------
*Concepts you may want to Google beforehand: GDT*

**Goal: program the GDT**

Remember segmentation from lesson 6? The offset was left shifted
to address an extra level of indirection.

In 32-bit mode, segmentation works differently. Now, the offset becomes an
index to a segment descriptor (SD) in the GDT. This descriptor defines
the base address (32 bits), the size (20 bits) and some flags, like
readonly, permissions, etc. To add confusion, the data structures are split,
so open the os-dev.pdf file and check out the figure on page 34 or the 
Wikipedia page for the GDT.

The easiest way to program the GDT is to define two segments, one for code
and another for data. These can overlap which means there is no memory protection,
but it's good enough to boot, we'll fix this later with a higher language.

As a curiosity, the first GDT entry must be `0x00` to make sure that the
programmer didn't make any mistakes managing addresses.

Furthermore, the CPU can't directly load the GDT address, but it requires
a meta structure called the "GDT descriptor" with the size (16b) and address
(32b) of our actual GDT. It is loaded with the `lgdt` operation.

Let's directly jump to the GDT code in assembly. Again, to understand
all the segment flags, refer to the os-dev.pdf document. The theory for
this lesson is quite complex.

In the next lesson we will make the switch to 32-bit protected mode
and test our code from these lessons.
👍  , , , , , , , , , , , , , , , ,
properties (23)
authorgeyu
permlink07-create-an-os-from-scratch-07-2598af2a206baest
categorycode
json_metadata{"links":["https://github.com/cfenollosa/os-tutorial/tree/master/09-32bit-gdt","https://github.com/cfenollosa/os-tutorial/tree/master/09-32bit-gdt"],"tags":["code","os","esteem-cn","esteem","cn"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"}
created2019-04-12 10:28:45
last_update2019-04-13 12:12:21
depth0
children16
last_payout2019-04-19 10:28:45
cashout_time1969-12-31 23:59:59
total_payout_value0.278 HBD
curator_payout_value0.098 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,204
author_reputation1,811,446,112,774
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,946,450
net_rshares668,022,289,091
author_curate_reward""
vote details (17)
@aellly ·
拉哥。为什么不撸stem

Posted using [Partiko Android](https://partiko.app/referral/aellly)
👍  
properties (23)
authoraellly
permlinkaellly-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t033430882z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-13 03:34:27
last_update2019-04-13 03:34:27
depth1
children7
last_payout2019-04-20 03:34:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length81
author_reputation869,204,613,425,845
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,997,362
net_rshares978,266,869
author_curate_reward""
vote details (1)
@geyu ·
这个不是在跟我说的吧?
properties (22)
authorgeyu
permlinkre-aellly-2019413t114432568z
categorycode
json_metadata{"tags":["esteem"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"}
created2019-04-13 03:44:36
last_update2019-04-13 03:44:36
depth2
children6
last_payout2019-04-20 03:44:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11
author_reputation1,811,446,112,774
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,997,806
net_rshares0
@aellly ·
你不是拉仔吗?

Posted using [Partiko Android](https://partiko.app/referral/aellly)
👍  
properties (23)
authoraellly
permlinkaellly-re-geyu-re-aellly-2019413t114432568z-20190413t034839384z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-13 03:48:39
last_update2019-04-13 03:48:39
depth3
children1
last_payout2019-04-20 03:48:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length76
author_reputation869,204,613,425,845
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,997,968
net_rshares964,171,963
author_curate_reward""
vote details (1)
@aellly ·
把标签改两个,换成steemstem 和cn-stem

Posted using [Partiko Android](https://partiko.app/referral/aellly)
properties (22)
authoraellly
permlinkaellly-re-geyu-re-aellly-2019413t114432568z-20190413t035005173z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-13 03:50:06
last_update2019-04-13 03:50:06
depth3
children3
last_payout2019-04-20 03:50:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length96
author_reputation869,204,613,425,845
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,998,003
net_rshares0
@davidke20 ·
今天忙些,出了乱子搞到现在才忙完。支持了。

Posted using [Partiko Android](https://partiko.app/referral/davidke20)
👍  
properties (23)
authordavidke20
permlinkdavidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190412t164510544z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-12 16:45:12
last_update2019-04-12 16:45:12
depth1
children6
last_payout2019-04-19 16:45:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length93
author_reputation952,374,392,858,364
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,968,869
net_rshares1,079,034,292
author_curate_reward""
vote details (1)
@aellly ·
拉哥,新人应该引导他。这么好的文章,埋没了

Posted using [Partiko Android](https://partiko.app/referral/aellly)
properties (22)
authoraellly
permlinkaellly-re-davidke20-davidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t035218685z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-13 03:52:15
last_update2019-04-13 03:52:15
depth2
children4
last_payout2019-04-20 03:52:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length90
author_reputation869,204,613,425,845
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,998,041
net_rshares0
@davidke20 ·
这新人的注册日期比我早

Posted using [Partiko Android](https://partiko.app/referral/davidke20)
properties (22)
authordavidke20
permlinkdavidke20-re-aellly-aellly-re-davidke20-davidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t043220762z
categorycode
json_metadata{"app":"partiko","client":"android"}
created2019-04-13 04:32:21
last_update2019-04-13 04:32:21
depth3
children3
last_payout2019-04-20 04:32:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length83
author_reputation952,374,392,858,364
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,999,086
net_rshares0
@geyu ·
感谢感谢!能来看看就是我的荣幸,我现在心里全是感激!
properties (22)
authorgeyu
permlinkre-davidke20-2019413t105543876z
categorycode
json_metadata{"tags":["esteem"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"}
created2019-04-13 02:55:45
last_update2019-04-13 02:55:45
depth2
children0
last_payout2019-04-20 02:55:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length26
author_reputation1,811,446,112,774
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,996,048
net_rshares0
@esteemapp ·
Thanks for using **eSteem**! <br>Your post has been voted as a part of [eSteem encouragement program](https://steemit.com/esteem/@good-karma/encouragement-program-continues-82eafcd10a299). Keep up the good work! Install [Android](https://play.google.com/store/apps/details?id=app.esteem.mobile), [iOS](https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1451896376&mt=8) Mobile app or [Windows, Mac, Linux](https://github.com/esteemapp/esteem-surfer/releases) Surfer app, if you haven't already!<br>Learn more: https://esteem.app <br>Join our discord: https://discord.gg/8eHupPq
properties (22)
authoresteemapp
permlinkre-2019412t14151869z
categorycode
json_metadata{"tags":["esteem"],"app":"esteem/2.0-welcome","format":"markdown+html","community":"esteem.app"}
created2019-04-12 12:15:18
last_update2019-04-12 12:15:18
depth1
children0
last_payout2019-04-19 12:15:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length593
author_reputation420,443,679,514,793
root_title"从头写一个操作系统 07 (create an OS from scratch 07)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,951,878
net_rshares0