aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/arch/x86_64.md
diff options
context:
space:
mode:
Diffstat (limited to 'src/arch/x86_64.md')
-rw-r--r--src/arch/x86_64.md64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/arch/x86_64.md b/src/arch/x86_64.md
index 1f7ffdd..8bcc308 100644
--- a/src/arch/x86_64.md
+++ b/src/arch/x86_64.md
@@ -312,7 +312,7 @@ must must save these registers in case they are used.
- `r12` - `r15`
- `xmm6` - `xmm15`
-## ASM skeleton
+## ASM skeleton - linux userspace
Small assembler skeleton, ready to use with following properties:
- use raw Linux syscalls (`man 2 syscall` for ABI)
- no `C runtime (crt)`
@@ -350,6 +350,67 @@ To compile and run:
Hi ASM-World!
```
+## MBR boot sectors example
+The following shows a non-minimal [MBR][mbr] boot sector, which transitions from
+16-bit _real mode_ to 32-bit _protected mode_ by setting up a small _global
+descriptor table (GDT)_. A string is printed in each mode.
+
+```x86asm
+{{ #include x86/mbr/mbr.S }}
+```
+
+The linker script.
+```x86asm
+{{ #include x86/mbr/mbr.ld }}
+```
+
+The build instructions.
+```make
+{{ #include x86/mbr/Makefile:1:6 }}
+```
+
+One can boot into the bootsector from legacy BIOS, either with qemu or by
+writing the mbr boot sector as first sector onto a usb stick.
+
+```sh
+qemu-system-i386 -hda mbr
+```
+
+The following gives some more detailed description for the _segment selector_
+registers, the _segment descriptors_ in the GDT, and the _GDT descriptor_
+itself.
+```
+# Segment Selector (cs, ds, es, ss, fs, gs).
+
+[15:3] I Descriptor Index
+ [2:1] TI Table Indicator (0=GTD | 1=LDT)
+ [0] RPL Requested Privilege Level
+
+
+# Segment Descriptor (2 x 4 byte words).
+
+0x4 [31:24] Base[31:24]
+0x4 [23] G Granularity, scaling of limit (0=1B | 1=4K)
+0x4 [22] D/B (0=16bit | 1=32bit)
+0x4 [21] L (0=compatibility mode | 1=64bit code) if 1 -> D/B = 0
+0x4 [20] AVL Free use for system sw
+0x4 [19:16] Limit[19:16]
+0x4 [15] P Present
+0x4 [14:13] DPL Descriptor privilege level
+0x4 [12] S (0=system segment | 1=code/data)
+0x4 [11:0] Type Code or data and access information.
+0x4 [7:0] Base[23:16]
+
+0x0 [31:16] Base[15:0]
+0x0 [15:0] Limit[15:0]
+
+
+# GDT descriptor (32bit mode)
+
+[47:16] Base address of GDT table.
+ [15:0] Length of GDT table.
+```
+
## References
- [SystemV AMD64 ABI][sysvabi]
- [AMD64 Vol1: Application Programming][amd64_vol1]
@@ -379,3 +440,4 @@ Hi ASM-World!
[gas_directives]: https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops
[gas_x86_64]: https://sourceware.org/binutils/docs/as/i386_002dDependent.html
[juicebox]: https://github.com/johannst/juicebox-asm
+[mbr]: https://en.wikipedia.org/wiki/Master_boot_record