[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.
author | geyu | ||||||
---|---|---|---|---|---|---|---|
permlink | 07-create-an-os-from-scratch-07-2598af2a206baest | ||||||
category | code | ||||||
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"} | ||||||
created | 2019-04-12 10:28:45 | ||||||
last_update | 2019-04-13 12:12:21 | ||||||
depth | 0 | ||||||
children | 16 | ||||||
last_payout | 2019-04-19 10:28:45 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.278 HBD | ||||||
curator_payout_value | 0.098 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 3,204 | ||||||
author_reputation | 1,811,446,112,774 | ||||||
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 82,946,450 | ||||||
net_rshares | 668,022,289,091 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tombstone | 0 | 7,296,641,761 | 0.03% | ||
kevinwong | 0 | 11,711,251,205 | 0.15% | ||
good-karma | 0 | 44,798,320,637 | 3.69% | ||
stomatolog2 | 0 | 39,566,200 | 100% | ||
mysteem | 0 | 64,294,257 | 3.69% | ||
demo | 0 | 100,799,428 | 3.69% | ||
feruz | 0 | 265,761,760 | 3.69% | ||
bukiland | 0 | 340,495,951 | 1% | ||
esteemapp | 0 | 577,513,819,254 | 3.69% | ||
monajam | 0 | 4,217,826,050 | 1% | ||
geyu | 0 | 1,414,346,606 | 100% | ||
davidke20 | 0 | 2,043,715,220 | 2.5% | ||
esteem.app | 0 | 605,039,426 | 3.69% | ||
aellly | 0 | 16,588,604,918 | 50% | ||
mini-zephalexia | 0 | 95,009,038 | 2.95% | ||
steeming-hot | 0 | 0 | 0.04% | ||
hamsa.quality | 0 | 926,797,380 | 1.1% |
拉哥。为什么不撸stem Posted using [Partiko Android](https://partiko.app/referral/aellly)
author | aellly |
---|---|
permlink | aellly-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t033430882z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-13 03:34:27 |
last_update | 2019-04-13 03:34:27 |
depth | 1 |
children | 7 |
last_payout | 2019-04-20 03:34:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 81 |
author_reputation | 869,204,613,425,845 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,997,362 |
net_rshares | 978,266,869 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
geyu | 0 | 978,266,869 | 100% |
这个不是在跟我说的吧?
author | geyu | ||||||
---|---|---|---|---|---|---|---|
permlink | re-aellly-2019413t114432568z | ||||||
category | code | ||||||
json_metadata | {"tags":["esteem"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"} | ||||||
created | 2019-04-13 03:44:36 | ||||||
last_update | 2019-04-13 03:44:36 | ||||||
depth | 2 | ||||||
children | 6 | ||||||
last_payout | 2019-04-20 03:44:36 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 11 | ||||||
author_reputation | 1,811,446,112,774 | ||||||
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 82,997,806 | ||||||
net_rshares | 0 |
你不是拉仔吗? Posted using [Partiko Android](https://partiko.app/referral/aellly)
author | aellly |
---|---|
permlink | aellly-re-geyu-re-aellly-2019413t114432568z-20190413t034839384z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-13 03:48:39 |
last_update | 2019-04-13 03:48:39 |
depth | 3 |
children | 1 |
last_payout | 2019-04-20 03:48:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 76 |
author_reputation | 869,204,613,425,845 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,997,968 |
net_rshares | 964,171,963 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
geyu | 0 | 964,171,963 | 100% |
把标签改两个,换成steemstem 和cn-stem Posted using [Partiko Android](https://partiko.app/referral/aellly)
author | aellly |
---|---|
permlink | aellly-re-geyu-re-aellly-2019413t114432568z-20190413t035005173z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-13 03:50:06 |
last_update | 2019-04-13 03:50:06 |
depth | 3 |
children | 3 |
last_payout | 2019-04-20 03:50:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 96 |
author_reputation | 869,204,613,425,845 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,998,003 |
net_rshares | 0 |
今天忙些,出了乱子搞到现在才忙完。支持了。 Posted using [Partiko Android](https://partiko.app/referral/davidke20)
author | davidke20 |
---|---|
permlink | davidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190412t164510544z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-12 16:45:12 |
last_update | 2019-04-12 16:45:12 |
depth | 1 |
children | 6 |
last_payout | 2019-04-19 16:45:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 93 |
author_reputation | 952,374,392,858,364 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,968,869 |
net_rshares | 1,079,034,292 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
geyu | 0 | 1,079,034,292 | 100% |
拉哥,新人应该引导他。这么好的文章,埋没了 Posted using [Partiko Android](https://partiko.app/referral/aellly)
author | aellly |
---|---|
permlink | aellly-re-davidke20-davidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t035218685z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-13 03:52:15 |
last_update | 2019-04-13 03:52:15 |
depth | 2 |
children | 4 |
last_payout | 2019-04-20 03:52:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 90 |
author_reputation | 869,204,613,425,845 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,998,041 |
net_rshares | 0 |
这新人的注册日期比我早 Posted using [Partiko Android](https://partiko.app/referral/davidke20)
author | davidke20 |
---|---|
permlink | davidke20-re-aellly-aellly-re-davidke20-davidke20-re-geyu-07-create-an-os-from-scratch-07-2598af2a206baest-20190413t043220762z |
category | code |
json_metadata | {"app":"partiko","client":"android"} |
created | 2019-04-13 04:32:21 |
last_update | 2019-04-13 04:32:21 |
depth | 3 |
children | 3 |
last_payout | 2019-04-20 04:32:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 83 |
author_reputation | 952,374,392,858,364 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,999,086 |
net_rshares | 0 |
感谢感谢!能来看看就是我的荣幸,我现在心里全是感激!
author | geyu | ||||||
---|---|---|---|---|---|---|---|
permlink | re-davidke20-2019413t105543876z | ||||||
category | code | ||||||
json_metadata | {"tags":["esteem"],"app":"esteem/2.0.7-surfer","format":"markdown+html","community":"esteem.app"} | ||||||
created | 2019-04-13 02:55:45 | ||||||
last_update | 2019-04-13 02:55:45 | ||||||
depth | 2 | ||||||
children | 0 | ||||||
last_payout | 2019-04-20 02:55:45 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 26 | ||||||
author_reputation | 1,811,446,112,774 | ||||||
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 82,996,048 | ||||||
net_rshares | 0 |
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
author | esteemapp |
---|---|
permlink | re-2019412t14151869z |
category | code |
json_metadata | {"tags":["esteem"],"app":"esteem/2.0-welcome","format":"markdown+html","community":"esteem.app"} |
created | 2019-04-12 12:15:18 |
last_update | 2019-04-12 12:15:18 |
depth | 1 |
children | 0 |
last_payout | 2019-04-19 12:15:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 593 |
author_reputation | 420,443,679,514,793 |
root_title | "从头写一个操作系统 07 (create an OS from scratch 07)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 82,951,878 |
net_rshares | 0 |