Master the fundamental concepts of build a mini kernel through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksThe GDT holds segment descriptors consumed by the CPU in protected mode. Even with paging, segment registers must reference valid entries. Null descriptor, code, and data entries are the minimum for a flat kernel.
Each 8-byte entry packs:
For example, selector 0x08 (index 1, RPL 0) might be a 4 GB readable code segment for kernel text starting at linear 0.
The flat-model GDT you're encoding here , ring 0 kernel segments, ring 3 user segments, all spanning 4GB , is exactly what every production x86 kernel sets up before touching paging, even though modern OSes rely on page tables rather than segmentation for real protection. Get an access byte wrong (0x9A vs 0x92, code vs data) and user code silently runs with kernel privileges instead of faulting.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will build GDT entries, load with lgdt, and reload segments. The task asks you to dump selector values in a small C routine compiled for protected mode.
Build GDT descriptors bit by bit. An x86 segment descriptor packs a 32-bit base, a 20-bit limit, an access byte and 4 flag bits into 8 bytes, scattered in a historical layout. Write the encoder that kernels use to fill their GDT, a decoder that explains any descriptor, and the 6-byte operand that lgdt loads.
| Bits | Field |
|---|---|
| 0-15 | limit 0-15 |
| 16-39 | base 0-23 |
| 40-47 | access byte: P (7), DPL (6-5), S (4), type (3-0) |
| 48-51 | limit 16-19 |
| 52-55 | flags: G (3), D/B (2), L (1), AVL (0) |
| 56-63 | base 24-31 |
Access type bits for S=1: bit 3 executable (code); for code, bit 2 conforming and bit 1 readable; for data, bit 2 expand-down and bit 1 writable; bit 0 accessed.
| Command | Meaning |
|---|---|
entry BASE LIMIT ACCESS FLAGS | encode a descriptor (all hex) as the next GDT entry |
named NAME | the next entry is a standard flat descriptor: null, kcode (access 9A), kdata (92), ucode (FA), udata (F2) (all base 0, limit FFFFF, flags C) or kcode64 (9A, flags A) |
decode VALUE | explain a 64-bit descriptor (hex); doesn't add an entry |
gdtr BASE | print the GDTR for the entries so far |
cLoading…
entry N (numbered from 0 in definition order), entry N (NAME) for named, or decode. The bytes are listed in memory order.(limit << 12 | 0xFFF) + 1 if G is set (4 KiB units), else limit + 1 (bytes).present/not present, ring DPL, then either system segment type 0xT (NAME) (S=0; names: 2 LDT, 9 32-bit TSS (available), B 32-bit TSS (busy), C call gate, E interrupt gate, F trap gate, otherwise reserved), code, readable|execute-only, conforming|non-conforming or data, writable|read-only, expands down|expands up, then, for code and data only, , accessed if bit 0 is set.64-bit code (L) if L is set, else 32-bit (D/B) if D/B is set, else 16-bit.named non-null entries: index << 3 | DPL as the RPL. null descriptor under its label.gdtr: base 0x00001000 limit 0x0027 (5 entries), lgdt operand bytes 27 00 00 10 00 00: limit = entries × 8 − 1 (2 bytes LE), then the base (4 bytes LE).entry N: error: limit does not fit in 20 bits (use the G flag) or entry N: error: field out of range. The entry number is not consumed.Input:
cLoading…
Output:
cLoading…
encode(base, limit, access, flags) returning a uint64_t built with shifts and masks, and one decoder that is its exact inverse.Hidden tests cover user-mode segments (ring 3 selectors), a byte-granular VGA-buffer segment, a 64-bit code descriptor, a TSS system descriptor and data flags decoded from raw values, and both errors.