From 833b6afea6d3710bd5679171fd97cd72d7d6bbeb Mon Sep 17 00:00:00 2001 From: kees Date: Tue, 3 Oct 2023 02:05:26 +0700 Subject: [PATCH 01/88] init first boot.img new file: .bochsrc new file: boot.asm new file: boot.bin new file: boot.img --- .bochsrc | 8 ++++++++ boot.asm | 9 +++++++++ boot.bin | Bin 0 -> 512 bytes boot.img | Bin 0 -> 1474560 bytes 4 files changed, 17 insertions(+) create mode 100644 .bochsrc create mode 100644 boot.asm create mode 100644 boot.bin create mode 100644 boot.img diff --git a/.bochsrc b/.bochsrc new file mode 100644 index 0000000..a98d60c --- /dev/null +++ b/.bochsrc @@ -0,0 +1,8 @@ +plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true +config_interface: textconfig +display_library: x +memory: host=32, guest=1024 +boot: floppy +floppy_bootsig_check: disabled=0 +floppya: type=1_44, 1_44="boot.img", status=inserted, write_protected=0 + diff --git a/boot.asm b/boot.asm new file mode 100644 index 0000000..c3c14bf --- /dev/null +++ b/boot.asm @@ -0,0 +1,9 @@ +[BITS 16] + +loop: + jmp loop + +times 510-($-$$) db 0 + +dw 0xAA55 + diff --git a/boot.bin b/boot.bin new file mode 100644 index 0000000000000000000000000000000000000000..980ee558a5c4d1d8da7286686b6e92025d0869e8 GIT binary patch literal 512 RcmaF;k6{#y*boR^1pw580_gw% literal 0 HcmV?d00001 diff --git a/boot.img b/boot.img new file mode 100644 index 0000000000000000000000000000000000000000..1b9c5d6ce8287235c5417117e7009099ea29b312 GIT binary patch literal 1474560 zcmeIuK@k7|1OP!Ll~hd!qlJ&X2Dd$-w*pyj*Nqb(K!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U NAV7cs0RjY$1QuAD0_gw% literal 0 HcmV?d00001 From beece61fada75af4d81ae5faf7ae29c4deb36b57 Mon Sep 17 00:00:00 2001 From: kees Date: Tue, 3 Oct 2023 14:36:37 +0700 Subject: [PATCH 02/88] edit and ungitignore .gitignore, delete .bin and .img files new file: .gitignore deleted: boot.bin deleted: boot.img --- .gitignore | 7 +++++++ boot.bin | Bin 512 -> 0 bytes boot.img | Bin 1474560 -> 0 bytes 3 files changed, 7 insertions(+) create mode 100644 .gitignore delete mode 100644 boot.bin delete mode 100644 boot.img diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74be2f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +* +!*.* +!*/ + +*/*.bin +*/*.img + diff --git a/boot.bin b/boot.bin deleted file mode 100644 index 980ee558a5c4d1d8da7286686b6e92025d0869e8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 512 RcmaF;k6{#y*boR^1pw580_gw% diff --git a/boot.img b/boot.img deleted file mode 100644 index 1b9c5d6ce8287235c5417117e7009099ea29b312..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1474560 zcmeIuK@k7|1OP!Ll~hd!qlJ&X2Dd$-w*pyj*Nqb(K!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U NAV7cs0RjY$1QuAD0_gw% From d66a70f605c3fc423966ae3c9530b3d941852c8d Mon Sep 17 00:00:00 2001 From: kees Date: Tue, 3 Oct 2023 15:14:35 +0700 Subject: [PATCH 03/88] edit .gitignore modified: .bochsrc modified: .gitignore --- .bochsrc | 1 - .gitignore | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.bochsrc b/.bochsrc index a98d60c..f341ab3 100644 --- a/.bochsrc +++ b/.bochsrc @@ -5,4 +5,3 @@ memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="boot.img", status=inserted, write_protected=0 - diff --git a/.gitignore b/.gitignore index 74be2f4..4a2bf96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ !*.* !*/ -*/*.bin -*/*.img +*.bin +*.img From 6e9b6e4a4ff92e054d6e2592bf7b80939c16a825 Mon Sep 17 00:00:00 2001 From: kees Date: Wed, 4 Oct 2023 02:59:58 +0700 Subject: [PATCH 04/88] create build bash-scripts --- .gitignore | 4 +++- boot.asm | 2 +- build_boot | 5 +++++ write_disk | 5 +++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100755 build_boot create mode 100755 write_disk diff --git a/.gitignore b/.gitignore index 4a2bf96..b57d1bc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ *.bin *.img - +!build_boot +!write_disk +test.* diff --git a/boot.asm b/boot.asm index c3c14bf..46079cf 100644 --- a/boot.asm +++ b/boot.asm @@ -1,7 +1,7 @@ [BITS 16] loop: - jmp loop + jmp loop times 510-($-$$) db 0 diff --git a/build_boot b/build_boot new file mode 100755 index 0000000..6af9267 --- /dev/null +++ b/build_boot @@ -0,0 +1,5 @@ +#!/bin/bash +ASM_SOURCE=./boot.asm +[ ! -f "$ASM_SOURCE" ] && { echo "$ASM_SOURCE does not exist."; exit 1; } +nasm -f bin boot.asm -o boot.bin +od -t x1 -A n boot.asm diff --git a/write_disk b/write_disk new file mode 100755 index 0000000..cf04457 --- /dev/null +++ b/write_disk @@ -0,0 +1,5 @@ +#!/bin/bash +BIN_SOURCE=./boot.bin +[ ! -f "$BIN_SOURCE" ] && { echo "$BIN_SOURCE does not exist."; exit 1; } +dd if=/dev/zero of=./boot.img bs=1024 count=1440 +dd if=./boot.bin of=./boot.img conv=notrunc From d6a6f94d532cd73b4deade8dc612bb4f81cece4e Mon Sep 17 00:00:00 2001 From: kees Date: Wed, 4 Oct 2023 03:31:59 +0700 Subject: [PATCH 05/88] edit boot.asm --- boot.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boot.asm b/boot.asm index 46079cf..f9ad9b1 100644 --- a/boot.asm +++ b/boot.asm @@ -5,5 +5,5 @@ loop: times 510-($-$$) db 0 -dw 0xAA55 +dw 0xaa55 From 8d9d63f9e348e0386f6a5bd71b9f756ef3b0aa4f Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 02:20:50 +0700 Subject: [PATCH 06/88] fix od output in ./build_boot --- build_boot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_boot b/build_boot index 6af9267..6b99acd 100755 --- a/build_boot +++ b/build_boot @@ -2,4 +2,4 @@ ASM_SOURCE=./boot.asm [ ! -f "$ASM_SOURCE" ] && { echo "$ASM_SOURCE does not exist."; exit 1; } nasm -f bin boot.asm -o boot.bin -od -t x1 -A n boot.asm +od -t x1 -A n boot.bin From 4aee8731b7d0bfb3d6e056454cd04c9db3563f28 Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 02:47:11 +0700 Subject: [PATCH 07/88] upd rep structure --- .bochsrc | 2 +- build_boot | 9 ++++++--- boot.asm => src/boot.asm | 1 + write_disk | 10 ++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) rename boot.asm => src/boot.asm (84%) diff --git a/.bochsrc b/.bochsrc index f341ab3..4df011f 100644 --- a/.bochsrc +++ b/.bochsrc @@ -4,4 +4,4 @@ display_library: x memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 -floppya: type=1_44, 1_44="boot.img", status=inserted, write_protected=0 +floppya: type=1_44, 1_44="./build/boot.img", status=inserted, write_protected=0 diff --git a/build_boot b/build_boot index 6b99acd..47ae5fb 100755 --- a/build_boot +++ b/build_boot @@ -1,5 +1,8 @@ #!/bin/bash -ASM_SOURCE=./boot.asm +SOURCE_DIR=./src +BUILD_DIR=./build +ASM_SOURCE="$SOURCE_DIR"/boot.asm +BIN="$BUILD_DIR"/boot.bin [ ! -f "$ASM_SOURCE" ] && { echo "$ASM_SOURCE does not exist."; exit 1; } -nasm -f bin boot.asm -o boot.bin -od -t x1 -A n boot.bin +nasm -f bin "$ASM_SOURCE" -o "$BIN" +od -t x1 -A n "$BIN" diff --git a/boot.asm b/src/boot.asm similarity index 84% rename from boot.asm rename to src/boot.asm index f9ad9b1..0eb6169 100644 --- a/boot.asm +++ b/src/boot.asm @@ -1,3 +1,4 @@ +ORG 0x7C00 [BITS 16] loop: diff --git a/write_disk b/write_disk index cf04457..331db81 100755 --- a/write_disk +++ b/write_disk @@ -1,5 +1,7 @@ #!/bin/bash -BIN_SOURCE=./boot.bin -[ ! -f "$BIN_SOURCE" ] && { echo "$BIN_SOURCE does not exist."; exit 1; } -dd if=/dev/zero of=./boot.img bs=1024 count=1440 -dd if=./boot.bin of=./boot.img conv=notrunc +BUILD_DIR=./build +BIN="$BUILD_DIR"/boot.bin +IMG="$BUILD_DIR"/boot.img +[ ! -f "$BIN" ] && { echo "$BIN does not exist."; exit 1; } +dd if=/dev/zero of="$IMG" bs=1024 count=1440 +dd if="$BIN" of="$IMG" conv=notrunc From bb4dc32e7d39aaae57a0a792a0ceed96f993e9e4 Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 04:27:17 +0700 Subject: [PATCH 08/88] edit rep structure --- .gitignore | 10 +++++----- Makefile | 43 +++++++++++++++++++++++++++++++++++++++++++ build_boot | 8 -------- write_disk | 7 ------- 4 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 Makefile delete mode 100755 build_boot delete mode 100755 write_disk diff --git a/.gitignore b/.gitignore index b57d1bc..9f0cbee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,8 @@ !*.* !*/ -*.bin -*.img -!build_boot -!write_disk -test.* +!Makefile + +build/* + +test.* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b41a6d0 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +SOURCE_DIR = ./src +BUILD_DIR = ./build +BOOT_ASM = $(SOURCE_DIR)/boot.asm +BOOT_BIN = $(BUILD_DIR)/boot.bin +BOOT_IMG = $(BUILD_DIR)/boot.img + +ifeq ($(OS), Windows_NT) + ASM = + DD = + LD = + BOOT = $(BOOT_IMG) +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + ASM = nasm + DD = dd + LD = ld + OD = od + ASM_OPT = -fbin + OD_OPT = -t x1 -A n + BOOT = $(BOOT_IMG) + endif +endif + +$(info init..) + +.PHONY: clean_boot build_boot +build_boot: $(BOOT) + +$(BOOT_BIN): $(BOOT_ASM) + $(info ) + $(info processing (stage 1)) + $(ASM) $(ASM_OPT) $< -o $@ + $(OD) $(OD_OPT) $@ + +$(BOOT): $(BOOT_BIN) + $(info ) + $(info processing (stage 2)) + $(DD) bs=1024 count=1440 if=/dev/zero of=$@ + $(DD) conv=notrunc if=$< of=$@ + +clean_boot: + rm $(BOOT_BIN) \ No newline at end of file diff --git a/build_boot b/build_boot deleted file mode 100755 index 47ae5fb..0000000 --- a/build_boot +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -SOURCE_DIR=./src -BUILD_DIR=./build -ASM_SOURCE="$SOURCE_DIR"/boot.asm -BIN="$BUILD_DIR"/boot.bin -[ ! -f "$ASM_SOURCE" ] && { echo "$ASM_SOURCE does not exist."; exit 1; } -nasm -f bin "$ASM_SOURCE" -o "$BIN" -od -t x1 -A n "$BIN" diff --git a/write_disk b/write_disk deleted file mode 100755 index 331db81..0000000 --- a/write_disk +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -BUILD_DIR=./build -BIN="$BUILD_DIR"/boot.bin -IMG="$BUILD_DIR"/boot.img -[ ! -f "$BIN" ] && { echo "$BIN does not exist."; exit 1; } -dd if=/dev/zero of="$IMG" bs=1024 count=1440 -dd if="$BIN" of="$IMG" conv=notrunc From 059b28792c8a79c0f01520bde2c623c7c3877751 Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 04:56:33 +0700 Subject: [PATCH 09/88] end up "hello world" task --- src/boot.asm | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/boot.asm b/src/boot.asm index 0eb6169..bc439d2 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -1,8 +1,42 @@ -ORG 0x7C00 -[BITS 16] +ORG 0x7c00 +BITS 16 -loop: - jmp loop +main: + mov ax, 0 + mov ds, ax + mov es, ax + mov ss, ax + + mov sp, 0x7c00 + mov si, os_boot_msg + call print + hlt + +halt: + jmp halt + +print: + push si + push ax + push bx + +print_loop: + lodsb + or al, al + jz done_print + mov ah, 0x0e + mov bh, 0 + int 0x10 + jmp print_loop + +done_print: + pop bx + pop ax + pop si + ret + +os_boot_msg: + DB 'Hello world', 0x0d, 0x0a, 0 times 510-($-$$) db 0 From 5fe048fb4c20481173100be52788e57646a3877c Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 14:29:38 +0700 Subject: [PATCH 10/88] edit rep structure #3 --- .bochsrc | 2 +- Makefile | 44 +++++++++++++++++++++++------------ src/{ => bootloader}/boot.asm | 2 +- src/kernel/main.asm | 0 4 files changed, 31 insertions(+), 17 deletions(-) rename src/{ => bootloader}/boot.asm (91%) create mode 100644 src/kernel/main.asm diff --git a/.bochsrc b/.bochsrc index 4df011f..460749a 100644 --- a/.bochsrc +++ b/.bochsrc @@ -4,4 +4,4 @@ display_library: x memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 -floppya: type=1_44, 1_44="./build/boot.img", status=inserted, write_protected=0 +floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/Makefile b/Makefile index b41a6d0..471032d 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,24 @@ SOURCE_DIR = ./src BUILD_DIR = ./build -BOOT_ASM = $(SOURCE_DIR)/boot.asm -BOOT_BIN = $(BUILD_DIR)/boot.bin -BOOT_IMG = $(BUILD_DIR)/boot.img + +BOOTLOADER_SOURCE_DIR = $(SOURCE_DIR)/bootloader +KERNEL_SOURCE_DIR = $(SOURCE_DIR)/kernel + +BOOTLOADER_BUILD_DIR = $(BUILD_DIR)/bootloader +KERNEL_BUILD_DIR = $(BUILD_DIR)/kernel + +BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm +KERNEL_ASM = $(KERNEL_SOURCE_DIR)/main.asm + +BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin +KERNEL_BIN = $(KERNEL_BUILD_DIR)/kernel.bin + +FLOPPY_IMG = $(BUILD_DIR)/floppy.img ifeq ($(OS), Windows_NT) ASM = DD = LD = - BOOT = $(BOOT_IMG) else UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Linux) @@ -17,27 +27,31 @@ else LD = ld OD = od ASM_OPT = -fbin - OD_OPT = -t x1 -A n - BOOT = $(BOOT_IMG) + OD_OPT = -t x1 -A n endif endif $(info init..) .PHONY: clean_boot build_boot -build_boot: $(BOOT) -$(BOOT_BIN): $(BOOT_ASM) - $(info ) - $(info processing (stage 1)) - $(ASM) $(ASM_OPT) $< -o $@ - $(OD) $(OD_OPT) $@ +floppy: $(FLOPPY_IMG) +kernel: $(KERNEL_BIN) +bootloader: $(BOOTLOADER_BIN) -$(BOOT): $(BOOT_BIN) +$(FLOPPY_IMG): $(BOOTLOADER_BIN) $(info ) - $(info processing (stage 2)) + $(info building: $@) $(DD) bs=1024 count=1440 if=/dev/zero of=$@ $(DD) conv=notrunc if=$< of=$@ +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) + $(info ) + $(info building: $@) + $(ASM) $(ASM_OPT) $< -o $@ + $(OD) $(OD_OPT) $@ + clean_boot: - rm $(BOOT_BIN) \ No newline at end of file + rm $(FLOPPY_IMG) + rm $(KERNEL_BIN) + rm $(BOOTLOADER_BIN) \ No newline at end of file diff --git a/src/boot.asm b/src/bootloader/boot.asm similarity index 91% rename from src/boot.asm rename to src/bootloader/boot.asm index bc439d2..a23066b 100644 --- a/src/boot.asm +++ b/src/bootloader/boot.asm @@ -36,7 +36,7 @@ done_print: ret os_boot_msg: - DB 'Hello world', 0x0d, 0x0a, 0 + DB 'Hello world!', 0x0d, 0x0a, 0 times 510-($-$$) db 0 diff --git a/src/kernel/main.asm b/src/kernel/main.asm new file mode 100644 index 0000000..e69de29 From 81663cfa55c79419bdffa31d578e6d605955dfb3 Mon Sep 17 00:00:00 2001 From: kees Date: Thu, 5 Oct 2023 14:43:12 +0700 Subject: [PATCH 11/88] edit Makefile --- Makefile | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 471032d..461520c 100644 --- a/Makefile +++ b/Makefile @@ -18,15 +18,20 @@ FLOPPY_IMG = $(BUILD_DIR)/floppy.img ifeq ($(OS), Windows_NT) ASM = DD = + MCOPY = + MKFS = LD = + OD = + OD_OPT = -t x1 -A n else UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Linux) ASM = nasm DD = dd + MCOPY = mcopy + MKFS = mkfs LD = ld OD = od - ASM_OPT = -fbin OD_OPT = -t x1 -A n endif endif @@ -35,20 +40,25 @@ $(info init..) .PHONY: clean_boot build_boot -floppy: $(FLOPPY_IMG) +floppy: bootloader kernel $(FLOPPY_IMG) kernel: $(KERNEL_BIN) bootloader: $(BOOTLOADER_BIN) -$(FLOPPY_IMG): $(BOOTLOADER_BIN) - $(info ) - $(info building: $@) +$(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) + $(info [MAKE] building: $@) $(DD) bs=1024 count=1440 if=/dev/zero of=$@ + $(MKFS) -t fat -F 12 -n "UNOS" $@ $(DD) conv=notrunc if=$< of=$@ + $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" + +$(KERNEL_BIN): $(KERNEL_ASM) + $(info [MAKE] building: $@) + $(ASM) -fbin $< -o $@ + $(OD) $(OD_OPT) $@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) - $(info ) - $(info building: $@) - $(ASM) $(ASM_OPT) $< -o $@ + $(info [MAKE] building: $@) + $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ clean_boot: From 42d04e9d4e074f320116f9c4d921d4a389286a2f Mon Sep 17 00:00:00 2001 From: QQ3646 <42693419+QQ3646@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:51:05 +0700 Subject: [PATCH 12/88] Update Makefile --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 461520c..c991db3 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,6 @@ $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) $(KERNEL_BIN): $(KERNEL_ASM) $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ - $(OD) $(OD_OPT) $@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(info [MAKE] building: $@) @@ -64,4 +63,4 @@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) clean_boot: rm $(FLOPPY_IMG) rm $(KERNEL_BIN) - rm $(BOOTLOADER_BIN) \ No newline at end of file + rm $(BOOTLOADER_BIN) From 0104fe7dd9c1c72a65e5be6aa2f9e4ebb42f4070 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 01:41:51 +0700 Subject: [PATCH 13/88] add launch.json, tasks.json, rename bochs.linux.conf, edit Makefile --- .gitignore | 5 ++++- .vscode/launch.json | 15 +++++++++++++++ .vscode/tasks.json | 10 ++++++++++ Makefile | 4 ++-- .bochsrc => bochs.linux.conf | 0 src/bootloader/boot.asm | 3 +-- 6 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json rename .bochsrc => bochs.linux.conf (100%) diff --git a/.gitignore b/.gitignore index 9f0cbee..9e9ee66 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ build/* -test.* \ No newline at end of file +test.* + +!.vscode/ +!.vscode/*.* \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e9424ba --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "bochs floppy", + "type": "f5anything", + "request": "launch", + "command": "bochs -q -f ./bochs.linux.conf", + "preLaunchTask": "make_floppy" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..c56ddec --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,10 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "make_floppy", + "type": "shell", + "command": "make floppy" + } + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile index c991db3..b37f5f3 100644 --- a/Makefile +++ b/Makefile @@ -47,9 +47,9 @@ bootloader: $(BOOTLOADER_BIN) $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) $(info [MAKE] building: $@) $(DD) bs=1024 count=1440 if=/dev/zero of=$@ - $(MKFS) -t fat -F 12 -n "UNOS" $@ +# $(MKFS) -t fat -F 12 -n "UNOS" $@ $(DD) conv=notrunc if=$< of=$@ - $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" +# $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" $(KERNEL_BIN): $(KERNEL_ASM) $(info [MAKE] building: $@) diff --git a/.bochsrc b/bochs.linux.conf similarity index 100% rename from .bochsrc rename to bochs.linux.conf diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index a23066b..9fc48bf 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -12,8 +12,7 @@ main: call print hlt -halt: - jmp halt + jmp $ print: push si From eb7684f2a8f73769dfb53be9c31f3bf60d479377 Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 6 Oct 2023 02:17:57 +0700 Subject: [PATCH 14/88] add bochs config for win & launch option for win --- .vscode/launch.json | 9 ++++++++- bochs.win.conf | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 bochs.win.conf diff --git a/.vscode/launch.json b/.vscode/launch.json index e9424ba..d94f870 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,11 +5,18 @@ "version": "0.2.0", "configurations": [ { - "name": "bochs floppy", + "name": "(linux) bochs floppy", "type": "f5anything", "request": "launch", "command": "bochs -q -f ./bochs.linux.conf", "preLaunchTask": "make_floppy" + }, + { + "name": "(win) bochs floppy", + "type": "f5anything", + "request": "launch", + "command": "& \"${env:BOCHS_PATH}\\bochs\" -q -f ./bochs.win.conf", + "preLaunchTask": "make_floppy" } ] } \ No newline at end of file diff --git a/bochs.win.conf b/bochs.win.conf new file mode 100644 index 0000000..91aa228 --- /dev/null +++ b/bochs.win.conf @@ -0,0 +1,7 @@ +plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true +config_interface: win32config +display_library: win32 +memory: host=32, guest=1024 +boot: floppy +floppy_bootsig_check: disabled=0 +floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 \ No newline at end of file From 7449bfe9b0c37298aa5bd118a173b1a176947fd2 Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 6 Oct 2023 02:35:30 +0700 Subject: [PATCH 15/88] add win params to makefile --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b37f5f3..96544d3 100644 --- a/Makefile +++ b/Makefile @@ -16,12 +16,12 @@ KERNEL_BIN = $(KERNEL_BUILD_DIR)/kernel.bin FLOPPY_IMG = $(BUILD_DIR)/floppy.img ifeq ($(OS), Windows_NT) - ASM = - DD = + ASM = ${NASM_PATH}\nasm + DD = ${CYGWIN_PATH}\bin\dd MCOPY = MKFS = LD = - OD = + OD = ${CYGWIN_PATH}\bin\od OD_OPT = -t x1 -A n else UNAME_S := $(shell uname -s) From b4b5aaca71e2da137704acf6a7537c22eb49c83e Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 03:28:59 +0700 Subject: [PATCH 16/88] separate print.asm, add read_floppy_disk.asm --- Makefile | 2 +- src/bootloader/boot.asm | 48 ++++++++++++++++------------- src/bootloader/read_floppy_disk.asm | 36 ++++++++++++++++++++++ src/misc/print.asm | 26 ++++++++++++++++ 4 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 src/bootloader/read_floppy_disk.asm create mode 100644 src/misc/print.asm diff --git a/Makefile b/Makefile index 96544d3..57dd65f 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ bootloader: $(BOOTLOADER_BIN) $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) $(info [MAKE] building: $@) - $(DD) bs=1024 count=1440 if=/dev/zero of=$@ + $(DD) bs=512 count=2880 if=/dev/zero of=$@ # $(MKFS) -t fat -F 12 -n "UNOS" $@ $(DD) conv=notrunc if=$< of=$@ # $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 9fc48bf..16b45d5 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -6,38 +6,42 @@ main: mov ds, ax mov es, ax mov ss, ax + mov [boot_drive], dl mov sp, 0x7c00 mov si, os_boot_msg call print - hlt + + mov bx, 0x9000 + mov dh, 2 + mov dl, [boot_drive] + call disk_load + + mov dx, [0x9000 + 512] + call print_hex ; TODO: implement print_hex, source=DX + + mov dx, [0x9000 + 512 + 512] + call print_hex ; TODO: implement print_hex, source=DX jmp $ -print: - push si - push ax - push bx - -print_loop: - lodsb - or al, al - jz done_print - mov ah, 0x0e - mov bh, 0 - int 0x10 - jmp print_loop - -done_print: - pop bx - pop ax - pop si - ret +%ifndef PRINT + %include "./src/misc/print.asm" +%endif + +%ifndef READ_FLOPPY_DISK + %include "./src/bootloader/read_floppy_disk.asm" +%endif os_boot_msg: DB 'Hello world!', 0x0d, 0x0a, 0 +boot_drive: + DB 0 -times 510-($-$$) db 0 - +times 510-($-$$) DB 0 dw 0xaa55 +times 512 DB 0 +dw 0xeffe +times 512 DB 0 +dw 0xfeef \ No newline at end of file diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm new file mode 100644 index 0000000..ff32c74 --- /dev/null +++ b/src/bootloader/read_floppy_disk.asm @@ -0,0 +1,36 @@ +%ifndef READ_FLOPPY_DISK + %define READ_FLOPPY_DISK +%endif + +ORG 0x7c00 +BITS 16 + +disk_load: + push dx + + mov ah, 0x02 + mov al, dh ; read DH sector + mov ch, 0x00 ; select cylinder 0 + mov dh, 0x00 ; select head 0 + mov cl, 0x02 ; select sector 2 (512B each sector, so it follows out boot sector) + + int 0x13 + + jc disk_error + + pop dx + cmp dh, al + jne disk_error + ret + +disk_error: + mov bx, disk_error_msg + call print + jmp $ + +%ifndef PRINT + %include "./src/misc/print.asm" +%endif + +disk_error_msg: + DB 'Disk read error!', 0x0d, 0x0a, 0 \ No newline at end of file diff --git a/src/misc/print.asm b/src/misc/print.asm new file mode 100644 index 0000000..4ba6243 --- /dev/null +++ b/src/misc/print.asm @@ -0,0 +1,26 @@ +%ifndef PRINT + %define PRINT +%endif + +ORG 0x7c00 +BITS 16 + +print: + push si + push ax + push bx + +print_loop: + lodsb + or al, al + jz done_print + mov ah, 0x0e + mov bh, 0 + int 0x10 + jmp print_loop + +done_print: + pop bx + pop ax + pop si + ret \ No newline at end of file From 24141da594fb5fe1d0662f0e73d1bf3051495b8c Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 04:03:30 +0700 Subject: [PATCH 17/88] upd .asm files - add square brackets --- src/bootloader/boot.asm | 20 +++++++++++--------- src/bootloader/read_floppy_disk.asm | 3 +-- src/misc/print.asm | 3 +-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 16b45d5..49446fe 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -1,5 +1,5 @@ -ORG 0x7c00 -BITS 16 +[ORG 0x7c00] +[BITS 16] main: mov ax, 0 @@ -13,15 +13,17 @@ main: call print mov bx, 0x9000 - mov dh, 2 + mov dh, 10 mov dl, [boot_drive] call disk_load mov dx, [0x9000 + 512] - call print_hex ; TODO: implement print_hex, source=DX + mov si, dx + call print ; TODO: implement print_hex, source=DX - mov dx, [0x9000 + 512 + 512] - call print_hex ; TODO: implement print_hex, source=DX + mov dx, [0x9000 + 1024] + mov si, dx + call print ; TODO: implement print_hex, source=DX jmp $ @@ -42,6 +44,6 @@ times 510-($-$$) DB 0 dw 0xaa55 times 512 DB 0 -dw 0xeffe -times 512 DB 0 -dw 0xfeef \ No newline at end of file +dw 'GL' +times 510 DB 0 +dw 'HF' \ No newline at end of file diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index ff32c74..7b2d504 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -2,8 +2,7 @@ %define READ_FLOPPY_DISK %endif -ORG 0x7c00 -BITS 16 +[BITS 16] disk_load: push dx diff --git a/src/misc/print.asm b/src/misc/print.asm index 4ba6243..0514094 100644 --- a/src/misc/print.asm +++ b/src/misc/print.asm @@ -2,8 +2,7 @@ %define PRINT %endif -ORG 0x7c00 -BITS 16 +[BITS 16] print: push si From 62e4e5aa363b76ca76156b94930decbec316d18e Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 13:22:27 +0700 Subject: [PATCH 18/88] bootloader: fix print data from read floppy disk --- bochs.linux.conf | 2 ++ src/bootloader/boot.asm | 12 +++++++----- src/bootloader/read_floppy_disk.asm | 2 +- src/misc/print_hex.asm | 6 ++++++ 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/misc/print_hex.asm diff --git a/bochs.linux.conf b/bochs.linux.conf index 460749a..94c1ac3 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,7 +1,9 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig display_library: x +# options="gui_debug" memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 +magic_break: enabled=1 diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 49446fe..cbbb811 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -13,16 +13,14 @@ main: call print mov bx, 0x9000 - mov dh, 10 + mov dh, 4 mov dl, [boot_drive] call disk_load - mov dx, [0x9000 + 512] - mov si, dx + mov si, 0x9000 + 512 call print ; TODO: implement print_hex, source=DX - mov dx, [0x9000 + 1024] - mov si, dx + mov si, 0x9000 + 1024 call print ; TODO: implement print_hex, source=DX jmp $ @@ -31,6 +29,10 @@ main: %include "./src/misc/print.asm" %endif +%ifndef PRINT_HEX + %include "./src/misc/print_hex.asm" +%endif + %ifndef READ_FLOPPY_DISK %include "./src/bootloader/read_floppy_disk.asm" %endif diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index 7b2d504..c44f259 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -23,7 +23,7 @@ disk_load: ret disk_error: - mov bx, disk_error_msg + mov si, disk_error_msg call print jmp $ diff --git a/src/misc/print_hex.asm b/src/misc/print_hex.asm new file mode 100644 index 0000000..437154a --- /dev/null +++ b/src/misc/print_hex.asm @@ -0,0 +1,6 @@ +%ifndef PRINT_HEX + %define PRINT_HEX +%endif + +[ORG 0x7c00] +[BITS 16] From 0d4c17fece613f82c132a2b1839ddeda64f3f8c6 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 15:56:17 +0700 Subject: [PATCH 19/88] bootloader: implement accumulate --- src/bootloader/boot.asm | 48 +++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index cbbb811..81e3efa 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -2,7 +2,7 @@ [BITS 16] main: - mov ax, 0 + xor ax, ax mov ds, ax mov es, ax mov ss, ax @@ -13,18 +13,45 @@ main: call print mov bx, 0x9000 - mov dh, 4 + mov dh, 5 mov dl, [boot_drive] call disk_load - mov si, 0x9000 + 512 - call print ; TODO: implement print_hex, source=DX + call accumulate - mov si, 0x9000 + 1024 - call print ; TODO: implement print_hex, source=DX + mov si, 0x9000 + 1024 + 512 + call print ; TODO: implement print_hex, source=? + + mov si, 0x9000 + 1024 + 1024 + call print ; TODO: implement print_hex, source=? jmp $ +accumulate: + pusha + xor bx, bx + mov si, 0x9000 + + xor cx, cx + mov al, dh + mov cx, 0x200 + mul cx + mov cx, ax + add cx, 0x9000 + xor ah, ah +accumulate_loop: + lodsb + add bx, ax + cmp si, cx + jl accumulate_loop + + mov [sum], bx + mov si, sum + xchg bx, bx + call print ; TODO: implement print_hex, source=? + popa + ret + %ifndef PRINT %include "./src/misc/print.asm" %endif @@ -41,11 +68,18 @@ os_boot_msg: DB 'Hello world!', 0x0d, 0x0a, 0 boot_drive: DB 0 +sum: + DB 0 times 510-($-$$) DB 0 dw 0xaa55 times 512 DB 0 +dw 0x3001 +times 510 DB 0 +dw 0x0203 +times 510 DB 0 dw 'GL' times 510 DB 0 -dw 'HF' \ No newline at end of file +dw 'HF' +times 510 DB 0 \ No newline at end of file From 12df0e197604e37e60400e316d2f7966750e9bf8 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 17:24:08 +0700 Subject: [PATCH 20/88] separate accumulate sum label --- src/bootloader/boot.asm | 37 ++++++++++++------------------------- src/misc/accumulate_sum.asm | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 src/misc/accumulate_sum.asm diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 81e3efa..5d2e87e 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -17,21 +17,7 @@ main: mov dl, [boot_drive] call disk_load - call accumulate - - mov si, 0x9000 + 1024 + 512 - call print ; TODO: implement print_hex, source=? - - mov si, 0x9000 + 1024 + 1024 - call print ; TODO: implement print_hex, source=? - - jmp $ - -accumulate: - pusha - xor bx, bx - mov si, 0x9000 - + mov si, bx xor cx, cx mov al, dh mov cx, 0x200 @@ -39,18 +25,15 @@ accumulate: mov cx, ax add cx, 0x9000 xor ah, ah -accumulate_loop: - lodsb - add bx, ax - cmp si, cx - jl accumulate_loop + call accumulate + + mov si, 0x9000 + 1024 + 512 + call print ; TODO: implement print_hex, source=? - mov [sum], bx - mov si, sum - xchg bx, bx + mov si, 0x9000 + 1024 + 1024 call print ; TODO: implement print_hex, source=? - popa - ret + + jmp $ %ifndef PRINT %include "./src/misc/print.asm" @@ -64,6 +47,10 @@ accumulate_loop: %include "./src/bootloader/read_floppy_disk.asm" %endif +%ifndef ACCUMULATE_SUM + %include "./src/misc/accumulate_sum.asm" +%endif + os_boot_msg: DB 'Hello world!', 0x0d, 0x0a, 0 boot_drive: diff --git a/src/misc/accumulate_sum.asm b/src/misc/accumulate_sum.asm new file mode 100644 index 0000000..01a1aca --- /dev/null +++ b/src/misc/accumulate_sum.asm @@ -0,0 +1,21 @@ +%ifndef ACCUMULATE_SUM + %define ACCUMULATE_SUM +%endif + +[BITS 16] + +accumulate: + pusha + xor bx, bx + +accumulate_loop: + lodsb + add bx, ax + cmp si, cx + jl accumulate_loop + + mov [sum], bx + mov si, sum + call print ; TODO: implement print_hex, source=? + popa + ret \ No newline at end of file From 6e9cd46c1c70f4db4a75af3d7f15a4994eb95ffc Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 6 Oct 2023 20:17:40 +0700 Subject: [PATCH 21/88] bootloader: misc: fix =) --- src/bootloader/boot.asm | 11 +++-------- src/misc/accumulate_sum.asm | 7 ++++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 5d2e87e..1f43fee 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -27,10 +27,7 @@ main: xor ah, ah call accumulate - mov si, 0x9000 + 1024 + 512 - call print ; TODO: implement print_hex, source=? - - mov si, 0x9000 + 1024 + 1024 + mov si, sum call print ; TODO: implement print_hex, source=? jmp $ @@ -51,12 +48,10 @@ main: %include "./src/misc/accumulate_sum.asm" %endif -os_boot_msg: - DB 'Hello world!', 0x0d, 0x0a, 0 boot_drive: DB 0 -sum: - DB 0 +os_boot_msg: + DB 'Hello world!', 0x0d, 0x0a, 0 times 510-($-$$) DB 0 dw 0xaa55 diff --git a/src/misc/accumulate_sum.asm b/src/misc/accumulate_sum.asm index 01a1aca..edc7b12 100644 --- a/src/misc/accumulate_sum.asm +++ b/src/misc/accumulate_sum.asm @@ -15,7 +15,8 @@ accumulate_loop: jl accumulate_loop mov [sum], bx - mov si, sum - call print ; TODO: implement print_hex, source=? popa - ret \ No newline at end of file + ret + +sum: + DB 0 \ No newline at end of file From 248f33b3eea322b6221d9be8feebd1c1de92dd4d Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 8 Oct 2023 19:17:46 +0700 Subject: [PATCH 22/88] IT DOESN'T WORK! WHY? --- src/bootloader/boot.asm | 5 ++++- src/misc/accumulate_sum.asm | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 1f43fee..d1cd37d 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -5,10 +5,13 @@ main: xor ax, ax mov ds, ax mov es, ax + +cli mov ss, ax + mov sp, 0x7c00 +rli mov [boot_drive], dl - mov sp, 0x7c00 mov si, os_boot_msg call print diff --git a/src/misc/accumulate_sum.asm b/src/misc/accumulate_sum.asm index edc7b12..c26559a 100644 --- a/src/misc/accumulate_sum.asm +++ b/src/misc/accumulate_sum.asm @@ -14,9 +14,12 @@ accumulate_loop: cmp si, cx jl accumulate_loop + xchg bx, bx mov [sum], bx popa ret sum: + DW 0 +null_byte: DB 0 \ No newline at end of file From 0a5ba2cb7fd779a2ae61a07faf74ebc109f0051b Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 8 Oct 2023 22:03:01 +0700 Subject: [PATCH 23/88] delete magic bp hint --- src/misc/accumulate_sum.asm | 1 - 1 file changed, 1 deletion(-) diff --git a/src/misc/accumulate_sum.asm b/src/misc/accumulate_sum.asm index c26559a..aa04eed 100644 --- a/src/misc/accumulate_sum.asm +++ b/src/misc/accumulate_sum.asm @@ -14,7 +14,6 @@ accumulate_loop: cmp si, cx jl accumulate_loop - xchg bx, bx mov [sum], bx popa ret From f9b7a274807e2d58b6f0e12cc48f542dacb6877c Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 9 Oct 2023 16:44:49 +0700 Subject: [PATCH 24/88] upd rep structure, add print_d2vm.asm --- src/bootloader/boot.asm | 8 ++++---- src/pm/io/print_d2vm.asm | 27 +++++++++++++++++++++++++++ src/{misc => rm/io}/print.asm | 0 src/{misc => rm/io}/print_hex.asm | 0 src/{ => rm}/misc/accumulate_sum.asm | 0 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/pm/io/print_d2vm.asm rename src/{misc => rm/io}/print.asm (100%) rename src/{misc => rm/io}/print_hex.asm (100%) rename src/{ => rm}/misc/accumulate_sum.asm (100%) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index d1cd37d..f9b07a0 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -9,7 +9,7 @@ main: cli mov ss, ax mov sp, 0x7c00 -rli +sti mov [boot_drive], dl mov si, os_boot_msg @@ -36,11 +36,11 @@ rli jmp $ %ifndef PRINT - %include "./src/misc/print.asm" + %include "./src/rm/io/print.asm" %endif %ifndef PRINT_HEX - %include "./src/misc/print_hex.asm" + %include "./src/rm/io/print_hex.asm" %endif %ifndef READ_FLOPPY_DISK @@ -48,7 +48,7 @@ rli %endif %ifndef ACCUMULATE_SUM - %include "./src/misc/accumulate_sum.asm" + %include "./src/rm/misc/accumulate_sum.asm" %endif boot_drive: diff --git a/src/pm/io/print_d2vm.asm b/src/pm/io/print_d2vm.asm new file mode 100644 index 0000000..7bb7ab4 --- /dev/null +++ b/src/pm/io/print_d2vm.asm @@ -0,0 +1,27 @@ +; Printing directly to video memory +[BITS 32] + +VIDEO_MEMORY equ 0xb8000 +WHITE_ON_BLACK equ 0x0f + +print_d2vm: + pusha + mov edx, VIDEO_MEMORY + +print_d2vm_loop: + mov al, [ebx] + mov ah, WHITE_ON_BLACK + + test al, al + jz print_d2vm_done + + mov [edx], ax + + add ebx, 1 + add edx, 2 + + jmp print_d2vm_loop + +print_d2vm_done: + pops + ret \ No newline at end of file diff --git a/src/misc/print.asm b/src/rm/io/print.asm similarity index 100% rename from src/misc/print.asm rename to src/rm/io/print.asm diff --git a/src/misc/print_hex.asm b/src/rm/io/print_hex.asm similarity index 100% rename from src/misc/print_hex.asm rename to src/rm/io/print_hex.asm diff --git a/src/misc/accumulate_sum.asm b/src/rm/misc/accumulate_sum.asm similarity index 100% rename from src/misc/accumulate_sum.asm rename to src/rm/misc/accumulate_sum.asm From e0a54357ff252e81987a827cc2330b0d406816ac Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 12 Oct 2023 13:27:22 +0700 Subject: [PATCH 25/88] env: add win debug config --- .vscode/launch.json | 9 ++++++++- bochs.win.conf | 3 ++- bochsdbg.win.conf | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 bochsdbg.win.conf diff --git a/.vscode/launch.json b/.vscode/launch.json index d94f870..af220ad 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,14 @@ "name": "(win) bochs floppy", "type": "f5anything", "request": "launch", - "command": "& \"${env:BOCHS_PATH}\\bochs\" -q -f ./bochs.win.conf", + "command": "& \"${env:BOCHS_PATH}\\bochs\"-q -f ./bochs.win.conf", + "preLaunchTask": "make_floppy" + }, + { + "name": "(win_dbg) bochs floppy", + "type": "f5anything", + "request": "launch", + "command": "& \"${env:BOCHS_PATH}\\bochsdbg\" -q -f ./bochsdbg.win.conf", "preLaunchTask": "make_floppy" } ] diff --git a/bochs.win.conf b/bochs.win.conf index 91aa228..8e37e23 100644 --- a/bochs.win.conf +++ b/bochs.win.conf @@ -4,4 +4,5 @@ display_library: win32 memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 -floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 \ No newline at end of file +floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 +magic_break: enabled=1 diff --git a/bochsdbg.win.conf b/bochsdbg.win.conf new file mode 100644 index 0000000..b3fa93a --- /dev/null +++ b/bochsdbg.win.conf @@ -0,0 +1,8 @@ +plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true +config_interface: win32config +display_library: win32, options="gui_debug" +memory: host=32, guest=1024 +boot: floppy +floppy_bootsig_check: disabled=0 +floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 +magic_break: enabled=1 From 187c3cdb7d9f580f4e3767f4539790cd4dabacb3 Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 12 Oct 2023 13:29:36 +0700 Subject: [PATCH 26/88] bootloader: 384kb read --- src/bootloader/boot.asm | 30 +++++++++++++---- src/bootloader/read_floppy_disk.asm | 52 ++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index f9b07a0..e88ef4f 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -14,11 +14,23 @@ sti mov si, os_boot_msg call print + + push ax + push bx - mov bx, 0x9000 + + mov ax, 0x2000 + mov es, ax + mov bx, 0 mov dh, 5 mov dl, [boot_drive] call disk_load + + xchg bx, bx + pop bx + pop ax + + xchg bx, bx mov si, bx xor cx, cx @@ -29,18 +41,18 @@ sti add cx, 0x9000 xor ah, ah call accumulate - + mov si, sum call print ; TODO: implement print_hex, source=? jmp $ %ifndef PRINT - %include "./src/rm/io/print.asm" + %include "./src/misc/print.asm" %endif %ifndef PRINT_HEX - %include "./src/rm/io/print_hex.asm" + %include "./src/misc/print_hex.asm" %endif %ifndef READ_FLOPPY_DISK @@ -48,7 +60,7 @@ sti %endif %ifndef ACCUMULATE_SUM - %include "./src/rm/misc/accumulate_sum.asm" + %include "./src/misc/accumulate_sum.asm" %endif boot_drive: @@ -59,7 +71,8 @@ os_boot_msg: times 510-($-$$) DB 0 dw 0xaa55 -times 512 DB 0 +times 510 DB 0 +dw 0x3001 dw 0x3001 times 510 DB 0 dw 0x0203 @@ -67,4 +80,7 @@ times 510 DB 0 dw 'GL' times 510 DB 0 dw 'HF' -times 510 DB 0 \ No newline at end of file +times 510 DB 0 + +times 456190 DB 1 +dw 'ye' \ No newline at end of file diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index c44f259..e1587b6 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -1,27 +1,61 @@ -%ifndef READ_FLOPPY_DISK - %define READ_FLOPPY_DISK +%ifndef READ_AFLOPPY_DISKS + %define READ_AFLOPPY_DISKS %endif [BITS 16] disk_load: - push dx + xor cx, cx - mov ah, 0x02 - mov al, dh ; read DH sector + mov ah, 0x02 ; 0x2 - read sectors from drive + mov al, 1 ; read DH sector mov ch, 0x00 ; select cylinder 0 mov dh, 0x00 ; select head 0 mov cl, 0x02 ; select sector 2 (512B each sector, so it follows out boot sector) +loop_load: int 0x13 - jc disk_error - pop dx - cmp dh, al - jne disk_error + mov ah, 0x02 ; 0x2 - read sectors from drive + + + push ax + mov ax, es + add ax, 0x20 + mov es, ax + pop ax + + add [iter_counter], byte 0x1 + + cmp [iter_counter], byte 18 + jne continue + mov [iter_counter], byte 0 + + push ax + mov ax, 65472 + and cx, ax + pop ax + + cmp dh, 0x1 + xor dh, 0x1 + jne continue + + add ch, 1 + +continue: + add cx, 0x1 + add [sector_counter], word 0x1 + cmp [sector_counter], word 896 + jne loop_load + xchg bx, bx ret +iter_counter: + db 1 +sector_counter: + dw 0 + disk_error: mov si, disk_error_msg call print From cdec38301397892a6e4c8b1f0c8c00524140b68f Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 12 Oct 2023 13:36:28 +0700 Subject: [PATCH 27/88] bootloader: path fix --- src/bootloader/boot.asm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index e88ef4f..42e5116 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -48,11 +48,11 @@ sti jmp $ %ifndef PRINT - %include "./src/misc/print.asm" + %include "./src/rm/io/print.asm" %endif %ifndef PRINT_HEX - %include "./src/misc/print_hex.asm" + %include "./src/rm/io/print_hex.asm" %endif %ifndef READ_FLOPPY_DISK @@ -60,7 +60,7 @@ sti %endif %ifndef ACCUMULATE_SUM - %include "./src/misc/accumulate_sum.asm" + %include "./src/rm/misc/accumulate_sum.asm" %endif boot_drive: From 9bb2cab0338ac46fe8aee378550987973dd7edb4 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Tue, 10 Oct 2023 00:56:19 +0700 Subject: [PATCH 28/88] add switch to protected mode --- Makefile | 4 +-- src/bootloader/boot.asm | 15 ++++++++--- src/pm/gdt.asm | 36 ++++++++++++++++++++++++++ src/pm/io/print_d2vm.asm | 7 ++++- src/pm/switch2pm.asm | 47 ++++++++++++++++++++++++++++++++++ src/rm/misc/accumulate_sum.asm | 2 ++ 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 src/pm/gdt.asm create mode 100644 src/pm/switch2pm.asm diff --git a/Makefile b/Makefile index 57dd65f..a879f04 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ endif $(info init..) -.PHONY: clean_boot build_boot +.PHONY: clean_boot build_boot floppy kernel bootloader floppy: bootloader kernel $(FLOPPY_IMG) kernel: $(KERNEL_BIN) @@ -55,7 +55,7 @@ $(KERNEL_BIN): $(KERNEL_ASM) $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ -$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 42e5116..e68424b 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -6,10 +6,10 @@ main: mov ds, ax mov es, ax -cli + cli mov ss, ax mov sp, 0x7c00 -sti + sti mov [boot_drive], dl mov si, os_boot_msg @@ -32,19 +32,22 @@ sti xchg bx, bx - mov si, bx + mov si, 0x9000 + ;mov dx, bx xor cx, cx mov al, dh mov cx, 0x200 mul cx mov cx, ax - add cx, 0x9000 + add cx, 0 xor ah, ah call accumulate mov si, sum call print ; TODO: implement print_hex, source=? + call switch_to_pm + jmp $ %ifndef PRINT @@ -68,6 +71,10 @@ boot_drive: os_boot_msg: DB 'Hello world!', 0x0d, 0x0a, 0 +%ifndef SWITCH_TO_PM + %include "./src/pm/switch2pm.asm" +%endif + times 510-($-$$) DB 0 dw 0xaa55 diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm new file mode 100644 index 0000000..9c76077 --- /dev/null +++ b/src/pm/gdt.asm @@ -0,0 +1,36 @@ +%ifndef GDT + %define GDT +%endif + +; GDT + +gdt_start: + +gdt_null: + dd 0x0 + dd 0x0 + +gdt_code: + dw 0xffff + dw 0x0 + db 0x0 + db 10011010b + db 11001111b + db 0x0 + +gdt_data: + dw 0xffff + dw 0x0 + db 0x0 + db 10010010b + db 11001111b + db 0x0 + +gdt_end: + +gdt_descriptor: + dw gdt_end - gdt_start - 1 + dd gdt_start + +CODE_SEG equ gdt_code - gdt_start +DATA_SEG equ gdt_data - gdt_start \ No newline at end of file diff --git a/src/pm/io/print_d2vm.asm b/src/pm/io/print_d2vm.asm index 7bb7ab4..3b849ba 100644 --- a/src/pm/io/print_d2vm.asm +++ b/src/pm/io/print_d2vm.asm @@ -1,4 +1,9 @@ +%ifndef PRINT_D2VM + %define PRINT_D2VM +%endif + ; Printing directly to video memory + [BITS 32] VIDEO_MEMORY equ 0xb8000 @@ -23,5 +28,5 @@ print_d2vm_loop: jmp print_d2vm_loop print_d2vm_done: - pops + popa ret \ No newline at end of file diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm new file mode 100644 index 0000000..1850039 --- /dev/null +++ b/src/pm/switch2pm.asm @@ -0,0 +1,47 @@ +%ifndef SWITCH_TO_PM + %define SWITCH_TO_PM +%endif + +; Switch to protected mode + +%ifndef GDT + %include "./src/pm/gdt.asm" +%endif + +%ifndef PRINT_D2VM + %include "./src/pm/io/print_d2vm.asm" +%endif + +[BITS 16] + +switch_to_pm: + cli + lgdt [gdt_descriptor] + + mov eax, cr0 + or eax, 0x1 + mov cr0, eax + + jmp CODE_SEG:init_pm + +[BITS 32] + +init_pm: + mov ax, DATA_SEG + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + + mov ebp, 0x20000 + mov esp, ebp + + call begin_pm + +begin_pm: + mov ebx, msg_pm_begin + call print_d2vm + jmp $ + +msg_pm_begin db "Hello from 32-bit protected mode!", 0 \ No newline at end of file diff --git a/src/rm/misc/accumulate_sum.asm b/src/rm/misc/accumulate_sum.asm index aa04eed..539cd5d 100644 --- a/src/rm/misc/accumulate_sum.asm +++ b/src/rm/misc/accumulate_sum.asm @@ -10,10 +10,12 @@ accumulate: accumulate_loop: lodsb + ;add dx, 2 add bx, ax cmp si, cx jl accumulate_loop + xchg bx, bx mov [sum], bx popa ret From 36224b6c658a62622a838995ab9ebaf76f038c26 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 13 Oct 2023 00:45:08 +0700 Subject: [PATCH 29/88] bootloader: delete unnecessary row --- src/bootloader/boot.asm | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index e68424b..df6cbd8 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -18,7 +18,6 @@ main: push ax push bx - mov ax, 0x2000 mov es, ax mov bx, 0 From 19111f428f7f4796c3fb4b7cfde30482e80b23f3 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 13 Oct 2023 03:54:02 +0700 Subject: [PATCH 30/88] bootloader: kernel: build: add kernel entry point prepare to "Go to C!" [THIS V. DOESN'T WORK!] --- .vscode/tasks.json | 2 +- Makefile | 37 +++++++++++++++++++----- src/bootloader/boot.asm | 43 +++++++++++++--------------- src/bootloader/read_floppy_disk.asm | 2 -- src/kernel/main.asm | 0 src/kernel/main.c | 5 ++++ src/kernel/main.o | Bin 0 -> 1240 bytes src/pm/switch2pm.asm | 4 +++ src/rm/misc/accumulate_sum.asm | 2 -- 9 files changed, 60 insertions(+), 35 deletions(-) delete mode 100644 src/kernel/main.asm create mode 100644 src/kernel/main.c create mode 100644 src/kernel/main.o diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c56ddec..a8120fa 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "make_floppy", "type": "shell", - "command": "make floppy" + "command": "./unix" } ] } \ No newline at end of file diff --git a/Makefile b/Makefile index a879f04..1e14dbb 100644 --- a/Makefile +++ b/Makefile @@ -8,31 +8,44 @@ BOOTLOADER_BUILD_DIR = $(BUILD_DIR)/bootloader KERNEL_BUILD_DIR = $(BUILD_DIR)/kernel BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm -KERNEL_ASM = $(KERNEL_SOURCE_DIR)/main.asm +KERNEL_C = $(KERNEL_SOURCE_DIR)/main.c BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin -KERNEL_BIN = $(KERNEL_BUILD_DIR)/kernel.bin +KERNEL_O = $(KERNEL_SOURCE_DIR)/main.o +KERNEL_TMP = $(KERNEL_BUILD_DIR)/kernel.tmp +KERNEL_BIN = $(KERNEL_BUILD_DIR)/main.bin FLOPPY_IMG = $(BUILD_DIR)/floppy.img +KERNEL_ENTRY_ADDRESS = 0x20200 + ifeq ($(OS), Windows_NT) ASM = ${NASM_PATH}\nasm DD = ${CYGWIN_PATH}\bin\dd + GCC = MCOPY = MKFS = LD = + LD_M_OPT = + OBJCOPY = OD = ${CYGWIN_PATH}\bin\od OD_OPT = -t x1 -A n + RM = else UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Linux) ASM = nasm + GCC = gcc DD = dd MCOPY = mcopy MKFS = mkfs LD = ld + LD_M_OPT = elf_i386 + OBJCOPY = objcopy + OBJCOPY_I_OPT = elf_i386 OD = od OD_OPT = -t x1 -A n + RM = rm endif endif @@ -50,10 +63,18 @@ $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) # $(MKFS) -t fat -F 12 -n "UNOS" $@ $(DD) conv=notrunc if=$< of=$@ # $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" + $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ -$(KERNEL_BIN): $(KERNEL_ASM) +$(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(ASM) -fbin $< -o $@ + $(GCC) -fno-pie -ffreestanding -c $< -o $@ +# -m32 + +$(KERNEL_BIN): $(KERNEL_O) + $(info [MAKE] building: $@) + $(LD) -Ttext $(KERNEL_ENTRY_ADDRESS) --oformat binary $< -o $@ +# -m $(LD_M_OPT) $(KERNEL_TMP) +# $(OBJCOPY) -I $(OBJCOPY_I_OPT) -O $(KERNEL_TMP) $< $@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm $(info [MAKE] building: $@) @@ -61,6 +82,8 @@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex $(OD) $(OD_OPT) $@ clean_boot: - rm $(FLOPPY_IMG) - rm $(KERNEL_BIN) - rm $(BOOTLOADER_BIN) + $(RM) -f $(FLOPPY_IMG) + $(RM) -f $(KERNEL_O) + $(RM) -f $(KERNEL_TMP) + $(RM) -f $(KERNEL_BIN) + $(RM) -f $(BOOTLOADER_BIN) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index df6cbd8..11ea970 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -1,6 +1,9 @@ [ORG 0x7c00] [BITS 16] +KERNEL_OFFSET_ES equ 0x2020 +KERNEL_OFFSET_BX equ 0 + main: xor ax, ax mov ds, ax @@ -18,21 +21,17 @@ main: push ax push bx - mov ax, 0x2000 + mov ax, KERNEL_OFFSET_ES mov es, ax - mov bx, 0 - mov dh, 5 + mov bx, KERNEL_OFFSET_BX + mov dh, 15 mov dl, [boot_drive] call disk_load - xchg bx, bx pop bx pop ax - xchg bx, bx - - mov si, 0x9000 - ;mov dx, bx + mov si, KERNEL_OFFSET_ES xor cx, cx mov al, dh mov cx, 0x200 @@ -47,8 +46,6 @@ main: call switch_to_pm - jmp $ - %ifndef PRINT %include "./src/rm/io/print.asm" %endif @@ -77,16 +74,16 @@ os_boot_msg: times 510-($-$$) DB 0 dw 0xaa55 -times 510 DB 0 -dw 0x3001 -dw 0x3001 -times 510 DB 0 -dw 0x0203 -times 510 DB 0 -dw 'GL' -times 510 DB 0 -dw 'HF' -times 510 DB 0 - -times 456190 DB 1 -dw 'ye' \ No newline at end of file +;times 510 DB 0 +;dw 0x3001 +;dw 0x3001 +;times 510 DB 0 +;dw 0x0203 +;times 510 DB 0 +;dw 'GL' +;times 510 DB 0 +;dw 'HF' +;times 510 DB 0 + +;times 456190 DB 1 +;dw 'ye' \ No newline at end of file diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index e1587b6..659d445 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -19,7 +19,6 @@ loop_load: mov ah, 0x02 ; 0x2 - read sectors from drive - push ax mov ax, es add ax, 0x20 @@ -48,7 +47,6 @@ continue: add [sector_counter], word 0x1 cmp [sector_counter], word 896 jne loop_load - xchg bx, bx ret iter_counter: diff --git a/src/kernel/main.asm b/src/kernel/main.asm deleted file mode 100644 index e69de29..0000000 diff --git a/src/kernel/main.c b/src/kernel/main.c new file mode 100644 index 0000000..747a614 --- /dev/null +++ b/src/kernel/main.c @@ -0,0 +1,5 @@ +void kernel_entry() +{ + char* video_memory = (char*) 0xb8000; + *video_memory = 'X'; +} \ No newline at end of file diff --git a/src/kernel/main.o b/src/kernel/main.o new file mode 100644 index 0000000000000000000000000000000000000000..6d79581d686f083f851f40a841b8b5e4f474ec15 GIT binary patch literal 1240 zcmbVLJ4*vW5T1*V_(nxV(BiCA&?OIqU?C?VCWQzJN*d8Kt`E#z!excTMk_&V@-zGy zf}nrF(#9WPXQwl9CoW5D9N3-tzL{@kvwOMDZEnOI2iP1~gI~lfz^g%eE!u-27=+!z z-Fx9B_X#(HP`J;1K0~p&{{;DLcGX?TZ|yF+$+Sp`q?<~l(uqtWsatlVcX9RL!ZL_~ z^GF<i2b}t-Gcy$LYhrR1d0J#sw5^Yf-o~=Sz1vto_z$DL-SF|%Dc#lK z2B_D#!MA3SIPlLyQGyE@g|d7gLlrfMc4D2ZGsg!qax;Phji60S# Date: Fri, 13 Oct 2023 22:53:29 +0700 Subject: [PATCH 31/88] bootloader, kernel: make switch to pm and C --- .vscode/tasks.json | 2 +- Makefile | 4 ++-- src/bootloader/boot.asm | 2 ++ src/kernel/main.o | Bin 1240 -> 0 bytes src/pm/switch2pm.asm | 3 +-- 5 files changed, 6 insertions(+), 5 deletions(-) delete mode 100644 src/kernel/main.o diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a8120fa..a85305a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "make_floppy", "type": "shell", - "command": "./unix" + "command": "./build_" } ] } \ No newline at end of file diff --git a/Makefile b/Makefile index 1e14dbb..f217459 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm KERNEL_C = $(KERNEL_SOURCE_DIR)/main.c BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin -KERNEL_O = $(KERNEL_SOURCE_DIR)/main.o +KERNEL_O = $(KERNEL_BUILD_DIR)/main.o KERNEL_TMP = $(KERNEL_BUILD_DIR)/kernel.tmp KERNEL_BIN = $(KERNEL_BUILD_DIR)/main.bin @@ -72,7 +72,7 @@ $(KERNEL_O): $(KERNEL_C) $(KERNEL_BIN): $(KERNEL_O) $(info [MAKE] building: $@) - $(LD) -Ttext $(KERNEL_ENTRY_ADDRESS) --oformat binary $< -o $@ + $(LD) -Ttext $(KERNEL_ENTRY_ADDRESS) $< -o $@ # -m $(LD_M_OPT) $(KERNEL_TMP) # $(OBJCOPY) -I $(OBJCOPY_I_OPT) -O $(KERNEL_TMP) $< $@ diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 11ea970..d62aeb1 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -46,6 +46,8 @@ main: call switch_to_pm + jmp $ + %ifndef PRINT %include "./src/rm/io/print.asm" %endif diff --git a/src/kernel/main.o b/src/kernel/main.o deleted file mode 100644 index 6d79581d686f083f851f40a841b8b5e4f474ec15..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1240 zcmbVLJ4*vW5T1*V_(nxV(BiCA&?OIqU?C?VCWQzJN*d8Kt`E#z!excTMk_&V@-zGy zf}nrF(#9WPXQwl9CoW5D9N3-tzL{@kvwOMDZEnOI2iP1~gI~lfz^g%eE!u-27=+!z z-Fx9B_X#(HP`J;1K0~p&{{;DLcGX?TZ|yF+$+Sp`q?<~l(uqtWsatlVcX9RL!ZL_~ z^GF<i2b}t-Gcy$LYhrR1d0J#sw5^Yf-o~=Sz1vto_z$DL-SF|%Dc#lK z2B_D#!MA3SIPlLyQGyE@g|d7gLlrfMc4D2ZGsg!qax;Phji60S# Date: Fri, 13 Oct 2023 22:55:56 +0700 Subject: [PATCH 32/88] switch_to_pm: fix --- src/pm/switch2pm.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 1ba68c6..dc1f365 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -43,7 +43,7 @@ begin_pm: mov ebx, msg_pm_begin call print_d2vm - call 0x20200 + call 0x20000 jmp $ From 1b02e8675d54d071dfc8ad2a19a2134324d1b84f Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 13 Oct 2023 23:31:31 +0700 Subject: [PATCH 33/88] kernel, makefile: add relative call to main --- Makefile | 23 +++++++++++++---------- src/kernel/{main.c => kernel.c} | 4 +++- src/kernel/kernel_entry.asm | 5 +++++ 3 files changed, 21 insertions(+), 11 deletions(-) rename src/kernel/{main.c => kernel.c} (70%) create mode 100644 src/kernel/kernel_entry.asm diff --git a/Makefile b/Makefile index f217459..8677bb6 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,14 @@ BOOTLOADER_BUILD_DIR = $(BUILD_DIR)/bootloader KERNEL_BUILD_DIR = $(BUILD_DIR)/kernel BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm -KERNEL_C = $(KERNEL_SOURCE_DIR)/main.c +KERNEL_C = $(KERNEL_SOURCE_DIR)/kernel.c +KERNEL_ENTRY_ASM = $(KERNEL_SOURCE_DIR)/kernel_entry.asm BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin -KERNEL_O = $(KERNEL_BUILD_DIR)/main.o +KERNEL_O = $(KERNEL_BUILD_DIR)/kernel.o +KERNEL_ENTRY_O = $(KERNEL_BUILD_DIR)/kernel_entry.o KERNEL_TMP = $(KERNEL_BUILD_DIR)/kernel.tmp -KERNEL_BIN = $(KERNEL_BUILD_DIR)/main.bin +KERNEL_BIN = $(KERNEL_BUILD_DIR)/kernel.bin FLOPPY_IMG = $(BUILD_DIR)/floppy.img @@ -26,7 +28,6 @@ ifeq ($(OS), Windows_NT) MCOPY = MKFS = LD = - LD_M_OPT = OBJCOPY = OD = ${CYGWIN_PATH}\bin\od OD_OPT = -t x1 -A n @@ -40,9 +41,7 @@ else MCOPY = mcopy MKFS = mkfs LD = ld - LD_M_OPT = elf_i386 OBJCOPY = objcopy - OBJCOPY_I_OPT = elf_i386 OD = od OD_OPT = -t x1 -A n RM = rm @@ -65,15 +64,18 @@ $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) # $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ +$(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) + $(ASM) -f elf $< -o $@ + $(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(GCC) -fno-pie -ffreestanding -c $< -o $@ + $(GCC) -m32 -fno-pie -ffreestanding -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_O) +$(KERNEL_BIN): $(KERNEL_O) $(KERNEL_ENTRY_O) $(info [MAKE] building: $@) - $(LD) -Ttext $(KERNEL_ENTRY_ADDRESS) $< -o $@ -# -m $(LD_M_OPT) $(KERNEL_TMP) + $(LD) -m elf_i386 -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $@ +# $(KERNEL_TMP) # $(OBJCOPY) -I $(OBJCOPY_I_OPT) -O $(KERNEL_TMP) $< $@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm @@ -83,6 +85,7 @@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex clean_boot: $(RM) -f $(FLOPPY_IMG) + $(RM) -f $(KERNEL_ENTRY_O) $(RM) -f $(KERNEL_O) $(RM) -f $(KERNEL_TMP) $(RM) -f $(KERNEL_BIN) diff --git a/src/kernel/main.c b/src/kernel/kernel.c similarity index 70% rename from src/kernel/main.c rename to src/kernel/kernel.c index 747a614..1696528 100644 --- a/src/kernel/main.c +++ b/src/kernel/kernel.c @@ -1,5 +1,7 @@ -void kernel_entry() +void main() { char* video_memory = (char*) 0xb8000; *video_memory = 'X'; + + while(1) {} } \ No newline at end of file diff --git a/src/kernel/kernel_entry.asm b/src/kernel/kernel_entry.asm new file mode 100644 index 0000000..fcbfd54 --- /dev/null +++ b/src/kernel/kernel_entry.asm @@ -0,0 +1,5 @@ +[BITS 32] +[extern main] + +call main +jmp $ \ No newline at end of file From e3645d7b8bdb2012eece15b7d10633aa755e13d5 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 13 Oct 2023 23:33:51 +0700 Subject: [PATCH 34/88] kernel: add test for func call in kernel.c --- src/kernel/kernel.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 1696528..9aa226b 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,7 +1,12 @@ +char hell_yeh() +{ + return 'Y'; +} + void main() { char* video_memory = (char*) 0xb8000; - *video_memory = 'X'; + *video_memory = hell_yeh(); while(1) {} } \ No newline at end of file From e7c228d48ea23d9721927d9351132dd47c8e9456 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 14 Oct 2023 00:12:36 +0700 Subject: [PATCH 35/88] bootload: comment unnecessary stuff --- src/bootloader/boot.asm | 42 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index d62aeb1..7c9afec 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -17,9 +17,6 @@ main: mov si, os_boot_msg call print - - push ax - push bx mov ax, KERNEL_OFFSET_ES mov es, ax @@ -27,22 +24,19 @@ main: mov dh, 15 mov dl, [boot_drive] call disk_load - - pop bx - pop ax - - mov si, KERNEL_OFFSET_ES - xor cx, cx - mov al, dh - mov cx, 0x200 - mul cx - mov cx, ax - add cx, 0 - xor ah, ah - call accumulate + + ;mov si, KERNEL_OFFSET_ES + ;xor cx, cx + ;mov al, dh + ;mov cx, 0x200 + ;mul cx + ;mov cx, ax + ;add cx, 0 + ;xor ah, ah + ;call accumulate - mov si, sum - call print ; TODO: implement print_hex, source=? + ;mov si, sum + ;call print ; TODO: implement print_hex, source=? call switch_to_pm @@ -52,17 +46,17 @@ main: %include "./src/rm/io/print.asm" %endif -%ifndef PRINT_HEX - %include "./src/rm/io/print_hex.asm" -%endif +;%ifndef PRINT_HEX +; %include "./src/rm/io/print_hex.asm" +;%endif %ifndef READ_FLOPPY_DISK %include "./src/bootloader/read_floppy_disk.asm" %endif -%ifndef ACCUMULATE_SUM - %include "./src/rm/misc/accumulate_sum.asm" -%endif +;%ifndef ACCUMULATE_SUM +; %include "./src/rm/misc/accumulate_sum.asm" +;%endif boot_drive: DB 0 From 0ac7607c0e90d6922694e40d0867d58f528478f6 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 20 Oct 2023 18:44:45 +0700 Subject: [PATCH 36/88] clean uneccessary stuff --- Makefile | 6 +++--- src/bootloader/boot.asm | 8 ++------ src/bootloader/read_floppy_disk.asm | 9 +++------ src/pm/switch2pm.asm | 7 ++----- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 8677bb6..dd40b26 100644 --- a/Makefile +++ b/Makefile @@ -72,11 +72,11 @@ $(KERNEL_O): $(KERNEL_C) $(GCC) -m32 -fno-pie -ffreestanding -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_O) $(KERNEL_ENTRY_O) +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) $(info [MAKE] building: $@) - $(LD) -m elf_i386 -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $@ + $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) -# $(OBJCOPY) -I $(OBJCOPY_I_OPT) -O $(KERNEL_TMP) $< $@ + $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm $(info [MAKE] building: $@) diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 7c9afec..0d0b1a6 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -1,7 +1,7 @@ [ORG 0x7c00] [BITS 16] -KERNEL_OFFSET_ES equ 0x2020 +KERNEL_OFFSET_ES equ 0x2000 ;!!! 0x2000 (VBR copy) KERNEL_OFFSET_BX equ 0 main: @@ -12,17 +12,13 @@ main: cli mov ss, ax mov sp, 0x7c00 - sti - mov [boot_drive], dl mov si, os_boot_msg call print mov ax, KERNEL_OFFSET_ES mov es, ax - mov bx, KERNEL_OFFSET_BX - mov dh, 15 - mov dl, [boot_drive] + mov bx, KERNEL_OFFSET_BX call disk_load ;mov si, KERNEL_OFFSET_ES diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index 659d445..c97d3cf 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -5,13 +5,11 @@ [BITS 16] disk_load: - xor cx, cx - mov ah, 0x02 ; 0x2 - read sectors from drive mov al, 1 ; read DH sector mov ch, 0x00 ; select cylinder 0 mov dh, 0x00 ; select head 0 - mov cl, 0x02 ; select sector 2 (512B each sector, so it follows out boot sector) + mov cl, 0x01 ; select sector 2 (512B each sector, so it follows out boot sector) loop_load: int 0x13 @@ -43,9 +41,8 @@ loop_load: add ch, 1 continue: - add cx, 0x1 - add [sector_counter], word 0x1 - cmp [sector_counter], word 896 + add cx, word 0x1 + cmp cx, word 768 jne loop_load ret diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index dc1f365..039059c 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -36,14 +36,11 @@ init_pm: mov ebp, 0x20000 mov esp, ebp - - call begin_pm - -begin_pm: + mov ebx, msg_pm_begin call print_d2vm - call 0x20000 + call 0x20200 jmp $ From bb6ca35fc16dbba021928dfc28039daf820d3e55 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 20 Oct 2023 19:12:45 +0700 Subject: [PATCH 37/88] another fix --- src/pm/switch2pm.asm | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 039059c..862d2ba 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -15,7 +15,6 @@ [BITS 16] switch_to_pm: - cli lgdt [gdt_descriptor] mov eax, cr0 From 75be4f7e3b17f4e1e279d1ac0f54739ff90fcb14 Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 20 Oct 2023 20:42:06 +0700 Subject: [PATCH 38/88] floppy: new version --- src/bootloader/read_floppy_disk.asm | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index c97d3cf..9d51589 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -5,12 +5,16 @@ [BITS 16] disk_load: + mov ax, 0x0 + mov ds, ax + mov ah, 0x02 ; 0x2 - read sectors from drive - mov al, 1 ; read DH sector + mov al, 1 ; read DH sector mov ch, 0x00 ; select cylinder 0 mov dh, 0x00 ; select head 0 - mov cl, 0x01 ; select sector 2 (512B each sector, so it follows out boot sector) - + mov cl, 0x01 ; select sector 1 (512B each sector, so it follows out boot sector) + ; now read w/bootloader + loop_load: int 0x13 jc disk_error @@ -21,18 +25,15 @@ loop_load: mov ax, es add ax, 0x20 mov es, ax - pop ax - add [iter_counter], byte 0x1 + add cl, 0x1 - cmp [iter_counter], byte 18 + cmp cl, 19 ; == 0x12 jne continue - mov [iter_counter], byte 0 - - push ax mov ax, 65472 and cx, ax - pop ax + + mov cl, 1 cmp dh, 0x1 xor dh, 0x1 @@ -41,16 +42,15 @@ loop_load: add ch, 1 continue: - add cx, word 0x1 - cmp cx, word 768 + mov ax, ds + add ax, 0x1 + cmp ax, 768 + mov ds, ax + + pop ax jne loop_load ret -iter_counter: - db 1 -sector_counter: - dw 0 - disk_error: mov si, disk_error_msg call print From b11cf9d8bfa2abea796454f423dd812d27158e8e Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 20 Oct 2023 20:47:48 +0700 Subject: [PATCH 39/88] floppy: use di instead ds --- src/bootloader/read_floppy_disk.asm | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index 9d51589..e8b7c4f 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -5,8 +5,7 @@ [BITS 16] disk_load: - mov ax, 0x0 - mov ds, ax + mov di, 0x0 mov ah, 0x02 ; 0x2 - read sectors from drive mov al, 1 ; read DH sector @@ -19,9 +18,6 @@ loop_load: int 0x13 jc disk_error - mov ah, 0x02 ; 0x2 - read sectors from drive - - push ax mov ax, es add ax, 0x20 mov es, ax @@ -42,12 +38,10 @@ loop_load: add ch, 1 continue: - mov ax, ds - add ax, 0x1 - cmp ax, 768 - mov ds, ax + add di, 0x1 + cmp di, 768 - pop ax + mov ax, 0x201 jne loop_load ret From 0f1d75b1085364336ed59ec3810fe185784f52a7 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 23 Oct 2023 20:14:14 +0700 Subject: [PATCH 40/88] run away from ORG --- bochs.linux.conf | 3 +-- src/bootloader/boot.asm | 27 ++++++--------------------- src/pm/gdt.asm | 2 +- src/pm/switch2pm.asm | 6 +++--- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/bochs.linux.conf b/bochs.linux.conf index 94c1ac3..81b7f3a 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,7 +1,6 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig -display_library: x -# options="gui_debug" +display_library: x, options="gui_debug" memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 0d0b1a6..7569ebf 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -1,17 +1,18 @@ -[ORG 0x7c00] [BITS 16] -KERNEL_OFFSET_ES equ 0x2000 ;!!! 0x2000 (VBR copy) +KERNEL_OFFSET_ES equ 0x2000 KERNEL_OFFSET_BX equ 0 main: xor ax, ax - mov ds, ax mov es, ax cli mov ss, ax - mov sp, 0x7c00 + mov sp, ax + mov ax, 0x7c0 + mov ds, ax + mov bp, 0x7c00 mov si, os_boot_msg call print @@ -54,8 +55,6 @@ main: ; %include "./src/rm/misc/accumulate_sum.asm" ;%endif -boot_drive: - DB 0 os_boot_msg: DB 'Hello world!', 0x0d, 0x0a, 0 @@ -64,18 +63,4 @@ os_boot_msg: %endif times 510-($-$$) DB 0 -dw 0xaa55 - -;times 510 DB 0 -;dw 0x3001 -;dw 0x3001 -;times 510 DB 0 -;dw 0x0203 -;times 510 DB 0 -;dw 'GL' -;times 510 DB 0 -;dw 'HF' -;times 510 DB 0 - -;times 456190 DB 1 -;dw 'ye' \ No newline at end of file +dw 0xaa55 \ No newline at end of file diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm index 9c76077..9499a9e 100644 --- a/src/pm/gdt.asm +++ b/src/pm/gdt.asm @@ -30,7 +30,7 @@ gdt_end: gdt_descriptor: dw gdt_end - gdt_start - 1 - dd gdt_start + dd gdt_start + 0x20000 CODE_SEG equ gdt_code - gdt_start DATA_SEG equ gdt_data - gdt_start \ No newline at end of file diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 862d2ba..8dee42e 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -21,7 +21,7 @@ switch_to_pm: or eax, 0x1 mov cr0, eax - jmp CODE_SEG:init_pm + jmp CODE_SEG:init_pm + 0x7c00 [BITS 32] @@ -36,10 +36,10 @@ init_pm: mov ebp, 0x20000 mov esp, ebp - mov ebx, msg_pm_begin + mov ebx, ds:msg_pm_begin call print_d2vm - call 0x20200 + call 0x20200 - 0x7c00 jmp $ From 086551830ac7dec3d7234d57158cc9464b1a9ae6 Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 26 Oct 2023 20:10:51 +0700 Subject: [PATCH 41/88] all: fix --- Makefile | 11 ++++++----- src/kernel/kernel.c | 2 +- src/kernel/kernel_entry.asm | 4 ++-- src/pm/switch2pm.asm | 9 +++++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index dd40b26..fe56278 100644 --- a/Makefile +++ b/Makefile @@ -24,11 +24,12 @@ KERNEL_ENTRY_ADDRESS = 0x20200 ifeq ($(OS), Windows_NT) ASM = ${NASM_PATH}\nasm DD = ${CYGWIN_PATH}\bin\dd - GCC = + GCC = gcc MCOPY = MKFS = - LD = - OBJCOPY = + LD = ld + LD_M_OPT = i386pe + OBJCOPY = objcopy OD = ${CYGWIN_PATH}\bin\od OD_OPT = -t x1 -A n RM = @@ -69,7 +70,7 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(GCC) -m32 -fno-pie -ffreestanding -c $< -o $@ + $(GCC) -m32 -ffreestanding -nostdlib -lgcc -c $< -o $@ # -m32 $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) @@ -78,7 +79,7 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) # $(KERNEL_TMP) $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ -$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm ./src/pm/gdt.asm $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 9aa226b..3a9e337 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -3,7 +3,7 @@ char hell_yeh() return 'Y'; } -void main() +void entry_point() { char* video_memory = (char*) 0xb8000; *video_memory = hell_yeh(); diff --git a/src/kernel/kernel_entry.asm b/src/kernel/kernel_entry.asm index fcbfd54..cc64f80 100644 --- a/src/kernel/kernel_entry.asm +++ b/src/kernel/kernel_entry.asm @@ -1,5 +1,5 @@ [BITS 32] -[extern main] +[extern _entry_point] -call main +call _entry_point jmp $ \ No newline at end of file diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 8dee42e..5f83773 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -26,8 +26,11 @@ switch_to_pm: [BITS 32] init_pm: + +xchg bx, bx mov ax, DATA_SEG mov ds, ax + mov ss, ax mov es, ax mov fs, ax @@ -36,11 +39,13 @@ init_pm: mov ebp, 0x20000 mov esp, ebp - mov ebx, ds:msg_pm_begin + xchg bx, bx + mov ebx, msg_pm_begin + 0x20000 + xchg bx, bx call print_d2vm call 0x20200 - 0x7c00 jmp $ -msg_pm_begin db "Hello from 32-bit protected mode!", 0 \ No newline at end of file +msg_pm_begin db "Hello from 32-bit protected mode!", 0 From 867ddeae47a7b15911814114fa6adfb4b23f3e05 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 26 Oct 2023 22:33:25 +0700 Subject: [PATCH 42/88] upd --- Makefile | 2 +- bochs.linux.conf | 3 ++- src/bootloader/boot.asm | 2 +- src/bootloader/read_floppy_disk.asm | 15 ++++++--------- src/kernel/kernel_entry.asm | 4 ++-- src/pm/switch2pm.asm | 5 ----- src/rm/io/print.asm | 8 ++------ 7 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index fe56278..e15e163 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(GCC) -m32 -ffreestanding -nostdlib -lgcc -c $< -o $@ + $(GCC) -m32 -fno-pie -ffreestanding -nostdlib -lgcc -c $< -o $@ # -m32 $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) diff --git a/bochs.linux.conf b/bochs.linux.conf index 81b7f3a..936ef14 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,6 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig -display_library: x, options="gui_debug" +display_library: x +#, options="gui_debug" memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 diff --git a/src/bootloader/boot.asm b/src/bootloader/boot.asm index 7569ebf..6e4e6be 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/boot.asm @@ -9,7 +9,7 @@ main: cli mov ss, ax - mov sp, ax + mov sp, 0x7c00 mov ax, 0x7c0 mov ds, ax mov bp, 0x7c00 diff --git a/src/bootloader/read_floppy_disk.asm b/src/bootloader/read_floppy_disk.asm index e8b7c4f..a7ff0c0 100644 --- a/src/bootloader/read_floppy_disk.asm +++ b/src/bootloader/read_floppy_disk.asm @@ -7,14 +7,14 @@ disk_load: mov di, 0x0 - mov ah, 0x02 ; 0x2 - read sectors from drive - mov al, 1 ; read DH sector mov ch, 0x00 ; select cylinder 0 mov dh, 0x00 ; select head 0 mov cl, 0x01 ; select sector 1 (512B each sector, so it follows out boot sector) ; now read w/bootloader loop_load: + mov ah, 0x02 ; 0x2 - read sectors from drive + mov al, 1 ; read DH sector int 0x13 jc disk_error @@ -24,14 +24,12 @@ loop_load: add cl, 0x1 - cmp cl, 19 ; == 0x12 - jne continue - mov ax, 65472 - and cx, ax + cmp cl, 0x13 + jl continue - mov cl, 1 + and cx, 0xffc0 + add cl, 1 - cmp dh, 0x1 xor dh, 0x1 jne continue @@ -41,7 +39,6 @@ continue: add di, 0x1 cmp di, 768 - mov ax, 0x201 jne loop_load ret diff --git a/src/kernel/kernel_entry.asm b/src/kernel/kernel_entry.asm index cc64f80..58b9a46 100644 --- a/src/kernel/kernel_entry.asm +++ b/src/kernel/kernel_entry.asm @@ -1,5 +1,5 @@ [BITS 32] -[extern _entry_point] +[extern entry_point] -call _entry_point +call entry_point jmp $ \ No newline at end of file diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 5f83773..4ba970f 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -26,11 +26,8 @@ switch_to_pm: [BITS 32] init_pm: - -xchg bx, bx mov ax, DATA_SEG mov ds, ax - mov ss, ax mov es, ax mov fs, ax @@ -39,9 +36,7 @@ xchg bx, bx mov ebp, 0x20000 mov esp, ebp - xchg bx, bx mov ebx, msg_pm_begin + 0x20000 - xchg bx, bx call print_d2vm call 0x20200 - 0x7c00 diff --git a/src/rm/io/print.asm b/src/rm/io/print.asm index 0514094..92cf3ab 100644 --- a/src/rm/io/print.asm +++ b/src/rm/io/print.asm @@ -5,9 +5,7 @@ [BITS 16] print: - push si - push ax - push bx + pusha print_loop: lodsb @@ -19,7 +17,5 @@ print_loop: jmp print_loop done_print: - pop bx - pop ax - pop si + popa ret \ No newline at end of file From 8f406d38a3227f0a45e6005193dcc6ce34ac28e7 Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 3 Nov 2023 21:46:26 +0700 Subject: [PATCH 43/88] kernel: printf --- src/kernel/kernel.c | 256 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 252 insertions(+), 4 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 3a9e337..01f828f 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,12 +1,260 @@ -char hell_yeh() +#define VGA_START 0xb8000 +#define va_arg(list, mode) ((mode *)(list = (char *)list + sizeof(mode)))[-1] + +enum color { + BLACK, + BLUE, + GREEN, + CYAN, + RED, + PURPLE, + BROWN, + GRAY, + DARK_GRAY, + LIGHT_BLUE, + LIGHT_GREEN, + LIGHT_CYAN, + LIGHT_RED, + LIGHT_PURPLE, + YELLOW, + WHITE, +}; + +// utils +void vga_clear_screen() { + for(int shift_x = 0; shift_x < 80; ++shift_x) { + for(int shift_y = 0; shift_y < 25; ++shift_y) { + vga_print_char(0, shift_x, shift_y, BLACK); + } + } +} + +void vga_print_char(char symbol, int x, int y, int foreground_color) { + if (x >= 80 || y >= 25) return; + *((char*) (VGA_START + 2 * (y * 80 + x))) = symbol; + *((char*) (VGA_START + 2 * (y * 80 + x)) + 1) = ((char) foreground_color); +} + +void vga_print_string(char* str, int x, int y, int text_color) { + int current_x = x, current_y = y; + int shift = 0; + while(*(str + shift) != '\0') { + vga_print_char(*(str + shift), current_x, current_y, text_color); + ++current_x; + if (current_x >= 80) { + current_x &= 0; + ++current_y; + } + ++shift; + } +} + +void shift_up() { + for (int i = 1; i < 25; i++) + { + for (int j = 0; j < 80; j++) + { + *((char*) (VGA_START + 2 * ((i - 1) * 80 + j))) = *((char*) (VGA_START + 2 * (i * 80 + j))); + } + } +} + + +// std + +struct va_list { - return 'Y'; + int *current_pos; +}; + +struct va_list va_start(int *start_pos) { + struct va_list list; + list.current_pos = start_pos + 1; + return list; +} + + +// compare [first, first_end) and second (until '\0') +char string_compare(char *first, char *first_end, char *second) { + for (int i = 0; (first + i) < first_end; i++) + { + if (*(second + i) == '\0') return 0; + if (*(second + i) != *(first + i)) return 0; + } + return 1; +} + +void str_reverse(char *str_start, char *str_end) { + int len = (str_end - str_start + 1) / 2; + for (int i = 0; i < len; i++) + { + char buf = *(str_start + i); + *(str_start + i) = *(str_end - i); + *(str_end - i) = buf; + } +} + + +// std (w/env = current_x, current_y) +int current_x, current_y; + +void init_printer() { + current_x = 0, current_y = 0; + vga_clear_screen(); +} + +void print_char(char s, int color) { + vga_print_char(s, current_x, current_y, color); + ++current_x; + if (current_x == 80) { + end_line(); + } +} + +void print_string(char *str, int color) { + while (*str) { + print_char(*str, color); + ++str; + } +} + +void end_line() { + if (current_y > 25) { + shift_up(); + } else { + ++current_y; + } + current_x = 0; +} + +void printf(char *fmt, ...) { + char **a = &fmt + 1; + for (char* i = fmt; *i != '\0'; i++) + { + if (*i == '%') { + if (*(i + 1) == '%') { + print_char('%', WHITE); + ++i; + } else { + char mode; + int color = WHITE; + + // mode select + mode = *(i + 1); + ++i; + + // color select (iff defined) + if (*(i + 1) == '_') { + i += 2; + char *start = i, *stop = i; + while (*i != '!') { + if (*i == '\0') return; + stop = i; + ++i; + } + + if (start != stop) { + char *color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" + "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" + "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; + int color_index[16] = {BLACK, BLUE, GREEN, CYAN, RED, PURPLE, + BROWN, GRAY, DARK_GRAY, LIGHT_BLUE, LIGHT_GREEN, LIGHT_CYAN, + LIGHT_RED, LIGHT_PURPLE, YELLOW, WHITE}; + char *cur_color = color_names; + for (int i = 0; i < 16; ++i) { + if (string_compare(start, stop, cur_color)) { + color = color_index[i]; + break; + } + while (*cur_color != '\0') { + ++cur_color; + } + ++cur_color; + } + } + } + + // print + if (mode == 's') { + char *b = va_arg(a, char*); + print_string(b, color); + } else if (mode == 'd') { + int b = va_arg(a, int); + char buffer[12] = "\0\0\0\0\0\0\0\0\0\0\0"; // (-)xxxxxxxxxx\0 + int bc = b; + + int counter = 0; + + if (bc < 0) { + bc *= -1; + } + while (bc != 0) { + buffer[counter++] = bc % 10 + 48; + bc /= 10; + } + + if (b < 0) { + buffer[counter] = '-'; + } else { + counter--; + } + + str_reverse(buffer, buffer + counter); + print_string(buffer, color); + } else if (mode == 'x') { + int b = va_arg(a, int); + print_string("0x", color); + + int bc = b; + } else if (mode == 'b') { + int b = va_arg(a, int); + print_string("0b", color); + + int bc = b; + } else { + end_line(); + print_string("UNSUPPORTED FORMAT (", RED); + print_char(mode, RED); + print_string(")!", RED); + end_line(); + } + } + } else if (*i == '\n') { + vga_print_char('\n', current_x, current_y, BLACK); + end_line(); + } else { + print_char(*i, WHITE); + } + } + +} + +void sample(char *str, ...) { + // struct va_list s = va_start(str); + char **a = &str + 1; + // int a = 432432; + char *b = va_arg(a, char*); } void entry_point() { - char* video_memory = (char*) 0xb8000; - *video_memory = hell_yeh(); + + sample("adsa", "321", 3); + + char *sample = "Finnaly!\n"; + + vga_clear_screen(); + vga_print_char('B', 0, 0, CYAN); + for (int i = 0; *(sample + i) != '\0'; i++) + { + vga_print_char(*(sample + i), 4 + i, 4 + i, i + 1); + } + + vga_print_string(sample, 3, 3, LIGHT_PURPLE); + vga_clear_screen(); + + init_printer(); + printf("%s_PURPLE!asdasd\n%%%d", "nbv", -3242); while(1) {} } \ No newline at end of file From a7b0fda264ea5fdb1d2670dae7abf190eccd76bb Mon Sep 17 00:00:00 2001 From: qq Date: Tue, 7 Nov 2023 18:10:15 +0700 Subject: [PATCH 44/88] all: practice #4 --- Makefile | 2 +- src/kernel/kernel.c | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index e15e163..7ad1ca1 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(GCC) -m32 -fno-pie -ffreestanding -nostdlib -lgcc -c $< -o $@ + $(GCC) -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -lgcc -O0 -c $< -o $@ # -m32 $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 01f828f..a4b685e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -201,16 +201,49 @@ void printf(char *fmt, ...) { str_reverse(buffer, buffer + counter); print_string(buffer, color); - } else if (mode == 'x') { + } else if (mode == 'x' || mode == 'X') { int b = va_arg(a, int); print_string("0x", color); + int a = sizeof(int); + char buffer[5] = "\0\0\0\0"; int bc = b; + int counter = 0; + if (bc < 0) { + bc *= -1; + } + while (bc != 0) { + int temp = bc % 16; + char symbol; + if (temp > 9) { + if (mode == 'X') { + symbol = temp + 'A' - 10; + } else { + symbol = temp + 'a' - 10; + } + } else { + symbol = temp + 48; + } + buffer[counter++] = symbol; + bc >>= 4; + } + + str_reverse(buffer, buffer + counter - 1); + print_string(buffer, color); } else if (mode == 'b') { - int b = va_arg(a, int); + unsigned int b = (unsigned) va_arg(a, int); print_string("0b", color); + char buffer[32] = {0}; - int bc = b; + unsigned int bc = b; + int counter = 0; + while (bc != 0) { + buffer[counter++] = (char)((bc & 1) + '0'); + bc >>= 1; + } + + str_reverse(buffer, buffer + counter - 1); + print_string(buffer, color); } else { end_line(); print_string("UNSUPPORTED FORMAT (", RED); @@ -254,7 +287,7 @@ void entry_point() vga_clear_screen(); init_printer(); - printf("%s_PURPLE!asdasd\n%%%d", "nbv", -3242); + printf("%b", -20); while(1) {} } \ No newline at end of file From a27a4a6fe3406f5f8ce16b15c43d0de9f0b68022 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 8 Nov 2023 21:38:35 +0700 Subject: [PATCH 45/88] format kernel.c, link ASM label to C code link ASM label to C code: in progress --- Makefile | 25 ++-- src/kernel/kernel.c | 271 +++++++++++++++++++++------------------ src/pm/io/print_d2vm.asm | 9 +- src/pm/switch2pm.asm | 3 +- 4 files changed, 173 insertions(+), 135 deletions(-) diff --git a/Makefile b/Makefile index 7ad1ca1..c05574f 100644 --- a/Makefile +++ b/Makefile @@ -65,29 +65,36 @@ $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) # $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ +$(KERNEL_BUILD_DIR)/print_d2vm.asm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm + $(ASM) -f elf $< -o $@ + $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(ASM) -f elf $< -o $@ $(KERNEL_O): $(KERNEL_C) $(info [MAKE] building: $@) - $(GCC) -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -lgcc -O0 -c $< -o $@ + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -O0 -fno-stack-protector -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) $(KERNEL_BUILD_DIR)/print_d2vm.asm.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ -$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) ./src/rm/io/print.asm ./src/rm/io/print_hex.asm ./src/bootloader/read_floppy_disk.asm ./src/rm/misc/accumulate_sum.asm ./src/pm/switch2pm.asm ./src/pm/gdt.asm +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ + $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ + $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ clean_boot: - $(RM) -f $(FLOPPY_IMG) - $(RM) -f $(KERNEL_ENTRY_O) - $(RM) -f $(KERNEL_O) - $(RM) -f $(KERNEL_TMP) - $(RM) -f $(KERNEL_BIN) - $(RM) -f $(BOOTLOADER_BIN) + $(RM) -f $(BOOTLOADER_BUILD_DIR)/* + $(RM) -f $(KERNEL_BUILD_DIR)/* +# $(RM) -f $(FLOPPY_IMG) +# $(RM) -f $(KERNEL_ENTRY_O) +# $(RM) -f $(KERNEL_O) +# $(RM) -f $(KERNEL_TMP) +# $(RM) -f $(KERNEL_BIN) +# $(RM) -f $(BOOTLOADER_BIN) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index a4b685e..e93b798 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,7 +1,10 @@ +extern void asm_print_d2vm(char* src); + #define VGA_START 0xb8000 -#define va_arg(list, mode) ((mode *)(list = (char *)list + sizeof(mode)))[-1] +#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] -enum color { +enum color +{ BLACK, BLUE, GREEN, @@ -21,35 +24,42 @@ enum color { }; // utils -void vga_clear_screen() { - for(int shift_x = 0; shift_x < 80; ++shift_x) { - for(int shift_y = 0; shift_y < 25; ++shift_y) { +void vga_print_char(char symbol, int x, int y, int foreground_color) +{ + if (x >= 80 || y >= 25) return; + *((char *)(VGA_START + 2 * (y * 80 + x))) = symbol; + *((char *)(VGA_START + 2 * (y * 80 + x)) + 1) = ((char)foreground_color); +} + +void vga_clear_screen() +{ + for (int shift_x = 0; shift_x < 80; ++shift_x) + { + for (int shift_y = 0; shift_y < 25; ++shift_y) + { vga_print_char(0, shift_x, shift_y, BLACK); } } } -void vga_print_char(char symbol, int x, int y, int foreground_color) { - if (x >= 80 || y >= 25) return; - *((char*) (VGA_START + 2 * (y * 80 + x))) = symbol; - *((char*) (VGA_START + 2 * (y * 80 + x)) + 1) = ((char) foreground_color); -} - -void vga_print_string(char* str, int x, int y, int text_color) { - int current_x = x, current_y = y; +void vga_print_string(char* str, int x, int y, int text_color) +{ + int current_x = x; + int current_y = y; int shift = 0; - while(*(str + shift) != '\0') { - vga_print_char(*(str + shift), current_x, current_y, text_color); - ++current_x; - if (current_x >= 80) { - current_x &= 0; + while (*(str + shift)) + { + vga_print_char(*(str + shift++), current_x++, current_y, text_color); + if (current_x >= 80) + { + current_x = 0; ++current_y; } - ++shift; } } -void shift_up() { +void shift_up() +{ for (int i = 1; i < 25; i++) { for (int j = 0; j < 80; j++) @@ -59,169 +69,182 @@ void shift_up() { } } - // std struct va_list { - int *current_pos; + int* current_pos; }; -struct va_list va_start(int *start_pos) { +struct va_list va_start(int* start_pos) +{ struct va_list list; list.current_pos = start_pos + 1; return list; } - // compare [first, first_end) and second (until '\0') -char string_compare(char *first, char *first_end, char *second) { - for (int i = 0; (first + i) < first_end; i++) - { - if (*(second + i) == '\0') return 0; - if (*(second + i) != *(first + i)) return 0; - } - return 1; +int string_compare(char* first, char* second, int n) +{ + int res = 0; + while(*first && *second && n-- && !(res = (*first++ - *second++))) {} + return !res; } -void str_reverse(char *str_start, char *str_end) { +void str_reverse(char* str_start, char* str_end) +{ int len = (str_end - str_start + 1) / 2; - for (int i = 0; i < len; i++) + for (int i = 0; i < len; ++i) { char buf = *(str_start + i); *(str_start + i) = *(str_end - i); *(str_end - i) = buf; - } + } } - // std (w/env = current_x, current_y) + int current_x, current_y; -void init_printer() { - current_x = 0, current_y = 0; +void init_printer() +{ + current_x = 0; + current_y = 0; vga_clear_screen(); } -void print_char(char s, int color) { +void end_line() +{ + if (current_y > 25) shift_up(); + else ++current_y; + current_x = 0; +} + +void print_char(char s, int color) +{ vga_print_char(s, current_x, current_y, color); ++current_x; - if (current_x == 80) { - end_line(); - } + if (current_x == 80) end_line(); } -void print_string(char *str, int color) { - while (*str) { +void print_string(char* str, int color) +{ + while (*str) + { print_char(*str, color); ++str; } } -void end_line() { - if (current_y > 25) { - shift_up(); - } else { - ++current_y; - } - current_x = 0; -} - -void printf(char *fmt, ...) { - char **a = &fmt + 1; - for (char* i = fmt; *i != '\0'; i++) +void printf(char* fmt, ...) +{ + char** a = &fmt + 1; + for (char* i = fmt; *i; ++i) { - if (*i == '%') { - if (*(i + 1) == '%') { + if (*i == '%') + { + if (*(i + 1) == '%') + { print_char('%', WHITE); ++i; - } else { + } + else + { char mode; int color = WHITE; // mode select - mode = *(i + 1); - ++i; + mode = *(i++ + 1); // color select (iff defined) - if (*(i + 1) == '_') { + if (*(i + 1) == '_') + { i += 2; - char *start = i, *stop = i; - while (*i != '!') { - if (*i == '\0') return; - stop = i; - ++i; + char* start = i; + char* stop = i; + while (*i != '!') + { + if (!*i) return; + stop = i++; } - if (start != stop) { - char *color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" + if (start != stop) + { + char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; - int color_index[16] = {BLACK, BLUE, GREEN, CYAN, RED, PURPLE, - BROWN, GRAY, DARK_GRAY, LIGHT_BLUE, LIGHT_GREEN, LIGHT_CYAN, - LIGHT_RED, LIGHT_PURPLE, YELLOW, WHITE}; - char *cur_color = color_names; - for (int i = 0; i < 16; ++i) { - if (string_compare(start, stop, cur_color)) { + int color_index[16] = { BLACK, BLUE, GREEN, CYAN, RED, PURPLE, + BROWN, GRAY, DARK_GRAY, LIGHT_BLUE, LIGHT_GREEN, LIGHT_CYAN, + LIGHT_RED, LIGHT_PURPLE, YELLOW, WHITE }; + char* cur_color = color_names; + for (int i = 0; i < 16; ++i) + { + if (string_compare(start, cur_color, stop - start)) + { color = color_index[i]; break; } - while (*cur_color != '\0') { - ++cur_color; - } - ++cur_color; + while (*cur_color++) {} } } } - + // print - if (mode == 's') { - char *b = va_arg(a, char*); + if (mode == 's') + { + char* b = va_arg(a, char*); print_string(b, color); - } else if (mode == 'd') { + } + else if (mode == 'd') + { int b = va_arg(a, int); - char buffer[12] = "\0\0\0\0\0\0\0\0\0\0\0"; // (-)xxxxxxxxxx\0 + char buffer[12] = {0}; // (-)xxxxxxxxxx\0 int bc = b; int counter = 0; - if (bc < 0) { - bc *= -1; - } - while (bc != 0) { + if (bc < 0) bc *= -1; + while (bc) + { buffer[counter++] = bc % 10 + 48; bc /= 10; } - if (b < 0) { - buffer[counter] = '-'; - } else { - counter--; - } + if (b < 0) buffer[counter] = '-'; + else counter--; str_reverse(buffer, buffer + counter); print_string(buffer, color); - } else if (mode == 'x' || mode == 'X') { + } + else if (mode == 'x' || mode == 'X') + { int b = va_arg(a, int); print_string("0x", color); - int a = sizeof(int); - char buffer[5] = "\0\0\0\0"; + char buffer[5] = {0}; int bc = b; int counter = 0; - if (bc < 0) { + if (bc < 0) + { bc *= -1; } - while (bc != 0) { + while (bc) + { int temp = bc % 16; char symbol; - if (temp > 9) { - if (mode == 'X') { + if (temp > 9) + { + if (mode == 'X') + { symbol = temp + 'A' - 10; - } else { + } + else + { symbol = temp + 'a' - 10; } - } else { + } + else + { symbol = temp + 48; } buffer[counter++] = symbol; @@ -230,64 +253,64 @@ void printf(char *fmt, ...) { str_reverse(buffer, buffer + counter - 1); print_string(buffer, color); - } else if (mode == 'b') { + } + else if (mode == 'b') + { unsigned int b = (unsigned) va_arg(a, int); print_string("0b", color); char buffer[32] = {0}; unsigned int bc = b; int counter = 0; - while (bc != 0) { + while (bc) + { buffer[counter++] = (char)((bc & 1) + '0'); bc >>= 1; } str_reverse(buffer, buffer + counter - 1); print_string(buffer, color); - } else { + } + else + { end_line(); print_string("UNSUPPORTED FORMAT (", RED); print_char(mode, RED); print_string(")!", RED); end_line(); } - } - } else if (*i == '\n') { + } + } + else if (*i == '\n') + { vga_print_char('\n', current_x, current_y, BLACK); end_line(); - } else { + } + else + { print_char(*i, WHITE); } } - -} - -void sample(char *str, ...) { - // struct va_list s = va_start(str); - char **a = &str + 1; - // int a = 432432; - char *b = va_arg(a, char*); } void entry_point() { - - sample("adsa", "321", 3); - - char *sample = "Finnaly!\n"; + asm_print_d2vm("Hello from C!"); + + char* sample = "Finally!\n"; vga_clear_screen(); vga_print_char('B', 0, 0, CYAN); - for (int i = 0; *(sample + i) != '\0'; i++) + for (int i = 0; *(sample + i); ++i) { vga_print_char(*(sample + i), 4 + i, 4 + i, i + 1); } - + vga_print_string(sample, 3, 3, LIGHT_PURPLE); vga_clear_screen(); init_printer(); - printf("%b", -20); - - while(1) {} + printf("%b %b_RED!hello world!", 20, 165); + + while (1) {} } \ No newline at end of file diff --git a/src/pm/io/print_d2vm.asm b/src/pm/io/print_d2vm.asm index 3b849ba..833045d 100644 --- a/src/pm/io/print_d2vm.asm +++ b/src/pm/io/print_d2vm.asm @@ -2,6 +2,8 @@ %define PRINT_D2VM %endif +[global asm_print_d2vm] + ; Printing directly to video memory [BITS 32] @@ -9,7 +11,10 @@ VIDEO_MEMORY equ 0xb8000 WHITE_ON_BLACK equ 0x0f -print_d2vm: +asm_print_d2vm: + push ebp + mov ebp, esp + mov ebx, [esp + 2 * 4] pusha mov edx, VIDEO_MEMORY @@ -29,4 +34,6 @@ print_d2vm_loop: print_d2vm_done: popa + mov esp, ebp + pop ebp ret \ No newline at end of file diff --git a/src/pm/switch2pm.asm b/src/pm/switch2pm.asm index 4ba970f..5c1c5cb 100644 --- a/src/pm/switch2pm.asm +++ b/src/pm/switch2pm.asm @@ -37,7 +37,8 @@ init_pm: mov esp, ebp mov ebx, msg_pm_begin + 0x20000 - call print_d2vm + push ebx + call asm_print_d2vm call 0x20200 - 0x7c00 From c5fd46992e143b9609d405687042961f6b323d2e Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 10 Nov 2023 16:53:03 +0700 Subject: [PATCH 46/88] upd since passing --- src/kernel/kernel.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index e93b798..8164865 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -71,18 +71,6 @@ void shift_up() // std -struct va_list -{ - int* current_pos; -}; - -struct va_list va_start(int* start_pos) -{ - struct va_list list; - list.current_pos = start_pos + 1; - return list; -} - // compare [first, first_end) and second (until '\0') int string_compare(char* first, char* second, int n) { @@ -228,6 +216,7 @@ void printf(char* fmt, ...) { bc *= -1; } + vga_print_char('\n', current_x, current_y, BLACK); while (bc) { int temp = bc % 16; @@ -295,8 +284,6 @@ void printf(char* fmt, ...) void entry_point() { - asm_print_d2vm("Hello from C!"); - char* sample = "Finally!\n"; vga_clear_screen(); @@ -311,6 +298,8 @@ void entry_point() init_printer(); printf("%b %b_RED!hello world!", 20, 165); + + asm_print_d2vm("Hello from C!"); while (1) {} } \ No newline at end of file From a515f61e9cee2d42f4885a4686222a0912929a3f Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 10 Nov 2023 17:41:41 +0700 Subject: [PATCH 47/88] printf: review 1 --- src/kernel/kernel.c | 246 +---------------------------------------- src/kernel/mem_utils.c | 37 +++++++ src/kernel/mem_utils.h | 3 + src/kernel/printf.c | 232 ++++++++++++++++++++++++++++++++++++++ src/kernel/printf.h | 40 +++++++ 5 files changed, 314 insertions(+), 244 deletions(-) create mode 100644 src/kernel/mem_utils.c create mode 100644 src/kernel/mem_utils.h create mode 100644 src/kernel/printf.c create mode 100644 src/kernel/printf.h diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index e93b798..483babc 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,62 +1,7 @@ extern void asm_print_d2vm(char* src); -#define VGA_START 0xb8000 -#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] - -enum color -{ - BLACK, - BLUE, - GREEN, - CYAN, - RED, - PURPLE, - BROWN, - GRAY, - DARK_GRAY, - LIGHT_BLUE, - LIGHT_GREEN, - LIGHT_CYAN, - LIGHT_RED, - LIGHT_PURPLE, - YELLOW, - WHITE, -}; - -// utils -void vga_print_char(char symbol, int x, int y, int foreground_color) -{ - if (x >= 80 || y >= 25) return; - *((char *)(VGA_START + 2 * (y * 80 + x))) = symbol; - *((char *)(VGA_START + 2 * (y * 80 + x)) + 1) = ((char)foreground_color); -} - -void vga_clear_screen() -{ - for (int shift_x = 0; shift_x < 80; ++shift_x) - { - for (int shift_y = 0; shift_y < 25; ++shift_y) - { - vga_print_char(0, shift_x, shift_y, BLACK); - } - } -} - -void vga_print_string(char* str, int x, int y, int text_color) -{ - int current_x = x; - int current_y = y; - int shift = 0; - while (*(str + shift)) - { - vga_print_char(*(str + shift++), current_x++, current_y, text_color); - if (current_x >= 80) - { - current_x = 0; - ++current_y; - } - } -} +#include "printf.h" +#include "mem_utils.h" void shift_up() { @@ -104,194 +49,7 @@ void str_reverse(char* str_start, char* str_end) // std (w/env = current_x, current_y) -int current_x, current_y; - -void init_printer() -{ - current_x = 0; - current_y = 0; - vga_clear_screen(); -} - -void end_line() -{ - if (current_y > 25) shift_up(); - else ++current_y; - current_x = 0; -} -void print_char(char s, int color) -{ - vga_print_char(s, current_x, current_y, color); - ++current_x; - if (current_x == 80) end_line(); -} - -void print_string(char* str, int color) -{ - while (*str) - { - print_char(*str, color); - ++str; - } -} - -void printf(char* fmt, ...) -{ - char** a = &fmt + 1; - for (char* i = fmt; *i; ++i) - { - if (*i == '%') - { - if (*(i + 1) == '%') - { - print_char('%', WHITE); - ++i; - } - else - { - char mode; - int color = WHITE; - - // mode select - mode = *(i++ + 1); - - // color select (iff defined) - if (*(i + 1) == '_') - { - i += 2; - char* start = i; - char* stop = i; - while (*i != '!') - { - if (!*i) return; - stop = i++; - } - - if (start != stop) - { - char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" - "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" - "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; - int color_index[16] = { BLACK, BLUE, GREEN, CYAN, RED, PURPLE, - BROWN, GRAY, DARK_GRAY, LIGHT_BLUE, LIGHT_GREEN, LIGHT_CYAN, - LIGHT_RED, LIGHT_PURPLE, YELLOW, WHITE }; - char* cur_color = color_names; - for (int i = 0; i < 16; ++i) - { - if (string_compare(start, cur_color, stop - start)) - { - color = color_index[i]; - break; - } - while (*cur_color++) {} - } - } - } - - // print - if (mode == 's') - { - char* b = va_arg(a, char*); - print_string(b, color); - } - else if (mode == 'd') - { - int b = va_arg(a, int); - char buffer[12] = {0}; // (-)xxxxxxxxxx\0 - int bc = b; - - int counter = 0; - - if (bc < 0) bc *= -1; - while (bc) - { - buffer[counter++] = bc % 10 + 48; - bc /= 10; - } - - if (b < 0) buffer[counter] = '-'; - else counter--; - - str_reverse(buffer, buffer + counter); - print_string(buffer, color); - } - else if (mode == 'x' || mode == 'X') - { - int b = va_arg(a, int); - print_string("0x", color); - char buffer[5] = {0}; - - int bc = b; - int counter = 0; - if (bc < 0) - { - bc *= -1; - } - while (bc) - { - int temp = bc % 16; - char symbol; - if (temp > 9) - { - if (mode == 'X') - { - symbol = temp + 'A' - 10; - } - else - { - symbol = temp + 'a' - 10; - } - } - else - { - symbol = temp + 48; - } - buffer[counter++] = symbol; - bc >>= 4; - } - - str_reverse(buffer, buffer + counter - 1); - print_string(buffer, color); - } - else if (mode == 'b') - { - unsigned int b = (unsigned) va_arg(a, int); - print_string("0b", color); - char buffer[32] = {0}; - - unsigned int bc = b; - int counter = 0; - while (bc) - { - buffer[counter++] = (char)((bc & 1) + '0'); - bc >>= 1; - } - - str_reverse(buffer, buffer + counter - 1); - print_string(buffer, color); - } - else - { - end_line(); - print_string("UNSUPPORTED FORMAT (", RED); - print_char(mode, RED); - print_string(")!", RED); - end_line(); - } - } - } - else if (*i == '\n') - { - vga_print_char('\n', current_x, current_y, BLACK); - end_line(); - } - else - { - print_char(*i, WHITE); - } - } -} void entry_point() { diff --git a/src/kernel/mem_utils.c b/src/kernel/mem_utils.c new file mode 100644 index 0000000..8073e91 --- /dev/null +++ b/src/kernel/mem_utils.c @@ -0,0 +1,37 @@ +#include "mem_utils.h" + +void* memcpy(void* dst, const void* src, int n) +{ + int m; + unsigned long *wdst = dst; // текущая позиция в буфере назначения + unsigned long *wsrc = src; // текущая позиция в источнике + unsigned char *cdst, *csrc; + + for(int i = 0, m = n / sizeof(long); i < m; i++) // копируем основную часть блоками по 4 или 8 байт + *(wdst++) = *(wsrc++); // (в зависимости от платформы) + + cdst = (unsigned char*)wdst; + csrc = (unsigned char*)wsrc; + + for(int i = 0, m = n % sizeof(long); i < m; i++) // остаток копируем побайтно + *(cdst++) = *(csrc++); + + return dst; +} + +void* memzero(void* dst, int n) +{ + int m; + unsigned long *wdst = dst; // текущая позиция в буфере назначения + unsigned char *cdst, *csrc; + + for(int i = 0, m = n / sizeof(long); i < m; i++) // копируем основную часть блоками по 4 или 8 байт + *(wdst++) = 0; // (в зависимости от платформы) + + cdst = (unsigned char*)wdst; + + for(int i = 0, m = n % sizeof(long); i < m; i++) // остаток копируем побайтно + *(cdst++) = 0; + + return dst; +} \ No newline at end of file diff --git a/src/kernel/mem_utils.h b/src/kernel/mem_utils.h new file mode 100644 index 0000000..1ecdeb1 --- /dev/null +++ b/src/kernel/mem_utils.h @@ -0,0 +1,3 @@ +void* memcpy(void* dst, const void* src, int n); + +void* memzero(void* dst, int n); \ No newline at end of file diff --git a/src/kernel/printf.c b/src/kernel/printf.c new file mode 100644 index 0000000..ca5a6b8 --- /dev/null +++ b/src/kernel/printf.c @@ -0,0 +1,232 @@ +#include "printf.h" + +static current_x; +static current_y; + +void vga_print_char(char symbol, int x, int y, int foreground_color) +{ + if (x >= 80 || y >= 25) return; + *((char *)(VGA_START + 2 * (y * 80 + x))) = symbol; + *((char *)(VGA_START + 2 * (y * 80 + x)) + 1) = ((char)foreground_color); +} + +void vga_clear_screen() +{ + for (int shift_x = 0; shift_x < 80; ++shift_x) + { + for (int shift_y = 0; shift_y < 25; ++shift_y) + { + vga_print_char(0, shift_x, shift_y, BLACK); + } + } +} + +void vga_print_string(char* str, int x, int y, int text_color) +{ + int current_x = x; + int current_y = y; + int shift = 0; + while (str[shift]) + { + vga_print_char(str[shift++], current_x++, current_y, text_color); + if (current_x >= 80) + { + end_line(); + } + } +} + +void shift_up() +{ + for (int i = 1; i < 25; i++) + { + for (int j = 0; j < 80; j++) + { + *((char*) (VGA_START + 2 * ((i - 1) * 80 + j))) = *((char*) (VGA_START + 2 * (i * 80 + j))); + } + } +} + +void init_printer() +{ + current_x = 0; + current_y = 0; + vga_clear_screen(); +} + +void end_line() +{ + if (current_y > 25) shift_up(); + else ++current_y; + current_x = 0; +} + +void print_char(char s, int color) +{ + vga_print_char(s, current_x, current_y, color); + ++current_x; + if (current_x == 80) end_line(); +} + +void print_string(char* str, int color) +{ + while (*str) + { + print_char(*str, color); + ++str; + } +} + +void printf(char* fmt, ...) +{ + char** a = &fmt + 1; + for (char* i = fmt; *i; ++i) + { + if (*i == '%') + { + if (*(i + 1) == '%') + { + print_char('%', WHITE); + ++i; + } + else + { + char mode; + int color = WHITE; + + // mode select + mode = *(i++ + 1); + + // color select (iff defined) + if (*(i + 1) == '_') + { + i += 2; + char* start = i; + char* stop = i; + while (*i != '!') + { + if (!*i) return; + stop = i++; + } + + if (start != stop) + { + char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" + "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" + "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; + char* cur_color = color_names; + for (int i = 0; i < 16; ++i) + { + if (string_compare(start, cur_color, stop - start)) + { + color = i; + break; + } + while (*cur_color++) {} + } + } + } + + // print + if (mode == 's') + { + char* b = va_arg(a, char*); + print_string(b, color); + } + else if (mode == 'd') + { + int b = va_arg(a, int); + char buffer[12] = {0}; // (-)xxxxxxxxxx\0 + int bc = b; + + int counter = 0; + + if (bc < 0) bc *= -1; + while (bc) + { + buffer[counter++] = bc % 10 + 48; + bc /= 10; + } + + if (b < 0) buffer[counter] = '-'; + else counter--; + + str_reverse(buffer, buffer + counter); + print_string(buffer, color); + } + else if (mode == 'x' || mode == 'X') + { + int b = va_arg(a, int); + print_string("0x", color); + char buffer[5] = {0}; + + int bc = b; + int counter = 0; + if (bc < 0) + { + bc *= -1; + } + while (bc) + { + int temp = bc % 16; + char symbol; + if (temp > 9) + { + if (mode == 'X') + { + symbol = temp + 'A' - 10; + } + else + { + symbol = temp + 'a' - 10; + } + } + else + { + symbol = temp + 48; + } + buffer[counter++] = symbol; + bc >>= 4; + } + + str_reverse(buffer, buffer + counter - 1); + print_string(buffer, color); + } + else if (mode == 'b') + { + unsigned int b = (unsigned) va_arg(a, int); + print_string("0b", color); + char buffer[32] = {0}; + + unsigned int bc = b; + int counter = 0; + while (bc) + { + buffer[counter++] = (char)((bc & 1) + '0'); + bc >>= 1; + } + + str_reverse(buffer, buffer + counter - 1); + print_string(buffer, color); + } + else + { + end_line(); + print_string("UNSUPPORTED FORMAT (", RED); + print_char(mode, RED); + print_string(")!", RED); + end_line(); + } + } + } + else if (*i == '\n') + { + vga_print_char('\n', current_x, current_y, BLACK); + end_line(); + } + else + { + print_char(*i, WHITE); + } + } +} \ No newline at end of file diff --git a/src/kernel/printf.h b/src/kernel/printf.h new file mode 100644 index 0000000..c8cdf14 --- /dev/null +++ b/src/kernel/printf.h @@ -0,0 +1,40 @@ +#define VGA_START 0xb8000 +#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] + +enum color +{ + BLACK, + BLUE, + GREEN, + CYAN, + RED, + PURPLE, + BROWN, + GRAY, + DARK_GRAY, + LIGHT_BLUE, + LIGHT_GREEN, + LIGHT_CYAN, + LIGHT_RED, + LIGHT_PURPLE, + YELLOW, + WHITE, +}; + +void vga_print_char(char symbol, int x, int y, int foreground_color); + +void vga_clear_screen(); + +void vga_print_string(char* str, int x, int y, int text_color); + +void shift_up(); + +void init_printer(); + +void end_line(); + +void print_char(char s, int color); + +void print_string(char* str, int color); + +void printf(char *fmt, ...); \ No newline at end of file From a4de9365ea42a0b81ab0b69ff4b150a0ab9a8978 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 10 Nov 2023 21:13:58 +0700 Subject: [PATCH 48/88] upd --- .gitignore | 4 ++- Makefile | 42 ++++++++++++++++--------- src/kernel/kernel.c | 15 ++------- src/kernel/sys/io/io.h | 3 ++ src/kernel/{ => sys/io}/printf.c | 0 src/kernel/{ => sys/io}/printf.h | 14 +++------ src/kernel/{ => sys/memory}/mem_utils.c | 0 src/kernel/{ => sys/memory}/mem_utils.h | 3 +- src/kernel/sys/memory/memory.h | 3 ++ 9 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 src/kernel/sys/io/io.h rename src/kernel/{ => sys/io}/printf.c (100%) rename src/kernel/{ => sys/io}/printf.h (79%) rename src/kernel/{ => sys/memory}/mem_utils.c (100%) rename src/kernel/{ => sys/memory}/mem_utils.h (86%) create mode 100644 src/kernel/sys/memory/memory.h diff --git a/.gitignore b/.gitignore index 9e9ee66..2209cb3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ build/* test.* !.vscode/ -!.vscode/*.* \ No newline at end of file +!.vscode/*.* +.vscode/c_cpp_properties.json +.vscode/settings.json diff --git a/Makefile b/Makefile index c05574f..8566c62 100644 --- a/Makefile +++ b/Makefile @@ -57,26 +57,33 @@ floppy: bootloader kernel $(FLOPPY_IMG) kernel: $(KERNEL_BIN) bootloader: $(BOOTLOADER_BIN) -$(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) +$(BUILD_DIR)/pm/io/print_d2vm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) - $(DD) bs=512 count=2880 if=/dev/zero of=$@ -# $(MKFS) -t fat -F 12 -n "UNOS" $@ - $(DD) conv=notrunc if=$< of=$@ -# $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" - $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ - -$(KERNEL_BUILD_DIR)/print_d2vm.asm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm $(ASM) -f elf $< -o $@ +$(KERNEL_BUILD_DIR)/sys/io/printf.o: $(KERNEL_SOURCE_DIR)/sys/io/printf.c \ + $(KERNEL_SOURCE_DIR)/sys/io/printf.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o: $(KERNEL_SOURCE_DIR)/sys/memory/mem_utils.c \ + $(KERNEL_SOURCE_DIR)/sys/memory/mem_utils.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) + $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_O): $(KERNEL_C) +$(KERNEL_O): $(KERNEL_C) \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(info [MAKE] building: $@) - $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -O0 -fno-stack-protector -c $< -o $@ + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) $(KERNEL_BUILD_DIR)/print_d2vm.asm.o +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ + $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/io/printf.o \ + $(BUILD_DIR)/pm/io/print_d2vm.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) @@ -84,14 +91,21 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) $(KERNEL_BUILD_DIR)/print_d2vm.asm. $(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ - $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm + $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm \ $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ +$(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) + $(info [MAKE] building: $@) + $(DD) bs=512 count=2880 if=/dev/zero of=$@ +# $(MKFS) -t fat -F 12 -n "UNOS" $@ + $(DD) conv=notrunc if=$< of=$@ +# $(MCOPY) -i $@ $(word 2,$^) "::kernel.bin" + $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ + clean_boot: - $(RM) -f $(BOOTLOADER_BUILD_DIR)/* - $(RM) -f $(KERNEL_BUILD_DIR)/* +# $(info [MAKE] do $@:) # $(RM) -f $(FLOPPY_IMG) # $(RM) -f $(KERNEL_ENTRY_O) # $(RM) -f $(KERNEL_O) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 815356b..5ac8728 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,18 +1,7 @@ extern void asm_print_d2vm(char* src); -#include "printf.h" -#include "mem_utils.h" - -void shift_up() -{ - for (int i = 1; i < 25; i++) - { - for (int j = 0; j < 80; j++) - { - *((char*) (VGA_START + 2 * ((i - 1) * 80 + j))) = *((char*) (VGA_START + 2 * (i * 80 + j))); - } - } -} +#include "./sys/io/io.h" +#include "./sys/memory/memory.h" // std diff --git a/src/kernel/sys/io/io.h b/src/kernel/sys/io/io.h new file mode 100644 index 0000000..e4b2ef1 --- /dev/null +++ b/src/kernel/sys/io/io.h @@ -0,0 +1,3 @@ +#pragma once + +#include "./printf.h" \ No newline at end of file diff --git a/src/kernel/printf.c b/src/kernel/sys/io/printf.c similarity index 100% rename from src/kernel/printf.c rename to src/kernel/sys/io/printf.c diff --git a/src/kernel/printf.h b/src/kernel/sys/io/printf.h similarity index 79% rename from src/kernel/printf.h rename to src/kernel/sys/io/printf.h index c8cdf14..f9865a3 100644 --- a/src/kernel/printf.h +++ b/src/kernel/sys/io/printf.h @@ -1,5 +1,7 @@ -#define VGA_START 0xb8000 -#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] +#pragma once + +#define VGA_START 0xb8000 +#define va_arg(list, type) ( ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] ) enum color { @@ -22,19 +24,11 @@ enum color }; void vga_print_char(char symbol, int x, int y, int foreground_color); - void vga_clear_screen(); - void vga_print_string(char* str, int x, int y, int text_color); - void shift_up(); - void init_printer(); - void end_line(); - void print_char(char s, int color); - void print_string(char* str, int color); - void printf(char *fmt, ...); \ No newline at end of file diff --git a/src/kernel/mem_utils.c b/src/kernel/sys/memory/mem_utils.c similarity index 100% rename from src/kernel/mem_utils.c rename to src/kernel/sys/memory/mem_utils.c diff --git a/src/kernel/mem_utils.h b/src/kernel/sys/memory/mem_utils.h similarity index 86% rename from src/kernel/mem_utils.h rename to src/kernel/sys/memory/mem_utils.h index 1ecdeb1..8e026b2 100644 --- a/src/kernel/mem_utils.h +++ b/src/kernel/sys/memory/mem_utils.h @@ -1,3 +1,4 @@ -void* memcpy(void* dst, const void* src, int n); +#pragma once +void* memcpy(void* dst, const void* src, int n); void* memzero(void* dst, int n); \ No newline at end of file diff --git a/src/kernel/sys/memory/memory.h b/src/kernel/sys/memory/memory.h new file mode 100644 index 0000000..e1b98f4 --- /dev/null +++ b/src/kernel/sys/memory/memory.h @@ -0,0 +1,3 @@ +#pragma once + +#include "./mem_utils.h" \ No newline at end of file From 143459b4b207974013b10e4139f233cd27e095fc Mon Sep 17 00:00:00 2001 From: qq Date: Fri, 10 Nov 2023 22:46:11 +0700 Subject: [PATCH 49/88] printf: review 1 --- src/kernel/kernel.c | 25 +----- src/kernel/sys/io/printf.c | 171 ++++++++++++------------------------- src/kernel/sys/io/printf.h | 2 + 3 files changed, 59 insertions(+), 139 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 5ac8728..98713c6 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,15 +1,13 @@ -extern void asm_print_d2vm(char* src); +// extern void asm_print_d2vm(char* src); #include "./sys/io/io.h" #include "./sys/memory/memory.h" -// std - // compare [first, first_end) and second (until '\0') int string_compare(char* first, char* second, int n) { int res = 0; - while(*first && *second && n-- && !(res = (*first++ - *second++))) {} + while(*first && *second && n-- && !(res = (*first++ - *second++))); return !res; } @@ -24,28 +22,13 @@ void str_reverse(char* str_start, char* str_end) } } -// std (w/env = current_x, current_y) - - void entry_point() { - char* sample = "Finally!\n"; - - vga_clear_screen(); - vga_print_char('B', 0, 0, CYAN); - for (int i = 0; *(sample + i); ++i) - { - vga_print_char(*(sample + i), 4 + i, 4 + i, i + 1); - } - - vga_print_string(sample, 3, 3, LIGHT_PURPLE); - vga_clear_screen(); - init_printer(); - printf("%b %b_RED!hello world!", 20, 165); + printf("%b %b_RED!hello world!", 20, -165); - asm_print_d2vm("Hello from C!"); + // asm_print_d2vm("Hello from C!"); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index ca5a6b8..7f6147f 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -3,94 +3,65 @@ static current_x; static current_y; -void vga_print_char(char symbol, int x, int y, int foreground_color) -{ +void vga_print_char(char symbol, int x, int y, int foreground_color) { if (x >= 80 || y >= 25) return; - *((char *)(VGA_START + 2 * (y * 80 + x))) = symbol; - *((char *)(VGA_START + 2 * (y * 80 + x)) + 1) = ((char)foreground_color); + *((short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; } -void vga_clear_screen() -{ - for (int shift_x = 0; shift_x < 80; ++shift_x) - { - for (int shift_y = 0; shift_y < 25; ++shift_y) - { - vga_print_char(0, shift_x, shift_y, BLACK); - } - } +void vga_clear_screen() { + memzero(VGA_START, sizeof(short) * 25 * 80); } -void vga_print_string(char* str, int x, int y, int text_color) -{ +void vga_print_string(char* str, int x, int y, int text_color) { int current_x = x; int current_y = y; int shift = 0; - while (str[shift]) - { + while (str[shift]) { vga_print_char(str[shift++], current_x++, current_y, text_color); - if (current_x >= 80) - { + if (current_x >= 80) { end_line(); } } } -void shift_up() -{ - for (int i = 1; i < 25; i++) - { - for (int j = 0; j < 80; j++) - { - *((char*) (VGA_START + 2 * ((i - 1) * 80 + j))) = *((char*) (VGA_START + 2 * (i * 80 + j))); - } - } +void shift_up() { + memcpy(VGA_START, VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); + memzero(VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); } -void init_printer() -{ +void init_printer() { current_x = 0; current_y = 0; vga_clear_screen(); } -void end_line() -{ +void end_line() { if (current_y > 25) shift_up(); else ++current_y; current_x = 0; } -void print_char(char s, int color) -{ +void print_char(char s, int color) { vga_print_char(s, current_x, current_y, color); ++current_x; if (current_x == 80) end_line(); } -void print_string(char* str, int color) -{ - while (*str) - { +void print_string(char* str, int color) { + while (*str) { print_char(*str, color); ++str; } } -void printf(char* fmt, ...) -{ +void printf(char* fmt, ...) { char** a = &fmt + 1; - for (char* i = fmt; *i; ++i) - { - if (*i == '%') - { - if (*(i + 1) == '%') - { + for (char* i = fmt; *i; ++i) { + if (*i == '%') { + if (*(i + 1) == '%') { print_char('%', WHITE); ++i; - } - else - { + } else { char mode; int color = WHITE; @@ -98,119 +69,88 @@ void printf(char* fmt, ...) mode = *(i++ + 1); // color select (iff defined) - if (*(i + 1) == '_') - { + if (*(i + 1) == '_') { i += 2; char* start = i; char* stop = i; - while (*i != '!') - { + while (*i != '!') { if (!*i) return; stop = i++; } - if (start != stop) - { + if (start != stop) { char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; char* cur_color = color_names; - for (int i = 0; i < 16; ++i) - { - if (string_compare(start, cur_color, stop - start)) - { + for (int i = 0; i < 16; ++i) { + if (string_compare(start, cur_color, stop - start)) { color = i; break; } - while (*cur_color++) {} + while (*cur_color++); } } } // print - if (mode == 's') - { + if (mode == 's') { char* b = va_arg(a, char*); print_string(b, color); - } - else if (mode == 'd') - { + } else if (mode == 'd') { int b = va_arg(a, int); char buffer[12] = {0}; // (-)xxxxxxxxxx\0 int bc = b; - int counter = 0; + int counter = 10; if (bc < 0) bc *= -1; - while (bc) - { - buffer[counter++] = bc % 10 + 48; + while (bc) { + buffer[counter--] = bc % 10 + 48; bc /= 10; } if (b < 0) buffer[counter] = '-'; - else counter--; + else counter++; - str_reverse(buffer, buffer + counter); - print_string(buffer, color); - } - else if (mode == 'x' || mode == 'X') - { + print_string(buffer + counter, color); + } else if (mode == 'x' || mode == 'X') { int b = va_arg(a, int); print_string("0x", color); char buffer[5] = {0}; int bc = b; - int counter = 0; - if (bc < 0) - { + int counter = 3; + if (bc < 0) { bc *= -1; } - while (bc) - { + while (bc) { int temp = bc % 16; char symbol; - if (temp > 9) - { - if (mode == 'X') - { - symbol = temp + 'A' - 10; - } - else - { - symbol = temp + 'a' - 10; - } + if (temp > 9) { + symbol = temp - 10 + (mode == 'X') ? 'A' : 'a'; + } else { + symbol = temp + '0'; } - else - { - symbol = temp + 48; - } - buffer[counter++] = symbol; + buffer[counter--] = symbol; bc >>= 4; } - str_reverse(buffer, buffer + counter - 1); - print_string(buffer, color); - } - else if (mode == 'b') - { - unsigned int b = (unsigned) va_arg(a, int); + print_string(buffer + counter + 1, color); + } else if (mode == 'b') { + unsigned int b = (unsigned int) va_arg(a, int); print_string("0b", color); - char buffer[32] = {0}; + char buffer[33] = {0}; unsigned int bc = b; - int counter = 0; - while (bc) - { - buffer[counter++] = (char)((bc & 1) + '0'); + int counter = 31; + while (bc) { + buffer[counter--] = (char)((bc & 1) + '0'); bc >>= 1; } - str_reverse(buffer, buffer + counter - 1); - print_string(buffer, color); - } - else - { + print_string(buffer + counter + 1, color); + } else { end_line(); print_string("UNSUPPORTED FORMAT (", RED); print_char(mode, RED); @@ -218,14 +158,9 @@ void printf(char* fmt, ...) end_line(); } } - } - else if (*i == '\n') - { - vga_print_char('\n', current_x, current_y, BLACK); + } else if (*i == '\n') { end_line(); - } - else - { + } else { print_char(*i, WHITE); } } diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index f9865a3..422b578 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -1,5 +1,7 @@ #pragma once +#include "../memory/memory.h" + #define VGA_START 0xb8000 #define va_arg(list, type) ( ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] ) From 274bcf16d1716ebbddf0e5ee62f07ac71e5ffad3 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 11 Nov 2023 02:34:27 +0700 Subject: [PATCH 50/88] upd #2 --- Makefile | 10 ++--- src/kernel/kernel.c | 17 +++---- src/kernel/sys/io/printf.c | 75 ++++++++++++++++--------------- src/kernel/sys/io/printf.h | 10 +++-- src/kernel/sys/memory/mem_utils.c | 34 +++++++------- 5 files changed, 75 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index 8566c62..1af463c 100644 --- a/Makefile +++ b/Makefile @@ -75,13 +75,13 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_O): $(KERNEL_C) \ +$(KERNEL_O): $(KERNEL_C) \ $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/io/printf.o \ $(BUILD_DIR)/pm/io/print_d2vm.o $(info [MAKE] building: $@) @@ -89,9 +89,9 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ # $(KERNEL_TMP) $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ -$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ - $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ - $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm \ +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ + $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ + $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm \ $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 98713c6..d51b8cc 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,18 +1,16 @@ -// extern void asm_print_d2vm(char* src); +extern void asm_print_d2vm(char* src); #include "./sys/io/io.h" #include "./sys/memory/memory.h" // compare [first, first_end) and second (until '\0') -int string_compare(char* first, char* second, int n) -{ +int string_compare(char* first, char* second, int n) { int res = 0; while(*first && *second && n-- && !(res = (*first++ - *second++))); return !res; } -void str_reverse(char* str_start, char* str_end) -{ +void str_reverse(char* str_start, char* str_end) { int len = (str_end - str_start + 1) / 2; for (int i = 0; i < len; ++i) { @@ -23,12 +21,11 @@ void str_reverse(char* str_start, char* str_end) } -void entry_point() -{ +void entry_point() { init_printer(); - printf("%b %b_RED!hello world!", 20, -165); - - // asm_print_d2vm("Hello from C!"); + printf( " %b %d_RED!hello world!", 20, 0x80000000); + // |||||||||||||| + asm_print_d2vm( "Hello from C!"); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 7f6147f..209ae0e 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -1,7 +1,7 @@ #include "printf.h" -static current_x; -static current_y; +static int current_x; +static int current_y; void vga_print_char(char symbol, int x, int y, int foreground_color) { if (x >= 80 || y >= 25) return; @@ -9,7 +9,7 @@ void vga_print_char(char symbol, int x, int y, int foreground_color) { } void vga_clear_screen() { - memzero(VGA_START, sizeof(short) * 25 * 80); + memzero((void*)VGA_START, sizeof(short) * 25 * 80); } void vga_print_string(char* str, int x, int y, int text_color) { @@ -43,8 +43,7 @@ void end_line() { void print_char(char s, int color) { vga_print_char(s, current_x, current_y, color); - ++current_x; - if (current_x == 80) end_line(); + if (++current_x == 80) end_line(); } void print_string(char* str, int color) { @@ -55,18 +54,17 @@ void print_string(char* str, int color) { } void printf(char* fmt, ...) { - char** a = &fmt + 1; + va_list a = va_start(fmt); for (char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { print_char('%', WHITE); ++i; } else { - char mode; int color = WHITE; // mode select - mode = *(i++ + 1); + char mode = *(i++ + 1); // color select (iff defined) if (*(i + 1) == '_') { @@ -83,7 +81,7 @@ void printf(char* fmt, ...) { "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; char* cur_color = color_names; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < _LAST; ++i) { if (string_compare(start, cur_color, stop - start)) { color = i; break; @@ -98,55 +96,58 @@ void printf(char* fmt, ...) { char* b = va_arg(a, char*); print_string(b, color); } else if (mode == 'd') { - int b = va_arg(a, int); - char buffer[12] = {0}; // (-)xxxxxxxxxx\0 - int bc = b; - - int counter = 10; - - if (bc < 0) bc *= -1; - while (bc) { - buffer[counter--] = bc % 10 + 48; - bc /= 10; + unsigned b = (unsigned) va_arg(a, int); +#define _MAX_N_DIGITS_FOR_DEC_CASE (int)(12) + char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; + + int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; + int is_neg = 0; + if (b & (1 << sizeof(int) * 8 - 1)) + { + is_neg = 1; + b = ~b + 1; + } + while (b) { + buffer[--counter] = b % 10 + '0'; + b /= 10; + } + if (is_neg) + { + buffer[--counter] = '-'; } - - if (b < 0) buffer[counter] = '-'; - else counter++; print_string(buffer + counter, color); } else if (mode == 'x' || mode == 'X') { int b = va_arg(a, int); print_string("0x", color); - char buffer[5] = {0}; + char buffer[sizeof(int) + 1] = {0}; - int bc = b; int counter = 3; - if (bc < 0) { - bc *= -1; + if (b < 0) { + b *= -1; } - while (bc) { - int temp = bc % 16; + while (b) { + int temp = b % 16; char symbol; if (temp > 9) { - symbol = temp - 10 + (mode == 'X') ? 'A' : 'a'; + symbol = (temp - 10 + (mode == 'X')) ? 'A' : 'a'; } else { symbol = temp + '0'; } buffer[counter--] = symbol; - bc >>= 4; + b >>= 4; } print_string(buffer + counter + 1, color); } else if (mode == 'b') { - unsigned int b = (unsigned int) va_arg(a, int); + unsigned b = (unsigned) va_arg(a, unsigned); print_string("0b", color); - char buffer[33] = {0}; + char buffer[sizeof(unsigned) * 8 + 1] = {0}; - unsigned int bc = b; - int counter = 31; - while (bc) { - buffer[counter--] = (char)((bc & 1) + '0'); - bc >>= 1; + int counter = sizeof(unsigned) * 8 - 1; + while (b) { + buffer[counter--] = (char)((b & 1) + '0'); + b >>= 1; } print_string(buffer + counter + 1, color); diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 422b578..97892e3 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -2,11 +2,12 @@ #include "../memory/memory.h" -#define VGA_START 0xb8000 +#define VGA_START (int)(0xb8000) #define va_arg(list, type) ( ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] ) +#define va_list char** +#define va_start(x) (char**)(&x + 1) -enum color -{ +enum color { BLACK, BLUE, GREEN, @@ -23,6 +24,7 @@ enum color LIGHT_PURPLE, YELLOW, WHITE, + _LAST }; void vga_print_char(char symbol, int x, int y, int foreground_color); @@ -33,4 +35,4 @@ void init_printer(); void end_line(); void print_char(char s, int color); void print_string(char* str, int color); -void printf(char *fmt, ...); \ No newline at end of file +void printf(char* fmt, ...); \ No newline at end of file diff --git a/src/kernel/sys/memory/mem_utils.c b/src/kernel/sys/memory/mem_utils.c index 8073e91..d9949e7 100644 --- a/src/kernel/sys/memory/mem_utils.c +++ b/src/kernel/sys/memory/mem_utils.c @@ -1,37 +1,41 @@ #include "mem_utils.h" -void* memcpy(void* dst, const void* src, int n) -{ - int m; - unsigned long *wdst = dst; // текущая позиция в буфере назначения - unsigned long *wsrc = src; // текущая позиция в источнике - unsigned char *cdst, *csrc; +// Why are the comments Russian? + +void* memcpy(void* dst, const void* src, int n) { + unsigned long* wdst = dst; // текущая позиция в буфере назначения + unsigned long* wsrc = src; // текущая позиция в источнике + unsigned char* cdst; + unsigned char* csrc; - for(int i = 0, m = n / sizeof(long); i < m; i++) // копируем основную часть блоками по 4 или 8 байт + for(int i = 0, m = n / sizeof(long); i < m; ++i) { // копируем основную часть блоками по 4 или 8 байт *(wdst++) = *(wsrc++); // (в зависимости от платформы) + } cdst = (unsigned char*)wdst; csrc = (unsigned char*)wsrc; - for(int i = 0, m = n % sizeof(long); i < m; i++) // остаток копируем побайтно + for(int i = 0, m = n % sizeof(long); i < m; ++i) { // остаток копируем побайтно *(cdst++) = *(csrc++); + } return dst; } -void* memzero(void* dst, int n) -{ +void* memzero(void* dst, int n) { int m; - unsigned long *wdst = dst; // текущая позиция в буфере назначения - unsigned char *cdst, *csrc; + unsigned long* wdst = dst; // текущая позиция в буфере назначения + unsigned char* cdst; - for(int i = 0, m = n / sizeof(long); i < m; i++) // копируем основную часть блоками по 4 или 8 байт - *(wdst++) = 0; // (в зависимости от платформы) + for(int i = 0, m = n / sizeof(long); i < m; ++i) { // копируем основную часть блоками по 4 или 8 байт + *(wdst++) = 0; // (в зависимости от платформы) + } cdst = (unsigned char*)wdst; - for(int i = 0, m = n % sizeof(long); i < m; i++) // остаток копируем побайтно + for(int i = 0, m = n % sizeof(long); i < m; ++i) { // остаток копируем побайтно *(cdst++) = 0; + } return dst; } \ No newline at end of file From 12b27606e592069e61e7ad9fb8986f6342cfe7e5 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 10 Dec 2023 22:16:01 +0700 Subject: [PATCH 51/88] upd (?) --- src/kernel/kernel.c | 5 ----- src/kernel/sys/io/printf.c | 20 +++++++++++++------- src/kernel/sys/io/printf.h | 9 +++++---- src/kernel/sys/memory/mem_utils.c | 8 ++++---- src/kernel/sys/memory/mem_utils.h | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index d51b8cc..df750c7 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -4,11 +4,6 @@ extern void asm_print_d2vm(char* src); #include "./sys/memory/memory.h" // compare [first, first_end) and second (until '\0') -int string_compare(char* first, char* second, int n) { - int res = 0; - while(*first && *second && n-- && !(res = (*first++ - *second++))); - return !res; -} void str_reverse(char* str_start, char* str_end) { int len = (str_end - str_start + 1) / 2; diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 209ae0e..f0d1893 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -3,6 +3,12 @@ static int current_x; static int current_y; +int string_compare(const char* first, const char* second, int n) { + int res = 0; + while(*first && *second && n-- && !(res = (*first++ - *second++))); + return !res; +} + void vga_print_char(char symbol, int x, int y, int foreground_color) { if (x >= 80 || y >= 25) return; *((short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; @@ -25,8 +31,8 @@ void vga_print_string(char* str, int x, int y, int text_color) { } void shift_up() { - memcpy(VGA_START, VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); - memzero(VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); + memcpy((char*) VGA_START, (char*) VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); + memzero((char*) VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); } void init_printer() { @@ -53,9 +59,9 @@ void print_string(char* str, int color) { } } -void printf(char* fmt, ...) { +void printf(const char* fmt, ...) { va_list a = va_start(fmt); - for (char* i = fmt; *i; ++i) { + for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { print_char('%', WHITE); @@ -69,8 +75,8 @@ void printf(char* fmt, ...) { // color select (iff defined) if (*(i + 1) == '_') { i += 2; - char* start = i; - char* stop = i; + const char* start = i; + const char* stop = i; while (*i != '!') { if (!*i) return; stop = i++; @@ -93,7 +99,7 @@ void printf(char* fmt, ...) { // print if (mode == 's') { - char* b = va_arg(a, char*); + char* b = &va_arg(a, char); print_string(b, color); } else if (mode == 'd') { unsigned b = (unsigned) va_arg(a, int); diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 97892e3..10ba047 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -3,9 +3,9 @@ #include "../memory/memory.h" #define VGA_START (int)(0xb8000) -#define va_arg(list, type) ( ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] ) -#define va_list char** -#define va_start(x) (char**)(&x + 1) +#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] +#define va_list char* +#define va_start(x) (char*)(&x + 1) enum color { BLACK, @@ -27,6 +27,7 @@ enum color { _LAST }; +int string_compare(const char* first, const char* second, int n); void vga_print_char(char symbol, int x, int y, int foreground_color); void vga_clear_screen(); void vga_print_string(char* str, int x, int y, int text_color); @@ -35,4 +36,4 @@ void init_printer(); void end_line(); void print_char(char s, int color); void print_string(char* str, int color); -void printf(char* fmt, ...); \ No newline at end of file +void printf(const char* fmt, ...); \ No newline at end of file diff --git a/src/kernel/sys/memory/mem_utils.c b/src/kernel/sys/memory/mem_utils.c index d9949e7..336e2cb 100644 --- a/src/kernel/sys/memory/mem_utils.c +++ b/src/kernel/sys/memory/mem_utils.c @@ -2,9 +2,9 @@ // Why are the comments Russian? -void* memcpy(void* dst, const void* src, int n) { - unsigned long* wdst = dst; // текущая позиция в буфере назначения - unsigned long* wsrc = src; // текущая позиция в источнике +const void* memcpy(const void* dst, const void* src, int n) { + unsigned long* wdst = (unsigned long*) dst; // текущая позиция в буфере назначения + unsigned long* wsrc = (unsigned long*) src; // текущая позиция в источнике unsigned char* cdst; unsigned char* csrc; @@ -24,7 +24,7 @@ void* memcpy(void* dst, const void* src, int n) { void* memzero(void* dst, int n) { int m; - unsigned long* wdst = dst; // текущая позиция в буфере назначения + unsigned long* wdst = (unsigned long*) dst; // текущая позиция в буфере назначения unsigned char* cdst; for(int i = 0, m = n / sizeof(long); i < m; ++i) { // копируем основную часть блоками по 4 или 8 байт diff --git a/src/kernel/sys/memory/mem_utils.h b/src/kernel/sys/memory/mem_utils.h index 8e026b2..9f33767 100644 --- a/src/kernel/sys/memory/mem_utils.h +++ b/src/kernel/sys/memory/mem_utils.h @@ -1,4 +1,4 @@ #pragma once -void* memcpy(void* dst, const void* src, int n); +const void* memcpy(const void* dst, const void* src, int n); void* memzero(void* dst, int n); \ No newline at end of file From 8f58a19f6031deba6f6060587299259736ee848a Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 24 Dec 2023 17:36:27 +0700 Subject: [PATCH 52/88] =) --- src/kernel/sys/gen_interruption.py | 15 ++ src/kernel/sys/idt.c | 12 + src/kernel/sys/interruption.c | 313 +++++++++++++++++++++++++++ src/kernel/sys/interruption.template | 25 +++ 4 files changed, 365 insertions(+) create mode 100644 src/kernel/sys/gen_interruption.py create mode 100644 src/kernel/sys/idt.c create mode 100644 src/kernel/sys/interruption.c create mode 100644 src/kernel/sys/interruption.template diff --git a/src/kernel/sys/gen_interruption.py b/src/kernel/sys/gen_interruption.py new file mode 100644 index 0000000..3124fa6 --- /dev/null +++ b/src/kernel/sys/gen_interruption.py @@ -0,0 +1,15 @@ +import os + +root = os.getcwd() + '/src/kernel/sys' + +with open(root + '/interruption.template','r') as ff, open(root + '/interruption.c','w') as sf: + for line in ff: + sf.write(line) + all_tramplins = '' + indent = '\n\t\t\t\t\t' + for i in range(255): + sf.write('__gen_tramplin(%s)\n' % hex(i).split('x')[-1]) + all_tramplins += 'tramplin_%s,%s' % (hex(i).split('x')[-1], indent if (i + 1) % 8 == 0 else ' ') + sf.write('__gen_tramplin(ff)\n') + all_tramplins += 'tramplin_ff' + sf.write('__gen_tramplins(%s)\n' % all_tramplins) \ No newline at end of file diff --git a/src/kernel/sys/idt.c b/src/kernel/sys/idt.c new file mode 100644 index 0000000..16b27c4 --- /dev/null +++ b/src/kernel/sys/idt.c @@ -0,0 +1,12 @@ +#pragma once + +typedef struct +{ + unsigned short offset_1; + unsigned short selector; + unsigned char ist; + unsigned char type_attributes; + unsigned short offset_2; + unsigned int offset_3; + unsigned int zero; +} InterruptDescriptor; \ No newline at end of file diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c new file mode 100644 index 0000000..d6e8ec6 --- /dev/null +++ b/src/kernel/sys/interruption.c @@ -0,0 +1,313 @@ +#pragma once + +#include "./idt.c" + +#define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } +#define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; + +static void panic_handler(int vector) +{ + kernel_panic("unhandled interrupt %x", vector); +} + +void register_handlers() +{ + for (int i = 0; i < sizeof(tramplins) / sizeof(tramplins[0]); ++i) + { + void* handler = tramplins[i]; + signed char* low_16_bits = (signed char*) handler; + signed char* high_16_bits = low_16_bits + 1; + InterruptDescriptor[i] = ... + } +} + +// AUTO-GENERATED CODE IS BELOW + +__gen_tramplin(0) +__gen_tramplin(1) +__gen_tramplin(2) +__gen_tramplin(3) +__gen_tramplin(4) +__gen_tramplin(5) +__gen_tramplin(6) +__gen_tramplin(7) +__gen_tramplin(8) +__gen_tramplin(9) +__gen_tramplin(a) +__gen_tramplin(b) +__gen_tramplin(c) +__gen_tramplin(d) +__gen_tramplin(e) +__gen_tramplin(f) +__gen_tramplin(10) +__gen_tramplin(11) +__gen_tramplin(12) +__gen_tramplin(13) +__gen_tramplin(14) +__gen_tramplin(15) +__gen_tramplin(16) +__gen_tramplin(17) +__gen_tramplin(18) +__gen_tramplin(19) +__gen_tramplin(1a) +__gen_tramplin(1b) +__gen_tramplin(1c) +__gen_tramplin(1d) +__gen_tramplin(1e) +__gen_tramplin(1f) +__gen_tramplin(20) +__gen_tramplin(21) +__gen_tramplin(22) +__gen_tramplin(23) +__gen_tramplin(24) +__gen_tramplin(25) +__gen_tramplin(26) +__gen_tramplin(27) +__gen_tramplin(28) +__gen_tramplin(29) +__gen_tramplin(2a) +__gen_tramplin(2b) +__gen_tramplin(2c) +__gen_tramplin(2d) +__gen_tramplin(2e) +__gen_tramplin(2f) +__gen_tramplin(30) +__gen_tramplin(31) +__gen_tramplin(32) +__gen_tramplin(33) +__gen_tramplin(34) +__gen_tramplin(35) +__gen_tramplin(36) +__gen_tramplin(37) +__gen_tramplin(38) +__gen_tramplin(39) +__gen_tramplin(3a) +__gen_tramplin(3b) +__gen_tramplin(3c) +__gen_tramplin(3d) +__gen_tramplin(3e) +__gen_tramplin(3f) +__gen_tramplin(40) +__gen_tramplin(41) +__gen_tramplin(42) +__gen_tramplin(43) +__gen_tramplin(44) +__gen_tramplin(45) +__gen_tramplin(46) +__gen_tramplin(47) +__gen_tramplin(48) +__gen_tramplin(49) +__gen_tramplin(4a) +__gen_tramplin(4b) +__gen_tramplin(4c) +__gen_tramplin(4d) +__gen_tramplin(4e) +__gen_tramplin(4f) +__gen_tramplin(50) +__gen_tramplin(51) +__gen_tramplin(52) +__gen_tramplin(53) +__gen_tramplin(54) +__gen_tramplin(55) +__gen_tramplin(56) +__gen_tramplin(57) +__gen_tramplin(58) +__gen_tramplin(59) +__gen_tramplin(5a) +__gen_tramplin(5b) +__gen_tramplin(5c) +__gen_tramplin(5d) +__gen_tramplin(5e) +__gen_tramplin(5f) +__gen_tramplin(60) +__gen_tramplin(61) +__gen_tramplin(62) +__gen_tramplin(63) +__gen_tramplin(64) +__gen_tramplin(65) +__gen_tramplin(66) +__gen_tramplin(67) +__gen_tramplin(68) +__gen_tramplin(69) +__gen_tramplin(6a) +__gen_tramplin(6b) +__gen_tramplin(6c) +__gen_tramplin(6d) +__gen_tramplin(6e) +__gen_tramplin(6f) +__gen_tramplin(70) +__gen_tramplin(71) +__gen_tramplin(72) +__gen_tramplin(73) +__gen_tramplin(74) +__gen_tramplin(75) +__gen_tramplin(76) +__gen_tramplin(77) +__gen_tramplin(78) +__gen_tramplin(79) +__gen_tramplin(7a) +__gen_tramplin(7b) +__gen_tramplin(7c) +__gen_tramplin(7d) +__gen_tramplin(7e) +__gen_tramplin(7f) +__gen_tramplin(80) +__gen_tramplin(81) +__gen_tramplin(82) +__gen_tramplin(83) +__gen_tramplin(84) +__gen_tramplin(85) +__gen_tramplin(86) +__gen_tramplin(87) +__gen_tramplin(88) +__gen_tramplin(89) +__gen_tramplin(8a) +__gen_tramplin(8b) +__gen_tramplin(8c) +__gen_tramplin(8d) +__gen_tramplin(8e) +__gen_tramplin(8f) +__gen_tramplin(90) +__gen_tramplin(91) +__gen_tramplin(92) +__gen_tramplin(93) +__gen_tramplin(94) +__gen_tramplin(95) +__gen_tramplin(96) +__gen_tramplin(97) +__gen_tramplin(98) +__gen_tramplin(99) +__gen_tramplin(9a) +__gen_tramplin(9b) +__gen_tramplin(9c) +__gen_tramplin(9d) +__gen_tramplin(9e) +__gen_tramplin(9f) +__gen_tramplin(a0) +__gen_tramplin(a1) +__gen_tramplin(a2) +__gen_tramplin(a3) +__gen_tramplin(a4) +__gen_tramplin(a5) +__gen_tramplin(a6) +__gen_tramplin(a7) +__gen_tramplin(a8) +__gen_tramplin(a9) +__gen_tramplin(aa) +__gen_tramplin(ab) +__gen_tramplin(ac) +__gen_tramplin(ad) +__gen_tramplin(ae) +__gen_tramplin(af) +__gen_tramplin(b0) +__gen_tramplin(b1) +__gen_tramplin(b2) +__gen_tramplin(b3) +__gen_tramplin(b4) +__gen_tramplin(b5) +__gen_tramplin(b6) +__gen_tramplin(b7) +__gen_tramplin(b8) +__gen_tramplin(b9) +__gen_tramplin(ba) +__gen_tramplin(bb) +__gen_tramplin(bc) +__gen_tramplin(bd) +__gen_tramplin(be) +__gen_tramplin(bf) +__gen_tramplin(c0) +__gen_tramplin(c1) +__gen_tramplin(c2) +__gen_tramplin(c3) +__gen_tramplin(c4) +__gen_tramplin(c5) +__gen_tramplin(c6) +__gen_tramplin(c7) +__gen_tramplin(c8) +__gen_tramplin(c9) +__gen_tramplin(ca) +__gen_tramplin(cb) +__gen_tramplin(cc) +__gen_tramplin(cd) +__gen_tramplin(ce) +__gen_tramplin(cf) +__gen_tramplin(d0) +__gen_tramplin(d1) +__gen_tramplin(d2) +__gen_tramplin(d3) +__gen_tramplin(d4) +__gen_tramplin(d5) +__gen_tramplin(d6) +__gen_tramplin(d7) +__gen_tramplin(d8) +__gen_tramplin(d9) +__gen_tramplin(da) +__gen_tramplin(db) +__gen_tramplin(dc) +__gen_tramplin(dd) +__gen_tramplin(de) +__gen_tramplin(df) +__gen_tramplin(e0) +__gen_tramplin(e1) +__gen_tramplin(e2) +__gen_tramplin(e3) +__gen_tramplin(e4) +__gen_tramplin(e5) +__gen_tramplin(e6) +__gen_tramplin(e7) +__gen_tramplin(e8) +__gen_tramplin(e9) +__gen_tramplin(ea) +__gen_tramplin(eb) +__gen_tramplin(ec) +__gen_tramplin(ed) +__gen_tramplin(ee) +__gen_tramplin(ef) +__gen_tramplin(f0) +__gen_tramplin(f1) +__gen_tramplin(f2) +__gen_tramplin(f3) +__gen_tramplin(f4) +__gen_tramplin(f5) +__gen_tramplin(f6) +__gen_tramplin(f7) +__gen_tramplin(f8) +__gen_tramplin(f9) +__gen_tramplin(fa) +__gen_tramplin(fb) +__gen_tramplin(fc) +__gen_tramplin(fd) +__gen_tramplin(fe) +__gen_tramplin(ff) +__gen_tramplins(tramplin_0, tramplin_1, tramplin_2, tramplin_3, tramplin_4, tramplin_5, tramplin_6, tramplin_7, + tramplin_8, tramplin_9, tramplin_a, tramplin_b, tramplin_c, tramplin_d, tramplin_e, tramplin_f, + tramplin_10, tramplin_11, tramplin_12, tramplin_13, tramplin_14, tramplin_15, tramplin_16, tramplin_17, + tramplin_18, tramplin_19, tramplin_1a, tramplin_1b, tramplin_1c, tramplin_1d, tramplin_1e, tramplin_1f, + tramplin_20, tramplin_21, tramplin_22, tramplin_23, tramplin_24, tramplin_25, tramplin_26, tramplin_27, + tramplin_28, tramplin_29, tramplin_2a, tramplin_2b, tramplin_2c, tramplin_2d, tramplin_2e, tramplin_2f, + tramplin_30, tramplin_31, tramplin_32, tramplin_33, tramplin_34, tramplin_35, tramplin_36, tramplin_37, + tramplin_38, tramplin_39, tramplin_3a, tramplin_3b, tramplin_3c, tramplin_3d, tramplin_3e, tramplin_3f, + tramplin_40, tramplin_41, tramplin_42, tramplin_43, tramplin_44, tramplin_45, tramplin_46, tramplin_47, + tramplin_48, tramplin_49, tramplin_4a, tramplin_4b, tramplin_4c, tramplin_4d, tramplin_4e, tramplin_4f, + tramplin_50, tramplin_51, tramplin_52, tramplin_53, tramplin_54, tramplin_55, tramplin_56, tramplin_57, + tramplin_58, tramplin_59, tramplin_5a, tramplin_5b, tramplin_5c, tramplin_5d, tramplin_5e, tramplin_5f, + tramplin_60, tramplin_61, tramplin_62, tramplin_63, tramplin_64, tramplin_65, tramplin_66, tramplin_67, + tramplin_68, tramplin_69, tramplin_6a, tramplin_6b, tramplin_6c, tramplin_6d, tramplin_6e, tramplin_6f, + tramplin_70, tramplin_71, tramplin_72, tramplin_73, tramplin_74, tramplin_75, tramplin_76, tramplin_77, + tramplin_78, tramplin_79, tramplin_7a, tramplin_7b, tramplin_7c, tramplin_7d, tramplin_7e, tramplin_7f, + tramplin_80, tramplin_81, tramplin_82, tramplin_83, tramplin_84, tramplin_85, tramplin_86, tramplin_87, + tramplin_88, tramplin_89, tramplin_8a, tramplin_8b, tramplin_8c, tramplin_8d, tramplin_8e, tramplin_8f, + tramplin_90, tramplin_91, tramplin_92, tramplin_93, tramplin_94, tramplin_95, tramplin_96, tramplin_97, + tramplin_98, tramplin_99, tramplin_9a, tramplin_9b, tramplin_9c, tramplin_9d, tramplin_9e, tramplin_9f, + tramplin_a0, tramplin_a1, tramplin_a2, tramplin_a3, tramplin_a4, tramplin_a5, tramplin_a6, tramplin_a7, + tramplin_a8, tramplin_a9, tramplin_aa, tramplin_ab, tramplin_ac, tramplin_ad, tramplin_ae, tramplin_af, + tramplin_b0, tramplin_b1, tramplin_b2, tramplin_b3, tramplin_b4, tramplin_b5, tramplin_b6, tramplin_b7, + tramplin_b8, tramplin_b9, tramplin_ba, tramplin_bb, tramplin_bc, tramplin_bd, tramplin_be, tramplin_bf, + tramplin_c0, tramplin_c1, tramplin_c2, tramplin_c3, tramplin_c4, tramplin_c5, tramplin_c6, tramplin_c7, + tramplin_c8, tramplin_c9, tramplin_ca, tramplin_cb, tramplin_cc, tramplin_cd, tramplin_ce, tramplin_cf, + tramplin_d0, tramplin_d1, tramplin_d2, tramplin_d3, tramplin_d4, tramplin_d5, tramplin_d6, tramplin_d7, + tramplin_d8, tramplin_d9, tramplin_da, tramplin_db, tramplin_dc, tramplin_dd, tramplin_de, tramplin_df, + tramplin_e0, tramplin_e1, tramplin_e2, tramplin_e3, tramplin_e4, tramplin_e5, tramplin_e6, tramplin_e7, + tramplin_e8, tramplin_e9, tramplin_ea, tramplin_eb, tramplin_ec, tramplin_ed, tramplin_ee, tramplin_ef, + tramplin_f0, tramplin_f1, tramplin_f2, tramplin_f3, tramplin_f4, tramplin_f5, tramplin_f6, tramplin_f7, + tramplin_f8, tramplin_f9, tramplin_fa, tramplin_fb, tramplin_fc, tramplin_fd, tramplin_fe, tramplin_ff) diff --git a/src/kernel/sys/interruption.template b/src/kernel/sys/interruption.template new file mode 100644 index 0000000..d0deefb --- /dev/null +++ b/src/kernel/sys/interruption.template @@ -0,0 +1,25 @@ +#pragma once + +#include "./idt.c" + +#define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } +#define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; + +static void panic_handler(int vector) +{ + kernel_panic("unhandled interrupt %x", vector); +} + +void register_handlers() +{ + for (int i = 0; i < sizeof(tramplins) / sizeof(tramplins[0]); ++i) + { + void* handler = tramplins[i]; + signed char* low_16_bits = (signed char*) handler; + signed char* high_16_bits = low_16_bits + 1; + InterruptDescriptor[i] = ... + } +} + +// AUTO-GENERATED CODE IS BELOW + From df1404e60cf7853747089ffbeee5044a4b6be342 Mon Sep 17 00:00:00 2001 From: qq Date: Wed, 31 Jan 2024 03:29:28 +0700 Subject: [PATCH 53/88] IDT: first try (without make) --- Makefile | 24 +++++++++++++------ src/kernel/kernel.c | 7 +++--- src/kernel/sys/idt.c | 12 ---------- src/kernel/sys/idt.h | 12 ++++++++++ src/kernel/sys/interruption.c | 39 ++++++++++++++++++++++--------- src/kernel/sys/memory/alloc.c | 33 ++++++++++++++++++++++++++ src/kernel/sys/memory/alloc.h | 10 ++++++++ src/kernel/sys/memory/mem_utils.c | 1 - src/kernel/sys/memory/memory.h | 2 +- 9 files changed, 105 insertions(+), 35 deletions(-) delete mode 100644 src/kernel/sys/idt.c create mode 100644 src/kernel/sys/idt.h create mode 100644 src/kernel/sys/memory/alloc.c create mode 100644 src/kernel/sys/memory/alloc.h diff --git a/Makefile b/Makefile index 1af463c..5ec3e5a 100644 --- a/Makefile +++ b/Makefile @@ -71,27 +71,36 @@ $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o: $(KERNEL_SOURCE_DIR)/sys/memory/mem_ $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ +$(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c \ + $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ + $(KERNEL_SOURCE_DIR)/sys/idt.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_O): $(KERNEL_C) \ +$(KERNEL_O): $(KERNEL_C) \ $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ - $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/io/printf.o \ - $(BUILD_DIR)/pm/io/print_d2vm.o +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_O) \ + $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/io/printf.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ -$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ - $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ - $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm \ +$(BOOTLOADER_BIN): $(BOOTLOADER_ASM) $(SOURCE_DIR)/rm/io/print.asm \ + $(SOURCE_DIR)/bootloader/read_floppy_disk.asm $(SOURCE_DIR)/pm/switch2pm.asm \ + $(SOURCE_DIR)/pm/gdt.asm $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) $(ASM) -fbin $< -o $@ $(OD) $(OD_OPT) $@ @@ -112,3 +121,4 @@ clean_boot: # $(RM) -f $(KERNEL_TMP) # $(RM) -f $(KERNEL_BIN) # $(RM) -f $(BOOTLOADER_BIN) +# $(RM) -f $(BOOTLOADER_BIN) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index df750c7..8995f92 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,7 +1,8 @@ -extern void asm_print_d2vm(char* src); +//extern void asm_print_d2vm(char* src); #include "./sys/io/io.h" #include "./sys/memory/memory.h" +#include "./sys/interruption.c" // compare [first, first_end) and second (until '\0') @@ -20,7 +21,7 @@ void entry_point() { init_printer(); printf( " %b %d_RED!hello world!", 20, 0x80000000); // |||||||||||||| - asm_print_d2vm( "Hello from C!"); - + // asm_print_d2vm( "Hello from C!"); + register_handlers(); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/idt.c b/src/kernel/sys/idt.c deleted file mode 100644 index 16b27c4..0000000 --- a/src/kernel/sys/idt.c +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -typedef struct -{ - unsigned short offset_1; - unsigned short selector; - unsigned char ist; - unsigned char type_attributes; - unsigned short offset_2; - unsigned int offset_3; - unsigned int zero; -} InterruptDescriptor; \ No newline at end of file diff --git a/src/kernel/sys/idt.h b/src/kernel/sys/idt.h new file mode 100644 index 0000000..d8bdd28 --- /dev/null +++ b/src/kernel/sys/idt.h @@ -0,0 +1,12 @@ +#include +#pragma once + +#pragma pack(push, 1) +typedef struct { + uint16_t offset_1; // offset bits 0..15 + uint16_t selector; // a code segment selector in GDT or LDT + uint8_t zero; // unused, set to 0 + uint8_t type_attributes; // gate type, dpl, and p fields + uint16_t offset_2; // offset bits 16..31 +} InterruptDescriptor32; +#pragma pack(pop) \ No newline at end of file diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index d6e8ec6..4d77f9b 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -1,6 +1,7 @@ #pragma once -#include "./idt.c" +#include "idt.h" +#include "memory/memory.h" #define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; @@ -10,16 +11,6 @@ static void panic_handler(int vector) kernel_panic("unhandled interrupt %x", vector); } -void register_handlers() -{ - for (int i = 0; i < sizeof(tramplins) / sizeof(tramplins[0]); ++i) - { - void* handler = tramplins[i]; - signed char* low_16_bits = (signed char*) handler; - signed char* high_16_bits = low_16_bits + 1; - InterruptDescriptor[i] = ... - } -} // AUTO-GENERATED CODE IS BELOW @@ -311,3 +302,29 @@ __gen_tramplins(tramplin_0, tramplin_1, tramplin_2, tramplin_3, tramplin_4, tram tramplin_e8, tramplin_e9, tramplin_ea, tramplin_eb, tramplin_ec, tramplin_ed, tramplin_ee, tramplin_ef, tramplin_f0, tramplin_f1, tramplin_f2, tramplin_f3, tramplin_f4, tramplin_f5, tramplin_f6, tramplin_f7, tramplin_f8, tramplin_f9, tramplin_fa, tramplin_fb, tramplin_fc, tramplin_fd, tramplin_fe, tramplin_ff) + +#pragma pack(push, 1) +typedef struct { + uint16_t IDT_size; + uint32_t linear_addr_IDT; +} IDTD; +#pragma pack(pop) + +void register_handlers() +{ + InterruptDescriptor32* idt = kernel_calloc(sizeof(InterruptDescriptor32), 256); + + for (int i = 0; i < 256; ++i) + { + idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits + idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits + idt[i].type_attributes = 0b10001110; + idt[i].selector = 0b1000; + } + + IDTD idtd; + idtd.IDT_size = sizeof(InterruptDescriptor32) * 256 - 1; + idtd.linear_addr_IDT = idt; + + asm("lidt %0" :: "m"(idtd)); +} diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c new file mode 100644 index 0000000..32b061d --- /dev/null +++ b/src/kernel/sys/memory/alloc.c @@ -0,0 +1,33 @@ +#include "./alloc.h" + +#define MEMORY_MAX 0x400000 + +static unsigned int* heap_start = 0x100000; +static unsigned int* top = 0x100000; + +void* kernel_malloc(unsigned int size) { + if (top + size > MEMORY_MAX) + return 0; + top += size; + return top - size; +} + +void* kernel_calloc(unsigned int size, unsigned int num) { + if (size * num + top > MEMORY_MAX) + return 0; + memzero(top, size * num); + top += size * num; + return top - size * num; +} + +void* kernel_realloc(void* old_addr, unsigned int new_size) { + if (new_size + top > MEMORY_MAX) + return 0; + void* new_addr = kernel_malloc(new_size); + memcpy(new_addr, old_addr, new_size); + return new_addr; +} + +void kernel_free(void* addr) { + top = 0x100000; +} diff --git a/src/kernel/sys/memory/alloc.h b/src/kernel/sys/memory/alloc.h new file mode 100644 index 0000000..69196f4 --- /dev/null +++ b/src/kernel/sys/memory/alloc.h @@ -0,0 +1,10 @@ +#include "mem_utils.h" +#pragma once + +void* kernel_malloc(unsigned int size); + +void* kernel_calloc(unsigned int size, unsigned int num); + +void* kernel_realloc(void* old_addr, unsigned int new_size); + +void kernel_free(void* addr); diff --git a/src/kernel/sys/memory/mem_utils.c b/src/kernel/sys/memory/mem_utils.c index 336e2cb..f64a1df 100644 --- a/src/kernel/sys/memory/mem_utils.c +++ b/src/kernel/sys/memory/mem_utils.c @@ -23,7 +23,6 @@ const void* memcpy(const void* dst, const void* src, int n) { } void* memzero(void* dst, int n) { - int m; unsigned long* wdst = (unsigned long*) dst; // текущая позиция в буфере назначения unsigned char* cdst; diff --git a/src/kernel/sys/memory/memory.h b/src/kernel/sys/memory/memory.h index e1b98f4..2749ae1 100644 --- a/src/kernel/sys/memory/memory.h +++ b/src/kernel/sys/memory/memory.h @@ -1,3 +1,3 @@ #pragma once -#include "./mem_utils.h" \ No newline at end of file +#include "./alloc.h" \ No newline at end of file From 324279b68392fb5007d6dbd6909671da24c719ca Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 31 Jan 2024 03:58:48 +0700 Subject: [PATCH 54/88] here we go --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5ec3e5a..d798570 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_O): $(KERNEL_C) \ +$(KERNEL_O): $(KERNEL_C) $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ From 0cbb29806a58d0d8d2a219cf05a1816f3a34a337 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 1 Feb 2024 15:12:59 +0700 Subject: [PATCH 55/88] edit stuff, resolve link problem --- Makefile | 18 +++++++----------- src/kernel/kernel.c | 2 +- src/kernel/sys/gen_interruption.py | 2 +- src/kernel/sys/idt.h | 3 ++- .../sys/{interruption.c => interruption.hpp} | 4 ++-- src/kernel/sys/interruption.template | 2 +- src/kernel/sys/memory/alloc.h | 3 ++- 7 files changed, 16 insertions(+), 18 deletions(-) rename src/kernel/sys/{interruption.c => interruption.hpp} (99%) diff --git a/Makefile b/Makefile index d798570..d924d61 100644 --- a/Makefile +++ b/Makefile @@ -71,28 +71,24 @@ $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o: $(KERNEL_SOURCE_DIR)/sys/memory/mem_ $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c \ +$(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c \ $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ - $(KERNEL_SOURCE_DIR)/sys/idt.h +$(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h \ + $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.hpp $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ +# -m32 $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_O): $(KERNEL_C) $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ - $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h - $(info [MAKE] building: $@) - $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -# -m32 - -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_O) \ - $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/io/printf.o +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ + $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o \ + $(KERNEL_BUILD_DIR)/sys/memory/alloc.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 8995f92..9befbf5 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,7 +2,7 @@ #include "./sys/io/io.h" #include "./sys/memory/memory.h" -#include "./sys/interruption.c" +#include "./sys/interruption.hpp" // compare [first, first_end) and second (until '\0') diff --git a/src/kernel/sys/gen_interruption.py b/src/kernel/sys/gen_interruption.py index 3124fa6..86813d5 100644 --- a/src/kernel/sys/gen_interruption.py +++ b/src/kernel/sys/gen_interruption.py @@ -2,7 +2,7 @@ root = os.getcwd() + '/src/kernel/sys' -with open(root + '/interruption.template','r') as ff, open(root + '/interruption.c','w') as sf: +with open(root + '/interruption.template','r') as ff, open(root + '/interruption.hpp','w') as sf: for line in ff: sf.write(line) all_tramplins = '' diff --git a/src/kernel/sys/idt.h b/src/kernel/sys/idt.h index d8bdd28..06693f0 100644 --- a/src/kernel/sys/idt.h +++ b/src/kernel/sys/idt.h @@ -1,6 +1,7 @@ -#include #pragma once +#include + #pragma pack(push, 1) typedef struct { uint16_t offset_1; // offset bits 0..15 diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.hpp similarity index 99% rename from src/kernel/sys/interruption.c rename to src/kernel/sys/interruption.hpp index 4d77f9b..2ad2fce 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.hpp @@ -1,7 +1,7 @@ #pragma once -#include "idt.h" -#include "memory/memory.h" +#include "./idt.h" +#include "./memory/memory.h" #define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; diff --git a/src/kernel/sys/interruption.template b/src/kernel/sys/interruption.template index d0deefb..357a93e 100644 --- a/src/kernel/sys/interruption.template +++ b/src/kernel/sys/interruption.template @@ -1,6 +1,6 @@ #pragma once -#include "./idt.c" +#include "./idt.h" #define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; diff --git a/src/kernel/sys/memory/alloc.h b/src/kernel/sys/memory/alloc.h index 69196f4..5ac42ac 100644 --- a/src/kernel/sys/memory/alloc.h +++ b/src/kernel/sys/memory/alloc.h @@ -1,6 +1,7 @@ -#include "mem_utils.h" #pragma once +#include "mem_utils.h" + void* kernel_malloc(unsigned int size); void* kernel_calloc(unsigned int size, unsigned int num); From 488bce6a012c80f4caaf123bc89f5a9393c37473 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 1 Feb 2024 15:15:28 +0700 Subject: [PATCH 56/88] renew interruption.hpp --- src/kernel/sys/interruption.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/kernel/sys/interruption.hpp b/src/kernel/sys/interruption.hpp index 2ad2fce..d4c8ac8 100644 --- a/src/kernel/sys/interruption.hpp +++ b/src/kernel/sys/interruption.hpp @@ -2,10 +2,17 @@ #include "./idt.h" #include "./memory/memory.h" +#include "./io/io.h" #define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; +static void kernel_panic(char* str, int num) +{ + init_printer(); + printf(str, num); +} + static void panic_handler(int vector) { kernel_panic("unhandled interrupt %x", vector); From 97145a5c8074e9ce2d6242ff89e3a191acce33c6 Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 1 Feb 2024 16:45:22 +0700 Subject: [PATCH 57/88] idt: fix printf(0x0), add loop in kernel_panic --- src/kernel/kernel.c | 7 ++++++ src/kernel/sys/interruption.hpp | 5 ++-- src/kernel/sys/io/printf.c | 42 ++++++++++++++++----------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 9befbf5..82a0e1a 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -23,5 +23,12 @@ void entry_point() { // |||||||||||||| // asm_print_d2vm( "Hello from C!"); register_handlers(); + init_printer(); + //char* str = "unhandled interrupt %x"; + //int num = 0x1e; + //asm("xchgw %bx, %bx"); + + //printf(str, num); + printf("%d", 1/0); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/interruption.hpp b/src/kernel/sys/interruption.hpp index d4c8ac8..03e8330 100644 --- a/src/kernel/sys/interruption.hpp +++ b/src/kernel/sys/interruption.hpp @@ -11,11 +11,12 @@ static void kernel_panic(char* str, int num) { init_printer(); printf(str, num); + while (1); } static void panic_handler(int vector) -{ - kernel_panic("unhandled interrupt %x", vector); +{ + kernel_panic("unhandled interrupt %x", vector); } diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index f0d1893..5d7fdd7 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -126,25 +126,29 @@ void printf(const char* fmt, ...) { } else if (mode == 'x' || mode == 'X') { int b = va_arg(a, int); print_string("0x", color); - char buffer[sizeof(int) + 1] = {0}; + if (b != 0) { + char buffer[sizeof(int) + 1] = {0}; - int counter = 3; - if (b < 0) { - b *= -1; - } - while (b) { - int temp = b % 16; - char symbol; - if (temp > 9) { - symbol = (temp - 10 + (mode == 'X')) ? 'A' : 'a'; - } else { - symbol = temp + '0'; + int counter = 3; + if (b < 0) { + b *= -1; + } + while (b) { + int temp = b % 16; + char symbol; + if (temp > 9) { + symbol = temp - 10 + (mode == 'X' ? 'A' : 'a'); + } else { + symbol = temp + '0'; + } + buffer[counter--] = symbol; + b >>= 4; } - buffer[counter--] = symbol; - b >>= 4; - } - print_string(buffer + counter + 1, color); + print_string(buffer + counter + 1, color); + } else { + print_char('0', color); + } } else if (mode == 'b') { unsigned b = (unsigned) va_arg(a, unsigned); print_string("0b", color); @@ -157,12 +161,6 @@ void printf(const char* fmt, ...) { } print_string(buffer + counter + 1, color); - } else { - end_line(); - print_string("UNSUPPORTED FORMAT (", RED); - print_char(mode, RED); - print_string(")!", RED); - end_line(); } } } else if (*i == '\n') { From 86417452ddade4cc1b92d633ba561684408557dc Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 1 Feb 2024 17:00:07 +0700 Subject: [PATCH 58/88] fix --- src/kernel/sys/idt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/sys/idt.h b/src/kernel/sys/idt.h index 06693f0..5c66a8f 100644 --- a/src/kernel/sys/idt.h +++ b/src/kernel/sys/idt.h @@ -9,5 +9,5 @@ typedef struct { uint8_t zero; // unused, set to 0 uint8_t type_attributes; // gate type, dpl, and p fields uint16_t offset_2; // offset bits 16..31 -} InterruptDescriptor32; +} InterruptDescriptor32; #pragma pack(pop) \ No newline at end of file From 2c356bff8a6cc6626a3c6661a43de7319d1814f3 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 1 Feb 2024 17:07:13 +0700 Subject: [PATCH 59/88] add checks on allocation success --- src/kernel/sys/interruption.hpp | 4 ++++ src/kernel/sys/memory/alloc.c | 1 + 2 files changed, 5 insertions(+) diff --git a/src/kernel/sys/interruption.hpp b/src/kernel/sys/interruption.hpp index 03e8330..f51c9a9 100644 --- a/src/kernel/sys/interruption.hpp +++ b/src/kernel/sys/interruption.hpp @@ -321,6 +321,10 @@ typedef struct { void register_handlers() { InterruptDescriptor32* idt = kernel_calloc(sizeof(InterruptDescriptor32), 256); + if (idt == (InterruptDescriptor32*)0) + { + printf("[EXC] register_handlers(): not enough memory!"); + } for (int i = 0; i < 256; ++i) { diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index 32b061d..606a47c 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -24,6 +24,7 @@ void* kernel_realloc(void* old_addr, unsigned int new_size) { if (new_size + top > MEMORY_MAX) return 0; void* new_addr = kernel_malloc(new_size); + if (new_addr == (void*) 0) return 0; memcpy(new_addr, old_addr, new_size); return new_addr; } From 4ac0fc0802ab7eeee635c5d90add2582314a6aa4 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 1 Feb 2024 18:59:10 +0700 Subject: [PATCH 60/88] little fix --- src/kernel/kernel.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 82a0e1a..7b70049 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -16,7 +16,6 @@ void str_reverse(char* str_start, char* str_end) { } } - void entry_point() { init_printer(); printf( " %b %d_RED!hello world!", 20, 0x80000000); From fcb0ba2f42f4905ba3d1b98aed50f2e4ef4ee2dd Mon Sep 17 00:00:00 2001 From: qq Date: Sat, 3 Feb 2024 04:26:33 +0700 Subject: [PATCH 61/88] pic --- Makefile | 9 +++- src/kernel/kernel.c | 9 ++-- src/kernel/sys/memory/alloc.c | 1 - src/kernel/sys/pic/pic.c | 77 +++++++++++++++++++++++++++++++++++ src/kernel/sys/pic/pic.h | 11 +++++ 5 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/kernel/sys/pic/pic.c create mode 100644 src/kernel/sys/pic/pic.h diff --git a/Makefile b/Makefile index d924d61..0da419a 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,11 @@ $(BUILD_DIR)/pm/io/print_d2vm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ +$(KERNEL_BUILD_DIR)/sys/pic/pic.o: $(KERNEL_SOURCE_DIR)/sys/pic/pic.c \ + $(KERNEL_SOURCE_DIR)/sys/pic/pic.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_BUILD_DIR)/sys/io/printf.o: $(KERNEL_SOURCE_DIR)/sys/io/printf.c \ $(KERNEL_SOURCE_DIR)/sys/io/printf.h $(info [MAKE] building: $@) @@ -77,7 +82,7 @@ $(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ $(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h \ - $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.hpp + $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.hpp $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 @@ -88,7 +93,7 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o \ - $(KERNEL_BUILD_DIR)/sys/memory/alloc.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o + $(KERNEL_BUILD_DIR)/sys/memory/alloc.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7b70049..a8abdea 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -3,6 +3,7 @@ #include "./sys/io/io.h" #include "./sys/memory/memory.h" #include "./sys/interruption.hpp" +#include "./sys/pic/pic.h" // compare [first, first_end) and second (until '\0') @@ -22,12 +23,14 @@ void entry_point() { // |||||||||||||| // asm_print_d2vm( "Hello from C!"); register_handlers(); + setup_pic(); init_printer(); - //char* str = "unhandled interrupt %x"; - //int num = 0x1e; + //asm("xchgw %bx, %bx"); //printf(str, num); - printf("%d", 1/0); + //printf("%d", 1/0); // 0x0 + + asm("sti"); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index 606a47c..32b061d 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -24,7 +24,6 @@ void* kernel_realloc(void* old_addr, unsigned int new_size) { if (new_size + top > MEMORY_MAX) return 0; void* new_addr = kernel_malloc(new_size); - if (new_addr == (void*) 0) return 0; memcpy(new_addr, old_addr, new_size); return new_addr; } diff --git a/src/kernel/sys/pic/pic.c b/src/kernel/sys/pic/pic.c new file mode 100644 index 0000000..b5e8283 --- /dev/null +++ b/src/kernel/sys/pic/pic.c @@ -0,0 +1,77 @@ +#include "pic.h" + +#define MASTER_COMMAND 0x20 +#define MASTER_DATA 0x21 + +#define SLAVE_COMMAND 0xA0 +#define SLAVE_DATA 0xA1 + + +uint8_t inb(uint16_t port) { + uint8_t data; + asm("inb %1, %0" : "=a"(data) : "d"(port)); + return data; +} + +void outb(uint16_t port, uint8_t) { + asm("outb %al, %dx"); +} + +void delay() { + outb(0x80, 0); +} + +void setup_pic() { + uint16_t icw1 = 0b10001; + outb(MASTER_COMMAND, icw1); + delay(); + outb(SLAVE_COMMAND, icw1); + delay(); + + uint16_t icw2m = 0x20; + outb(MASTER_DATA, icw2m); + delay(); + uint16_t icw2s = 0x28; + outb(SLAVE_DATA, icw2s); + delay(); + + uint16_t icw3m = 0b100; + outb(MASTER_DATA, icw3m); + delay(); + uint16_t icw3s = 0b10; + outb(SLAVE_DATA, icw3s); + delay(); + + uint16_t icw4 = 0b1; + outb(MASTER_DATA, icw4); + delay(); + outb(SLAVE_DATA, icw4); + delay(); + + // 0 -- Programmable Interval Timer + // 1 -- Keyboard + // 2 -- Slave PIC + // 3 -- COM2 + // 4 -- COM1 + // 5 -- LPT2 + // 6 -- Floppy Disk + // 7 -- LPT / Spurious + // 8 -- CMOS Real-Time Clock + // 9-11 -- Free + // 12 -- PS/2 Mouse + // 13 -- Coprocessor + // 14 -- Primary ATA Hard Disk + // 15 -- Secondary ATA Hard Disk + + // 1. Упасть с вектором 0x20 + // outb(MASTER_DATA, 0b11111110); + // outb(SLAVE_DATA, 0b11111111); + + // 2. При нажатии на любую клавишу + outb(MASTER_DATA, 0b11111101); + outb(SLAVE_DATA, 0b11111111); + + // 3. Ничего не происходит при замаскированных битах + // outb(MASTER_DATA, 0b11111111); + // outb(SLAVE_DATA, 0b11111111); +} \ No newline at end of file diff --git a/src/kernel/sys/pic/pic.h b/src/kernel/sys/pic/pic.h new file mode 100644 index 0000000..4eb9c3d --- /dev/null +++ b/src/kernel/sys/pic/pic.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +// byte -> uint8_t + +uint8_t inb(uint16_t port); + +void outb(uint16_t port, uint8_t); + +void setup_pic(); \ No newline at end of file From 5e3e11a5d54a65ee85bf598b672d22f21f79de58 Mon Sep 17 00:00:00 2001 From: qq Date: Sat, 3 Feb 2024 05:14:10 +0700 Subject: [PATCH 62/88] context: auto generate tramplins w/ context --- src/kernel/sys/context/gen_asm_context.py | 28 + src/kernel/sys/context/tramplinsWContext.asm | 1537 ++++++++++++++++++ 2 files changed, 1565 insertions(+) create mode 100644 src/kernel/sys/context/gen_asm_context.py create mode 100644 src/kernel/sys/context/tramplinsWContext.asm diff --git a/src/kernel/sys/context/gen_asm_context.py b/src/kernel/sys/context/gen_asm_context.py new file mode 100644 index 0000000..1c4aa56 --- /dev/null +++ b/src/kernel/sys/context/gen_asm_context.py @@ -0,0 +1,28 @@ +exceptions_with_error_code = {8, 10, 11, 12, 13, 14, 17, 21, 29, 30} + +with open("tramplinsWContext.asm", "w") as f: + + + wr = f.write + wr("[BITS 32]\n\n") + + for i in range(0, 256): + wr(f"[GLOBAL _tramplin_{('0' if i <= 15 else '') + str(hex(i))[2:]}]\n") + + wr(''' +collect_context: + push ds + push es + push fs + push gs + pusha +''') + + wr("\n\n") + for i in range(0, 256): + wr(f"tramplin_{('0' if i <= 15 else '') + str(hex(i))[2:]}:\n") + if i not in exceptions_with_error_code: + wr("\tpush 0x0\n") + wr(f"\tpush 0x{('0' if i <= 15 else '') + str(hex(i))[2:]}\n") + wr("\tjmp collect_context") + wr("\n\n") \ No newline at end of file diff --git a/src/kernel/sys/context/tramplinsWContext.asm b/src/kernel/sys/context/tramplinsWContext.asm new file mode 100644 index 0000000..84c9ed8 --- /dev/null +++ b/src/kernel/sys/context/tramplinsWContext.asm @@ -0,0 +1,1537 @@ +[BITS 32] + +[GLOBAL _tramplin_00] +[GLOBAL _tramplin_01] +[GLOBAL _tramplin_02] +[GLOBAL _tramplin_03] +[GLOBAL _tramplin_04] +[GLOBAL _tramplin_05] +[GLOBAL _tramplin_06] +[GLOBAL _tramplin_07] +[GLOBAL _tramplin_08] +[GLOBAL _tramplin_09] +[GLOBAL _tramplin_0a] +[GLOBAL _tramplin_0b] +[GLOBAL _tramplin_0c] +[GLOBAL _tramplin_0d] +[GLOBAL _tramplin_0e] +[GLOBAL _tramplin_0f] +[GLOBAL _tramplin_10] +[GLOBAL _tramplin_11] +[GLOBAL _tramplin_12] +[GLOBAL _tramplin_13] +[GLOBAL _tramplin_14] +[GLOBAL _tramplin_15] +[GLOBAL _tramplin_16] +[GLOBAL _tramplin_17] +[GLOBAL _tramplin_18] +[GLOBAL _tramplin_19] +[GLOBAL _tramplin_1a] +[GLOBAL _tramplin_1b] +[GLOBAL _tramplin_1c] +[GLOBAL _tramplin_1d] +[GLOBAL _tramplin_1e] +[GLOBAL _tramplin_1f] +[GLOBAL _tramplin_20] +[GLOBAL _tramplin_21] +[GLOBAL _tramplin_22] +[GLOBAL _tramplin_23] +[GLOBAL _tramplin_24] +[GLOBAL _tramplin_25] +[GLOBAL _tramplin_26] +[GLOBAL _tramplin_27] +[GLOBAL _tramplin_28] +[GLOBAL _tramplin_29] +[GLOBAL _tramplin_2a] +[GLOBAL _tramplin_2b] +[GLOBAL _tramplin_2c] +[GLOBAL _tramplin_2d] +[GLOBAL _tramplin_2e] +[GLOBAL _tramplin_2f] +[GLOBAL _tramplin_30] +[GLOBAL _tramplin_31] +[GLOBAL _tramplin_32] +[GLOBAL _tramplin_33] +[GLOBAL _tramplin_34] +[GLOBAL _tramplin_35] +[GLOBAL _tramplin_36] +[GLOBAL _tramplin_37] +[GLOBAL _tramplin_38] +[GLOBAL _tramplin_39] +[GLOBAL _tramplin_3a] +[GLOBAL _tramplin_3b] +[GLOBAL _tramplin_3c] +[GLOBAL _tramplin_3d] +[GLOBAL _tramplin_3e] +[GLOBAL _tramplin_3f] +[GLOBAL _tramplin_40] +[GLOBAL _tramplin_41] +[GLOBAL _tramplin_42] +[GLOBAL _tramplin_43] +[GLOBAL _tramplin_44] +[GLOBAL _tramplin_45] +[GLOBAL _tramplin_46] +[GLOBAL _tramplin_47] +[GLOBAL _tramplin_48] +[GLOBAL _tramplin_49] +[GLOBAL _tramplin_4a] +[GLOBAL _tramplin_4b] +[GLOBAL _tramplin_4c] +[GLOBAL _tramplin_4d] +[GLOBAL _tramplin_4e] +[GLOBAL _tramplin_4f] +[GLOBAL _tramplin_50] +[GLOBAL _tramplin_51] +[GLOBAL _tramplin_52] +[GLOBAL _tramplin_53] +[GLOBAL _tramplin_54] +[GLOBAL _tramplin_55] +[GLOBAL _tramplin_56] +[GLOBAL _tramplin_57] +[GLOBAL _tramplin_58] +[GLOBAL _tramplin_59] +[GLOBAL _tramplin_5a] +[GLOBAL _tramplin_5b] +[GLOBAL _tramplin_5c] +[GLOBAL _tramplin_5d] +[GLOBAL _tramplin_5e] +[GLOBAL _tramplin_5f] +[GLOBAL _tramplin_60] +[GLOBAL _tramplin_61] +[GLOBAL _tramplin_62] +[GLOBAL _tramplin_63] +[GLOBAL _tramplin_64] +[GLOBAL _tramplin_65] +[GLOBAL _tramplin_66] +[GLOBAL _tramplin_67] +[GLOBAL _tramplin_68] +[GLOBAL _tramplin_69] +[GLOBAL _tramplin_6a] +[GLOBAL _tramplin_6b] +[GLOBAL _tramplin_6c] +[GLOBAL _tramplin_6d] +[GLOBAL _tramplin_6e] +[GLOBAL _tramplin_6f] +[GLOBAL _tramplin_70] +[GLOBAL _tramplin_71] +[GLOBAL _tramplin_72] +[GLOBAL _tramplin_73] +[GLOBAL _tramplin_74] +[GLOBAL _tramplin_75] +[GLOBAL _tramplin_76] +[GLOBAL _tramplin_77] +[GLOBAL _tramplin_78] +[GLOBAL _tramplin_79] +[GLOBAL _tramplin_7a] +[GLOBAL _tramplin_7b] +[GLOBAL _tramplin_7c] +[GLOBAL _tramplin_7d] +[GLOBAL _tramplin_7e] +[GLOBAL _tramplin_7f] +[GLOBAL _tramplin_80] +[GLOBAL _tramplin_81] +[GLOBAL _tramplin_82] +[GLOBAL _tramplin_83] +[GLOBAL _tramplin_84] +[GLOBAL _tramplin_85] +[GLOBAL _tramplin_86] +[GLOBAL _tramplin_87] +[GLOBAL _tramplin_88] +[GLOBAL _tramplin_89] +[GLOBAL _tramplin_8a] +[GLOBAL _tramplin_8b] +[GLOBAL _tramplin_8c] +[GLOBAL _tramplin_8d] +[GLOBAL _tramplin_8e] +[GLOBAL _tramplin_8f] +[GLOBAL _tramplin_90] +[GLOBAL _tramplin_91] +[GLOBAL _tramplin_92] +[GLOBAL _tramplin_93] +[GLOBAL _tramplin_94] +[GLOBAL _tramplin_95] +[GLOBAL _tramplin_96] +[GLOBAL _tramplin_97] +[GLOBAL _tramplin_98] +[GLOBAL _tramplin_99] +[GLOBAL _tramplin_9a] +[GLOBAL _tramplin_9b] +[GLOBAL _tramplin_9c] +[GLOBAL _tramplin_9d] +[GLOBAL _tramplin_9e] +[GLOBAL _tramplin_9f] +[GLOBAL _tramplin_a0] +[GLOBAL _tramplin_a1] +[GLOBAL _tramplin_a2] +[GLOBAL _tramplin_a3] +[GLOBAL _tramplin_a4] +[GLOBAL _tramplin_a5] +[GLOBAL _tramplin_a6] +[GLOBAL _tramplin_a7] +[GLOBAL _tramplin_a8] +[GLOBAL _tramplin_a9] +[GLOBAL _tramplin_aa] +[GLOBAL _tramplin_ab] +[GLOBAL _tramplin_ac] +[GLOBAL _tramplin_ad] +[GLOBAL _tramplin_ae] +[GLOBAL _tramplin_af] +[GLOBAL _tramplin_b0] +[GLOBAL _tramplin_b1] +[GLOBAL _tramplin_b2] +[GLOBAL _tramplin_b3] +[GLOBAL _tramplin_b4] +[GLOBAL _tramplin_b5] +[GLOBAL _tramplin_b6] +[GLOBAL _tramplin_b7] +[GLOBAL _tramplin_b8] +[GLOBAL _tramplin_b9] +[GLOBAL _tramplin_ba] +[GLOBAL _tramplin_bb] +[GLOBAL _tramplin_bc] +[GLOBAL _tramplin_bd] +[GLOBAL _tramplin_be] +[GLOBAL _tramplin_bf] +[GLOBAL _tramplin_c0] +[GLOBAL _tramplin_c1] +[GLOBAL _tramplin_c2] +[GLOBAL _tramplin_c3] +[GLOBAL _tramplin_c4] +[GLOBAL _tramplin_c5] +[GLOBAL _tramplin_c6] +[GLOBAL _tramplin_c7] +[GLOBAL _tramplin_c8] +[GLOBAL _tramplin_c9] +[GLOBAL _tramplin_ca] +[GLOBAL _tramplin_cb] +[GLOBAL _tramplin_cc] +[GLOBAL _tramplin_cd] +[GLOBAL _tramplin_ce] +[GLOBAL _tramplin_cf] +[GLOBAL _tramplin_d0] +[GLOBAL _tramplin_d1] +[GLOBAL _tramplin_d2] +[GLOBAL _tramplin_d3] +[GLOBAL _tramplin_d4] +[GLOBAL _tramplin_d5] +[GLOBAL _tramplin_d6] +[GLOBAL _tramplin_d7] +[GLOBAL _tramplin_d8] +[GLOBAL _tramplin_d9] +[GLOBAL _tramplin_da] +[GLOBAL _tramplin_db] +[GLOBAL _tramplin_dc] +[GLOBAL _tramplin_dd] +[GLOBAL _tramplin_de] +[GLOBAL _tramplin_df] +[GLOBAL _tramplin_e0] +[GLOBAL _tramplin_e1] +[GLOBAL _tramplin_e2] +[GLOBAL _tramplin_e3] +[GLOBAL _tramplin_e4] +[GLOBAL _tramplin_e5] +[GLOBAL _tramplin_e6] +[GLOBAL _tramplin_e7] +[GLOBAL _tramplin_e8] +[GLOBAL _tramplin_e9] +[GLOBAL _tramplin_ea] +[GLOBAL _tramplin_eb] +[GLOBAL _tramplin_ec] +[GLOBAL _tramplin_ed] +[GLOBAL _tramplin_ee] +[GLOBAL _tramplin_ef] +[GLOBAL _tramplin_f0] +[GLOBAL _tramplin_f1] +[GLOBAL _tramplin_f2] +[GLOBAL _tramplin_f3] +[GLOBAL _tramplin_f4] +[GLOBAL _tramplin_f5] +[GLOBAL _tramplin_f6] +[GLOBAL _tramplin_f7] +[GLOBAL _tramplin_f8] +[GLOBAL _tramplin_f9] +[GLOBAL _tramplin_fa] +[GLOBAL _tramplin_fb] +[GLOBAL _tramplin_fc] +[GLOBAL _tramplin_fd] +[GLOBAL _tramplin_fe] +[GLOBAL _tramplin_ff] + +collect_context: + push ds + push es + push fs + push gs + pusha + + +tramplin_00: + push 0x0 + push 0x00 + jmp collect_context + +tramplin_01: + push 0x0 + push 0x01 + jmp collect_context + +tramplin_02: + push 0x0 + push 0x02 + jmp collect_context + +tramplin_03: + push 0x0 + push 0x03 + jmp collect_context + +tramplin_04: + push 0x0 + push 0x04 + jmp collect_context + +tramplin_05: + push 0x0 + push 0x05 + jmp collect_context + +tramplin_06: + push 0x0 + push 0x06 + jmp collect_context + +tramplin_07: + push 0x0 + push 0x07 + jmp collect_context + +tramplin_08: + push 0x08 + jmp collect_context + +tramplin_09: + push 0x0 + push 0x09 + jmp collect_context + +tramplin_0a: + push 0x0a + jmp collect_context + +tramplin_0b: + push 0x0b + jmp collect_context + +tramplin_0c: + push 0x0c + jmp collect_context + +tramplin_0d: + push 0x0d + jmp collect_context + +tramplin_0e: + push 0x0e + jmp collect_context + +tramplin_0f: + push 0x0 + push 0x0f + jmp collect_context + +tramplin_10: + push 0x0 + push 0x10 + jmp collect_context + +tramplin_11: + push 0x11 + jmp collect_context + +tramplin_12: + push 0x0 + push 0x12 + jmp collect_context + +tramplin_13: + push 0x0 + push 0x13 + jmp collect_context + +tramplin_14: + push 0x0 + push 0x14 + jmp collect_context + +tramplin_15: + push 0x15 + jmp collect_context + +tramplin_16: + push 0x0 + push 0x16 + jmp collect_context + +tramplin_17: + push 0x0 + push 0x17 + jmp collect_context + +tramplin_18: + push 0x0 + push 0x18 + jmp collect_context + +tramplin_19: + push 0x0 + push 0x19 + jmp collect_context + +tramplin_1a: + push 0x0 + push 0x1a + jmp collect_context + +tramplin_1b: + push 0x0 + push 0x1b + jmp collect_context + +tramplin_1c: + push 0x0 + push 0x1c + jmp collect_context + +tramplin_1d: + push 0x1d + jmp collect_context + +tramplin_1e: + push 0x1e + jmp collect_context + +tramplin_1f: + push 0x0 + push 0x1f + jmp collect_context + +tramplin_20: + push 0x0 + push 0x20 + jmp collect_context + +tramplin_21: + push 0x0 + push 0x21 + jmp collect_context + +tramplin_22: + push 0x0 + push 0x22 + jmp collect_context + +tramplin_23: + push 0x0 + push 0x23 + jmp collect_context + +tramplin_24: + push 0x0 + push 0x24 + jmp collect_context + +tramplin_25: + push 0x0 + push 0x25 + jmp collect_context + +tramplin_26: + push 0x0 + push 0x26 + jmp collect_context + +tramplin_27: + push 0x0 + push 0x27 + jmp collect_context + +tramplin_28: + push 0x0 + push 0x28 + jmp collect_context + +tramplin_29: + push 0x0 + push 0x29 + jmp collect_context + +tramplin_2a: + push 0x0 + push 0x2a + jmp collect_context + +tramplin_2b: + push 0x0 + push 0x2b + jmp collect_context + +tramplin_2c: + push 0x0 + push 0x2c + jmp collect_context + +tramplin_2d: + push 0x0 + push 0x2d + jmp collect_context + +tramplin_2e: + push 0x0 + push 0x2e + jmp collect_context + +tramplin_2f: + push 0x0 + push 0x2f + jmp collect_context + +tramplin_30: + push 0x0 + push 0x30 + jmp collect_context + +tramplin_31: + push 0x0 + push 0x31 + jmp collect_context + +tramplin_32: + push 0x0 + push 0x32 + jmp collect_context + +tramplin_33: + push 0x0 + push 0x33 + jmp collect_context + +tramplin_34: + push 0x0 + push 0x34 + jmp collect_context + +tramplin_35: + push 0x0 + push 0x35 + jmp collect_context + +tramplin_36: + push 0x0 + push 0x36 + jmp collect_context + +tramplin_37: + push 0x0 + push 0x37 + jmp collect_context + +tramplin_38: + push 0x0 + push 0x38 + jmp collect_context + +tramplin_39: + push 0x0 + push 0x39 + jmp collect_context + +tramplin_3a: + push 0x0 + push 0x3a + jmp collect_context + +tramplin_3b: + push 0x0 + push 0x3b + jmp collect_context + +tramplin_3c: + push 0x0 + push 0x3c + jmp collect_context + +tramplin_3d: + push 0x0 + push 0x3d + jmp collect_context + +tramplin_3e: + push 0x0 + push 0x3e + jmp collect_context + +tramplin_3f: + push 0x0 + push 0x3f + jmp collect_context + +tramplin_40: + push 0x0 + push 0x40 + jmp collect_context + +tramplin_41: + push 0x0 + push 0x41 + jmp collect_context + +tramplin_42: + push 0x0 + push 0x42 + jmp collect_context + +tramplin_43: + push 0x0 + push 0x43 + jmp collect_context + +tramplin_44: + push 0x0 + push 0x44 + jmp collect_context + +tramplin_45: + push 0x0 + push 0x45 + jmp collect_context + +tramplin_46: + push 0x0 + push 0x46 + jmp collect_context + +tramplin_47: + push 0x0 + push 0x47 + jmp collect_context + +tramplin_48: + push 0x0 + push 0x48 + jmp collect_context + +tramplin_49: + push 0x0 + push 0x49 + jmp collect_context + +tramplin_4a: + push 0x0 + push 0x4a + jmp collect_context + +tramplin_4b: + push 0x0 + push 0x4b + jmp collect_context + +tramplin_4c: + push 0x0 + push 0x4c + jmp collect_context + +tramplin_4d: + push 0x0 + push 0x4d + jmp collect_context + +tramplin_4e: + push 0x0 + push 0x4e + jmp collect_context + +tramplin_4f: + push 0x0 + push 0x4f + jmp collect_context + +tramplin_50: + push 0x0 + push 0x50 + jmp collect_context + +tramplin_51: + push 0x0 + push 0x51 + jmp collect_context + +tramplin_52: + push 0x0 + push 0x52 + jmp collect_context + +tramplin_53: + push 0x0 + push 0x53 + jmp collect_context + +tramplin_54: + push 0x0 + push 0x54 + jmp collect_context + +tramplin_55: + push 0x0 + push 0x55 + jmp collect_context + +tramplin_56: + push 0x0 + push 0x56 + jmp collect_context + +tramplin_57: + push 0x0 + push 0x57 + jmp collect_context + +tramplin_58: + push 0x0 + push 0x58 + jmp collect_context + +tramplin_59: + push 0x0 + push 0x59 + jmp collect_context + +tramplin_5a: + push 0x0 + push 0x5a + jmp collect_context + +tramplin_5b: + push 0x0 + push 0x5b + jmp collect_context + +tramplin_5c: + push 0x0 + push 0x5c + jmp collect_context + +tramplin_5d: + push 0x0 + push 0x5d + jmp collect_context + +tramplin_5e: + push 0x0 + push 0x5e + jmp collect_context + +tramplin_5f: + push 0x0 + push 0x5f + jmp collect_context + +tramplin_60: + push 0x0 + push 0x60 + jmp collect_context + +tramplin_61: + push 0x0 + push 0x61 + jmp collect_context + +tramplin_62: + push 0x0 + push 0x62 + jmp collect_context + +tramplin_63: + push 0x0 + push 0x63 + jmp collect_context + +tramplin_64: + push 0x0 + push 0x64 + jmp collect_context + +tramplin_65: + push 0x0 + push 0x65 + jmp collect_context + +tramplin_66: + push 0x0 + push 0x66 + jmp collect_context + +tramplin_67: + push 0x0 + push 0x67 + jmp collect_context + +tramplin_68: + push 0x0 + push 0x68 + jmp collect_context + +tramplin_69: + push 0x0 + push 0x69 + jmp collect_context + +tramplin_6a: + push 0x0 + push 0x6a + jmp collect_context + +tramplin_6b: + push 0x0 + push 0x6b + jmp collect_context + +tramplin_6c: + push 0x0 + push 0x6c + jmp collect_context + +tramplin_6d: + push 0x0 + push 0x6d + jmp collect_context + +tramplin_6e: + push 0x0 + push 0x6e + jmp collect_context + +tramplin_6f: + push 0x0 + push 0x6f + jmp collect_context + +tramplin_70: + push 0x0 + push 0x70 + jmp collect_context + +tramplin_71: + push 0x0 + push 0x71 + jmp collect_context + +tramplin_72: + push 0x0 + push 0x72 + jmp collect_context + +tramplin_73: + push 0x0 + push 0x73 + jmp collect_context + +tramplin_74: + push 0x0 + push 0x74 + jmp collect_context + +tramplin_75: + push 0x0 + push 0x75 + jmp collect_context + +tramplin_76: + push 0x0 + push 0x76 + jmp collect_context + +tramplin_77: + push 0x0 + push 0x77 + jmp collect_context + +tramplin_78: + push 0x0 + push 0x78 + jmp collect_context + +tramplin_79: + push 0x0 + push 0x79 + jmp collect_context + +tramplin_7a: + push 0x0 + push 0x7a + jmp collect_context + +tramplin_7b: + push 0x0 + push 0x7b + jmp collect_context + +tramplin_7c: + push 0x0 + push 0x7c + jmp collect_context + +tramplin_7d: + push 0x0 + push 0x7d + jmp collect_context + +tramplin_7e: + push 0x0 + push 0x7e + jmp collect_context + +tramplin_7f: + push 0x0 + push 0x7f + jmp collect_context + +tramplin_80: + push 0x0 + push 0x80 + jmp collect_context + +tramplin_81: + push 0x0 + push 0x81 + jmp collect_context + +tramplin_82: + push 0x0 + push 0x82 + jmp collect_context + +tramplin_83: + push 0x0 + push 0x83 + jmp collect_context + +tramplin_84: + push 0x0 + push 0x84 + jmp collect_context + +tramplin_85: + push 0x0 + push 0x85 + jmp collect_context + +tramplin_86: + push 0x0 + push 0x86 + jmp collect_context + +tramplin_87: + push 0x0 + push 0x87 + jmp collect_context + +tramplin_88: + push 0x0 + push 0x88 + jmp collect_context + +tramplin_89: + push 0x0 + push 0x89 + jmp collect_context + +tramplin_8a: + push 0x0 + push 0x8a + jmp collect_context + +tramplin_8b: + push 0x0 + push 0x8b + jmp collect_context + +tramplin_8c: + push 0x0 + push 0x8c + jmp collect_context + +tramplin_8d: + push 0x0 + push 0x8d + jmp collect_context + +tramplin_8e: + push 0x0 + push 0x8e + jmp collect_context + +tramplin_8f: + push 0x0 + push 0x8f + jmp collect_context + +tramplin_90: + push 0x0 + push 0x90 + jmp collect_context + +tramplin_91: + push 0x0 + push 0x91 + jmp collect_context + +tramplin_92: + push 0x0 + push 0x92 + jmp collect_context + +tramplin_93: + push 0x0 + push 0x93 + jmp collect_context + +tramplin_94: + push 0x0 + push 0x94 + jmp collect_context + +tramplin_95: + push 0x0 + push 0x95 + jmp collect_context + +tramplin_96: + push 0x0 + push 0x96 + jmp collect_context + +tramplin_97: + push 0x0 + push 0x97 + jmp collect_context + +tramplin_98: + push 0x0 + push 0x98 + jmp collect_context + +tramplin_99: + push 0x0 + push 0x99 + jmp collect_context + +tramplin_9a: + push 0x0 + push 0x9a + jmp collect_context + +tramplin_9b: + push 0x0 + push 0x9b + jmp collect_context + +tramplin_9c: + push 0x0 + push 0x9c + jmp collect_context + +tramplin_9d: + push 0x0 + push 0x9d + jmp collect_context + +tramplin_9e: + push 0x0 + push 0x9e + jmp collect_context + +tramplin_9f: + push 0x0 + push 0x9f + jmp collect_context + +tramplin_a0: + push 0x0 + push 0xa0 + jmp collect_context + +tramplin_a1: + push 0x0 + push 0xa1 + jmp collect_context + +tramplin_a2: + push 0x0 + push 0xa2 + jmp collect_context + +tramplin_a3: + push 0x0 + push 0xa3 + jmp collect_context + +tramplin_a4: + push 0x0 + push 0xa4 + jmp collect_context + +tramplin_a5: + push 0x0 + push 0xa5 + jmp collect_context + +tramplin_a6: + push 0x0 + push 0xa6 + jmp collect_context + +tramplin_a7: + push 0x0 + push 0xa7 + jmp collect_context + +tramplin_a8: + push 0x0 + push 0xa8 + jmp collect_context + +tramplin_a9: + push 0x0 + push 0xa9 + jmp collect_context + +tramplin_aa: + push 0x0 + push 0xaa + jmp collect_context + +tramplin_ab: + push 0x0 + push 0xab + jmp collect_context + +tramplin_ac: + push 0x0 + push 0xac + jmp collect_context + +tramplin_ad: + push 0x0 + push 0xad + jmp collect_context + +tramplin_ae: + push 0x0 + push 0xae + jmp collect_context + +tramplin_af: + push 0x0 + push 0xaf + jmp collect_context + +tramplin_b0: + push 0x0 + push 0xb0 + jmp collect_context + +tramplin_b1: + push 0x0 + push 0xb1 + jmp collect_context + +tramplin_b2: + push 0x0 + push 0xb2 + jmp collect_context + +tramplin_b3: + push 0x0 + push 0xb3 + jmp collect_context + +tramplin_b4: + push 0x0 + push 0xb4 + jmp collect_context + +tramplin_b5: + push 0x0 + push 0xb5 + jmp collect_context + +tramplin_b6: + push 0x0 + push 0xb6 + jmp collect_context + +tramplin_b7: + push 0x0 + push 0xb7 + jmp collect_context + +tramplin_b8: + push 0x0 + push 0xb8 + jmp collect_context + +tramplin_b9: + push 0x0 + push 0xb9 + jmp collect_context + +tramplin_ba: + push 0x0 + push 0xba + jmp collect_context + +tramplin_bb: + push 0x0 + push 0xbb + jmp collect_context + +tramplin_bc: + push 0x0 + push 0xbc + jmp collect_context + +tramplin_bd: + push 0x0 + push 0xbd + jmp collect_context + +tramplin_be: + push 0x0 + push 0xbe + jmp collect_context + +tramplin_bf: + push 0x0 + push 0xbf + jmp collect_context + +tramplin_c0: + push 0x0 + push 0xc0 + jmp collect_context + +tramplin_c1: + push 0x0 + push 0xc1 + jmp collect_context + +tramplin_c2: + push 0x0 + push 0xc2 + jmp collect_context + +tramplin_c3: + push 0x0 + push 0xc3 + jmp collect_context + +tramplin_c4: + push 0x0 + push 0xc4 + jmp collect_context + +tramplin_c5: + push 0x0 + push 0xc5 + jmp collect_context + +tramplin_c6: + push 0x0 + push 0xc6 + jmp collect_context + +tramplin_c7: + push 0x0 + push 0xc7 + jmp collect_context + +tramplin_c8: + push 0x0 + push 0xc8 + jmp collect_context + +tramplin_c9: + push 0x0 + push 0xc9 + jmp collect_context + +tramplin_ca: + push 0x0 + push 0xca + jmp collect_context + +tramplin_cb: + push 0x0 + push 0xcb + jmp collect_context + +tramplin_cc: + push 0x0 + push 0xcc + jmp collect_context + +tramplin_cd: + push 0x0 + push 0xcd + jmp collect_context + +tramplin_ce: + push 0x0 + push 0xce + jmp collect_context + +tramplin_cf: + push 0x0 + push 0xcf + jmp collect_context + +tramplin_d0: + push 0x0 + push 0xd0 + jmp collect_context + +tramplin_d1: + push 0x0 + push 0xd1 + jmp collect_context + +tramplin_d2: + push 0x0 + push 0xd2 + jmp collect_context + +tramplin_d3: + push 0x0 + push 0xd3 + jmp collect_context + +tramplin_d4: + push 0x0 + push 0xd4 + jmp collect_context + +tramplin_d5: + push 0x0 + push 0xd5 + jmp collect_context + +tramplin_d6: + push 0x0 + push 0xd6 + jmp collect_context + +tramplin_d7: + push 0x0 + push 0xd7 + jmp collect_context + +tramplin_d8: + push 0x0 + push 0xd8 + jmp collect_context + +tramplin_d9: + push 0x0 + push 0xd9 + jmp collect_context + +tramplin_da: + push 0x0 + push 0xda + jmp collect_context + +tramplin_db: + push 0x0 + push 0xdb + jmp collect_context + +tramplin_dc: + push 0x0 + push 0xdc + jmp collect_context + +tramplin_dd: + push 0x0 + push 0xdd + jmp collect_context + +tramplin_de: + push 0x0 + push 0xde + jmp collect_context + +tramplin_df: + push 0x0 + push 0xdf + jmp collect_context + +tramplin_e0: + push 0x0 + push 0xe0 + jmp collect_context + +tramplin_e1: + push 0x0 + push 0xe1 + jmp collect_context + +tramplin_e2: + push 0x0 + push 0xe2 + jmp collect_context + +tramplin_e3: + push 0x0 + push 0xe3 + jmp collect_context + +tramplin_e4: + push 0x0 + push 0xe4 + jmp collect_context + +tramplin_e5: + push 0x0 + push 0xe5 + jmp collect_context + +tramplin_e6: + push 0x0 + push 0xe6 + jmp collect_context + +tramplin_e7: + push 0x0 + push 0xe7 + jmp collect_context + +tramplin_e8: + push 0x0 + push 0xe8 + jmp collect_context + +tramplin_e9: + push 0x0 + push 0xe9 + jmp collect_context + +tramplin_ea: + push 0x0 + push 0xea + jmp collect_context + +tramplin_eb: + push 0x0 + push 0xeb + jmp collect_context + +tramplin_ec: + push 0x0 + push 0xec + jmp collect_context + +tramplin_ed: + push 0x0 + push 0xed + jmp collect_context + +tramplin_ee: + push 0x0 + push 0xee + jmp collect_context + +tramplin_ef: + push 0x0 + push 0xef + jmp collect_context + +tramplin_f0: + push 0x0 + push 0xf0 + jmp collect_context + +tramplin_f1: + push 0x0 + push 0xf1 + jmp collect_context + +tramplin_f2: + push 0x0 + push 0xf2 + jmp collect_context + +tramplin_f3: + push 0x0 + push 0xf3 + jmp collect_context + +tramplin_f4: + push 0x0 + push 0xf4 + jmp collect_context + +tramplin_f5: + push 0x0 + push 0xf5 + jmp collect_context + +tramplin_f6: + push 0x0 + push 0xf6 + jmp collect_context + +tramplin_f7: + push 0x0 + push 0xf7 + jmp collect_context + +tramplin_f8: + push 0x0 + push 0xf8 + jmp collect_context + +tramplin_f9: + push 0x0 + push 0xf9 + jmp collect_context + +tramplin_fa: + push 0x0 + push 0xfa + jmp collect_context + +tramplin_fb: + push 0x0 + push 0xfb + jmp collect_context + +tramplin_fc: + push 0x0 + push 0xfc + jmp collect_context + +tramplin_fd: + push 0x0 + push 0xfd + jmp collect_context + +tramplin_fe: + push 0x0 + push 0xfe + jmp collect_context + +tramplin_ff: + push 0x0 + push 0xff + jmp collect_context + From c87c6667c7d0a7c0cd431c7d5ee64c1fc5c3e405 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 3 Feb 2024 23:37:41 +0700 Subject: [PATCH 63/88] make things clean, add context.h --- Makefile | 13 ++- src/kernel/kernel.c | 2 +- src/kernel/sys/context/context.h | 35 +++++++ src/kernel/sys/context/gen_asm_context.py | 8 +- src/kernel/sys/context/tramplinsWContext.asm | 96 +++++++++---------- .../{gen_interruption.py => gen_tramplins.py} | 2 +- src/kernel/sys/idt.h | 10 +- src/kernel/sys/interruption.c | 51 ++++++++++ src/kernel/sys/interruption.h | 20 ++++ src/kernel/sys/interruption.template | 25 ----- .../sys/{interruption.hpp => tramplins.hpp} | 47 --------- src/kernel/sys/tramplins.hpp.template | 7 ++ 12 files changed, 181 insertions(+), 135 deletions(-) create mode 100644 src/kernel/sys/context/context.h rename src/kernel/sys/{gen_interruption.py => gen_tramplins.py} (82%) create mode 100644 src/kernel/sys/interruption.c create mode 100644 src/kernel/sys/interruption.h delete mode 100644 src/kernel/sys/interruption.template rename src/kernel/sys/{interruption.hpp => tramplins.hpp} (89%) create mode 100644 src/kernel/sys/tramplins.hpp.template diff --git a/Makefile b/Makefile index 0da419a..097261a 100644 --- a/Makefile +++ b/Makefile @@ -81,8 +81,13 @@ $(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ +$(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ + $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h \ - $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.hpp $(KERNEL_SOURCE_DIR)/sys/pic/pic.h + $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 @@ -91,9 +96,9 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ - $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o \ - $(KERNEL_BUILD_DIR)/sys/memory/alloc.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o +$(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ + $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ + $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index a8abdea..cf61f9b 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,7 +2,7 @@ #include "./sys/io/io.h" #include "./sys/memory/memory.h" -#include "./sys/interruption.hpp" +#include "./sys/interruption.h" #include "./sys/pic/pic.h" // compare [first, first_end) and second (until '\0') diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h new file mode 100644 index 0000000..511b5eb --- /dev/null +++ b/src/kernel/sys/context/context.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +#pragma pack(push, 4) +typedef struct +{ + uint32_t edi; + uint32_t esi; + uint32_t ebp; + uint32_t esp; + uint32_t ebx; + uint32_t edx; + uint32_t ecx; + uint32_t eax; + uint16_t gs; + //uint16_t padding_1; + uint16_t fs; + //uint16_t padding_2; + uint16_t es; + //uint16_t padding_3; + uint16_t ds; + //uint16_t padding_4; + uint32_t vector; + uint32_t error_code; + uint32_t eip; + uint16_t cs; + //uint16_t padding_5; + uint32_t eflags; + uint32_t prev_esp; + uint16_t ss; + //uint16_t padding_6; + +} context; +#pragma pack(pop) \ No newline at end of file diff --git a/src/kernel/sys/context/gen_asm_context.py b/src/kernel/sys/context/gen_asm_context.py index 1c4aa56..8e30fe7 100644 --- a/src/kernel/sys/context/gen_asm_context.py +++ b/src/kernel/sys/context/gen_asm_context.py @@ -1,13 +1,13 @@ exceptions_with_error_code = {8, 10, 11, 12, 13, 14, 17, 21, 29, 30} -with open("tramplinsWContext.asm", "w") as f: +with open("./src/kernel/sys/context/tramplinsWContext.asm", "w") as f: wr = f.write wr("[BITS 32]\n\n") for i in range(0, 256): - wr(f"[GLOBAL _tramplin_{('0' if i <= 15 else '') + str(hex(i))[2:]}]\n") + wr(f"[GLOBAL _tramplin_{str(hex(i))[2:]}]\n") wr(''' collect_context: @@ -20,9 +20,9 @@ wr("\n\n") for i in range(0, 256): - wr(f"tramplin_{('0' if i <= 15 else '') + str(hex(i))[2:]}:\n") + wr(f"tramplin_{str(hex(i))[2:]}:\n") if i not in exceptions_with_error_code: wr("\tpush 0x0\n") - wr(f"\tpush 0x{('0' if i <= 15 else '') + str(hex(i))[2:]}\n") + wr(f"\tpush 0x{str(hex(i))[2:]}\n") wr("\tjmp collect_context") wr("\n\n") \ No newline at end of file diff --git a/src/kernel/sys/context/tramplinsWContext.asm b/src/kernel/sys/context/tramplinsWContext.asm index 84c9ed8..e7ca549 100644 --- a/src/kernel/sys/context/tramplinsWContext.asm +++ b/src/kernel/sys/context/tramplinsWContext.asm @@ -1,21 +1,21 @@ [BITS 32] -[GLOBAL _tramplin_00] -[GLOBAL _tramplin_01] -[GLOBAL _tramplin_02] -[GLOBAL _tramplin_03] -[GLOBAL _tramplin_04] -[GLOBAL _tramplin_05] -[GLOBAL _tramplin_06] -[GLOBAL _tramplin_07] -[GLOBAL _tramplin_08] -[GLOBAL _tramplin_09] -[GLOBAL _tramplin_0a] -[GLOBAL _tramplin_0b] -[GLOBAL _tramplin_0c] -[GLOBAL _tramplin_0d] -[GLOBAL _tramplin_0e] -[GLOBAL _tramplin_0f] +[GLOBAL _tramplin_0] +[GLOBAL _tramplin_1] +[GLOBAL _tramplin_2] +[GLOBAL _tramplin_3] +[GLOBAL _tramplin_4] +[GLOBAL _tramplin_5] +[GLOBAL _tramplin_6] +[GLOBAL _tramplin_7] +[GLOBAL _tramplin_8] +[GLOBAL _tramplin_9] +[GLOBAL _tramplin_a] +[GLOBAL _tramplin_b] +[GLOBAL _tramplin_c] +[GLOBAL _tramplin_d] +[GLOBAL _tramplin_e] +[GLOBAL _tramplin_f] [GLOBAL _tramplin_10] [GLOBAL _tramplin_11] [GLOBAL _tramplin_12] @@ -265,78 +265,78 @@ collect_context: pusha -tramplin_00: +tramplin_0: + push 0x0 push 0x0 - push 0x00 jmp collect_context -tramplin_01: +tramplin_1: push 0x0 - push 0x01 + push 0x1 jmp collect_context -tramplin_02: +tramplin_2: push 0x0 - push 0x02 + push 0x2 jmp collect_context -tramplin_03: +tramplin_3: push 0x0 - push 0x03 + push 0x3 jmp collect_context -tramplin_04: +tramplin_4: push 0x0 - push 0x04 + push 0x4 jmp collect_context -tramplin_05: +tramplin_5: push 0x0 - push 0x05 + push 0x5 jmp collect_context -tramplin_06: +tramplin_6: push 0x0 - push 0x06 + push 0x6 jmp collect_context -tramplin_07: +tramplin_7: push 0x0 - push 0x07 + push 0x7 jmp collect_context -tramplin_08: - push 0x08 +tramplin_8: + push 0x8 jmp collect_context -tramplin_09: +tramplin_9: push 0x0 - push 0x09 + push 0x9 jmp collect_context -tramplin_0a: - push 0x0a +tramplin_a: + push 0xa jmp collect_context -tramplin_0b: - push 0x0b +tramplin_b: + push 0xb jmp collect_context -tramplin_0c: - push 0x0c +tramplin_c: + push 0xc jmp collect_context -tramplin_0d: - push 0x0d +tramplin_d: + push 0xd jmp collect_context -tramplin_0e: - push 0x0e +tramplin_e: + push 0xe jmp collect_context -tramplin_0f: +tramplin_f: push 0x0 - push 0x0f + push 0xf jmp collect_context tramplin_10: diff --git a/src/kernel/sys/gen_interruption.py b/src/kernel/sys/gen_tramplins.py similarity index 82% rename from src/kernel/sys/gen_interruption.py rename to src/kernel/sys/gen_tramplins.py index 86813d5..16b5cc1 100644 --- a/src/kernel/sys/gen_interruption.py +++ b/src/kernel/sys/gen_tramplins.py @@ -2,7 +2,7 @@ root = os.getcwd() + '/src/kernel/sys' -with open(root + '/interruption.template','r') as ff, open(root + '/interruption.hpp','w') as sf: +with open(root + '/tramplins.hpp.template','r') as ff, open(root + '/tramplins.hpp','w') as sf: for line in ff: sf.write(line) all_tramplins = '' diff --git a/src/kernel/sys/idt.h b/src/kernel/sys/idt.h index 5c66a8f..c834ddd 100644 --- a/src/kernel/sys/idt.h +++ b/src/kernel/sys/idt.h @@ -4,10 +4,10 @@ #pragma pack(push, 1) typedef struct { - uint16_t offset_1; // offset bits 0..15 - uint16_t selector; // a code segment selector in GDT or LDT - uint8_t zero; // unused, set to 0 - uint8_t type_attributes; // gate type, dpl, and p fields - uint16_t offset_2; // offset bits 16..31 + uint16_t offset_1; // offset bits 0..15 + uint16_t selector; // a code segment selector in GDT or LDT + uint8_t zero; // unused, set to 0 + uint8_t gate_type; // gate type, dpl, and p fields + uint16_t offset_2; // offset bits 16..31 } InterruptDescriptor32; #pragma pack(pop) \ No newline at end of file diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c new file mode 100644 index 0000000..e9aca13 --- /dev/null +++ b/src/kernel/sys/interruption.c @@ -0,0 +1,51 @@ +#include "./interruption.h" + +void kernel_panic(char* str, int num) +{ + init_printer(); + printf(str, num); + while (1); +} + +void panic_handler(int vector) +{ + kernel_panic("unhandled interrupt %x", vector); +} + +void interrupt_handler(context* ctx) +{ + switch(ctx->vector) + { + + } +} + +void register_handlers() +{ + InterruptDescriptor32* idt = kernel_calloc(sizeof(InterruptDescriptor32), 256); + if (idt == (InterruptDescriptor32*)0) + { + printf("[EXC] register_handlers(): not enough memory!"); + } + + for (int i = 0; i < 256; ++i) + { + if (i < 32) + { + idt[i].gate_type = 0xF; + } + else + { + idt[i].gate_type = 0xE; + } + idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits + idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits + idt[i].selector = 0b1000; + } + + IDTD idtd; + idtd.IDT_size = sizeof(InterruptDescriptor32) * 256 - 1; + idtd.linear_addr_IDT = idt; + + asm("lidt %0" :: "m"(idtd)); +} \ No newline at end of file diff --git a/src/kernel/sys/interruption.h b/src/kernel/sys/interruption.h new file mode 100644 index 0000000..efb6672 --- /dev/null +++ b/src/kernel/sys/interruption.h @@ -0,0 +1,20 @@ +#pragma once + +#include "./io/io.h" +#include "./tramplins.hpp" +#include "./idt.h" +#include "./memory/memory.h" +#include "./context/context.h" + +#pragma pack(push, 1) +typedef struct { + uint16_t IDT_size; + uint32_t linear_addr_IDT; +} IDTD; +#pragma pack(pop) + +void kernel_panic(char* str, int num); +void panic_handler(int vector); + +void interrupt_handler(context* ctx); +void register_handlers(); \ No newline at end of file diff --git a/src/kernel/sys/interruption.template b/src/kernel/sys/interruption.template deleted file mode 100644 index 357a93e..0000000 --- a/src/kernel/sys/interruption.template +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "./idt.h" - -#define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } -#define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; - -static void panic_handler(int vector) -{ - kernel_panic("unhandled interrupt %x", vector); -} - -void register_handlers() -{ - for (int i = 0; i < sizeof(tramplins) / sizeof(tramplins[0]); ++i) - { - void* handler = tramplins[i]; - signed char* low_16_bits = (signed char*) handler; - signed char* high_16_bits = low_16_bits + 1; - InterruptDescriptor[i] = ... - } -} - -// AUTO-GENERATED CODE IS BELOW - diff --git a/src/kernel/sys/interruption.hpp b/src/kernel/sys/tramplins.hpp similarity index 89% rename from src/kernel/sys/interruption.hpp rename to src/kernel/sys/tramplins.hpp index f51c9a9..f086fcc 100644 --- a/src/kernel/sys/interruption.hpp +++ b/src/kernel/sys/tramplins.hpp @@ -1,25 +1,8 @@ #pragma once -#include "./idt.h" -#include "./memory/memory.h" -#include "./io/io.h" - #define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; -static void kernel_panic(char* str, int num) -{ - init_printer(); - printf(str, num); - while (1); -} - -static void panic_handler(int vector) -{ - kernel_panic("unhandled interrupt %x", vector); -} - - // AUTO-GENERATED CODE IS BELOW __gen_tramplin(0) @@ -310,33 +293,3 @@ __gen_tramplins(tramplin_0, tramplin_1, tramplin_2, tramplin_3, tramplin_4, tram tramplin_e8, tramplin_e9, tramplin_ea, tramplin_eb, tramplin_ec, tramplin_ed, tramplin_ee, tramplin_ef, tramplin_f0, tramplin_f1, tramplin_f2, tramplin_f3, tramplin_f4, tramplin_f5, tramplin_f6, tramplin_f7, tramplin_f8, tramplin_f9, tramplin_fa, tramplin_fb, tramplin_fc, tramplin_fd, tramplin_fe, tramplin_ff) - -#pragma pack(push, 1) -typedef struct { - uint16_t IDT_size; - uint32_t linear_addr_IDT; -} IDTD; -#pragma pack(pop) - -void register_handlers() -{ - InterruptDescriptor32* idt = kernel_calloc(sizeof(InterruptDescriptor32), 256); - if (idt == (InterruptDescriptor32*)0) - { - printf("[EXC] register_handlers(): not enough memory!"); - } - - for (int i = 0; i < 256; ++i) - { - idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits - idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits - idt[i].type_attributes = 0b10001110; - idt[i].selector = 0b1000; - } - - IDTD idtd; - idtd.IDT_size = sizeof(InterruptDescriptor32) * 256 - 1; - idtd.linear_addr_IDT = idt; - - asm("lidt %0" :: "m"(idtd)); -} diff --git a/src/kernel/sys/tramplins.hpp.template b/src/kernel/sys/tramplins.hpp.template new file mode 100644 index 0000000..4a0d521 --- /dev/null +++ b/src/kernel/sys/tramplins.hpp.template @@ -0,0 +1,7 @@ +#pragma once + +#define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } +#define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; + +// AUTO-GENERATED CODE IS BELOW + From 61dd0b67d94e9285a92ddfdb3d1593e2f778ecd4 Mon Sep 17 00:00:00 2001 From: qq Date: Mon, 12 Feb 2024 03:01:32 +0700 Subject: [PATCH 64/88] make: add flag for windows (remove leading underscores) --- Makefile | 2 +- src/kernel/sys/context/context.c | 38 ++ src/kernel/sys/context/context.h | 6 +- src/kernel/sys/context/gen_asm_context.py | 2 +- src/kernel/sys/context/tramplinsWContext.asm | 520 ++++++++++--------- 5 files changed, 309 insertions(+), 259 deletions(-) create mode 100644 src/kernel/sys/context/context.c diff --git a/Makefile b/Makefile index 097261a..698e762 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ KERNEL_ENTRY_ADDRESS = 0x20200 ifeq ($(OS), Windows_NT) ASM = ${NASM_PATH}\nasm DD = ${CYGWIN_PATH}\bin\dd - GCC = gcc + GCC = gcc -fno-leading-underscore MCOPY = MKFS = LD = ld diff --git a/src/kernel/sys/context/context.c b/src/kernel/sys/context/context.c new file mode 100644 index 0000000..e67832f --- /dev/null +++ b/src/kernel/sys/context/context.c @@ -0,0 +1,38 @@ +#pragma once + +#include "context.h" +#include "../io/io.h" + +void interrupt_handler(context* ctx) { + switch (ctx->vector) { + case 0x20: timer_interrupt(ctx); break; + default: default_panic_handler(ctx); break; + } +} + +void default_panic_handler(context* ctx) { + init_printer(); + + unsigned int current_eflags; + + asm("pushf\n\t" + "pop %0" + : "=g"(current_eflags) + : + : "memory" + ); + + printf("Kernel panic: unhandled interrupt %X, interrupted process context:\n" + " EAX = %X ECX = %X EDX = %X EBX = %X\n" + " ESP = %X EBP = %X ESI = %X EDI = %X\n" + " DS = %X ES = %X FS = %X GS = %X\n" + " CS = %X SS = %X EIP = %X\n" + " EFLAGS (interrupted) = %X " "EFLAGS (current) = %X\n" + " error code = %X", + ctx->vector, ctx->eax, ctx->edx, ctx->ebx, + ctx->esp, ctx->ebp, ctx->ebp, ctx->esi, ctx->edi, + ctx->ds, ctx->es, ctx->fs, ctx->gs, + ctx->cs, ctx->ss, ctx->eip, + ctx->eflags, current_eflags, + ctx->error_code); +} \ No newline at end of file diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index 511b5eb..84120e0 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -32,4 +32,8 @@ typedef struct //uint16_t padding_6; } context; -#pragma pack(pop) \ No newline at end of file +#pragma pack(pop) + +void interrupt_handler(context* ctx); + +void default_panic_handler(context* ctx); \ No newline at end of file diff --git a/src/kernel/sys/context/gen_asm_context.py b/src/kernel/sys/context/gen_asm_context.py index 8e30fe7..30166b2 100644 --- a/src/kernel/sys/context/gen_asm_context.py +++ b/src/kernel/sys/context/gen_asm_context.py @@ -7,7 +7,7 @@ wr("[BITS 32]\n\n") for i in range(0, 256): - wr(f"[GLOBAL _tramplin_{str(hex(i))[2:]}]\n") + wr(f"[GLOBAL tramplin_{str(hex(i))[2:]}]\n") wr(''' collect_context: diff --git a/src/kernel/sys/context/tramplinsWContext.asm b/src/kernel/sys/context/tramplinsWContext.asm index e7ca549..2687636 100644 --- a/src/kernel/sys/context/tramplinsWContext.asm +++ b/src/kernel/sys/context/tramplinsWContext.asm @@ -1,261 +1,263 @@ [BITS 32] -[GLOBAL _tramplin_0] -[GLOBAL _tramplin_1] -[GLOBAL _tramplin_2] -[GLOBAL _tramplin_3] -[GLOBAL _tramplin_4] -[GLOBAL _tramplin_5] -[GLOBAL _tramplin_6] -[GLOBAL _tramplin_7] -[GLOBAL _tramplin_8] -[GLOBAL _tramplin_9] -[GLOBAL _tramplin_a] -[GLOBAL _tramplin_b] -[GLOBAL _tramplin_c] -[GLOBAL _tramplin_d] -[GLOBAL _tramplin_e] -[GLOBAL _tramplin_f] -[GLOBAL _tramplin_10] -[GLOBAL _tramplin_11] -[GLOBAL _tramplin_12] -[GLOBAL _tramplin_13] -[GLOBAL _tramplin_14] -[GLOBAL _tramplin_15] -[GLOBAL _tramplin_16] -[GLOBAL _tramplin_17] -[GLOBAL _tramplin_18] -[GLOBAL _tramplin_19] -[GLOBAL _tramplin_1a] -[GLOBAL _tramplin_1b] -[GLOBAL _tramplin_1c] -[GLOBAL _tramplin_1d] -[GLOBAL _tramplin_1e] -[GLOBAL _tramplin_1f] -[GLOBAL _tramplin_20] -[GLOBAL _tramplin_21] -[GLOBAL _tramplin_22] -[GLOBAL _tramplin_23] -[GLOBAL _tramplin_24] -[GLOBAL _tramplin_25] -[GLOBAL _tramplin_26] -[GLOBAL _tramplin_27] -[GLOBAL _tramplin_28] -[GLOBAL _tramplin_29] -[GLOBAL _tramplin_2a] -[GLOBAL _tramplin_2b] -[GLOBAL _tramplin_2c] -[GLOBAL _tramplin_2d] -[GLOBAL _tramplin_2e] -[GLOBAL _tramplin_2f] -[GLOBAL _tramplin_30] -[GLOBAL _tramplin_31] -[GLOBAL _tramplin_32] -[GLOBAL _tramplin_33] -[GLOBAL _tramplin_34] -[GLOBAL _tramplin_35] -[GLOBAL _tramplin_36] -[GLOBAL _tramplin_37] -[GLOBAL _tramplin_38] -[GLOBAL _tramplin_39] -[GLOBAL _tramplin_3a] -[GLOBAL _tramplin_3b] -[GLOBAL _tramplin_3c] -[GLOBAL _tramplin_3d] -[GLOBAL _tramplin_3e] -[GLOBAL _tramplin_3f] -[GLOBAL _tramplin_40] -[GLOBAL _tramplin_41] -[GLOBAL _tramplin_42] -[GLOBAL _tramplin_43] -[GLOBAL _tramplin_44] -[GLOBAL _tramplin_45] -[GLOBAL _tramplin_46] -[GLOBAL _tramplin_47] -[GLOBAL _tramplin_48] -[GLOBAL _tramplin_49] -[GLOBAL _tramplin_4a] -[GLOBAL _tramplin_4b] -[GLOBAL _tramplin_4c] -[GLOBAL _tramplin_4d] -[GLOBAL _tramplin_4e] -[GLOBAL _tramplin_4f] -[GLOBAL _tramplin_50] -[GLOBAL _tramplin_51] -[GLOBAL _tramplin_52] -[GLOBAL _tramplin_53] -[GLOBAL _tramplin_54] -[GLOBAL _tramplin_55] -[GLOBAL _tramplin_56] -[GLOBAL _tramplin_57] -[GLOBAL _tramplin_58] -[GLOBAL _tramplin_59] -[GLOBAL _tramplin_5a] -[GLOBAL _tramplin_5b] -[GLOBAL _tramplin_5c] -[GLOBAL _tramplin_5d] -[GLOBAL _tramplin_5e] -[GLOBAL _tramplin_5f] -[GLOBAL _tramplin_60] -[GLOBAL _tramplin_61] -[GLOBAL _tramplin_62] -[GLOBAL _tramplin_63] -[GLOBAL _tramplin_64] -[GLOBAL _tramplin_65] -[GLOBAL _tramplin_66] -[GLOBAL _tramplin_67] -[GLOBAL _tramplin_68] -[GLOBAL _tramplin_69] -[GLOBAL _tramplin_6a] -[GLOBAL _tramplin_6b] -[GLOBAL _tramplin_6c] -[GLOBAL _tramplin_6d] -[GLOBAL _tramplin_6e] -[GLOBAL _tramplin_6f] -[GLOBAL _tramplin_70] -[GLOBAL _tramplin_71] -[GLOBAL _tramplin_72] -[GLOBAL _tramplin_73] -[GLOBAL _tramplin_74] -[GLOBAL _tramplin_75] -[GLOBAL _tramplin_76] -[GLOBAL _tramplin_77] -[GLOBAL _tramplin_78] -[GLOBAL _tramplin_79] -[GLOBAL _tramplin_7a] -[GLOBAL _tramplin_7b] -[GLOBAL _tramplin_7c] -[GLOBAL _tramplin_7d] -[GLOBAL _tramplin_7e] -[GLOBAL _tramplin_7f] -[GLOBAL _tramplin_80] -[GLOBAL _tramplin_81] -[GLOBAL _tramplin_82] -[GLOBAL _tramplin_83] -[GLOBAL _tramplin_84] -[GLOBAL _tramplin_85] -[GLOBAL _tramplin_86] -[GLOBAL _tramplin_87] -[GLOBAL _tramplin_88] -[GLOBAL _tramplin_89] -[GLOBAL _tramplin_8a] -[GLOBAL _tramplin_8b] -[GLOBAL _tramplin_8c] -[GLOBAL _tramplin_8d] -[GLOBAL _tramplin_8e] -[GLOBAL _tramplin_8f] -[GLOBAL _tramplin_90] -[GLOBAL _tramplin_91] -[GLOBAL _tramplin_92] -[GLOBAL _tramplin_93] -[GLOBAL _tramplin_94] -[GLOBAL _tramplin_95] -[GLOBAL _tramplin_96] -[GLOBAL _tramplin_97] -[GLOBAL _tramplin_98] -[GLOBAL _tramplin_99] -[GLOBAL _tramplin_9a] -[GLOBAL _tramplin_9b] -[GLOBAL _tramplin_9c] -[GLOBAL _tramplin_9d] -[GLOBAL _tramplin_9e] -[GLOBAL _tramplin_9f] -[GLOBAL _tramplin_a0] -[GLOBAL _tramplin_a1] -[GLOBAL _tramplin_a2] -[GLOBAL _tramplin_a3] -[GLOBAL _tramplin_a4] -[GLOBAL _tramplin_a5] -[GLOBAL _tramplin_a6] -[GLOBAL _tramplin_a7] -[GLOBAL _tramplin_a8] -[GLOBAL _tramplin_a9] -[GLOBAL _tramplin_aa] -[GLOBAL _tramplin_ab] -[GLOBAL _tramplin_ac] -[GLOBAL _tramplin_ad] -[GLOBAL _tramplin_ae] -[GLOBAL _tramplin_af] -[GLOBAL _tramplin_b0] -[GLOBAL _tramplin_b1] -[GLOBAL _tramplin_b2] -[GLOBAL _tramplin_b3] -[GLOBAL _tramplin_b4] -[GLOBAL _tramplin_b5] -[GLOBAL _tramplin_b6] -[GLOBAL _tramplin_b7] -[GLOBAL _tramplin_b8] -[GLOBAL _tramplin_b9] -[GLOBAL _tramplin_ba] -[GLOBAL _tramplin_bb] -[GLOBAL _tramplin_bc] -[GLOBAL _tramplin_bd] -[GLOBAL _tramplin_be] -[GLOBAL _tramplin_bf] -[GLOBAL _tramplin_c0] -[GLOBAL _tramplin_c1] -[GLOBAL _tramplin_c2] -[GLOBAL _tramplin_c3] -[GLOBAL _tramplin_c4] -[GLOBAL _tramplin_c5] -[GLOBAL _tramplin_c6] -[GLOBAL _tramplin_c7] -[GLOBAL _tramplin_c8] -[GLOBAL _tramplin_c9] -[GLOBAL _tramplin_ca] -[GLOBAL _tramplin_cb] -[GLOBAL _tramplin_cc] -[GLOBAL _tramplin_cd] -[GLOBAL _tramplin_ce] -[GLOBAL _tramplin_cf] -[GLOBAL _tramplin_d0] -[GLOBAL _tramplin_d1] -[GLOBAL _tramplin_d2] -[GLOBAL _tramplin_d3] -[GLOBAL _tramplin_d4] -[GLOBAL _tramplin_d5] -[GLOBAL _tramplin_d6] -[GLOBAL _tramplin_d7] -[GLOBAL _tramplin_d8] -[GLOBAL _tramplin_d9] -[GLOBAL _tramplin_da] -[GLOBAL _tramplin_db] -[GLOBAL _tramplin_dc] -[GLOBAL _tramplin_dd] -[GLOBAL _tramplin_de] -[GLOBAL _tramplin_df] -[GLOBAL _tramplin_e0] -[GLOBAL _tramplin_e1] -[GLOBAL _tramplin_e2] -[GLOBAL _tramplin_e3] -[GLOBAL _tramplin_e4] -[GLOBAL _tramplin_e5] -[GLOBAL _tramplin_e6] -[GLOBAL _tramplin_e7] -[GLOBAL _tramplin_e8] -[GLOBAL _tramplin_e9] -[GLOBAL _tramplin_ea] -[GLOBAL _tramplin_eb] -[GLOBAL _tramplin_ec] -[GLOBAL _tramplin_ed] -[GLOBAL _tramplin_ee] -[GLOBAL _tramplin_ef] -[GLOBAL _tramplin_f0] -[GLOBAL _tramplin_f1] -[GLOBAL _tramplin_f2] -[GLOBAL _tramplin_f3] -[GLOBAL _tramplin_f4] -[GLOBAL _tramplin_f5] -[GLOBAL _tramplin_f6] -[GLOBAL _tramplin_f7] -[GLOBAL _tramplin_f8] -[GLOBAL _tramplin_f9] -[GLOBAL _tramplin_fa] -[GLOBAL _tramplin_fb] -[GLOBAL _tramplin_fc] -[GLOBAL _tramplin_fd] -[GLOBAL _tramplin_fe] -[GLOBAL _tramplin_ff] +[GLOBAL tramplin_0] +[GLOBAL tramplin_1] +[GLOBAL tramplin_2] +[GLOBAL tramplin_3] +[GLOBAL tramplin_4] +[GLOBAL tramplin_5] +[GLOBAL tramplin_6] +[GLOBAL tramplin_7] +[GLOBAL tramplin_8] +[GLOBAL tramplin_9] +[GLOBAL tramplin_a] +[GLOBAL tramplin_b] +[GLOBAL tramplin_c] +[GLOBAL tramplin_d] +[GLOBAL tramplin_e] +[GLOBAL tramplin_f] +[GLOBAL tramplin_10] +[GLOBAL tramplin_11] +[GLOBAL tramplin_12] +[GLOBAL tramplin_13] +[GLOBAL tramplin_14] +[GLOBAL tramplin_15] +[GLOBAL tramplin_16] +[GLOBAL tramplin_17] +[GLOBAL tramplin_18] +[GLOBAL tramplin_19] +[GLOBAL tramplin_1a] +[GLOBAL tramplin_1b] +[GLOBAL tramplin_1c] +[GLOBAL tramplin_1d] +[GLOBAL tramplin_1e] +[GLOBAL tramplin_1f] +[GLOBAL tramplin_20] +[GLOBAL tramplin_21] +[GLOBAL tramplin_22] +[GLOBAL tramplin_23] +[GLOBAL tramplin_24] +[GLOBAL tramplin_25] +[GLOBAL tramplin_26] +[GLOBAL tramplin_27] +[GLOBAL tramplin_28] +[GLOBAL tramplin_29] +[GLOBAL tramplin_2a] +[GLOBAL tramplin_2b] +[GLOBAL tramplin_2c] +[GLOBAL tramplin_2d] +[GLOBAL tramplin_2e] +[GLOBAL tramplin_2f] +[GLOBAL tramplin_30] +[GLOBAL tramplin_31] +[GLOBAL tramplin_32] +[GLOBAL tramplin_33] +[GLOBAL tramplin_34] +[GLOBAL tramplin_35] +[GLOBAL tramplin_36] +[GLOBAL tramplin_37] +[GLOBAL tramplin_38] +[GLOBAL tramplin_39] +[GLOBAL tramplin_3a] +[GLOBAL tramplin_3b] +[GLOBAL tramplin_3c] +[GLOBAL tramplin_3d] +[GLOBAL tramplin_3e] +[GLOBAL tramplin_3f] +[GLOBAL tramplin_40] +[GLOBAL tramplin_41] +[GLOBAL tramplin_42] +[GLOBAL tramplin_43] +[GLOBAL tramplin_44] +[GLOBAL tramplin_45] +[GLOBAL tramplin_46] +[GLOBAL tramplin_47] +[GLOBAL tramplin_48] +[GLOBAL tramplin_49] +[GLOBAL tramplin_4a] +[GLOBAL tramplin_4b] +[GLOBAL tramplin_4c] +[GLOBAL tramplin_4d] +[GLOBAL tramplin_4e] +[GLOBAL tramplin_4f] +[GLOBAL tramplin_50] +[GLOBAL tramplin_51] +[GLOBAL tramplin_52] +[GLOBAL tramplin_53] +[GLOBAL tramplin_54] +[GLOBAL tramplin_55] +[GLOBAL tramplin_56] +[GLOBAL tramplin_57] +[GLOBAL tramplin_58] +[GLOBAL tramplin_59] +[GLOBAL tramplin_5a] +[GLOBAL tramplin_5b] +[GLOBAL tramplin_5c] +[GLOBAL tramplin_5d] +[GLOBAL tramplin_5e] +[GLOBAL tramplin_5f] +[GLOBAL tramplin_60] +[GLOBAL tramplin_61] +[GLOBAL tramplin_62] +[GLOBAL tramplin_63] +[GLOBAL tramplin_64] +[GLOBAL tramplin_65] +[GLOBAL tramplin_66] +[GLOBAL tramplin_67] +[GLOBAL tramplin_68] +[GLOBAL tramplin_69] +[GLOBAL tramplin_6a] +[GLOBAL tramplin_6b] +[GLOBAL tramplin_6c] +[GLOBAL tramplin_6d] +[GLOBAL tramplin_6e] +[GLOBAL tramplin_6f] +[GLOBAL tramplin_70] +[GLOBAL tramplin_71] +[GLOBAL tramplin_72] +[GLOBAL tramplin_73] +[GLOBAL tramplin_74] +[GLOBAL tramplin_75] +[GLOBAL tramplin_76] +[GLOBAL tramplin_77] +[GLOBAL tramplin_78] +[GLOBAL tramplin_79] +[GLOBAL tramplin_7a] +[GLOBAL tramplin_7b] +[GLOBAL tramplin_7c] +[GLOBAL tramplin_7d] +[GLOBAL tramplin_7e] +[GLOBAL tramplin_7f] +[GLOBAL tramplin_80] +[GLOBAL tramplin_81] +[GLOBAL tramplin_82] +[GLOBAL tramplin_83] +[GLOBAL tramplin_84] +[GLOBAL tramplin_85] +[GLOBAL tramplin_86] +[GLOBAL tramplin_87] +[GLOBAL tramplin_88] +[GLOBAL tramplin_89] +[GLOBAL tramplin_8a] +[GLOBAL tramplin_8b] +[GLOBAL tramplin_8c] +[GLOBAL tramplin_8d] +[GLOBAL tramplin_8e] +[GLOBAL tramplin_8f] +[GLOBAL tramplin_90] +[GLOBAL tramplin_91] +[GLOBAL tramplin_92] +[GLOBAL tramplin_93] +[GLOBAL tramplin_94] +[GLOBAL tramplin_95] +[GLOBAL tramplin_96] +[GLOBAL tramplin_97] +[GLOBAL tramplin_98] +[GLOBAL tramplin_99] +[GLOBAL tramplin_9a] +[GLOBAL tramplin_9b] +[GLOBAL tramplin_9c] +[GLOBAL tramplin_9d] +[GLOBAL tramplin_9e] +[GLOBAL tramplin_9f] +[GLOBAL tramplin_a0] +[GLOBAL tramplin_a1] +[GLOBAL tramplin_a2] +[GLOBAL tramplin_a3] +[GLOBAL tramplin_a4] +[GLOBAL tramplin_a5] +[GLOBAL tramplin_a6] +[GLOBAL tramplin_a7] +[GLOBAL tramplin_a8] +[GLOBAL tramplin_a9] +[GLOBAL tramplin_aa] +[GLOBAL tramplin_ab] +[GLOBAL tramplin_ac] +[GLOBAL tramplin_ad] +[GLOBAL tramplin_ae] +[GLOBAL tramplin_af] +[GLOBAL tramplin_b0] +[GLOBAL tramplin_b1] +[GLOBAL tramplin_b2] +[GLOBAL tramplin_b3] +[GLOBAL tramplin_b4] +[GLOBAL tramplin_b5] +[GLOBAL tramplin_b6] +[GLOBAL tramplin_b7] +[GLOBAL tramplin_b8] +[GLOBAL tramplin_b9] +[GLOBAL tramplin_ba] +[GLOBAL tramplin_bb] +[GLOBAL tramplin_bc] +[GLOBAL tramplin_bd] +[GLOBAL tramplin_be] +[GLOBAL tramplin_bf] +[GLOBAL tramplin_c0] +[GLOBAL tramplin_c1] +[GLOBAL tramplin_c2] +[GLOBAL tramplin_c3] +[GLOBAL tramplin_c4] +[GLOBAL tramplin_c5] +[GLOBAL tramplin_c6] +[GLOBAL tramplin_c7] +[GLOBAL tramplin_c8] +[GLOBAL tramplin_c9] +[GLOBAL tramplin_ca] +[GLOBAL tramplin_cb] +[GLOBAL tramplin_cc] +[GLOBAL tramplin_cd] +[GLOBAL tramplin_ce] +[GLOBAL tramplin_cf] +[GLOBAL tramplin_d0] +[GLOBAL tramplin_d1] +[GLOBAL tramplin_d2] +[GLOBAL tramplin_d3] +[GLOBAL tramplin_d4] +[GLOBAL tramplin_d5] +[GLOBAL tramplin_d6] +[GLOBAL tramplin_d7] +[GLOBAL tramplin_d8] +[GLOBAL tramplin_d9] +[GLOBAL tramplin_da] +[GLOBAL tramplin_db] +[GLOBAL tramplin_dc] +[GLOBAL tramplin_dd] +[GLOBAL tramplin_de] +[GLOBAL tramplin_df] +[GLOBAL tramplin_e0] +[GLOBAL tramplin_e1] +[GLOBAL tramplin_e2] +[GLOBAL tramplin_e3] +[GLOBAL tramplin_e4] +[GLOBAL tramplin_e5] +[GLOBAL tramplin_e6] +[GLOBAL tramplin_e7] +[GLOBAL tramplin_e8] +[GLOBAL tramplin_e9] +[GLOBAL tramplin_ea] +[GLOBAL tramplin_eb] +[GLOBAL tramplin_ec] +[GLOBAL tramplin_ed] +[GLOBAL tramplin_ee] +[GLOBAL tramplin_ef] +[GLOBAL tramplin_f0] +[GLOBAL tramplin_f1] +[GLOBAL tramplin_f2] +[GLOBAL tramplin_f3] +[GLOBAL tramplin_f4] +[GLOBAL tramplin_f5] +[GLOBAL tramplin_f6] +[GLOBAL tramplin_f7] +[GLOBAL tramplin_f8] +[GLOBAL tramplin_f9] +[GLOBAL tramplin_fa] +[GLOBAL tramplin_fb] +[GLOBAL tramplin_fc] +[GLOBAL tramplin_fd] +[GLOBAL tramplin_fe] +[GLOBAL tramplin_ff] + +[EXTERN interrupt_handler] collect_context: push ds @@ -264,6 +266,12 @@ collect_context: push gs pusha + mov eax, 0x10 + mov ds, eax + mov es, eax + + call interrupt_handler + tramplin_0: push 0x0 From 1010c99705cb667bcda6a47033f2cf581cad70ba Mon Sep 17 00:00:00 2001 From: qq Date: Mon, 12 Feb 2024 03:36:16 +0700 Subject: [PATCH 65/88] fix: fix lab n.6 --- src/kernel/sys/interruption.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index e9aca13..a831a7f 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -12,12 +12,11 @@ void panic_handler(int vector) kernel_panic("unhandled interrupt %x", vector); } -void interrupt_handler(context* ctx) -{ - switch(ctx->vector) - { - - } +void interrupt_handler(context* ctx) { + switch (ctx->vector) { + //case 0x20: timer_interrupt(ctx); break; + default: default_panic_handler(ctx); break; + } } void register_handlers() @@ -32,11 +31,11 @@ void register_handlers() { if (i < 32) { - idt[i].gate_type = 0xF; + idt[i].gate_type = 0x8F; } else { - idt[i].gate_type = 0xE; + idt[i].gate_type = 0x8E; } idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits From da711c1f7f1a48dfd6cb1ec30fa763480d19a738 Mon Sep 17 00:00:00 2001 From: qq Date: Thu, 4 Apr 2024 12:32:03 +0700 Subject: [PATCH 66/88] practice #7 --- Makefile | 21 +++++- src/kernel/kernel.c | 12 +++ src/kernel/sys/context/context.c | 45 ++++++----- src/kernel/sys/context/context.h | 15 ++-- src/kernel/sys/context/experiment.asm | 14 ++++ src/kernel/sys/context/tramplinsWContext.asm | 12 +++ src/kernel/sys/interruption.c | 11 ++- src/kernel/sys/io/printf.c | 78 +++++++++++--------- src/kernel/sys/pic/pic.c | 4 +- src/kernel/sys/tramplins.hpp | 2 +- 10 files changed, 145 insertions(+), 69 deletions(-) create mode 100644 src/kernel/sys/context/experiment.asm diff --git a/Makefile b/Makefile index 698e762..38d2ca0 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm KERNEL_C = $(KERNEL_SOURCE_DIR)/kernel.c KERNEL_ENTRY_ASM = $(KERNEL_SOURCE_DIR)/kernel_entry.asm +TRAMPLINS_WITH_CONTEXT_O = $(KERNEL_BUILD_DIR)/sys/context/tramplinsWContext.o + BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin KERNEL_O = $(KERNEL_BUILD_DIR)/kernel.o KERNEL_ENTRY_O = $(KERNEL_BUILD_DIR)/kernel_entry.o @@ -81,8 +83,22 @@ $(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ +$(KERNEL_BUILD_DIR)/sys/context/exp.o: $(KERNEL_SOURCE_DIR)/sys/context/experiment.asm + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + +$(TRAMPLINS_WITH_CONTEXT_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + +$(KERNEL_BUILD_DIR)/sys/context/context.o: $(KERNEL_SOURCE_DIR)/sys/context/context.c $(KERNEL_SOURCE_DIR)/sys/context/context.h \ + $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_SOURCE_DIR)/sys/io/io.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ - $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp + $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp $(TRAMPLINS_WITH_CONTEXT_O) \ + $(KERNEL_BUILD_DIR)/sys/context/context.o $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ @@ -98,7 +114,8 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ - $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o + $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o \ + $(KERNEL_BUILD_DIR)/sys/context/context.o $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cf61f9b..7c57cf9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,4 +1,5 @@ //extern void asm_print_d2vm(char* src); +extern void experiment(); #include "./sys/io/io.h" #include "./sys/memory/memory.h" @@ -25,6 +26,17 @@ void entry_point() { register_handlers(); setup_pic(); init_printer(); + + // lab №7, #1 + // experiment(); + + asm("sti"); + while(1) { + printf("*"); + } + + + //printf("%X", -192); //asm("xchgw %bx, %bx"); diff --git a/src/kernel/sys/context/context.c b/src/kernel/sys/context/context.c index e67832f..7f25d33 100644 --- a/src/kernel/sys/context/context.c +++ b/src/kernel/sys/context/context.c @@ -1,17 +1,16 @@ -#pragma once - #include "context.h" -#include "../io/io.h" -void interrupt_handler(context* ctx) { - switch (ctx->vector) { - case 0x20: timer_interrupt(ctx); break; - default: default_panic_handler(ctx); break; - } -} +// void interrupt_handler(context* ctx) { +// switch (ctx->vector) { +// //case 0x20: timer_interrupt(ctx); break; +// default: default_panic_handler(ctx); break; +// } +// } + void default_panic_handler(context* ctx) { init_printer(); + asm("cli"); unsigned int current_eflags; @@ -22,17 +21,25 @@ void default_panic_handler(context* ctx) { : "memory" ); - printf("Kernel panic: unhandled interrupt %X, interrupted process context:\n" - " EAX = %X ECX = %X EDX = %X EBX = %X\n" - " ESP = %X EBP = %X ESI = %X EDI = %X\n" - " DS = %X ES = %X FS = %X GS = %X\n" - " CS = %X SS = %X EIP = %X\n" - " EFLAGS (interrupted) = %X " "EFLAGS (current) = %X\n" - " error code = %X", - ctx->vector, ctx->eax, ctx->edx, ctx->ebx, - ctx->esp, ctx->ebp, ctx->ebp, ctx->esi, ctx->edi, + + + printf("Kernel panic: unhandled interrupt %x, interrupted process context:\n" + " EAX = %x ECX = %x EDX = %x EBX = %x\n" + " ESP = %x EBP = %x ESI = %x EDI = %x\n" + " DS = %x ES = %x FS = %x GS = %x\n" + " CS = %x SS = %x EIP = %x\n" + " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + " error code = %x", + ctx->vector, + ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, + ctx->esp, ctx->ebp, ctx->esi, ctx->edi, ctx->ds, ctx->es, ctx->fs, ctx->gs, ctx->cs, ctx->ss, ctx->eip, ctx->eflags, current_eflags, ctx->error_code); -} \ No newline at end of file + + if (ctx->vector >= 32) { + asm("sti"); + } +} + diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index 84120e0..fed1096 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -1,8 +1,8 @@ #pragma once #include +#include "../io/io.h" -#pragma pack(push, 4) typedef struct { uint32_t edi; @@ -14,25 +14,24 @@ typedef struct uint32_t ecx; uint32_t eax; uint16_t gs; - //uint16_t padding_1; + uint16_t padding_1; uint16_t fs; - //uint16_t padding_2; + uint16_t padding_2; uint16_t es; - //uint16_t padding_3; + uint16_t padding_3; uint16_t ds; - //uint16_t padding_4; + uint16_t padding_4; uint32_t vector; uint32_t error_code; uint32_t eip; uint16_t cs; - //uint16_t padding_5; + uint16_t padding_5; uint32_t eflags; uint32_t prev_esp; uint16_t ss; - //uint16_t padding_6; + uint16_t padding_6; } context; -#pragma pack(pop) void interrupt_handler(context* ctx); diff --git a/src/kernel/sys/context/experiment.asm b/src/kernel/sys/context/experiment.asm new file mode 100644 index 0000000..c217d0f --- /dev/null +++ b/src/kernel/sys/context/experiment.asm @@ -0,0 +1,14 @@ +[BITS 32] +[GLOBAL experiment] + +experiment: + xchg bx, bx + mov eax, 1 + mov ecx, 2 + mov edx, 3 + mov ebx, 4 + mov esi, 5 + mov edi, 6 + mov ebp, 7 + int 42 + ret \ No newline at end of file diff --git a/src/kernel/sys/context/tramplinsWContext.asm b/src/kernel/sys/context/tramplinsWContext.asm index 2687636..9912a7f 100644 --- a/src/kernel/sys/context/tramplinsWContext.asm +++ b/src/kernel/sys/context/tramplinsWContext.asm @@ -265,6 +265,7 @@ collect_context: push fs push gs pusha + push esp mov eax, 0x10 mov ds, eax @@ -272,6 +273,16 @@ collect_context: call interrupt_handler + pop esp + popa + pop gs + pop fs + pop es + pop ds + + add esp, 8 + + iretd tramplin_0: push 0x0 @@ -431,6 +442,7 @@ tramplin_20: tramplin_21: push 0x0 push 0x21 + ; xchg bx, bx jmp collect_context tramplin_22: diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index a831a7f..ccfd5a7 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -14,9 +14,18 @@ void panic_handler(int vector) void interrupt_handler(context* ctx) { switch (ctx->vector) { - //case 0x20: timer_interrupt(ctx); break; + case 0x20: timer_interrupt(ctx); outb(0x20, 0x20); break; default: default_panic_handler(ctx); break; } + +} + +static int counter = 0; + +void timer_interrupt(context* ctx) { + //asm("xchgw %bx, %bx"); + + printf(" %d ", counter++); } void register_handlers() diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 5d7fdd7..ac33f81 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -10,26 +10,15 @@ int string_compare(const char* first, const char* second, int n) { } void vga_print_char(char symbol, int x, int y, int foreground_color) { - if (x >= 80 || y >= 25) return; - *((short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; + // if (current_x >= 80) end_line(); + + *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; } void vga_clear_screen() { memzero((void*)VGA_START, sizeof(short) * 25 * 80); } -void vga_print_string(char* str, int x, int y, int text_color) { - int current_x = x; - int current_y = y; - int shift = 0; - while (str[shift]) { - vga_print_char(str[shift++], current_x++, current_y, text_color); - if (current_x >= 80) { - end_line(); - } - } -} - void shift_up() { memcpy((char*) VGA_START, (char*) VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); memzero((char*) VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); @@ -42,14 +31,21 @@ void init_printer() { } void end_line() { - if (current_y > 25) shift_up(); + // asm("xchgw %bx, %bx"); + if (current_y >= 24) shift_up(); else ++current_y; current_x = 0; } void print_char(char s, int color) { + asm("xchgw %bx, %bx"); + if (current_y > 24) { + shift_up(); + current_y--; + } vga_print_char(s, current_x, current_y, color); - if (++current_x == 80) end_line(); + // current_x++; + if (++current_x >= 80) end_line(); } void print_string(char* str, int color) { @@ -61,6 +57,8 @@ void print_string(char* str, int color) { void printf(const char* fmt, ...) { va_list a = va_start(fmt); + // asm("xchgw %bx, %bx"); + for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -104,27 +102,32 @@ void printf(const char* fmt, ...) { } else if (mode == 'd') { unsigned b = (unsigned) va_arg(a, int); #define _MAX_N_DIGITS_FOR_DEC_CASE (int)(12) - char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; - - int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; - int is_neg = 0; - if (b & (1 << sizeof(int) * 8 - 1)) - { - is_neg = 1; - b = ~b + 1; - } - while (b) { - buffer[--counter] = b % 10 + '0'; - b /= 10; - } - if (is_neg) - { - buffer[--counter] = '-'; - } - print_string(buffer + counter, color); + if (b != 0) { + char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; + + int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; + int is_neg = 0; + if (b & (1 << sizeof(int) * 8 - 1)) + { + is_neg = 1; + b = ~b + 1; + } + while (b) { + buffer[--counter] = b % 10 + '0'; + b /= 10; + } + if (is_neg) + { + buffer[--counter] = '-'; + } + + print_string(buffer + counter, color); + } else { + print_char('0', color); + } } else if (mode == 'x' || mode == 'X') { - int b = va_arg(a, int); + unsigned int b = (unsigned int) va_arg(a, int); print_string("0x", color); if (b != 0) { char buffer[sizeof(int) + 1] = {0}; @@ -135,6 +138,7 @@ void printf(const char* fmt, ...) { } while (b) { int temp = b % 16; + char symbol; if (temp > 9) { symbol = temp - 10 + (mode == 'X' ? 'A' : 'a'); @@ -164,7 +168,9 @@ void printf(const char* fmt, ...) { } } } else if (*i == '\n') { - end_line(); + // end_line(); + current_y++; + current_x = 0; } else { print_char(*i, WHITE); } diff --git a/src/kernel/sys/pic/pic.c b/src/kernel/sys/pic/pic.c index b5e8283..4551273 100644 --- a/src/kernel/sys/pic/pic.c +++ b/src/kernel/sys/pic/pic.c @@ -65,10 +65,10 @@ void setup_pic() { // 1. Упасть с вектором 0x20 // outb(MASTER_DATA, 0b11111110); - // outb(SLAVE_DATA, 0b11111111); + // outb(SLAVE_DATA, 0b11111110); // 2. При нажатии на любую клавишу - outb(MASTER_DATA, 0b11111101); + outb(MASTER_DATA, 0b11111100); outb(SLAVE_DATA, 0b11111111); // 3. Ничего не происходит при замаскированных битах diff --git a/src/kernel/sys/tramplins.hpp b/src/kernel/sys/tramplins.hpp index f086fcc..6cdcf12 100644 --- a/src/kernel/sys/tramplins.hpp +++ b/src/kernel/sys/tramplins.hpp @@ -1,6 +1,6 @@ #pragma once -#define __gen_tramplin(x) static void tramplin_##x() { panic_handler(0x##x); } +#define __gen_tramplin(x) static void tramplin_##x(); #define __gen_tramplins(...) static void (*tramplins[])(void) = {__VA_ARGS__}; // AUTO-GENERATED CODE IS BELOW From 8318981b9839b273910496c1b9ed1828515555e7 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 4 Apr 2024 12:52:01 +0700 Subject: [PATCH 67/88] some edits --- src/kernel/kernel_entry.asm | 1 + src/kernel/sys/interruption.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/kernel/kernel_entry.asm b/src/kernel/kernel_entry.asm index 58b9a46..5241307 100644 --- a/src/kernel/kernel_entry.asm +++ b/src/kernel/kernel_entry.asm @@ -1,4 +1,5 @@ [BITS 32] + [extern entry_point] call entry_point diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index ccfd5a7..846d55f 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -56,4 +56,5 @@ void register_handlers() idtd.linear_addr_IDT = idt; asm("lidt %0" :: "m"(idtd)); -} \ No newline at end of file +} + From 513d6552c1b64e18525da69466058dd08b0d8f15 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 1 May 2024 15:56:59 +0700 Subject: [PATCH 68/88] ahareravbearb --- bochs.linux.conf | 3 +-- src/kernel/kernel.c | 4 ++-- src/kernel/sys/context/experiment.asm | 2 +- src/kernel/sys/interruption.c | 2 +- src/kernel/sys/io/printf.c | 2 +- src/kernel/sys/pic/pic.c | 1 + 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bochs.linux.conf b/bochs.linux.conf index 936ef14..81b7f3a 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,7 +1,6 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig -display_library: x -#, options="gui_debug" +display_library: x, options="gui_debug" memory: host=32, guest=1024 boot: floppy floppy_bootsig_check: disabled=0 diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7c57cf9..8800dd0 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -25,10 +25,10 @@ void entry_point() { // asm_print_d2vm( "Hello from C!"); register_handlers(); setup_pic(); - init_printer(); + // init_printer(); // lab №7, #1 - // experiment(); + experiment(); asm("sti"); while(1) { diff --git a/src/kernel/sys/context/experiment.asm b/src/kernel/sys/context/experiment.asm index c217d0f..d6ae533 100644 --- a/src/kernel/sys/context/experiment.asm +++ b/src/kernel/sys/context/experiment.asm @@ -2,7 +2,7 @@ [GLOBAL experiment] experiment: - xchg bx, bx + ; xchg bx, bx mov eax, 1 mov ecx, 2 mov edx, 3 diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 846d55f..390631a 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -25,7 +25,7 @@ static int counter = 0; void timer_interrupt(context* ctx) { //asm("xchgw %bx, %bx"); - printf(" %d ", counter++); + printf("\n%d ", counter++); } void register_handlers() diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index ac33f81..d595f9f 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -38,7 +38,7 @@ void end_line() { } void print_char(char s, int color) { - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); if (current_y > 24) { shift_up(); current_y--; diff --git a/src/kernel/sys/pic/pic.c b/src/kernel/sys/pic/pic.c index 4551273..79e50ad 100644 --- a/src/kernel/sys/pic/pic.c +++ b/src/kernel/sys/pic/pic.c @@ -9,6 +9,7 @@ uint8_t inb(uint16_t port) { uint8_t data; + // asm("xchgw %bx, %bx"); asm("inb %1, %0" : "=a"(data) : "d"(port)); return data; } From 7bc2e03a3f54ca4023744df8b32b23c44336aeac Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 1 May 2024 15:58:32 +0700 Subject: [PATCH 69/88] pizdec a ne code --- src/kernel/sys/interruption.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 390631a..e688323 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -14,8 +14,17 @@ void panic_handler(int vector) void interrupt_handler(context* ctx) { switch (ctx->vector) { - case 0x20: timer_interrupt(ctx); outb(0x20, 0x20); break; - default: default_panic_handler(ctx); break; + case 0x20: + { + timer_interrupt(ctx); + outb(0x20, 0x20); + break; + } + default: + { + default_panic_handler(ctx); + break; + } } } From d315bfb5b54722ec2daeaa8973110e9ec5f56ef3 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 5 May 2024 21:16:43 +0700 Subject: [PATCH 70/88] init pdt (ps==0 case) --- Makefile | 43 +-- bx_enh_dbg.ini | 26 ++ src/kernel/kernel.c | 112 ++++++- src/kernel/sys/context/context.c | 45 --- src/kernel/sys/context/context.h | 6 +- src/kernel/sys/context/experiment.asm | 9 + src/kernel/sys/context/tramplinsWContext.asm | 1 - src/kernel/sys/interruption.c | 252 +++++++++++++--- src/kernel/sys/interruption.h | 3 +- src/kernel/sys/io/printf.c | 293 ++++++++++--------- src/kernel/sys/memory/alloc.c | 59 ++-- src/kernel/sys/memory/alloc.h | 25 +- src/kernel/sys/memory/mem_utils.h | 4 + src/kernel/sys/memory/memory.h | 4 + src/kernel/sys/paging/paging.asm | 127 ++++++++ src/kernel/sys/paging/paging.c | 184 ++++++++++++ src/kernel/sys/paging/paging.h | 24 ++ src/kernel/sys/paging/pdt.h | 58 ++++ src/kernel/sys/paging/pt.h | 35 +++ 19 files changed, 1029 insertions(+), 281 deletions(-) create mode 100644 bx_enh_dbg.ini delete mode 100644 src/kernel/sys/context/context.c create mode 100644 src/kernel/sys/paging/paging.asm create mode 100644 src/kernel/sys/paging/paging.c create mode 100644 src/kernel/sys/paging/paging.h create mode 100644 src/kernel/sys/paging/pdt.h create mode 100644 src/kernel/sys/paging/pt.h diff --git a/Makefile b/Makefile index 38d2ca0..1ebe1d1 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,10 @@ $(BUILD_DIR)/pm/io/print_d2vm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ +$(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o: $(KERNEL_SOURCE_DIR)/sys/paging/paging.asm + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + $(KERNEL_BUILD_DIR)/sys/pic/pic.o: $(KERNEL_SOURCE_DIR)/sys/pic/pic.c \ $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) @@ -87,23 +91,27 @@ $(KERNEL_BUILD_DIR)/sys/context/exp.o: $(KERNEL_SOURCE_DIR)/sys/context/experime $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ -$(TRAMPLINS_WITH_CONTEXT_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm +$(KERNEL_BUILD_DIR)/sys/paging/paging.o: $(KERNEL_SOURCE_DIR)/sys/paging/paging.c \ + $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h \ + $(KERNEL_SOURCE_DIR)/sys/paging/pdt.h $(KERNEL_SOURCE_DIR)/sys/paging/pt.h \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(info [MAKE] building: $@) - $(ASM) -f elf $< -o $@ + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(KERNEL_BUILD_DIR)/sys/context/context.o: $(KERNEL_SOURCE_DIR)/sys/context/context.c $(KERNEL_SOURCE_DIR)/sys/context/context.h \ - $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_SOURCE_DIR)/sys/io/io.h +$(TRAMPLINS_WITH_CONTEXT_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm $(info [MAKE] building: $@) - $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(ASM) -f elf $< -o $@ -$(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ +$(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp $(TRAMPLINS_WITH_CONTEXT_O) \ - $(KERNEL_BUILD_DIR)/sys/context/context.o + $(KERNEL_SOURCE_DIR)/sys/context/context.h $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h \ + $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ $(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h \ - $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h + $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.h \ + $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 @@ -115,7 +123,8 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o \ - $(KERNEL_BUILD_DIR)/sys/context/context.o $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o + $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ + $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) @@ -137,11 +146,11 @@ $(FLOPPY_IMG): $(BOOTLOADER_BIN) $(KERNEL_BIN) $(DD) conv=notrunc seek=1 if=$(word 2,$^) of=$@ clean_boot: -# $(info [MAKE] do $@:) -# $(RM) -f $(FLOPPY_IMG) -# $(RM) -f $(KERNEL_ENTRY_O) -# $(RM) -f $(KERNEL_O) -# $(RM) -f $(KERNEL_TMP) -# $(RM) -f $(KERNEL_BIN) -# $(RM) -f $(BOOTLOADER_BIN) -# $(RM) -f $(BOOTLOADER_BIN) + $(info [MAKE] do $@:) + $(RM) -f $(FLOPPY_IMG) + $(RM) -f $(KERNEL_ENTRY_O) + $(RM) -f $(KERNEL_O) + $(RM) -f $(KERNEL_TMP) + $(RM) -f $(KERNEL_BIN) + $(RM) -f $(BOOTLOADER_BIN) + $(RM) -f $(BOOTLOADER_BIN) diff --git a/bx_enh_dbg.ini b/bx_enh_dbg.ini new file mode 100644 index 0000000..fb832bc --- /dev/null +++ b/bx_enh_dbg.ini @@ -0,0 +1,26 @@ +# bx_enh_dbg_ini +SeeReg[0] = TRUE +SeeReg[1] = TRUE +SeeReg[2] = TRUE +SeeReg[3] = TRUE +SeeReg[4] = FALSE +SeeReg[5] = FALSE +SeeReg[6] = FALSE +SeeReg[7] = FALSE +SingleCPU = FALSE +ShowIOWindows = TRUE +ShowButtons = TRUE +SeeRegColors = TRUE +ignoreNxtT = TRUE +ignSSDisasm = TRUE +UprCase = 0 +DumpInAsciiMode = 3 +isLittleEndian = TRUE +DefaultAsmLines = 512 +DumpWSIndex = 0 +DockOrder = 0x123 +ListWidthPix[0] = 304 +ListWidthPix[1] = 244 +ListWidthPix[2] = 165 +MainWindow = 0, 0, 709, 500 +FontName = Normal diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 8800dd0..36a2ed8 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,10 +1,11 @@ -//extern void asm_print_d2vm(char* src); +extern void asm_print_d2vm(char* src); extern void experiment(); #include "./sys/io/io.h" #include "./sys/memory/memory.h" #include "./sys/interruption.h" #include "./sys/pic/pic.h" +#include "./sys/paging/paging.h" // compare [first, first_end) and second (until '\0') @@ -18,31 +19,112 @@ void str_reverse(char* str_start, char* str_end) { } } -void entry_point() { +void entry_point() +{ init_printer(); - printf( " %b %d_RED!hello world!", 20, 0x80000000); - // |||||||||||||| - // asm_print_d2vm( "Hello from C!"); + printf( " %b %d_RED!hello world!\n", 20, 0x80000000); + // ||||||||||||| + asm_print_d2vm("Hello from C!"); + register_handlers(); setup_pic(); - // init_printer(); + + ppdt actual_pdt = markup_pdt(); + if (!actual_pdt) + { + printf("PDT initialization finished bad!\n"); + asm("xchgw %bx, %bx"); + 1/0; // YEAH + } + else + { + printf("PDT on %x\n", actual_pdt); + } + load_page_directory(actual_pdt); + enable_paging(); + + int* temp = kernel_malloc(sizeof(int) * 100); + asm("xchgw %bx, %bx"); + if (!temp) + { + printf("Bad allocation\n"); + asm("xchgw %bx, %bx"); + } + else + { + printf("Temp on %x\n", temp); + for(ptr_t i = 0u; i < 100; ++i) + { + *temp = 0x1; + } + } // lab №7, #1 experiment(); + + int* temp2 = kernel_malloc(sizeof(int) * 0x10000000); + temp2 = kernel_malloc(sizeof(int) * 0x10000000); + temp2 = kernel_malloc(sizeof(int) * 0x100000); + asm("xchgw %bx, %bx"); + if (!temp2) + { + printf("Bad allocation\n"); + asm("xchgw %bx, %bx"); + } + else + { + printf("Temp on %x\n", temp2); + for(ptr_t i = 0u; i < 0x100000; ++i) + { + *temp2 = 0x10 + i; + } + } + + int* temp3 = kernel_malloc(sizeof(int) * 0x1000); + asm("xchgw %bx, %bx"); + if (!temp3) + { + printf("Bad allocation\n"); + asm("xchgw %bx, %bx"); + } + else + { + printf("Temp on %x\n", temp3); + for(ptr_t i = 0u; i < 0x1000; ++i) + { + printf("%d ", temp3[i]); + } + printf("\n"); + } + asm("xchgw %bx, %bx"); + + // int* temp3 = kernel_malloc(sizeof(int) * 0x100); + // asm("xchgw %bx, %bx"); + // if (!temp3) + // { + // printf("Bad allocation\n"); + // asm("xchgw %bx, %bx"); + // } + // else + // { + // printf("Temp on %x\n", temp3); + // for(ptr_t i = 0u; i < 0x100; ++i) + // { + // printf("%d", temp3[i]); + // } + // } asm("sti"); - while(1) { - printf("*"); - } + // while(1) { + // printf("*"); + // } - //printf("%X", -192); - - //asm("xchgw %bx, %bx"); + // printf("%X", -192); - //printf(str, num); - //printf("%d", 1/0); // 0x0 + // printf(str, num); + // printf("%d", 1/0); // 0x0 - asm("sti"); + // asm("sti"); while (1) {} } \ No newline at end of file diff --git a/src/kernel/sys/context/context.c b/src/kernel/sys/context/context.c deleted file mode 100644 index 7f25d33..0000000 --- a/src/kernel/sys/context/context.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "context.h" - -// void interrupt_handler(context* ctx) { -// switch (ctx->vector) { -// //case 0x20: timer_interrupt(ctx); break; -// default: default_panic_handler(ctx); break; -// } -// } - - -void default_panic_handler(context* ctx) { - init_printer(); - asm("cli"); - - unsigned int current_eflags; - - asm("pushf\n\t" - "pop %0" - : "=g"(current_eflags) - : - : "memory" - ); - - - - printf("Kernel panic: unhandled interrupt %x, interrupted process context:\n" - " EAX = %x ECX = %x EDX = %x EBX = %x\n" - " ESP = %x EBP = %x ESI = %x EDI = %x\n" - " DS = %x ES = %x FS = %x GS = %x\n" - " CS = %x SS = %x EIP = %x\n" - " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" - " error code = %x", - ctx->vector, - ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, - ctx->esp, ctx->ebp, ctx->esi, ctx->edi, - ctx->ds, ctx->es, ctx->fs, ctx->gs, - ctx->cs, ctx->ss, ctx->eip, - ctx->eflags, current_eflags, - ctx->error_code); - - if (ctx->vector >= 32) { - asm("sti"); - } -} - diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index fed1096..ece9c5e 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -31,8 +31,4 @@ typedef struct uint16_t ss; uint16_t padding_6; -} context; - -void interrupt_handler(context* ctx); - -void default_panic_handler(context* ctx); \ No newline at end of file +} context; \ No newline at end of file diff --git a/src/kernel/sys/context/experiment.asm b/src/kernel/sys/context/experiment.asm index d6ae533..790e2e9 100644 --- a/src/kernel/sys/context/experiment.asm +++ b/src/kernel/sys/context/experiment.asm @@ -1,7 +1,12 @@ [BITS 32] + [GLOBAL experiment] experiment: + push ebp + mov ebp, esp + pusha + ; xchg bx, bx mov eax, 1 mov ecx, 2 @@ -11,4 +16,8 @@ experiment: mov edi, 6 mov ebp, 7 int 42 + + popa + mov esp, ebp + pop ebp ret \ No newline at end of file diff --git a/src/kernel/sys/context/tramplinsWContext.asm b/src/kernel/sys/context/tramplinsWContext.asm index 9912a7f..49ba201 100644 --- a/src/kernel/sys/context/tramplinsWContext.asm +++ b/src/kernel/sys/context/tramplinsWContext.asm @@ -442,7 +442,6 @@ tramplin_20: tramplin_21: push 0x0 push 0x21 - ; xchg bx, bx jmp collect_context tramplin_22: diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index e688323..c9a0593 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -1,52 +1,28 @@ #include "./interruption.h" +#include "./paging/paging.h" -void kernel_panic(char* str, int num) +void kernel_panic(char* str, int type) { init_printer(); - printf(str, num); + printf(str, type); while (1); } void panic_handler(int vector) -{ +{ kernel_panic("unhandled interrupt %x", vector); } -void interrupt_handler(context* ctx) { - switch (ctx->vector) { - case 0x20: - { - timer_interrupt(ctx); - outb(0x20, 0x20); - break; - } - default: - { - default_panic_handler(ctx); - break; - } - } - -} - -static int counter = 0; - -void timer_interrupt(context* ctx) { - //asm("xchgw %bx, %bx"); - - printf("\n%d ", counter++); -} - void register_handlers() { - InterruptDescriptor32* idt = kernel_calloc(sizeof(InterruptDescriptor32), 256); + InterruptDescriptor32* idt = (InterruptDescriptor32*)kernel_calloc(sizeof(InterruptDescriptor32) * 256u, 12u); if (idt == (InterruptDescriptor32*)0) { printf("[EXC] register_handlers(): not enough memory!"); } - for (int i = 0; i < 256; ++i) - { + for (int i = 0; i < 256; ++i) + { if (i < 32) { idt[i].gate_type = 0x8F; @@ -58,12 +34,222 @@ void register_handlers() idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits idt[i].selector = 0b1000; - } + } IDTD idtd; idtd.IDT_size = sizeof(InterruptDescriptor32) * 256 - 1; - idtd.linear_addr_IDT = idt; + idtd.linear_addr_IDT = (uint32_t)(idt); asm("lidt %0" :: "m"(idtd)); } +void timer_interrupt(context* ctx); +void page_fault(context* ctx); +void default_panic_handler(context* ctx); + +void interrupt_handler(context* ctx) { + switch (ctx->vector) { + case 0x20: + { + timer_interrupt(ctx); + outb(0x20, 0x20); + break; + } + case 0xe: + { + page_fault(ctx); + outb(0x20, 0x20); + break; + } + default: + { + default_panic_handler(ctx); + break; + } + } + +} + +void timer_interrupt(context* ctx) { + static unsigned int counter = 0u; + printf("\n%d ", counter++); +} + +void page_fault(context* ctx) +{ + // init_printer(); + // printf("[PAGE FAULT HABDLING] START\n"); + unsigned int ec = ctx->error_code; + if (!(ec & 1)) + { + ptr_t* cr2 = nullptr; + ppdt cr3 = nullptr; + get_cr2_and_cr3(&cr2, &cr3); + // printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS %x\n", cr2); + unsigned int idx1 = 0u; + unsigned int idx2 = 0u; + ppt cur_pt = nullptr; + ppte cur_ppte = nullptr; + do + { + // printf("[PAGE FAULT HABDLING] FOUND PDT (PDT IDX : %d)\n", idx1); + pde cur_pde = cr3->pages[idx1]; + // printf("ACTUAL PDT: %x\nIT's PT: %x\nIT's PT << 12u: %x\n", cr3, cur_pde.page_table, (cur_pde.page_table << 12)); + // printf( "uint32_t present : %x\n" + // "uint32_t rw : %x\n" + // "uint32_t us : %x\n" + // // "uint32_t pwt : %x\n" + // // "uint32_t pcd : %x\n" + // // "uint32_t a : %x\n" + // // "uint32_t ignored1 : %x\n" + // // "uint32_t ps : %x\n" + // // "uint32_t ignored2 : %x\n" + // "uint32_t page_table << 12: %x\n", + // cur_pde.present, cur_pde.rw, cur_pde.us, /* cur_pde.pwt, + // cur_pde.pcd, cur_pde.a, cur_pde.ignored1, cur_pde.ps, + // cur_pde.ignored2, */(cur_pde.page_table << 12)); + if (!cur_pde.present) + { + cr3->pages[idx1].present = 1; + // printf( "[UPDATE] uint32_t present : %x\n", cr3->pages[idx1].present); + } + cur_pt = (cr3->pages[idx1].page_table << 12); + idx2 = 0u; + while(idx2 < 1023u && !((cur_pt->pages[idx2].page_frame << 12) <= (ptr_t)cr2 && + (cur_pt->pages[idx2 + 1].page_frame << 12) > (ptr_t)cr2)) { ++idx2; } + if (idx2 < 1023u || (idx2 == 1023u && (cur_pt->pages[idx2].page_frame << 12) >= (ptr_t)cr2)) + { + // if ((idx2 == 1023u && (cur_pt->pages[idx2].page_frame << 12) >= (ptr_t)cr2)) printf("--------------------EGDE CASE--------------------\n"); + cur_ppte = &cur_pt->pages[idx2]; + } + else + { + ++idx1; + if (idx1 >= 1024u) + { + printf("[PAGE FAULT HABDLING] PDT NOT FOUND (PDT IDX : %d)\n", idx1); + asm("xchgw %bx, %bx"); + return; + } + } + } while(!cur_ppte); + // printf("PDE IDX: %d\n", idx1); + // printf("PTE IDX: %d\n", idx2); + // printf("FOUND PTE: %x\n", cur_ppte); + // printf( "uint32_t present : %x\n" + // "uint32_t rw : %x\n" + // "uint32_t us : %x\n" + // // "uint32_t pwt : %x\n" + // // "uint32_t pcd : %x\n" + // // "uint32_t a : %x\n" + // // "uint32_t d : %x\n" + // // "uint32_t pat : %x\n" + // // "uint32_t g : %x\n" + // "uint32_t ignored1 : %x\n" + // "uint32_t page_frame << 12: %x\n", + // cur_ppte->present, cur_ppte->rw, cur_ppte->us, /* cur_ppte->pwt, + // cur_ppte->pcd, cur_ppte->a, cur_ppte->d, cur_ppte->pat, + // cur_ppte->g, */cur_ppte->ignored1, (cur_ppte->page_frame << 12)); + if (idx2 < 1023u) + { + pte next_pte = *(cur_ppte + 1); + // printf("NEXT PTE: %x\n", (cur_ppte + 1)); + // printf( "uint32_t present : %x\n" + // "uint32_t rw : %x\n" + // "uint32_t us : %x\n" + // // "uint32_t pwt : %x\n" + // // "uint32_t pcd : %x\n" + // // "uint32_t a : %x\n" + // // "uint32_t d : %x\n" + // // "uint32_t pat : %x\n" + // // "uint32_t g : %x\n" + // "uint32_t ignored1 : %x\n" + // "uint32_t page_frame << 12: %x\n", + // next_pte.present, next_pte.rw, next_pte.us, /* next_pte.pwt, + // next_pte.pcd, next_pte.a, next_pte.d, next_pte.pat, + // next_pte.g, */next_pte.ignored1, (next_pte.page_frame << 12)); + } + switch(cur_ppte->ignored1) + { + case not_stated: + { + printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + case disabled_page_frame: + { + cur_ppte->present = 1u; + cur_ppte->ignored1 = enabled_page_frame; + // invalidate_page(cr2); + load_page_directory(cr3); + enable_paging(); + // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); + break; + } + case enabled_page_frame: + { + printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + case free_page_frame: + { + cur_ppte->ignored1 = enabled_page_frame; + cur_ppte->present = 1u; + cur_ppte->rw = 1u; + cur_ppte->us = 1u; + // invalidate_page(cr2); + load_page_directory(cr3); + enable_paging(); + // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); + break; + } + default: + { + printf("[PAGE FAULT HABDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: %d\n", cur_ppte->ignored1); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + } + } +} + +void default_panic_handler(context* ctx) { + // init_printer(); + asm("cli"); + + unsigned int current_eflags; + + asm("pushf\n\t" + "pop %0" + : "=g"(current_eflags) + : + : "memory" + ); + + + + printf("Kernel panic: unhandled interrupt %x, interrupted process context:\n" + " EAX = %x ECX = %x EDX = %x EBX = %x\n" + " ESP = %x EBP = %x ESI = %x EDI = %x\n" + " DS = %x ES = %x FS = %x GS = %x\n" + " CS = %x SS = %x EIP = %x\n" + " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + " error code = %x\n", + ctx->vector, + ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, + ctx->esp, ctx->ebp, ctx->esi, ctx->edi, + ctx->ds, ctx->es, ctx->fs, ctx->gs, + ctx->cs, ctx->ss, ctx->eip, + ctx->eflags, current_eflags, + ctx->error_code); + + if (ctx->vector >= 0x20) { + asm("sti"); + } + asm("xchgw %bx, %bx"); +} \ No newline at end of file diff --git a/src/kernel/sys/interruption.h b/src/kernel/sys/interruption.h index efb6672..34c73cc 100644 --- a/src/kernel/sys/interruption.h +++ b/src/kernel/sys/interruption.h @@ -3,6 +3,7 @@ #include "./io/io.h" #include "./tramplins.hpp" #include "./idt.h" +#include "./pic/pic.h" #include "./memory/memory.h" #include "./context/context.h" @@ -13,7 +14,7 @@ typedef struct { } IDTD; #pragma pack(pop) -void kernel_panic(char* str, int num); +void kernel_panic(char* str, int type); void panic_handler(int vector); void interrupt_handler(context* ctx); diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index d595f9f..186751a 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -4,175 +4,180 @@ static int current_x; static int current_y; int string_compare(const char* first, const char* second, int n) { - int res = 0; - while(*first && *second && n-- && !(res = (*first++ - *second++))); - return !res; + int res = 0; + while(*first && *second && n-- && !(res = (*first++ - *second++))); + return !res; } void vga_print_char(char symbol, int x, int y, int foreground_color) { - // if (current_x >= 80) end_line(); + // if (current_x >= 80) end_line(); - *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; + *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; } void vga_clear_screen() { - memzero((void*)VGA_START, sizeof(short) * 25 * 80); + memzero((void*)VGA_START, sizeof(short) * 25 * 80); } void shift_up() { - memcpy((char*) VGA_START, (char*) VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); - memzero((char*) VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); + memcpy((char*) VGA_START, (char*) VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); + memzero((char*) VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); } void init_printer() { - current_x = 0; - current_y = 0; - vga_clear_screen(); + current_x = 0; + current_y = 0; + vga_clear_screen(); } void end_line() { - // asm("xchgw %bx, %bx"); - if (current_y >= 24) shift_up(); - else ++current_y; - current_x = 0; + // asm("xchgw %bx, %bx"); + if (current_y >= 24) shift_up(); + else ++current_y; + current_x = 0; } void print_char(char s, int color) { - // asm("xchgw %bx, %bx"); - if (current_y > 24) { - shift_up(); - current_y--; - } - vga_print_char(s, current_x, current_y, color); - // current_x++; - if (++current_x >= 80) end_line(); + // asm("xchgw %bx, %bx"); + if (current_y > 24) { + shift_up(); + current_y--; + } + vga_print_char(s, current_x, current_y, color); + // current_x++; + if (++current_x >= 80) end_line(); } void print_string(char* str, int color) { - while (*str) { - print_char(*str, color); - ++str; - } + while (*str) { + print_char(*str, color); + ++str; + } } void printf(const char* fmt, ...) { - va_list a = va_start(fmt); - // asm("xchgw %bx, %bx"); - - for (const char* i = fmt; *i; ++i) { - if (*i == '%') { - if (*(i + 1) == '%') { - print_char('%', WHITE); - ++i; - } else { - int color = WHITE; - - // mode select - char mode = *(i++ + 1); - - // color select (iff defined) - if (*(i + 1) == '_') { - i += 2; - const char* start = i; - const char* stop = i; - while (*i != '!') { - if (!*i) return; - stop = i++; - } - - if (start != stop) { - char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" - "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" - "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; - char* cur_color = color_names; - for (int i = 0; i < _LAST; ++i) { - if (string_compare(start, cur_color, stop - start)) { - color = i; - break; - } - while (*cur_color++); - } - } - } - - // print - if (mode == 's') { - char* b = &va_arg(a, char); - print_string(b, color); - } else if (mode == 'd') { - unsigned b = (unsigned) va_arg(a, int); + va_list a = va_start(fmt); + // asm("xchgw %bx, %bx"); + + for (const char* i = fmt; *i; ++i) { + if (*i == '%') { + if (*(i + 1) == '%') { + print_char('%', WHITE); + ++i; + } else { + int color = WHITE; + + // mode select + char mode = *(i++ + 1); + + // color select (iff defined) + if (*(i + 1) == '_') { + i += 2; + const char* start = i; + const char* stop = i; + while (*i != '!') { + if (!*i) return; + stop = i++; + } + + if (start != stop) { + char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" + "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" + "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; + char* cur_color = color_names; + for (int i = 0; i < _LAST; ++i) { + if (string_compare(start, cur_color, stop - start)) { + color = i; + break; + } + while (*cur_color++); + } + } + } + + // print + if (mode == 's') { + char* b = &va_arg(a, char); + print_string(b, color); + } else if (mode == 'd') { + unsigned b = (unsigned) va_arg(a, int); #define _MAX_N_DIGITS_FOR_DEC_CASE (int)(12) - if (b != 0) { - char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; - - int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; - int is_neg = 0; - if (b & (1 << sizeof(int) * 8 - 1)) - { - is_neg = 1; - b = ~b + 1; - } - while (b) { - buffer[--counter] = b % 10 + '0'; - b /= 10; - } - if (is_neg) - { - buffer[--counter] = '-'; - } - - print_string(buffer + counter, color); - } else { - print_char('0', color); - } - } else if (mode == 'x' || mode == 'X') { - unsigned int b = (unsigned int) va_arg(a, int); - print_string("0x", color); - if (b != 0) { - char buffer[sizeof(int) + 1] = {0}; - - int counter = 3; - if (b < 0) { - b *= -1; - } - while (b) { - int temp = b % 16; - - char symbol; - if (temp > 9) { - symbol = temp - 10 + (mode == 'X' ? 'A' : 'a'); - } else { - symbol = temp + '0'; - } - buffer[counter--] = symbol; - b >>= 4; - } - - print_string(buffer + counter + 1, color); - } else { - print_char('0', color); - } - } else if (mode == 'b') { - unsigned b = (unsigned) va_arg(a, unsigned); - print_string("0b", color); - char buffer[sizeof(unsigned) * 8 + 1] = {0}; - - int counter = sizeof(unsigned) * 8 - 1; - while (b) { - buffer[counter--] = (char)((b & 1) + '0'); - b >>= 1; - } - - print_string(buffer + counter + 1, color); - } - } - } else if (*i == '\n') { - // end_line(); - current_y++; - current_x = 0; - } else { - print_char(*i, WHITE); - } - } + if (b != 0) { + char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; + + int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; + int is_neg = 0; + if (b & (1 << sizeof(int) * 8 - 1)) + { + is_neg = 1; + b = ~b + 1; + } + while (b) { + buffer[--counter] = b % 10 + '0'; + b /= 10; + } + if (is_neg) + { + buffer[--counter] = '-'; + } + + print_string(buffer + counter, color); + } else { + print_char('0', color); + } + } else if (mode == 'x' || mode == 'X') { + unsigned int b = (unsigned int) va_arg(a, int); + print_string("0x", color); + if (b != 0) { + char buffer[sizeof(int) + 1] = {0}; + + int counter = 3; + if (b < 0) { + b *= -1; + } + while (b) { + int temp = b % 16; + + char symbol; + if (temp > 9) { + symbol = temp - 10 + (mode == 'X' ? 'A' : 'a'); + } else { + symbol = temp + '0'; + } + buffer[counter--] = symbol; + b >>= 4; + } + + print_string(buffer + counter + 1, color); + } else { + print_char('0', color); + } + } else if (mode == 'b') { + unsigned b = (unsigned) va_arg(a, unsigned); + print_string("0b", color); + char buffer[sizeof(unsigned) * 8 + 1] = {0}; + + int counter = sizeof(unsigned) * 8 - 1; + while (b) { + buffer[counter--] = (char)((b & 1) + '0'); + b >>= 1; + } + + print_string(buffer + counter + 1, color); + } + } + } else if (*i == '\n') { + // end_line(); + current_y++; + current_x = 0; + + } else if (*i == '\t' || *i == " ") { + // end_line(); + while(current_x++ % 4) {} + + } else { + print_char(*i, WHITE); + } + } } \ No newline at end of file diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index 32b061d..acf6ac2 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -1,33 +1,58 @@ #include "./alloc.h" +#include "../paging/paging.h" +#include "../io/io.h" -#define MEMORY_MAX 0x400000 +#define MEMORY_MAX ((ptr_t)(0x80000000)) -static unsigned int* heap_start = 0x100000; -static unsigned int* top = 0x100000; +static unsigned char* const heap_start = (unsigned char*)(0x100000); +static unsigned char* top = (unsigned char*)(0x100000); -void* kernel_malloc(unsigned int size) { - if (top + size > MEMORY_MAX) - return 0; +void* malloc(ptr_t size) { + if ((ptr_t)top + size > MEMORY_MAX) + return nullptr; + printf("\n-----------------\nAlloc: %x [%x]\n", top, size); top += size; return top - size; } -void* kernel_calloc(unsigned int size, unsigned int num) { - if (size * num + top > MEMORY_MAX) - return 0; - memzero(top, size * num); - top += size * num; - return top - size * num; +// kernel_malloc, but aligned to (1u << align_bit) bytes +void* kernel_kmalloc(ptr_t size, unsigned int align_bit) { + return malloc(kernel_mem_align(size, align_bit)); + // return malloc(size); } -void* kernel_realloc(void* old_addr, unsigned int new_size) { - if (new_size + top > MEMORY_MAX) - return 0; +void* kernel_malloc(ptr_t size) { + void* allocated_mem = kernel_kmalloc(size, 12u); + /* void* last_pte = markup_mem(allocated_mem, count_aligned_chunks(size, 12u) + 1); */ + return allocated_mem; +} + +void* calloc(ptr_t size) { + if (size + (ptr_t)top > MEMORY_MAX) + return nullptr; + memzero(top, size); + printf("\n-----------------\nAlloc: %x [%x]\n", top, size); + top += size; + return top - size; +} + +void* kernel_calloc(ptr_t size, ptr_t num) { + void* prepared_mem = kernel_kmalloc(size * num, 12u); + memzero(prepared_mem, size * num); + /* void* last_pte = markup_mem(prepared_mem, count_aligned_chunks( + kernel_mem_align(size * num, 12u), // gag (we need to return pair (void*, size) from malloc()) + 12u) + 1); */ + return prepared_mem; +} + +/* void* kernel_realloc(void* old_addr, ptr_t new_size) { + if (new_size + (ptr_t)top > MEMORY_MAX) + return nullptr; void* new_addr = kernel_malloc(new_size); memcpy(new_addr, old_addr, new_size); return new_addr; -} +} */ void kernel_free(void* addr) { - top = 0x100000; + top = heap_start; // we need to unset relevant present bits in pde's and pte's } diff --git a/src/kernel/sys/memory/alloc.h b/src/kernel/sys/memory/alloc.h index 5ac42ac..9756bfb 100644 --- a/src/kernel/sys/memory/alloc.h +++ b/src/kernel/sys/memory/alloc.h @@ -1,11 +1,30 @@ #pragma once +#ifndef nullptr +#define nullptr ((void*)(0)) +#endif + +#ifndef __ptr_base_def +#define __ptr_base_def +typedef unsigned int ptr_t; +#endif + +#define SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit) ((1u) << (ptr_t)(align_bit)) +#define count_aligned_chunks(mem, align_bit) ((ptr_t)(mem) / SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit)) +#define kernel_mem_is_aligned(mem, align_bit) (!((ptr_t)(mem) % SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit))) +#define kernel_mem_align_pad(mem, align_bit) (SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit) * ((1u) + count_aligned_chunks(mem, align_bit)) - (ptr_t)(mem)) +// #define kernel_mem_align_pad(mem, align_bit) ((ptr_t)(mem) + (SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit) - ((ptr_t)(mem) % SZ_IN_BYTES_OF_ALIGN_CHUNK(align_bit)))) +#define kernel_mem_align(mem, align_bit) ((ptr_t)(mem) + (kernel_mem_is_aligned(mem, align_bit) ? (0u) : kernel_mem_align_pad(mem, align_bit))) + #include "mem_utils.h" -void* kernel_malloc(unsigned int size); +// kernel_malloc, but aligned to (1u << align_bit) bytes +void* kernel_kmalloc(ptr_t size, ptr_t align_bit); + +void* kernel_malloc(ptr_t size); -void* kernel_calloc(unsigned int size, unsigned int num); +void* kernel_calloc(ptr_t size, ptr_t num); -void* kernel_realloc(void* old_addr, unsigned int new_size); +/* void* kernel_realloc(void* old_addr, ptr_t new_size); */ void kernel_free(void* addr); diff --git a/src/kernel/sys/memory/mem_utils.h b/src/kernel/sys/memory/mem_utils.h index 9f33767..7f41c71 100644 --- a/src/kernel/sys/memory/mem_utils.h +++ b/src/kernel/sys/memory/mem_utils.h @@ -1,4 +1,8 @@ #pragma once +#ifndef nullptr +#define nullptr ((void*)(0)) +#endif + const void* memcpy(const void* dst, const void* src, int n); void* memzero(void* dst, int n); \ No newline at end of file diff --git a/src/kernel/sys/memory/memory.h b/src/kernel/sys/memory/memory.h index 2749ae1..adcb969 100644 --- a/src/kernel/sys/memory/memory.h +++ b/src/kernel/sys/memory/memory.h @@ -1,3 +1,7 @@ #pragma once +#ifndef nullptr +#define nullptr ((void*)(0)) +#endif + #include "./alloc.h" \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm new file mode 100644 index 0000000..bddb79f --- /dev/null +++ b/src/kernel/sys/paging/paging.asm @@ -0,0 +1,127 @@ +[BITS 32] + +[GLOBAL load_page_directory] +[GLOBAL enable_paging] +[GLOBAL get_cr2_and_cr3] +[GLOBAL invalidate_page] +; [GLOBAL all_init_pdt] + +; all_init_pdt: +; push ebp +; mov ebp, esp +; pusha +; mov eax, [ebp + 36] + +; mov ebx, 0x0 +; mov edx, eax +; add edx, 4096 + +; .fill_pdt: +; ; pop +; push eax +; mov eax, [ebp + 4] +; add eax, 4096 +; mov [ebp + 4], eax +; pop +; mov ecx, 0x0 +; or ecx, [ebp + 40] +; mov [eax + ebx * 4], ecx +; cmp ebx, 0 +; jne .skip +; push 0x1 +; push 0x0 + +; .skip: +; push 0x0 +; push 0x0 +; ; push edx +; call all_init_pt + +; inc ebx, 1 +; cmp ebx, 1024 +; jne .fill_pdt + +; pop +; pop + +; popa +; mov esp, ebp +; pop ebp +; ret + +; all_init_pt: +; push ebp +; mov ebp, esp +; pusha +; ; mov edx, [ebp + 36] + +; mov eax, 0x0 +; mov ebx, [ebp + 40] + +; .fill_table: +; mov ecx, ebx +; or ecx, [ebp + 44] +; mov [edx + eax * 4], ecx +; add ebx, 4096 +; inc eax +; cmp eax, 1024 +; jne .fill_table + +; popa +; mov esp, ebp +; pop ebp +; ret + +load_page_directory: ; put &boot_page_directory in high 20 bits of cr3 register + push ebp + mov ebp, esp + + mov eax, [ebp + 8] + mov ebx, cr3 + and ebx, 0xfff ; zero out existing 20 high bits + and eax, 0xfffff000 + or ebx, eax + mov cr3, ebx + + mov esp, ebp + pop ebp + ret + +enable_paging: + ; enable 4MB paging + mov eax, cr4 ; read current cr4 + or eax, 0x00 ; set PSE + mov cr4, eax ; update cr4 + + ; enable paging (PG bit) + mov eax, cr0 ; read current cr0 + or eax, 0x80000001 ; set PE (bit 0) and PG (bit 31) + mov cr0, eax ; update cr0 + ; now paging is enabled + ret + +get_cr2_and_cr3: + push ebp + mov ebp, esp + + mov eax, [ebp + 8] + mov ebx, cr2 + mov [eax], ebx + mov eax, [ebp + 12] + mov ebx, cr3 + mov [eax], ebx + + mov esp, ebp + pop ebp + ret + +invalidate_page: + push ebp + mov ebp, esp + + xchg bx, bx + INVLPG [ebp + 8] + + mov esp, ebp + pop ebp + ret \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.c b/src/kernel/sys/paging/paging.c new file mode 100644 index 0000000..8c31f60 --- /dev/null +++ b/src/kernel/sys/paging/paging.c @@ -0,0 +1,184 @@ +#include "./paging.h" +#include "../io/io.h" + +static unsigned int sz_cur_dir = 0u; +static unsigned int sz_cur_page = 0u; +static ppde cur_dir = nullptr; +static ppte cur_page = nullptr; + +ppdt markup_pdt() +{ + ppdt pdt_mem = kernel_kmalloc(1024u * sizeof(pde), 12u); + if (!pdt_mem) + { + printf("[EXC] alloc_pdt():1: not enough memory!\n"); + return nullptr; + } + // count each 4 MB page (since 0x0) + // + 1u for mark up pdt itself (and each pt after it) + unsigned int total_amount_of_mem_to_mark = (unsigned)pdt_mem + (1u << 12); + unsigned int count_dir_pages_to_mark = count_aligned_chunks(total_amount_of_mem_to_mark, 22u); + total_amount_of_mem_to_mark += count_dir_pages_to_mark * (1u << 12); + //count_dir_pages_to_mark = count_aligned_chunks(total_amount_of_mem_to_mark, 22u) + 1; // adaptive mode + count_dir_pages_to_mark = 1024u; // all mode + unsigned int cur_phys_address = 0u; + for(unsigned int i = 0u; i < count_dir_pages_to_mark; ++i) + { + unsigned int count_pages_to_mark = 1024u; // adaptive/all mode + /* if (i == count_dir_pages_to_mark - 1 && + !kernel_mem_is_aligned(total_amount_of_mem_to_mark, 22u)) + { + // count each 4 KB page (since cur_phys_address) + unsigned int cur_mem_diff = total_amount_of_mem_to_mark - cur_phys_address; + count_pages_to_mark = count_aligned_chunks(cur_mem_diff, 12u); + } */ // adaptiva mode + // ppte pt_mem = kernel_kmalloc(1024u * sizeof(pte), 12u); // adaptiva mode + ppt pt_mem = kernel_kmalloc(1024u * sizeof(pte), 12u); // all mode + if (!pt_mem) + { + printf("[EXC] alloc_pdt():2: not enough memory!\n"); + return nullptr; + } + + ppde cur_pde = &pdt_mem->pages[i]; + // *cur_pde = *(ppde)(pt_mem); // maybe one day it will work + if (// cur_phys_address < (ptr_t)(pt_mem + 1024)) // adaptiva mode + cur_phys_address <= (ptr_t)(pt_mem + 1)) // all mode + { + cur_pde->present = 1u; + cur_pde->rw = 0u; + cur_pde->us = 0u; + } + else + { + cur_pde->present = 0u; + cur_pde->rw = 1u; + cur_pde->us = 1u; + } + cur_pde->pwt = 0u; + cur_pde->pcd = 0u; + cur_pde->a = 0u; + cur_pde->ignored1 = 0u; + cur_pde->ps = 0u; + cur_pde->ignored2 = 0u; + cur_pde->page_table = ((ptr_t)pt_mem >> 12); + + for(unsigned int j = 0u; j < count_pages_to_mark; ++j) + { + // ppte cur_pte = &pt_mem[j]; // adaptiva mode + ppte cur_pte = &pt_mem->pages[j]; // all mode + // *cur_pte = *(ppte)(cur_phys_address); // maybe one day it will work + if (// cur_phys_address < (ptr_t)(pt_mem + 1024)) // adaptiva mode + cur_phys_address <= (ptr_t)(pt_mem + 1)) // all mode + { + cur_pte-> present = 1u; + cur_pte-> rw = 0u; + cur_pte-> us = 0u; + cur_pte-> ignored1 = enabled_page_frame; + } + else + { + cur_pte-> present = 0u; + cur_pte-> rw = 1u; + cur_pte-> us = 1u; + cur_pte-> ignored1 = disabled_page_frame; + } + cur_pte-> pwt = 0u; + cur_pte-> pcd = 0u; + cur_pte-> a = 0u; + cur_pte-> d = 0u; + cur_pte-> pat = 0u; + cur_pte-> g = 0u; + cur_pte-> page_frame = (cur_phys_address >> 12); + cur_phys_address += (1u << 12); + + // cur_page = (j < 1023u) ? (cur_pte + 1) : nullptr; // adaptiva mode + // sz_cur_page = j; // adaptiva mode + } + + // cur_dir = (i < 1023u) ? (cur_pde + 1) : nullptr; // adaptiva mode + // sz_cur_dir = i; // adaptiva mode + } + return pdt_mem; +} + +// marks memory with each page of 4 KB to PDT +// [[deprecated]] +void* adaptive_markup_mem(void* mem, unsigned int count_pages) +{ + // if (count_pages > 1023u) + // { + // printf("[EXC] markup_mem():1: identity paging does not support allocating more than (4MB - 4 KB) data!"); + // return; + // } // invalid + void* result = nullptr; + for(unsigned int i = 0u; i < count_pages; ++i) + { + if (!cur_page || !cur_dir) + { + if (sz_cur_page != 1023) + { + if (sz_cur_page > 1023u) printf("[EXC] markup_mem():2: wrong sz_cur_page\n"); + else printf("[EXC] markup_mem():3: current PTE is nullptr\n"); + return nullptr; + } + if (sz_cur_dir >= 1023) + { + if (sz_cur_dir > 1023u) printf("[EXC] markup_mem():4: wrong sz_cur_dir\n"); + else printf("[EXC] markup_mem():5: not enough memory!\n"); + return nullptr; + } + cur_page = kernel_kmalloc(1024u * sizeof(pte), 12u); + if (!cur_page) + { + printf("[EXC] markup_mem():6: not enough memory!\n"); + return nullptr; + } + // *cur_pte = *(ppte)(cur_phys_address); // maybe one day it will work + cur_page-> present = 1u; + cur_page-> rw = 0u; + cur_page-> us = 0u; + cur_page-> pwt = 0u; + cur_page-> pcd = 0u; + cur_page-> a = 0u; + cur_page-> d = 0u; + cur_page-> pat = 0u; + cur_page-> g = 0u; + cur_page-> ignored1 = enabled_page_frame; + cur_page-> page_frame = ((ptr_t)cur_page >> 12); + + // *cur_pde = *(ppde)(pt_mem); // maybe one day it will work + cur_dir-> present = 1u; + cur_dir-> rw = 0u; + cur_dir-> us = 0u; + cur_dir-> pwt = 0u; + cur_dir-> pcd = 0u; + cur_dir-> a = 0u; + cur_dir-> ignored1 = 0u; + cur_dir-> ps = 0u; + cur_dir-> ignored2 = 0u; + cur_dir-> page_table = ((ptr_t)cur_page >> 12); + + result = (void*)(cur_page + 1024); + + cur_page = (++sz_cur_page < 1023u) ? (cur_page + 1) : nullptr; + cur_dir = (++sz_cur_dir < 1023u) ? (cur_dir + 1) : nullptr; + } + ppte cur_pte = cur_page; + // *cur_pte = *(ppte)(cur_phys_address); // maybe one day it will work + cur_pte-> present = 1u; + cur_pte-> rw = 1u; + cur_pte-> us = 1u; + cur_pte-> pwt = 0u; + cur_pte-> pcd = 0u; + cur_pte-> a = 0u; + cur_pte-> d = 0u; + cur_pte-> pat = 0u; + cur_pte-> g = 0u; + cur_pte-> ignored1 = enabled_page_frame; + cur_pte-> page_frame = ((ptr_t)mem >> 12); + cur_page = (++sz_cur_page < 1023u) ? (cur_page + 1) : nullptr; + mem = (void*)((ptr_t)mem + (1u << 12)); + } + return result; +} \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.h b/src/kernel/sys/paging/paging.h new file mode 100644 index 0000000..eebd26f --- /dev/null +++ b/src/kernel/sys/paging/paging.h @@ -0,0 +1,24 @@ +#pragma once + +#include "./pdt.h" +#include "./pt.h" +#include "../memory/alloc.h" + +#define not_stated 0 +#define disabled_page_frame 0b01 +#define enabled_page_frame 0b10 +#define free_page_frame 0b11 + +extern void load_page_directory(ppdt pdt); +extern void enable_paging(); +extern void get_cr2_and_cr3(void* dest_cr2, void* dest_cr3); +extern void invalidate_page(void* virt_address); + +ppdt markup_pdt(); + +// marks memory with each page of 4 KB to PDT +void* markup_mem(void* mem, unsigned int count_pages); + +// marks memory with each page of 4 KB to PDT +// [[deprecated]] +void* adaptive_markup_mem(void* mem, unsigned int count_pages); \ No newline at end of file diff --git a/src/kernel/sys/paging/pdt.h b/src/kernel/sys/paging/pdt.h new file mode 100644 index 0000000..0f0180f --- /dev/null +++ b/src/kernel/sys/paging/pdt.h @@ -0,0 +1,58 @@ +#pragma once + +#include + +#ifndef MAX_PDE_PER_PDT +#define MAX_PDE_PER_PDT (int)(1024) +#elif +#error [DEF_ERROR] MAX_PDE_PER_PDT was defined earlier +#endif + +#pragma pack(push, 1) +struct page_directory_entry +{ + uint32_t present : 1; // bit 0: always 1 + uint32_t rw : 1; // bit 1: read/write + uint32_t us : 1; // bit 2: user or supervisor + uint32_t pwt : 1; // bit 3: page-level write-through + uint32_t pcd : 1; // bit 4: page-level cache disable + uint32_t a : 1; // bit 5: accessed + uint32_t ignored1 : 1; // bit 6: available + uint32_t ps : 1; // bit 7: page size, 0=4KB 1=4MB, must be 0 for this struct + uint32_t ignored2 : 4; // bit 8 - 11: available + uint32_t page_table : 20; // bit 12 - 31: physical address of 4KB aligned page table referenced by this entry + +} __attribute__((packed)); +#pragma pack(pop) +typedef struct page_directory_entry page_directory_entry_t, pde, *ppde; + +#pragma pack(push, 1) +struct page_directory_entry_ext +{ + uint32_t present : 1; // bit 0: always 1 + uint32_t rw : 1; // bit 1: read/write + uint32_t us : 1; // bit 2: user or supervisor + uint32_t cache_ctrl1 : 4; // bit 3 - 6: cache control + uint32_t ps : 1; // bit 7: page size, 0=4KB 1=4MB, must be 1 for this struct + uint32_t cache_ctrl2 : 1; // bit 8: cache control + uint32_t ignored1 : 3; // bit 9 - 11: available + uint32_t cache_ctrl3 : 1; // bit 12: cache control + uint32_t phys_address : 8; // bit 13 - 20: bits of physical address of page frame + uint32_t reserved : 8; // bit 21: reserved bit + uint32_t frame_address : 20; // bit 12 - 31: frame address + +} __attribute__((packed)); +#pragma pack(pop) +typedef struct page_directory_entry_ext page_directory_entry_x_t, pdex, *ppdex; + +typedef struct page_directory_table +{ + pde pages[MAX_PDE_PER_PDT]; + +} page_directory_table_t, pdt, *ppdt; + +typedef struct page_directory_table_ext +{ + pdex pages[MAX_PDE_PER_PDT]; + +} page_directory_table_x_t, pdtx, *ppdtx; \ No newline at end of file diff --git a/src/kernel/sys/paging/pt.h b/src/kernel/sys/paging/pt.h new file mode 100644 index 0000000..2a4a10c --- /dev/null +++ b/src/kernel/sys/paging/pt.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +#ifndef MAX_PTE_PER_PT +#define MAX_PTE_PER_PT (int)(1024) +#elif +#error [DEF_ERROR] MAX_PTE_PER_PT was defined earlier +#endif + +#pragma pack(push, 1) +struct page_table_entry +{ + uint32_t present : 1; // bit 0: always 1 + uint32_t rw : 1; // bit 1: read/write + uint32_t us : 1; // bit 2: user or supervisor + uint32_t pwt : 1; // bit 3: page-level write-through + uint32_t pcd : 1; // bit 4: page-level cache disable + uint32_t a : 1; // bit 5: accessed + uint32_t d : 1; // bit 6: dirty + uint32_t pat : 1; // bit 7: must be 0 unless PAT supported + uint32_t g : 1; // bit 8: global translation + uint32_t ignored1 : 3; // bit 9 - 11: available + uint32_t page_frame : 20; // bit 12 - 31: physical address of 4KB page frame + +} __attribute__((packed)); +#pragma pack(pop) +typedef struct page_table_entry page_table_entry_t, pte, *ppte; + +// full capacity page table +typedef struct page_table +{ + pte pages[MAX_PTE_PER_PT]; + +} page_table_t, pt, *ppt; \ No newline at end of file From 803d332d2e315a92bc51deb65f49cd6e5ee28190 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 6 May 2024 20:33:10 +0700 Subject: [PATCH 71/88] upd --- bx_enh_dbg.ini | 26 -------------------------- src/kernel/sys/paging/paging.asm | 2 +- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 bx_enh_dbg.ini diff --git a/bx_enh_dbg.ini b/bx_enh_dbg.ini deleted file mode 100644 index fb832bc..0000000 --- a/bx_enh_dbg.ini +++ /dev/null @@ -1,26 +0,0 @@ -# bx_enh_dbg_ini -SeeReg[0] = TRUE -SeeReg[1] = TRUE -SeeReg[2] = TRUE -SeeReg[3] = TRUE -SeeReg[4] = FALSE -SeeReg[5] = FALSE -SeeReg[6] = FALSE -SeeReg[7] = FALSE -SingleCPU = FALSE -ShowIOWindows = TRUE -ShowButtons = TRUE -SeeRegColors = TRUE -ignoreNxtT = TRUE -ignSSDisasm = TRUE -UprCase = 0 -DumpInAsciiMode = 3 -isLittleEndian = TRUE -DefaultAsmLines = 512 -DumpWSIndex = 0 -DockOrder = 0x123 -ListWidthPix[0] = 304 -ListWidthPix[1] = 244 -ListWidthPix[2] = 165 -MainWindow = 0, 0, 709, 500 -FontName = Normal diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm index bddb79f..4f3caa2 100644 --- a/src/kernel/sys/paging/paging.asm +++ b/src/kernel/sys/paging/paging.asm @@ -95,7 +95,7 @@ enable_paging: ; enable paging (PG bit) mov eax, cr0 ; read current cr0 - or eax, 0x80000001 ; set PE (bit 0) and PG (bit 31) + or eax, 0x80000000 ; set PE (bit 0) and PG (bit 31) mov cr0, eax ; update cr0 ; now paging is enabled ret From 46bf27e0b28349c2ca09f1c37b7082a0419994f9 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 6 May 2024 20:37:26 +0700 Subject: [PATCH 72/88] upd 2 --- src/kernel/sys/interruption.c | 12 ++++++------ src/kernel/sys/paging/paging.asm | 9 +-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index c9a0593..0a37fcb 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -182,9 +182,9 @@ void page_fault(context* ctx) { cur_ppte->present = 1u; cur_ppte->ignored1 = enabled_page_frame; - // invalidate_page(cr2); - load_page_directory(cr3); - enable_paging(); + invalidate_page(cr2); + // load_page_directory(cr3); + // enable_paging(); // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); break; } @@ -201,9 +201,9 @@ void page_fault(context* ctx) cur_ppte->present = 1u; cur_ppte->rw = 1u; cur_ppte->us = 1u; - // invalidate_page(cr2); - load_page_directory(cr3); - enable_paging(); + invalidate_page(cr2); + // load_page_directory(cr3); + // enable_paging(); // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); break; } diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm index 4f3caa2..266f288 100644 --- a/src/kernel/sys/paging/paging.asm +++ b/src/kernel/sys/paging/paging.asm @@ -116,12 +116,5 @@ get_cr2_and_cr3: ret invalidate_page: - push ebp - mov ebp, esp - - xchg bx, bx - INVLPG [ebp + 8] - - mov esp, ebp - pop ebp + INVLPG [ebp + 4] ret \ No newline at end of file From 6e49896208107859db5d56eb50cff66f51989841 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Fri, 10 May 2024 00:01:15 +0700 Subject: [PATCH 73/88] impl pse case, resolves #3 --- src/kernel/kernel.c | 23 +++++++++- src/kernel/sys/interruption.c | 77 +++++++++++++++++++++++++++++++- src/kernel/sys/memory/alloc.c | 4 +- src/kernel/sys/paging/paging.asm | 16 ++++++- src/kernel/sys/paging/paging.c | 43 +++++++++++++++++- src/kernel/sys/paging/paging.h | 11 ++--- src/kernel/sys/paging/pdt.h | 10 ++--- 7 files changed, 166 insertions(+), 18 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 36a2ed8..8c7d96e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -29,7 +29,8 @@ void entry_point() register_handlers(); setup_pic(); - ppdt actual_pdt = markup_pdt(); + // non-PSE + /* ppdt actual_pdt = markup_pdt(); if (!actual_pdt) { printf("PDT initialization finished bad!\n"); @@ -41,7 +42,25 @@ void entry_point() printf("PDT on %x\n", actual_pdt); } load_page_directory(actual_pdt); - enable_paging(); + enable_paging(); */ + + // PSE + ppdtx actual_pdt = markup_pdt_pse(); + asm("xchgw %bx, %bx"); + if (!actual_pdt) + { + printf("PDT initialization finished bad!\n"); + asm("xchgw %bx, %bx"); + 1/0; // YEAH + } + else + { + printf("PDT on %x\n", actual_pdt); + } + asm("xchgw %bx, %bx"); + load_page_directory(actual_pdt); + asm("xchgw %bx, %bx"); + enable_paging_pse(); int* temp = kernel_malloc(sizeof(int) * 100); asm("xchgw %bx, %bx"); diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 0a37fcb..c9f32cc 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -57,8 +57,8 @@ void interrupt_handler(context* ctx) { } case 0xe: { - page_fault(ctx); - outb(0x20, 0x20); + // page_fault(ctx); // non-PSE + page_fault_pse(ctx); // PSE break; } default: @@ -218,6 +218,79 @@ void page_fault(context* ctx) } } +void page_fault_pse(context* ctx) +{ + init_printer(); + printf("[PAGE FAULT HABDLING] START\n"); + unsigned int ec = ctx->error_code; + if (!(ec & 1)) + { + ptr_t* cr2 = nullptr; + ppdtx cr3 = nullptr; + get_cr2_and_cr3(&cr2, &cr3); + printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS %x\n", cr2); + int found = 0; + for(unsigned int i = 0u; i < 1024u; ++i) + { + if ((cr3->pages[i].frame_address << 22) <= (ptr_t)cr2 && + (cr3->pages[i + 1].frame_address << 22) > (ptr_t)cr2) + { + switch(cr3->pages[i].ignored1) + { + case not_stated: + { + printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + case disabled_page_frame: + { + cr3->pages[i].present = 1u; + cr3->pages[i].ignored1 = enabled_page_frame; + invalidate_page(cr2); + // load_page_directory(cr3); + // enable_paging(); + // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); + break; + } + case enabled_page_frame: + { + printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + case free_page_frame: + { + cr3->pages[i].ignored1 = enabled_page_frame; + cr3->pages[i].present = 1u; + cr3->pages[i].rw = 1u; + cr3->pages[i].us = 1u; + invalidate_page(cr2); + // load_page_directory(cr3); + // enable_paging(); + // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); + break; + } + default: + { + printf("[PAGE FAULT HABDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: PAGE-FLAG = %d\n", cr3->pages[i].ignored1); + asm("xchgw %bx, %bx"); + default_panic_handler(ctx); + return; + } + } + found = 1; + } + } + if (!found) + { + printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS NOT FOUND IN PDTX"); + } + } +} + void default_panic_handler(context* ctx) { // init_printer(); asm("cli"); diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index acf6ac2..98119a3 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -7,7 +7,7 @@ static unsigned char* const heap_start = (unsigned char*)(0x100000); static unsigned char* top = (unsigned char*)(0x100000); -void* malloc(ptr_t size) { +static void* malloc(ptr_t size) { if ((ptr_t)top + size > MEMORY_MAX) return nullptr; printf("\n-----------------\nAlloc: %x [%x]\n", top, size); @@ -27,7 +27,7 @@ void* kernel_malloc(ptr_t size) { return allocated_mem; } -void* calloc(ptr_t size) { +static void* calloc(ptr_t size) { if (size + (ptr_t)top > MEMORY_MAX) return nullptr; memzero(top, size); diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm index 266f288..57cc448 100644 --- a/src/kernel/sys/paging/paging.asm +++ b/src/kernel/sys/paging/paging.asm @@ -2,6 +2,7 @@ [GLOBAL load_page_directory] [GLOBAL enable_paging] +[GLOBAL enable_paging_pse] [GLOBAL get_cr2_and_cr3] [GLOBAL invalidate_page] ; [GLOBAL all_init_pdt] @@ -95,7 +96,20 @@ enable_paging: ; enable paging (PG bit) mov eax, cr0 ; read current cr0 - or eax, 0x80000000 ; set PE (bit 0) and PG (bit 31) + or eax, 0x80000001 ; set PE (bit 0) and PG (bit 31) + mov cr0, eax ; update cr0 + ; now paging is enabled + ret + +enable_paging_pse: + ; enable 4MB paging + mov eax, cr4 ; read current cr4 + or eax, 0x10 ; set PSE + mov cr4, eax ; update cr4 + + ; enable paging (PG bit) + mov eax, cr0 ; read current cr0 + or eax, 0x80000001 ; set PE (bit 0) and PG (bit 31) mov cr0, eax ; update cr0 ; now paging is enabled ret diff --git a/src/kernel/sys/paging/paging.c b/src/kernel/sys/paging/paging.c index 8c31f60..cef0401 100644 --- a/src/kernel/sys/paging/paging.c +++ b/src/kernel/sys/paging/paging.c @@ -21,7 +21,7 @@ ppdt markup_pdt() total_amount_of_mem_to_mark += count_dir_pages_to_mark * (1u << 12); //count_dir_pages_to_mark = count_aligned_chunks(total_amount_of_mem_to_mark, 22u) + 1; // adaptive mode count_dir_pages_to_mark = 1024u; // all mode - unsigned int cur_phys_address = 0u; + ptr_t cur_phys_address = 0u; for(unsigned int i = 0u; i < count_dir_pages_to_mark; ++i) { unsigned int count_pages_to_mark = 1024u; // adaptive/all mode @@ -102,6 +102,47 @@ ppdt markup_pdt() return pdt_mem; } +ppdtx markup_pdt_pse() +{ + ppdtx pdtx_mem = kernel_kmalloc(1024u * sizeof(pde), 12u); + if (!pdtx_mem) + { + printf("[EXC] alloc_pdt():1: not enough memory!\n"); + return nullptr; + } + + ptr_t cur_phys_address = 0u; + for(unsigned int i = 0u; i < 1024u; ++i) + { + ppdex cur_pdex = &pdtx_mem->pages[i]; + if (cur_phys_address <= (ptr_t)(pdtx_mem + 1)) + { + cur_pdex->present = 1u; + cur_pdex->rw = 0u; + cur_pdex->us = 0u; + cur_pdex->ignored1 = enabled_page_frame; + } + else + { + cur_pdex->present = 0u; + cur_pdex->rw = 1u; + cur_pdex->us = 1u; + cur_pdex->ignored1 = disabled_page_frame; + } + cur_pdex->cache_ctrl1 = 0u; + cur_pdex->ps = 1u; + cur_pdex->cache_ctrl2 = 0u; + cur_pdex->cache_ctrl3 = 0u; + cur_pdex->phys_address = 0u; + cur_pdex->reserved = 0u; + cur_pdex->frame_address = (cur_phys_address >> 22); + + cur_phys_address += (1u << 22); + } + + return pdtx_mem; +} + // marks memory with each page of 4 KB to PDT // [[deprecated]] void* adaptive_markup_mem(void* mem, unsigned int count_pages) diff --git a/src/kernel/sys/paging/paging.h b/src/kernel/sys/paging/paging.h index eebd26f..8706099 100644 --- a/src/kernel/sys/paging/paging.h +++ b/src/kernel/sys/paging/paging.h @@ -9,15 +9,16 @@ #define enabled_page_frame 0b10 #define free_page_frame 0b11 -extern void load_page_directory(ppdt pdt); -extern void enable_paging(); +extern void load_page_directory(void* pdt); +// extern void enable_paging(); // non-PSE +extern void enable_paging_pse(); // PSE extern void get_cr2_and_cr3(void* dest_cr2, void* dest_cr3); extern void invalidate_page(void* virt_address); +// marks memory with each page of 4 KB to PDT, non-PSE ppdt markup_pdt(); - -// marks memory with each page of 4 KB to PDT -void* markup_mem(void* mem, unsigned int count_pages); +// PSE +ppdtx markup_pdt_pse(); // marks memory with each page of 4 KB to PDT // [[deprecated]] diff --git a/src/kernel/sys/paging/pdt.h b/src/kernel/sys/paging/pdt.h index 0f0180f..8695aed 100644 --- a/src/kernel/sys/paging/pdt.h +++ b/src/kernel/sys/paging/pdt.h @@ -27,7 +27,7 @@ struct page_directory_entry typedef struct page_directory_entry page_directory_entry_t, pde, *ppde; #pragma pack(push, 1) -struct page_directory_entry_ext +struct page_directory_entry_pse { uint32_t present : 1; // bit 0: always 1 uint32_t rw : 1; // bit 1: read/write @@ -38,12 +38,12 @@ struct page_directory_entry_ext uint32_t ignored1 : 3; // bit 9 - 11: available uint32_t cache_ctrl3 : 1; // bit 12: cache control uint32_t phys_address : 8; // bit 13 - 20: bits of physical address of page frame - uint32_t reserved : 8; // bit 21: reserved bit - uint32_t frame_address : 20; // bit 12 - 31: frame address + uint32_t reserved : 1; // bit 21: reserved bit + uint32_t frame_address : 10; // bit 12 - 31: frame address } __attribute__((packed)); #pragma pack(pop) -typedef struct page_directory_entry_ext page_directory_entry_x_t, pdex, *ppdex; +typedef struct page_directory_entry_pse page_directory_entry_x_t, pdex, *ppdex; typedef struct page_directory_table { @@ -51,7 +51,7 @@ typedef struct page_directory_table } page_directory_table_t, pdt, *ppdt; -typedef struct page_directory_table_ext +typedef struct page_directory_table_pse { pdex pages[MAX_PDE_PER_PDT]; From 3114c6ed5471d3b5b0de3c1eed547645c8ab427c Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 16 May 2024 04:45:06 +0700 Subject: [PATCH 74/88] temp commit --- Makefile | 51 +++++++-- bochs.linux.conf | 2 +- bochs.win.conf | 2 +- bochsdbg.win.conf | 2 +- src/kernel/kernel.c | 143 ++++++++++++++---------- src/kernel/link.ld | 35 ++++++ src/kernel/misc/list.c | 83 ++++++++++++++ src/kernel/misc/list.h | 24 ++++ src/kernel/sys/interruption.c | 60 +++++----- src/kernel/sys/interruption.h | 6 +- src/kernel/sys/io/printf.c | 2 +- src/kernel/sys/memory/alloc.c | 170 ++++++++++++++++++++++++++++- src/kernel/sys/memory/alloc.h | 13 ++- src/kernel/sys/paging/paging.asm | 16 ++- src/kernel/sys/paging/paging.c | 29 +++-- src/kernel/sys/paging/paging.h | 10 +- src/kernel/sys/process/process.c | 86 +++++++++++++++ src/kernel/sys/process/process.h | 10 ++ src/kernel/sys/process/scheduler.c | 131 ++++++++++++++++++++++ src/kernel/sys/process/scheduler.h | 44 ++++++++ 20 files changed, 802 insertions(+), 117 deletions(-) create mode 100644 src/kernel/link.ld create mode 100644 src/kernel/misc/list.c create mode 100644 src/kernel/misc/list.h create mode 100644 src/kernel/sys/process/process.c create mode 100644 src/kernel/sys/process/process.h create mode 100644 src/kernel/sys/process/scheduler.c create mode 100644 src/kernel/sys/process/scheduler.h diff --git a/Makefile b/Makefile index 1ebe1d1..336a568 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm KERNEL_C = $(KERNEL_SOURCE_DIR)/kernel.c KERNEL_ENTRY_ASM = $(KERNEL_SOURCE_DIR)/kernel_entry.asm -TRAMPLINS_WITH_CONTEXT_O = $(KERNEL_BUILD_DIR)/sys/context/tramplinsWContext.o +TRAMP_WC_O = $(KERNEL_BUILD_DIR)/sys/context/tramplinsWContext.o BOOTLOADER_BIN = $(BOOTLOADER_BUILD_DIR)/bootloader.bin KERNEL_O = $(KERNEL_BUILD_DIR)/kernel.o @@ -22,6 +22,8 @@ KERNEL_BIN = $(KERNEL_BUILD_DIR)/kernel.bin FLOPPY_IMG = $(BUILD_DIR)/floppy.img KERNEL_ENTRY_ADDRESS = 0x20200 +# 0xC0020200 +# 0x20200 ifeq ($(OS), Windows_NT) ASM = ${NASM_PATH}\nasm @@ -53,7 +55,7 @@ endif $(info init..) -.PHONY: clean_boot build_boot floppy kernel bootloader +.PHONY: clean_boot floppy kernel bootloader floppy: bootloader kernel $(FLOPPY_IMG) kernel: $(KERNEL_BIN) @@ -83,7 +85,9 @@ $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o: $(KERNEL_SOURCE_DIR)/sys/memory/mem_ $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ $(KERNEL_BUILD_DIR)/sys/memory/alloc.o: $(KERNEL_SOURCE_DIR)/sys/memory/alloc.c \ - $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h + $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h \ + $(KERNEL_SOURCE_DIR)/misc/list.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ @@ -94,24 +98,45 @@ $(KERNEL_BUILD_DIR)/sys/context/exp.o: $(KERNEL_SOURCE_DIR)/sys/context/experime $(KERNEL_BUILD_DIR)/sys/paging/paging.o: $(KERNEL_SOURCE_DIR)/sys/paging/paging.c \ $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h \ $(KERNEL_SOURCE_DIR)/sys/paging/pdt.h $(KERNEL_SOURCE_DIR)/sys/paging/pt.h \ - $(KERNEL_SOURCE_DIR)/sys/io/io.h + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(TRAMPLINS_WITH_CONTEXT_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm +$(KERNEL_BUILD_DIR)/misc/list.o: $(KERNEL_SOURCE_DIR)/misc/list.c \ + $(KERNEL_SOURCE_DIR)/misc/list.h $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(KERNEL_BUILD_DIR)/sys/process/process.o: $(KERNEL_SOURCE_DIR)/sys/process/process.c \ + $(KERNEL_SOURCE_DIR)/sys/process/process.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(KERNEL_BUILD_DIR)/sys/process/scheduler.o: $(KERNEL_SOURCE_DIR)/sys/process/scheduler.c \ + $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(KERNEL_SOURCE_DIR)/misc/list.h \ + $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h $(KERNEL_SOURCE_DIR)/sys/context/context.h \ + $(KERNEL_SOURCE_DIR)/sys/process/process.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(TRAMP_WC_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ $(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ - $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp $(TRAMPLINS_WITH_CONTEXT_O) \ - $(KERNEL_SOURCE_DIR)/sys/context/context.h $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/memory.h \ - $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h + $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp \ + $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm \ + $(KERNEL_SOURCE_DIR)/sys/context/context.h $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h \ + $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h \ + $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h \ +$(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h \ $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.h \ - $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h + $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h \ + $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 @@ -123,10 +148,12 @@ $(KERNEL_ENTRY_O): $(KERNEL_ENTRY_ASM) $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/io/printf.o $(BUILD_DIR)/pm/io/print_d2vm.o $(KERNEL_BUILD_DIR)/sys/memory/alloc.o \ $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o \ - $(TRAMPLINS_WITH_CONTEXT_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ - $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o + $(TRAMP_WC_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ + $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o $(KERNEL_BUILD_DIR)/misc/list.o \ + $(KERNEL_BUILD_DIR)/sys/process/process.o $(KERNEL_BUILD_DIR)/sys/process/scheduler.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) +# $(LD) -m i386pe -T $(KERNEL_SOURCE_DIR)/link.ld $^ -o $(KERNEL_TMP) # $(KERNEL_TMP) $(OBJCOPY) -I pe-i386 -O binary $(KERNEL_TMP) $@ diff --git a/bochs.linux.conf b/bochs.linux.conf index 81b7f3a..f00bb31 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig display_library: x, options="gui_debug" -memory: host=32, guest=1024 +memory: host=1024, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/bochs.win.conf b/bochs.win.conf index 8e37e23..8b6f8c8 100644 --- a/bochs.win.conf +++ b/bochs.win.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: win32config display_library: win32 -memory: host=32, guest=1024 +memory: host=1024, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/bochsdbg.win.conf b/bochsdbg.win.conf index b3fa93a..8a539a8 100644 --- a/bochsdbg.win.conf +++ b/bochsdbg.win.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: win32config display_library: win32, options="gui_debug" -memory: host=32, guest=1024 +memory: host=1024, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 8c7d96e..441101c 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -6,6 +6,7 @@ extern void experiment(); #include "./sys/interruption.h" #include "./sys/pic/pic.h" #include "./sys/paging/paging.h" +#include "./sys/process/scheduler.h" // compare [first, first_end) and second (until '\0') @@ -19,6 +20,12 @@ void str_reverse(char* str_start, char* str_end) { } } +void task1() +{ + static unsigned int i = 0u; + while(i < 100000) { if (i % 10) { printf("\n"); } printf("%d ", i++); } +} + void entry_point() { init_printer(); @@ -26,9 +33,6 @@ void entry_point() // ||||||||||||| asm_print_d2vm("Hello from C!"); - register_handlers(); - setup_pic(); - // non-PSE /* ppdt actual_pdt = markup_pdt(); if (!actual_pdt) @@ -45,8 +49,8 @@ void entry_point() enable_paging(); */ // PSE - ppdtx actual_pdt = markup_pdt_pse(); - asm("xchgw %bx, %bx"); + ppdtx actual_pdt = create_pdt_pse(); + if (!actual_pdt) { printf("PDT initialization finished bad!\n"); @@ -57,68 +61,93 @@ void entry_point() { printf("PDT on %x\n", actual_pdt); } - asm("xchgw %bx, %bx"); load_page_directory(actual_pdt); - asm("xchgw %bx, %bx"); enable_paging_pse(); + jmp_to_higher_half(); - int* temp = kernel_malloc(sizeof(int) * 100); - asm("xchgw %bx, %bx"); - if (!temp) - { - printf("Bad allocation\n"); - asm("xchgw %bx, %bx"); - } - else + fix_heap_to_higher_half(); + InterruptDescriptor32* pidt = init_idt(0xC0000000); + reg_idtd(pidt); + setup_pic(); + // GDT move here + // ... + unfix_heap_to_higher_half(); + + actual_pdt = (ppdtx)((ptr_t)actual_pdt + 0xC0000000); + for(unsigned int i = 0; i < 64; ++i) { - printf("Temp on %x\n", temp); - for(ptr_t i = 0u; i < 100; ++i) - { - *temp = 0x1; - } + if (!actual_pdt->pages[i].present) break; + actual_pdt->pages[i].present = 0; + invalidate_page((void*)((ptr_t)actual_pdt->pages[i].frame_address >> 22)); } - + // GDT does not move, so + actual_pdt->pages[0ul].present = 1; + invalidate_page((void*)((ptr_t)actual_pdt->pages[0u].frame_address >> 22)); + + fix_heap_to_N(0x200000); + // lab №7, #1 experiment(); - int* temp2 = kernel_malloc(sizeof(int) * 0x10000000); - temp2 = kernel_malloc(sizeof(int) * 0x10000000); - temp2 = kernel_malloc(sizeof(int) * 0x100000); + //----------------------------------------------------------------------- TEST ZONE : C_MALLOC, C_DELETE + int* temp = c_malloc(sizeof(int) * 10); + int* temp1 = c_malloc(sizeof(int) * 20); + int* temp2 = c_malloc(sizeof(int) * 10); + int* temp3 = c_malloc(sizeof(int) * 10); + int* temp4 = c_malloc(sizeof(int) * 10); + int* temp5 = c_malloc(sizeof(int) * 30); + int* temp6 = c_malloc(sizeof(int) * 20); + c_delete(temp, sizeof(int) * 10); + c_delete(temp1, sizeof(int) * 20); + c_delete(temp2, sizeof(int) * 10); + int* temp7 = c_malloc(sizeof(int) * 30); + c_delete(temp3, sizeof(int) * 10); + int* temp8 = c_malloc(sizeof(int) * 20); asm("xchgw %bx, %bx"); - if (!temp2) - { - printf("Bad allocation\n"); - asm("xchgw %bx, %bx"); - } - else - { - printf("Temp on %x\n", temp2); - for(ptr_t i = 0u; i < 0x100000; ++i) - { - *temp2 = 0x10 + i; - } - } + //----------------------------------------------------------------------- - int* temp3 = kernel_malloc(sizeof(int) * 0x1000); - asm("xchgw %bx, %bx"); - if (!temp3) - { - printf("Bad allocation\n"); - asm("xchgw %bx, %bx"); - } - else - { - printf("Temp on %x\n", temp3); - for(ptr_t i = 0u; i < 0x1000; ++i) - { - printf("%d ", temp3[i]); - } - printf("\n"); - } + //----------------------------------------------------------------------- TEST ZONE : PROCESSES + reg_os(); + add_task(ARBITRARY, task1, 0u); asm("xchgw %bx, %bx"); + //----------------------------------------------------------------------- - // int* temp3 = kernel_malloc(sizeof(int) * 0x100); + //----------------------------------------------------------------------- TEST ZONE : PAGING, KERNEL_KMALLOC // asm("xchgw %bx, %bx"); + // int* temp = malloc(sizeof(int) * 100); + // if (!temp) + // { + // printf("Bad allocation\n"); + // asm("xchgw %bx, %bx"); + // } + // else + // { + // asm("xchgw %bx, %bx"); + // printf("Temp on %x\n", temp); + // for(ptr_t i = 0u; i < 100; ++i) + // { + // *temp = 0x1; + // } + // } + + // int* temp2 = malloc(sizeof(int) * 0x10000000); + // temp2 = malloc(sizeof(int) * 0x10000000); + // temp2 = malloc(sizeof(int) * 0x100000); + // if (!temp2) + // { + // printf("Bad allocation\n"); + // asm("xchgw %bx, %bx"); + // } + // else + // { + // printf("Temp on %x\n", temp2); + // for(ptr_t i = 0u; i < 0x100000; ++i) + // { + // *temp2 = 0x10 + i; + // } + // } + + // int* temp3 = malloc(sizeof(int) * 0x1000); // if (!temp3) // { // printf("Bad allocation\n"); @@ -127,11 +156,13 @@ void entry_point() // else // { // printf("Temp on %x\n", temp3); - // for(ptr_t i = 0u; i < 0x100; ++i) + // for(ptr_t i = 0u; i < 0x1000; ++i) // { - // printf("%d", temp3[i]); + // printf("%d ", temp3[i]); // } + // printf("\n"); // } + //----------------------------------------------------------------------- asm("sti"); // while(1) { diff --git a/src/kernel/link.ld b/src/kernel/link.ld new file mode 100644 index 0000000..4fde54e --- /dev/null +++ b/src/kernel/link.ld @@ -0,0 +1,35 @@ +ENTRY(loader) +/* the name of the entry symbol */ + +. = 0xC0100000 +/* the code should be relocated to 3GB + 1MB */ +/* align at 4 KB and load at 1 MB */ + +.text ALIGN (0x1000) : AT(ADDR(.text) - 0xC0000000) +{ + *(.text) + /* all text sections from all files */ +} +/* align at 4 KB and load at 1 MB + . */ + +.rodata ALIGN (0x1000) : AT(ADDR(.text) - 0xC0000000) +{ + *(.rodata*) + /* all read-only data sections from all files */ +} +/* align at 4 KB and load at 1 MB + . */ + +.data ALIGN (0x1000) : AT(ADDR(.text) - 0xC0000000) +{ + *(.data) + /* all data sections from all files */ +} +/* align at 4 KB and load at 1 MB + . */ + +.bss ALIGN (0x1000) : AT(ADDR(.text) - 0xC0000000) +{ + *(COMMON) + /* all COMMON sections from all files */ + *(.bss) + /* all bss sections from all files */ +} \ No newline at end of file diff --git a/src/kernel/misc/list.c b/src/kernel/misc/list.c new file mode 100644 index 0000000..1970ebb --- /dev/null +++ b/src/kernel/misc/list.c @@ -0,0 +1,83 @@ +#include "./list.h" +#include "../sys/memory/alloc.h" + +node* find_tail(node* root) +{ + while(root && root->next) { root = root->next; } + return root; +} + +node* find_n(node* root, unsigned int n) +{ + while(root && root->next && n > 0) { root = root->next; --n; } + return root; +} + +node* add_head(node* root, uint32_t data) +{ + node* new_node = malloc(sizeof(node)); + new_node->data = data; + new_node->data2 = 0; + new_node->data3 = 0; + new_node->next = root; + return new_node; +} + +node* add_tail(node* root, uint32_t data) +{ + node* tail = find_tail(root); + node* new_node = malloc(sizeof(node)); + new_node->data = data; + new_node->data2 = 0; + new_node->data3 = 0; + new_node->next = nullptr; + if (tail == nullptr) return new_node; + tail->next = new_node; + return tail; +} + +node* add_after_n(node* root, uint32_t data, unsigned int n) +{ + node* prev = find_n(root, n); + node* new_node = malloc(sizeof(node)); + new_node->data = data; + new_node->data2 = 0; + new_node->data3 = 0; + new_node->next = nullptr; + if (prev == nullptr) return new_node; + if (prev->next) new_node->next = prev->next; + prev->next = new_node; + return root; +} + +node* remove_head(node* root) +{ + if (root == nullptr || root->next == nullptr) return nullptr; + node* ret = root->next; + root->next = nullptr; + return ret; +} + +node* remove_tail(node* root) +{ + node* cur = root; + while(cur && cur->next && cur->next->next) { cur = cur->next; } + if (cur == nullptr || cur->next == nullptr) return nullptr; + cur->next = nullptr; + return root; +} + +node* remove_n(node* root, unsigned int n) +{ + if (root == nullptr || root->next == nullptr) return nullptr; + if (n == 0) return remove_head(root); + node* prev = find_n(root, n - 1); + if (prev->next == nullptr) return remove_n(root, n - 2); + if (prev->next->next == nullptr) + { + prev->next = nullptr; + return root; + } + prev->next == prev->next->next; + return root; +} \ No newline at end of file diff --git a/src/kernel/misc/list.h b/src/kernel/misc/list.h new file mode 100644 index 0000000..45fa5cd --- /dev/null +++ b/src/kernel/misc/list.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#pragma pack(push, 1) +typedef struct node_struct +{ + uint32_t data; + uint32_t data2; + uint32_t data3; + struct node_struct* next; + +} node, *pnode; +#pragma pack(pop) + + +node* find_tail(node* root); +node* find_n(node* root, unsigned int n); +node* add_head(node* root, uint32_t data); +node* add_tail(node* root, uint32_t data); +node* add_after_n(node* root, uint32_t data, unsigned int n); +node* remove_head(node* root); +node* remove_tail(node* root); +node* remove_n(node* root, unsigned int n); \ No newline at end of file diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index c9f32cc..852eaad 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -1,5 +1,6 @@ #include "./interruption.h" #include "./paging/paging.h" +#include "./process/scheduler.h" void kernel_panic(char* str, int type) { @@ -13,10 +14,10 @@ void panic_handler(int vector) kernel_panic("unhandled interrupt %x", vector); } -void register_handlers() +InterruptDescriptor32* init_idt(ptr_t shift) { InterruptDescriptor32* idt = (InterruptDescriptor32*)kernel_calloc(sizeof(InterruptDescriptor32) * 256u, 12u); - if (idt == (InterruptDescriptor32*)0) + if (idt == nullptr) { printf("[EXC] register_handlers(): not enough memory!"); } @@ -31,11 +32,16 @@ void register_handlers() { idt[i].gate_type = 0x8E; } - idt[i].offset_1 = (uint16_t) tramplins[i]; // lower bits - idt[i].offset_2 = (uint16_t) (((uint32_t) tramplins[i]) >> 16); // upper bits + idt[i].offset_1 = (uint16_t) ((ptr_t)tramplins[i] + shift); // lower bits + idt[i].offset_2 = (uint16_t) (((ptr_t)tramplins[i] + shift) >> 16); // upper bits idt[i].selector = 0b1000; } + return idt; +} + +void reg_idtd(InterruptDescriptor32* idt) +{ IDTD idtd; idtd.IDT_size = sizeof(InterruptDescriptor32) * 256 - 1; idtd.linear_addr_IDT = (uint32_t)(idt); @@ -45,6 +51,7 @@ void register_handlers() void timer_interrupt(context* ctx); void page_fault(context* ctx); +void page_fault_pse(context* ctx); void default_panic_handler(context* ctx); void interrupt_handler(context* ctx) { @@ -53,6 +60,7 @@ void interrupt_handler(context* ctx) { { timer_interrupt(ctx); outb(0x20, 0x20); + asm("xchgw %bx, %bx"); break; } case 0xe: @@ -67,32 +75,32 @@ void interrupt_handler(context* ctx) { break; } } - } void timer_interrupt(context* ctx) { static unsigned int counter = 0u; printf("\n%d ", counter++); + switch_to_next(ctx); } void page_fault(context* ctx) { // init_printer(); - // printf("[PAGE FAULT HABDLING] START\n"); + // printf("[PAGE FAULT HANDLING] START\n"); unsigned int ec = ctx->error_code; if (!(ec & 1)) { ptr_t* cr2 = nullptr; ppdt cr3 = nullptr; get_cr2_and_cr3(&cr2, &cr3); - // printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS %x\n", cr2); + // printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS %x\n", cr2); unsigned int idx1 = 0u; unsigned int idx2 = 0u; ppt cur_pt = nullptr; ppte cur_ppte = nullptr; do { - // printf("[PAGE FAULT HABDLING] FOUND PDT (PDT IDX : %d)\n", idx1); + // printf("[PAGE FAULT HANDLING] FOUND PDT (PDT IDX : %d)\n", idx1); pde cur_pde = cr3->pages[idx1]; // printf("ACTUAL PDT: %x\nIT's PT: %x\nIT's PT << 12u: %x\n", cr3, cur_pde.page_table, (cur_pde.page_table << 12)); // printf( "uint32_t present : %x\n" @@ -113,7 +121,7 @@ void page_fault(context* ctx) cr3->pages[idx1].present = 1; // printf( "[UPDATE] uint32_t present : %x\n", cr3->pages[idx1].present); } - cur_pt = (cr3->pages[idx1].page_table << 12); + cur_pt = (ppt)(cr3->pages[idx1].page_table << 12); idx2 = 0u; while(idx2 < 1023u && !((cur_pt->pages[idx2].page_frame << 12) <= (ptr_t)cr2 && (cur_pt->pages[idx2 + 1].page_frame << 12) > (ptr_t)cr2)) { ++idx2; } @@ -127,7 +135,7 @@ void page_fault(context* ctx) ++idx1; if (idx1 >= 1024u) { - printf("[PAGE FAULT HABDLING] PDT NOT FOUND (PDT IDX : %d)\n", idx1); + printf("[PAGE FAULT HANDLING] PDT NOT FOUND (PDT IDX : %d)\n", idx1); asm("xchgw %bx, %bx"); return; } @@ -173,7 +181,7 @@ void page_fault(context* ctx) { case not_stated: { - printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); + printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; @@ -185,12 +193,12 @@ void page_fault(context* ctx) invalidate_page(cr2); // load_page_directory(cr3); // enable_paging(); - // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); + // printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); break; } case enabled_page_frame: { - printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); + printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; @@ -204,12 +212,12 @@ void page_fault(context* ctx) invalidate_page(cr2); // load_page_directory(cr3); // enable_paging(); - // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); + // printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); break; } default: { - printf("[PAGE FAULT HABDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: %d\n", cur_ppte->ignored1); + printf("[PAGE FAULT HANDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: %d\n", cur_ppte->ignored1); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; @@ -220,15 +228,15 @@ void page_fault(context* ctx) void page_fault_pse(context* ctx) { - init_printer(); - printf("[PAGE FAULT HABDLING] START\n"); + // init_printer(); + // printf("[PAGE FAULT HANDLING] START\n"); unsigned int ec = ctx->error_code; if (!(ec & 1)) { ptr_t* cr2 = nullptr; ppdtx cr3 = nullptr; get_cr2_and_cr3(&cr2, &cr3); - printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS %x\n", cr2); + // printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS %x\n", cr2); int found = 0; for(unsigned int i = 0u; i < 1024u; ++i) { @@ -239,7 +247,7 @@ void page_fault_pse(context* ctx) { case not_stated: { - printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); + printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:1\n"); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; @@ -251,12 +259,13 @@ void page_fault_pse(context* ctx) invalidate_page(cr2); // load_page_directory(cr3); // enable_paging(); - // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); + // printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION HANDLED:1\n"); + found = 1; break; } case enabled_page_frame: { - printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); + printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION CANNOT BE HANDLED:2\n"); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; @@ -270,23 +279,24 @@ void page_fault_pse(context* ctx) invalidate_page(cr2); // load_page_directory(cr3); // enable_paging(); - // printf("[PAGE FAULT HABDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); + //printf("[PAGE FAULT HANDLING] PAGE FAULT EXCEPTION HANDLED:2\n"); + found = 1; break; } default: { - printf("[PAGE FAULT HABDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: PAGE-FLAG = %d\n", cr3->pages[i].ignored1); + printf("[PAGE FAULT HANDLING] SWITCH-CASE TROUBLE IN PAGE FAULT EXCEPTION HANDLING: PAGE-FLAG = %d\n", cr3->pages[i].ignored1); asm("xchgw %bx, %bx"); default_panic_handler(ctx); return; } } - found = 1; + if (found) break; } } if (!found) { - printf("[PAGE FAULT HABDLING] CAUSE-ADDRESS NOT FOUND IN PDTX"); + printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS NOT FOUND IN PDTX"); } } } diff --git a/src/kernel/sys/interruption.h b/src/kernel/sys/interruption.h index 34c73cc..a1e8575 100644 --- a/src/kernel/sys/interruption.h +++ b/src/kernel/sys/interruption.h @@ -17,5 +17,7 @@ typedef struct { void kernel_panic(char* str, int type); void panic_handler(int vector); -void interrupt_handler(context* ctx); -void register_handlers(); \ No newline at end of file +InterruptDescriptor32* init_idt(); +void reg_idtd(InterruptDescriptor32* idt); + +void interrupt_handler(context* ctx); \ No newline at end of file diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 186751a..14bd10c 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -102,11 +102,11 @@ void printf(const char* fmt, ...) { } else if (mode == 'd') { unsigned b = (unsigned) va_arg(a, int); #define _MAX_N_DIGITS_FOR_DEC_CASE (int)(12) - if (b != 0) { char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; +#undef _MAX_N_DIGITS_FOR_DEC_CASE int is_neg = 0; if (b & (1 << sizeof(int) * 8 - 1)) { diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index 98119a3..3aa3078 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -1,13 +1,34 @@ #include "./alloc.h" #include "../paging/paging.h" #include "../io/io.h" +#include "../../misc/list.h" -#define MEMORY_MAX ((ptr_t)(0x80000000)) +#define MEMORY_MAX ((ptr_t)(0xC0000000)) +// #define MEMORY_MAX ((ptr_t)(0x2001F8)) -static unsigned char* const heap_start = (unsigned char*)(0x100000); -static unsigned char* top = (unsigned char*)(0x100000); +static unsigned char* heap_start = (unsigned char*)(0x100000); +static unsigned char* top = (unsigned char*)(0x100000); -static void* malloc(ptr_t size) { +void fix_heap_to_higher_half() +{ + heap_start = (unsigned char*)((ptr_t)heap_start + 0xC0000000); + top = (unsigned char*)((ptr_t)top + 0xC0000000); +} + +void unfix_heap_to_higher_half() +{ + heap_start = (unsigned char*)((ptr_t)heap_start - 0xC0000000); + top = (unsigned char*)((ptr_t)top - 0xC0000000); +} + +ptr_t fix_heap_to_N(unsigned long N) +{ + heap_start = (unsigned char*)(N); + top = (unsigned char*)(N); + return (ptr_t)top; +} + +void* malloc(ptr_t size) { if ((ptr_t)top + size > MEMORY_MAX) return nullptr; printf("\n-----------------\nAlloc: %x [%x]\n", top, size); @@ -56,3 +77,144 @@ void* kernel_calloc(ptr_t size, ptr_t num) { void kernel_free(void* addr) { top = heap_start; // we need to unset relevant present bits in pde's and pte's } + +// ------------------ + +node* free_list = nullptr; + +void print_free_list() +{ + printf("\n ----------\n free_list:\n "); + node* cur = free_list; + unsigned int i = 0u; + while(cur) + { + printf("[%d] on %x : .data = %x, .data2 = %x, .next = %x\n ", i, cur, cur->data, cur->data2, cur->next); + cur = cur->next; + ++i; + } +} + +void* c_malloc(ptr_t size) +{ + if ((ptr_t)top + size > MEMORY_MAX) + { + if (free_list == nullptr) + { + printf("\n-----------------\nc_alloc(empty)\n"); + return nullptr; + } + node* cur = free_list; + node* found = nullptr; + unsigned int n_cur = 0u; + unsigned int n_found = 0u; + while(cur) + { + if ((ptr_t)cur->data2 >= size && ((found == nullptr) || (cur->data2 < found->data2))) + { + found = cur; + n_found = n_cur; + } + cur = cur->next; + ++n_cur; + } + if (found) + { + void* target = (void*)found->data; + printf("\n-----------------\nc_alloc(use free_list[%d]): %x [%x] [free_list.size = %x]\n", n_found, target, size, found->data2); + if (found->data2 > size) + { + found->data += size; + found->data2 -= size; + } + else + { + free_list = remove_n(free_list, n_found); + } + printf("address of free_list = %x\n", free_list); + return target; + } + return nullptr; + } + top += size; + printf("\n-----------------\nc_alloc: %x [%x]\n", top - size, size); + return top - size; +} + +void c_delete(void* ptr, ptr_t size) +{ + node* found = free_list; + if (found && (ptr_t)found->data >= (ptr_t)ptr) + { + free_list = add_head(free_list, (uint32_t)ptr); + free_list->data2 = size; + // printf("\n-----------------\nc_delete(free_list[:0]): %x [%x]\n", ptr, size); + print_free_list(); + if ((ptr_t)free_list->data + size >= (ptr_t)free_list->next->data) + { + free_list->data2 += free_list->next->data2; + free_list = remove_n(free_list, 1u); + // printf("\n-----------------\nc_delete(free_list[:0]): concat with free_list[1], " + // "free_list[0].ptr = %x, .size = %x\n", free_list->data, free_list->data2); + print_free_list(); + } + return; + } + unsigned int i = 0u; + while(found) + { + node* cur = nullptr; + if ((ptr_t)found->data <= (ptr_t)ptr && ((cur = found->next) == nullptr || (ptr_t)cur->data >= (ptr_t)ptr)) + { + free_list = add_after_n(free_list, (uint32_t)ptr, i); + cur = found->next; + cur->data2 = size; + node* next = cur->next; + // printf("\n-----------------\nc_delete(free_list[%d]): %x [%x]\n", i + 1, ptr, size); + print_free_list(); + if (next && ((ptr_t)cur->data + size >= (ptr_t)next->data)) + { + cur->data2 += next->data2; + free_list = remove_n(free_list, i + 2); + // printf("\n-----------------\nc_delete(free_list[%d]): concat with free_list[%d], " + // "free_list[0].ptr = %x, .size = %x\n", i + 1, i + 2, cur->data, cur->data2); + print_free_list(); + } + if ((ptr_t)found->data + found->data2 >= (ptr_t)cur->data) + { + found->data2 += cur->data2; + free_list = remove_n(free_list, i + 1); + // printf("\n-----------------\nc_delete(free_list[%d]): concat with free_list[%d], " + // "free_list[0].ptr = %x, .size = %x\n", i + 1, i, found->data, found->data2); + print_free_list(); + } + return; + } + found = found->next; + ++i; + } + node* prev = add_tail(free_list, (uint32_t)ptr); + if (free_list == nullptr) + { + free_list = prev; + found = free_list; + found->data2 = size; + // printf("\n-----------------\nc_delete(free_list[::%d]): %x [%x]\n", i, found->data, found->data2); + print_free_list(); + } + else + { + found = prev->next; + found->data2 = size; + // printf("\n-----------------\nc_delete(free_list[::%d]): %x [%x]\n", i, found->data, found->data2); + print_free_list(); + if ((ptr_t)prev->data + prev->data2 >= (ptr_t)found->data) + { + prev->data2 += found->data2; + free_list = remove_tail(free_list); + // printf("\n-----------------\nc_delete(free_list[::%d]): concat with free_list[%d], " + // "free_list[0].ptr = %x, .size = %x\n", i, i - 1, prev->data, prev->data2); + print_free_list(); + } + } +} \ No newline at end of file diff --git a/src/kernel/sys/memory/alloc.h b/src/kernel/sys/memory/alloc.h index 9756bfb..4c17c8e 100644 --- a/src/kernel/sys/memory/alloc.h +++ b/src/kernel/sys/memory/alloc.h @@ -18,13 +18,20 @@ typedef unsigned int ptr_t; #include "mem_utils.h" +void fix_heap_to_higher_half(); +void unfix_heap_to_higher_half(); +ptr_t fix_heap_to_N(unsigned long N); + +void* malloc(ptr_t size); + // kernel_malloc, but aligned to (1u << align_bit) bytes void* kernel_kmalloc(ptr_t size, ptr_t align_bit); void* kernel_malloc(ptr_t size); - void* kernel_calloc(ptr_t size, ptr_t num); - /* void* kernel_realloc(void* old_addr, ptr_t new_size); */ - void kernel_free(void* addr); + +void init_alloc(); +void* c_malloc(ptr_t size); +void c_delete(void* ptr, ptr_t size); \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm index 57cc448..993f6a0 100644 --- a/src/kernel/sys/paging/paging.asm +++ b/src/kernel/sys/paging/paging.asm @@ -5,6 +5,7 @@ [GLOBAL enable_paging_pse] [GLOBAL get_cr2_and_cr3] [GLOBAL invalidate_page] +[GLOBAL jmp_to_higher_half] ; [GLOBAL all_init_pdt] ; all_init_pdt: @@ -112,7 +113,10 @@ enable_paging_pse: or eax, 0x80000001 ; set PE (bit 0) and PG (bit 31) mov cr0, eax ; update cr0 ; now paging is enabled - ret + ; xchg bx, bx + ; ret + pop eax + jmp eax get_cr2_and_cr3: push ebp @@ -130,5 +134,13 @@ get_cr2_and_cr3: ret invalidate_page: - INVLPG [ebp + 4] + INVLPG [esp + 4] + ret + +jmp_to_higher_half: + pop ebx + add ebx, 0xC0000000 + add ebp, 0xC0000000 + add esp, 0xC0000000 + push ebx ret \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.c b/src/kernel/sys/paging/paging.c index cef0401..84abc9f 100644 --- a/src/kernel/sys/paging/paging.c +++ b/src/kernel/sys/paging/paging.c @@ -102,25 +102,40 @@ ppdt markup_pdt() return pdt_mem; } -ppdtx markup_pdt_pse() +ppdtx create_pdt_pse() { - ppdtx pdtx_mem = kernel_kmalloc(1024u * sizeof(pde), 12u); + return markup_pdt_pse(kernel_kmalloc(1024u * sizeof(pde), 12u)); +} + +// demand 1024u * sizeof(pde) of memory +ppdtx markup_pdt_pse(void* mem_place) +{ + ppdtx pdtx_mem = mem_place; if (!pdtx_mem) { printf("[EXC] alloc_pdt():1: not enough memory!\n"); return nullptr; } - ptr_t cur_phys_address = 0u; + ptr_t cur_virt_address = 0u; for(unsigned int i = 0u; i < 1024u; ++i) { ppdex cur_pdex = &pdtx_mem->pages[i]; - if (cur_phys_address <= (ptr_t)(pdtx_mem + 1)) + if (0xC0000000 <= cur_virt_address) { cur_pdex->present = 1u; cur_pdex->rw = 0u; cur_pdex->us = 0u; cur_pdex->ignored1 = enabled_page_frame; + cur_pdex->frame_address = ((cur_virt_address - 0xC0000000) >> 22); + } + else if (cur_virt_address < 0x40000000) + { + cur_pdex->present = 1u; + cur_pdex->rw = 0u; + cur_pdex->us = 0u; + cur_pdex->ignored1 = disabled_page_frame; + cur_pdex->frame_address = (cur_virt_address >> 22); } else { @@ -128,6 +143,7 @@ ppdtx markup_pdt_pse() cur_pdex->rw = 1u; cur_pdex->us = 1u; cur_pdex->ignored1 = disabled_page_frame; + cur_pdex->frame_address = (cur_virt_address >> 22); } cur_pdex->cache_ctrl1 = 0u; cur_pdex->ps = 1u; @@ -135,11 +151,10 @@ ppdtx markup_pdt_pse() cur_pdex->cache_ctrl3 = 0u; cur_pdex->phys_address = 0u; cur_pdex->reserved = 0u; - cur_pdex->frame_address = (cur_phys_address >> 22); - cur_phys_address += (1u << 22); + cur_virt_address += (1u << 22); } - + return pdtx_mem; } diff --git a/src/kernel/sys/paging/paging.h b/src/kernel/sys/paging/paging.h index 8706099..70b826f 100644 --- a/src/kernel/sys/paging/paging.h +++ b/src/kernel/sys/paging/paging.h @@ -15,10 +15,16 @@ extern void enable_paging_pse(); // PSE extern void get_cr2_and_cr3(void* dest_cr2, void* dest_cr3); extern void invalidate_page(void* virt_address); +extern void jmp_to_higher_half(); + +//----------------------------------------------------------------- non-PSE // marks memory with each page of 4 KB to PDT, non-PSE ppdt markup_pdt(); -// PSE -ppdtx markup_pdt_pse(); +//----------------------------------------------------------------- PSE +ppdtx create_pdt_pse(); +// demand 1024u * sizeof(pde) of memory +ppdtx markup_pdt_pse(void* mem_place); +//----------------------------------------------------------------- // marks memory with each page of 4 KB to PDT // [[deprecated]] diff --git a/src/kernel/sys/process/process.c b/src/kernel/sys/process/process.c new file mode 100644 index 0000000..ae2caa3 --- /dev/null +++ b/src/kernel/sys/process/process.c @@ -0,0 +1,86 @@ +#include "process.h" +#include "../io/io.h" + +// creates new space (virtual memory pages) for new processes +// big bang +ppdtx bb() +{ + // creates memory for new process PDT on OS heap + ppdtx proc_pdt_mem = c_malloc(sizeof(pde) * 1024); + if (!proc_pdt_mem) + { + printf("[EXC] alloc_pdt():1: not enough memory!\n"); + return nullptr; + } + // proc_pdt_mem = markup_pdt_pse(proc_pdt_mem); + + // for first page (legacy reason for now, since GDT was set up in ASM in early period) + { + ppdex cur_pdex = &proc_pdt_mem->pages[0u]; + cur_pdex->present = 1u; + cur_pdex->ignored1 = enabled_page_frame; + cur_pdex->frame_address = 1u; + cur_pdex->rw = 0u; + cur_pdex->us = 0u; + cur_pdex->cache_ctrl1 = 0u; + cur_pdex->ps = 1u; + cur_pdex->cache_ctrl2 = 0u; + cur_pdex->cache_ctrl3 = 0u; + cur_pdex->phys_address = 0u; + cur_pdex->reserved = 0u; + } + for(unsigned int i = 0u; i < 1024u; ++i) + { + ppdex cur_pdex = &proc_pdt_mem->pages[i]; + // 1024 - 256 == 768 + // 0xC0 * 4 == 768 + // 0xC00 / 4 == 768 + if (i < 768) + { + cur_pdex->present = 0u; + cur_pdex->ignored1 = disabled_page_frame; + cur_pdex->frame_address = 0u; + cur_pdex->rw = 1u; + cur_pdex->us = 1u; + cur_pdex->cache_ctrl1 = 0u; + cur_pdex->ps = 1u; + cur_pdex->cache_ctrl2 = 0u; + cur_pdex->cache_ctrl3 = 0u; + cur_pdex->phys_address = 0u; + cur_pdex->reserved = 0u; + } + else + { + cur_pdex->present = 1u; + cur_pdex->ignored1 = enabled_page_frame; + cur_pdex->frame_address = i - 768; + cur_pdex->rw = 0u; + cur_pdex->us = 0u; + cur_pdex->cache_ctrl1 = 0u; + cur_pdex->ps = 1u; + cur_pdex->cache_ctrl2 = 0u; + cur_pdex->cache_ctrl3 = 0u; + cur_pdex->phys_address = 0u; + cur_pdex->reserved = 0u; + } + } + + // enable stack + { + ptr_t pad_size = ((fix_heap_to_N(0) >> 22) + 1) - fix_heap_to_N(0); + void* padding = c_malloc(pad_size); + void* stack_mem = c_malloc(1u << 22); + c_delete((void*)padding, pad_size); + if (!stack_mem) + { + printf("[EXC] create stack: not enough memory!\n"); + return nullptr; + } + ppdex cur_pdex = &proc_pdt_mem->pages[768 - 1]; // 0xC0000000 - 4MB (ebp will be on 0xC0000000 - 1MB, so stack size = 3MB) + cur_pdex->present = 1u; + cur_pdex->ignored1 = enabled_page_frame; + cur_pdex->frame_address = (ptr_t)stack_mem; + } + + return proc_pdt_mem; +} \ No newline at end of file diff --git a/src/kernel/sys/process/process.h b/src/kernel/sys/process/process.h new file mode 100644 index 0000000..30c9e8a --- /dev/null +++ b/src/kernel/sys/process/process.h @@ -0,0 +1,10 @@ +#pragma once + +#include "../paging/paging.h" + +static unsigned char* process_stack_start = ((unsigned char*)(0xC0000000 - 0x100000 - 1)); // - 1MB +static unsigned char* process_heap_start = ((unsigned char*)(0x40000000)); // 1GB + +// creates new space (virtual memory pages) for new processes +// big bang +ppdtx bb(); \ No newline at end of file diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c new file mode 100644 index 0000000..5ab402b --- /dev/null +++ b/src/kernel/sys/process/scheduler.c @@ -0,0 +1,131 @@ +#include "scheduler.h" +#include "process.h" +#include "../paging/paging.h" + +#define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] +#define va_list char* +#define va_start(x) (char*)(&x + 1) + +void reg_os() +{ + os_task = c_malloc(sizeof(node)) + 0xC0000000; + os_task->data3 = 0xC0100000; +} + +void add_task(task_t type, void (*entry)(void), unsigned int sz_args, ...) +{ + va_list VAARGS = va_start(sz_args); + ptask new_task = c_malloc(sizeof(task)); + tasks = add_after_n(tasks, (uint32_t)new_task, ntasks); + node* cur_node = find_n(tasks, ntasks); + if (ntasks == 0u) + { + tasks->next = tasks; + ++ntasks; + } + new_task->entry = entry; + new_task->type = type; + new_task->state = INIT; + new_task->ctx->ebp = (uint32_t)process_stack_start; + new_task->ctx->esp = (uint32_t)process_stack_start; + ppdtx new_space = bb(); + tasks->data3 = (uint32_t)new_space; + int args[sz_args]; + if (sz_args > 0) + { + for(unsigned int i = 0u; i < sz_args; ++i) + { + args[i] = va_arg(VAARGS, int); + } + } + int* process_stack_esp_int = (int*)new_task->ctx->esp; + load_page_directory((void*)new_space); + if (sz_args > 0) + { + for(unsigned int i = 0u; i < sz_args; ++i, --process_stack_esp_int) + { + *process_stack_esp_int = args[sz_args - i - 1]; + } + } + load_page_directory((void*)os_task->data3); + *process_stack_esp_int = (ptr_t)&self_destruct; + --process_stack_esp_int; + new_task->ctx->esp = (uint32_t)process_stack_esp_int; +} + +void terminate_task() +{ + // ... +} + +void self_destruct() +{ + load_page_directory((void*)os_task->data3); + ((ptask)tasks->data)->state = DEAD; + while(1) {} +} + +void switch_to_next(context* cur_ctx) +{ + load_page_directory((void*)os_task->data3); + node* old_task_node = tasks; + ptask old_task = (ptask)tasks->data; + old_task->state = ON_PAUSE; + *(old_task->ctx) = *cur_ctx; // memcpy action + + tasks = tasks->next; + ptask next_task = (ptask)tasks->data; + do + { + switch (next_task->state) + { + case ACTIVE: + { + asm("xchgw %bx, %bx"); + asm("xchgw %bx, %bx"); + asm("xchgw %bx, %bx"); + asm("xchgw %bx, %bx"); + asm("xchgw %bx, %bx"); + return; + } + case ON_PAUSE: + { + next_task->state = ACTIVE; + *cur_ctx = *(next_task->ctx); // memcpy action + return; + } + case INIT: + { + *cur_ctx = *(next_task->ctx); // memcpy action + cur_ctx->eip = (uint32_t)next_task->entry; + load_page_directory((void*)tasks->data3); + return; + } + case DEAD: + { + for(unsigned int i = 1u; i < 768u; ++i) + { + c_delete((void*)(((ppdtx)tasks->data3)->pages[i].frame_address << 22), 1u << 22); + } + c_delete((void*)tasks->data3, sizeof(pde) * 1024); + c_delete((void*)tasks->data, sizeof(task)); + // manual remove head (cyclic list case) + if (ntasks > 1) + { + old_task_node->next = tasks->next; + } + c_delete((void*)tasks, sizeof(node)); + } + } + } while(1); +} + +context* switch_to_os(context* cur_ctx) +{ + // ... +} + +task_t get_type() +{ + return ((ptask)tasks->data)->type; +} \ No newline at end of file diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h new file mode 100644 index 0000000..852ddcb --- /dev/null +++ b/src/kernel/sys/process/scheduler.h @@ -0,0 +1,44 @@ +#pragma once + +#include "../../misc/list.h" +#include "../memory/alloc.h" +#include "../context/context.h" + +static unsigned int ntasks = 0u; +static node* tasks = nullptr; +static node* os_task = nullptr; + +typedef enum task_type +{ + ARBITRARY = 0b0, + OS = 0b1 + +} task_t; + +typedef enum state_type +{ + ON_PAUSE = 0b00, + ACTIVE = 0b01, + INIT = 0b10, + DEAD = 0b11 + +} state_t; + +#pragma pack(push, 1) +typedef struct task_struct +{ + task_t type; + state_t state; + void (*entry)(void); + context* ctx; + +} task, *ptask; +#pragma pack(pop) + +void reg_os(); +void add_task(task_t type, void (*entry)(void), unsigned int sz_args, ...); +void terminate_task(); +void self_destruct(); +void switch_to_next(context* cur_ctx); +context* switch_to_os(context* cur_ctx); +task_t get_type(); \ No newline at end of file From 7c708aa41d047a696fe863af1e01e00547fd9afd Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 18 May 2024 18:24:30 +0700 Subject: [PATCH 75/88] hell --- Makefile | 15 ++- bochs.linux.conf | 2 +- bochs.win.conf | 2 +- bochsdbg.win.conf | 2 +- src/kernel/kernel.c | 54 ++++++--- src/kernel/misc/list.c | 6 +- src/kernel/sys/context/context.h | 28 ++++- src/kernel/sys/interruption.c | 49 ++++++-- src/kernel/sys/memory/alloc.c | 33 ++++- src/kernel/sys/memory/alloc.h | 10 +- src/kernel/sys/paging/paging.asm | 4 + src/kernel/sys/paging/paging.c | 6 +- src/kernel/sys/paging/pdt.h | 2 +- src/kernel/sys/paging/pt.h | 2 +- src/kernel/sys/process/exec.asm | 50 ++++++++ src/kernel/sys/process/process.c | 16 ++- src/kernel/sys/process/process.h | 1 + src/kernel/sys/process/scheduler.c | 185 ++++++++++++++++++++++------- src/kernel/sys/process/scheduler.h | 16 +-- 19 files changed, 375 insertions(+), 108 deletions(-) create mode 100644 src/kernel/sys/process/exec.asm diff --git a/Makefile b/Makefile index 336a568..adbac91 100644 --- a/Makefile +++ b/Makefile @@ -65,10 +65,18 @@ $(BUILD_DIR)/pm/io/print_d2vm.o: $(SOURCE_DIR)/pm/io/print_d2vm.asm $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ +$(TRAMP_WC_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o: $(KERNEL_SOURCE_DIR)/sys/paging/paging.asm $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ +$(KERNEL_BUILD_DIR)/sys/process/exec.asm.o: $(KERNEL_SOURCE_DIR)/sys/process/exec.asm + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + $(KERNEL_BUILD_DIR)/sys/pic/pic.o: $(KERNEL_SOURCE_DIR)/sys/pic/pic.c \ $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) @@ -120,10 +128,6 @@ $(KERNEL_BUILD_DIR)/sys/process/scheduler.o: $(KERNEL_SOURCE_DIR)/sys/process/sc $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ -$(TRAMP_WC_O): $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm - $(info [MAKE] building: $@) - $(ASM) -f elf $< -o $@ - $(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c \ $(KERNEL_SOURCE_DIR)/sys/interruption.h $(KERNEL_SOURCE_DIR)/sys/tramplins.hpp \ $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm \ @@ -150,7 +154,8 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(KERNEL_BUILD_DIR)/sys/interruption.o $(KERNEL_BUILD_DIR)/sys/memory/mem_utils.o $(KERNEL_BUILD_DIR)/sys/pic/pic.o \ $(TRAMP_WC_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o $(KERNEL_BUILD_DIR)/misc/list.o \ - $(KERNEL_BUILD_DIR)/sys/process/process.o $(KERNEL_BUILD_DIR)/sys/process/scheduler.o + $(KERNEL_BUILD_DIR)/sys/process/process.o $(KERNEL_BUILD_DIR)/sys/process/scheduler.o \ + $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(LD) -m i386pe -T $(KERNEL_SOURCE_DIR)/link.ld $^ -o $(KERNEL_TMP) diff --git a/bochs.linux.conf b/bochs.linux.conf index f00bb31..768f329 100644 --- a/bochs.linux.conf +++ b/bochs.linux.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: textconfig display_library: x, options="gui_debug" -memory: host=1024, guest=4096 +memory: host=2048, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/bochs.win.conf b/bochs.win.conf index 8b6f8c8..b5ce8d7 100644 --- a/bochs.win.conf +++ b/bochs.win.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: win32config display_library: win32 -memory: host=1024, guest=4096 +memory: host=2048, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/bochsdbg.win.conf b/bochsdbg.win.conf index 8a539a8..8b6b602 100644 --- a/bochsdbg.win.conf +++ b/bochsdbg.win.conf @@ -1,7 +1,7 @@ plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true config_interface: win32config display_library: win32, options="gui_debug" -memory: host=1024, guest=4096 +memory: host=2048, guest=4096 boot: floppy floppy_bootsig_check: disabled=0 floppya: type=1_44, 1_44="./build/floppy.img", status=inserted, write_protected=0 diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 441101c..edbe5f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -20,10 +20,25 @@ void str_reverse(char* str_start, char* str_end) { } } -void task1() +void task1(int i, int j, int k) { - static unsigned int i = 0u; - while(i < 100000) { if (i % 10) { printf("\n"); } printf("%d ", i++); } + static unsigned int counter = 0u; + printf("Hello from process #1 : %d %d %d;\n", i, j, k); + asm("xchgw %bx, %bx"); + while(counter < 100000) { if (!(counter % 10)) { printf("\n"); } printf("%d ", counter++); } +} + +void task2(int i, int j, int k, int l) +{ + static unsigned int counter = 0u; + printf("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); + asm("xchgw %bx, %bx"); + while(counter < 100000) { printf("-+"); ++counter; } +} + +void ostask1() +{ + while(1) {} } void entry_point() @@ -90,26 +105,27 @@ void entry_point() experiment(); //----------------------------------------------------------------------- TEST ZONE : C_MALLOC, C_DELETE - int* temp = c_malloc(sizeof(int) * 10); - int* temp1 = c_malloc(sizeof(int) * 20); - int* temp2 = c_malloc(sizeof(int) * 10); - int* temp3 = c_malloc(sizeof(int) * 10); - int* temp4 = c_malloc(sizeof(int) * 10); - int* temp5 = c_malloc(sizeof(int) * 30); - int* temp6 = c_malloc(sizeof(int) * 20); - c_delete(temp, sizeof(int) * 10); - c_delete(temp1, sizeof(int) * 20); - c_delete(temp2, sizeof(int) * 10); - int* temp7 = c_malloc(sizeof(int) * 30); - c_delete(temp3, sizeof(int) * 10); - int* temp8 = c_malloc(sizeof(int) * 20); - asm("xchgw %bx, %bx"); + // int* temp = c_malloc(sizeof(int) * 10); + // int* temp1 = c_malloc(sizeof(int) * 20); + // int* temp2 = c_malloc(sizeof(int) * 10); + // int* temp3 = c_malloc(sizeof(int) * 10); + // int* temp4 = c_malloc(sizeof(int) * 10); + // int* temp5 = c_malloc(sizeof(int) * 30); + // int* temp6 = c_malloc(sizeof(int) * 20); + // c_delete(temp, sizeof(int) * 10); + // c_delete(temp1, sizeof(int) * 20); + // c_delete(temp2, sizeof(int) * 10); + // int* temp7 = c_malloc(sizeof(int) * 30); + // c_delete(temp3, sizeof(int) * 10); + // int* temp8 = c_malloc(sizeof(int) * 20); + // asm("xchgw %bx, %bx"); //----------------------------------------------------------------------- //----------------------------------------------------------------------- TEST ZONE : PROCESSES reg_os(); - add_task(ARBITRARY, task1, 0u); - asm("xchgw %bx, %bx"); + add_task(ARBITRARY, (ptr_t*)task1, 3u, 1337, 228, 322); + add_task(ARBITRARY, (ptr_t*)task2, 4u, 5, 10, 15, 20); + reg_os(); //----------------------------------------------------------------------- //----------------------------------------------------------------------- TEST ZONE : PAGING, KERNEL_KMALLOC diff --git a/src/kernel/misc/list.c b/src/kernel/misc/list.c index 1970ebb..7c9ad61 100644 --- a/src/kernel/misc/list.c +++ b/src/kernel/misc/list.c @@ -15,7 +15,7 @@ node* find_n(node* root, unsigned int n) node* add_head(node* root, uint32_t data) { - node* new_node = malloc(sizeof(node)); + node* new_node = os_malloc(sizeof(node)); new_node->data = data; new_node->data2 = 0; new_node->data3 = 0; @@ -26,7 +26,7 @@ node* add_head(node* root, uint32_t data) node* add_tail(node* root, uint32_t data) { node* tail = find_tail(root); - node* new_node = malloc(sizeof(node)); + node* new_node = os_malloc(sizeof(node)); new_node->data = data; new_node->data2 = 0; new_node->data3 = 0; @@ -39,7 +39,7 @@ node* add_tail(node* root, uint32_t data) node* add_after_n(node* root, uint32_t data, unsigned int n) { node* prev = find_n(root, n); - node* new_node = malloc(sizeof(node)); + node* new_node = os_malloc(sizeof(node)); new_node->data = data; new_node->data2 = 0; new_node->data3 = 0; diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index ece9c5e..53d5616 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -31,4 +31,30 @@ typedef struct uint16_t ss; uint16_t padding_6; -} context; \ No newline at end of file +} context; + +typedef struct +{ + uint32_t edi; + uint32_t esi; + uint32_t ebp; + uint32_t esp; + uint32_t ebx; + uint32_t edx; + uint32_t ecx; + uint32_t eax; + uint16_t gs; + uint16_t padding_1; + uint16_t fs; + uint16_t padding_2; + uint16_t es; + uint16_t padding_3; + uint16_t ds; + uint16_t padding_4; + uint32_t vector; + uint32_t error_code; + uint32_t eip; + uint16_t cs; + uint16_t padding_5; + +} crop_context; diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 852eaad..30482da 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -60,7 +60,6 @@ void interrupt_handler(context* ctx) { { timer_interrupt(ctx); outb(0x20, 0x20); - asm("xchgw %bx, %bx"); break; } case 0xe: @@ -79,8 +78,34 @@ void interrupt_handler(context* ctx) { void timer_interrupt(context* ctx) { static unsigned int counter = 0u; - printf("\n%d ", counter++); - switch_to_next(ctx); + // printf("\n%d ", counter); + if (counter == 10) + { + unsigned int current_eflags; + asm("pushf\n\t" + "pop %0" + : "=g"(current_eflags) + : + : "memory" + ); + printf("T[INFO] Interrupt %x, interrupted process context:\n" + " EAX = %x ECX = %x EDX = %x EBX = %x\n" + " ESP = %x EBP = %x ESI = %x EDI = %x\n" + " DS = %x ES = %x FS = %x GS = %x\n" + " CS = %x SS = %x EIP = %x\n" + " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + " error code = %x\n", + ctx->vector, + ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, + ctx->esp, ctx->ebp, ctx->esi, ctx->edi, + ctx->ds, ctx->es, ctx->fs, ctx->gs, + ctx->cs, ctx->ss, ctx->eip, + ctx->eflags, current_eflags, + ctx->error_code); + switch_to_next((crop_context*)ctx); + counter = 0u; + } + ++counter; } void page_fault(context* ctx) @@ -238,11 +263,12 @@ void page_fault_pse(context* ctx) get_cr2_and_cr3(&cr2, &cr3); // printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS %x\n", cr2); int found = 0; - for(unsigned int i = 0u; i < 1024u; ++i) + for(unsigned int i = 0u; i < 1023u; ++i) { - if ((cr3->pages[i].frame_address << 22) <= (ptr_t)cr2 && - (cr3->pages[i + 1].frame_address << 22) > (ptr_t)cr2) + if ((cr3->pages[i].frame_address << 22) <= (ptr_t)cr2 && (i == 767 || i == 1023 || + (cr3->pages[i + 1].frame_address << 22) > (ptr_t)cr2)) { + // printf("[PAGE FAULT HANDLING] PDE FOUND : %d\n", i); switch(cr3->pages[i].ignored1) { case not_stated: @@ -297,6 +323,7 @@ void page_fault_pse(context* ctx) if (!found) { printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS NOT FOUND IN PDTX"); + asm("xchgw %bx, %bx"); } } } @@ -323,11 +350,11 @@ void default_panic_handler(context* ctx) { " CS = %x SS = %x EIP = %x\n" " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" " error code = %x\n", - ctx->vector, - ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, - ctx->esp, ctx->ebp, ctx->esi, ctx->edi, - ctx->ds, ctx->es, ctx->fs, ctx->gs, - ctx->cs, ctx->ss, ctx->eip, + ctx->vector, + ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, + ctx->esp, ctx->ebp, ctx->esi, ctx->edi, + ctx->ds, ctx->es, ctx->fs, ctx->gs, + ctx->cs, ctx->ss, ctx->eip, ctx->eflags, current_eflags, ctx->error_code); diff --git a/src/kernel/sys/memory/alloc.c b/src/kernel/sys/memory/alloc.c index 3aa3078..7aa3c36 100644 --- a/src/kernel/sys/memory/alloc.c +++ b/src/kernel/sys/memory/alloc.c @@ -3,7 +3,7 @@ #include "../io/io.h" #include "../../misc/list.h" -#define MEMORY_MAX ((ptr_t)(0xC0000000)) +#define MEMORY_MAX ((ptr_t)(0xFF000000)) // #define MEMORY_MAX ((ptr_t)(0x2001F8)) static unsigned char* heap_start = (unsigned char*)(0x100000); @@ -21,11 +21,16 @@ void unfix_heap_to_higher_half() top = (unsigned char*)((ptr_t)top - 0xC0000000); } -ptr_t fix_heap_to_N(unsigned long N) +void fix_heap_to_N(unsigned long N) { heap_start = (unsigned char*)(N); top = (unsigned char*)(N); - return (ptr_t)top; +} + +ptr_t get_top() +{ + ptr_t ret = (ptr_t)top; + return ret; } void* malloc(ptr_t size) { @@ -141,6 +146,13 @@ void* c_malloc(ptr_t size) return top - size; } +void* c_calloc(ptr_t size) +{ + void* mem = c_malloc(size); + memzero(mem, size); + return mem; +} + void c_delete(void* ptr, ptr_t size) { node* found = free_list; @@ -217,4 +229,19 @@ void c_delete(void* ptr, ptr_t size) print_free_list(); } } +} + +void* os_malloc(ptr_t size) +{ + return (void*)((ptr_t)c_malloc(size) + 0xC0000000); +} + +void* os_calloc(ptr_t size) +{ + return (void*)((ptr_t)c_calloc(size) + 0xC0000000); +} + +void os_delete(void* ptr, ptr_t size) +{ + c_delete((void*)((ptr_t)ptr + 0xC0000000), size); } \ No newline at end of file diff --git a/src/kernel/sys/memory/alloc.h b/src/kernel/sys/memory/alloc.h index 4c17c8e..a1e7483 100644 --- a/src/kernel/sys/memory/alloc.h +++ b/src/kernel/sys/memory/alloc.h @@ -20,7 +20,8 @@ typedef unsigned int ptr_t; void fix_heap_to_higher_half(); void unfix_heap_to_higher_half(); -ptr_t fix_heap_to_N(unsigned long N); +void fix_heap_to_N(unsigned long N); +ptr_t get_top(); void* malloc(ptr_t size); @@ -34,4 +35,9 @@ void kernel_free(void* addr); void init_alloc(); void* c_malloc(ptr_t size); -void c_delete(void* ptr, ptr_t size); \ No newline at end of file +void* c_calloc(ptr_t size); +void c_delete(void* ptr, ptr_t size); + +void* os_malloc(ptr_t size); +void* os_calloc(ptr_t size); +void os_delete(void* ptr, ptr_t size); \ No newline at end of file diff --git a/src/kernel/sys/paging/paging.asm b/src/kernel/sys/paging/paging.asm index 993f6a0..839a6cc 100644 --- a/src/kernel/sys/paging/paging.asm +++ b/src/kernel/sys/paging/paging.asm @@ -77,6 +77,8 @@ load_page_directory: ; put &boot_page_directory in high 20 bits of cr3 register push ebp mov ebp, esp + push eax + push ebx mov eax, [ebp + 8] mov ebx, cr3 @@ -85,6 +87,8 @@ load_page_directory: ; put &boot_page_directory in high 20 bits of cr3 re or ebx, eax mov cr3, ebx + pop ebx + pop eax mov esp, ebp pop ebp ret diff --git a/src/kernel/sys/paging/paging.c b/src/kernel/sys/paging/paging.c index 84abc9f..24dd310 100644 --- a/src/kernel/sys/paging/paging.c +++ b/src/kernel/sys/paging/paging.c @@ -121,15 +121,15 @@ ppdtx markup_pdt_pse(void* mem_place) for(unsigned int i = 0u; i < 1024u; ++i) { ppdex cur_pdex = &pdtx_mem->pages[i]; - if (0xC0000000 <= cur_virt_address) + if (0xC0000000u <= cur_virt_address) { cur_pdex->present = 1u; cur_pdex->rw = 0u; cur_pdex->us = 0u; cur_pdex->ignored1 = enabled_page_frame; - cur_pdex->frame_address = ((cur_virt_address - 0xC0000000) >> 22); + cur_pdex->frame_address = ((cur_virt_address - 0xC0000000u) >> 22); } - else if (cur_virt_address < 0x40000000) + else if (cur_virt_address < 0x40000000u) { cur_pdex->present = 1u; cur_pdex->rw = 0u; diff --git a/src/kernel/sys/paging/pdt.h b/src/kernel/sys/paging/pdt.h index 8695aed..a6105ec 100644 --- a/src/kernel/sys/paging/pdt.h +++ b/src/kernel/sys/paging/pdt.h @@ -4,7 +4,7 @@ #ifndef MAX_PDE_PER_PDT #define MAX_PDE_PER_PDT (int)(1024) -#elif +#else #error [DEF_ERROR] MAX_PDE_PER_PDT was defined earlier #endif diff --git a/src/kernel/sys/paging/pt.h b/src/kernel/sys/paging/pt.h index 2a4a10c..7f84b34 100644 --- a/src/kernel/sys/paging/pt.h +++ b/src/kernel/sys/paging/pt.h @@ -4,7 +4,7 @@ #ifndef MAX_PTE_PER_PT #define MAX_PTE_PER_PT (int)(1024) -#elif +#else #error [DEF_ERROR] MAX_PTE_PER_PT was defined earlier #endif diff --git a/src/kernel/sys/process/exec.asm b/src/kernel/sys/process/exec.asm new file mode 100644 index 0000000..f92b73d --- /dev/null +++ b/src/kernel/sys/process/exec.asm @@ -0,0 +1,50 @@ +[BITS 32] + +[GLOBAL exec] +[GLOBAL get_ebp] +[GLOBAL get_esp] +[GLOBAL set_ebp_esp] + +[EXTERN load_proc_ebp_pdt] + +exec: + add esp, 4 + xchg bx, bx + mov esp, ebp + push ebx + push eax + mov eax, 0x100000 + mov cr3, eax + push eax + push eax + push eax + call load_proc_ebp_pdt + pop eax + mov ebp, eax + pop eax + mov cr3, eax + pop eax + mov ebx, esp + mov esp, ebp + push eax + mov esp, ebx + pop eax + pop ebx + mov esp, ebp + sub esp, 4 + ret + +get_ebp: + mov eax, ebp + ret + +get_esp: + mov eax, esp + ret + +set_ebp_esp: + pop eax + mov ebp, [esp] + mov esp, [esp + 4] + push eax + ret \ No newline at end of file diff --git a/src/kernel/sys/process/process.c b/src/kernel/sys/process/process.c index ae2caa3..5f4b123 100644 --- a/src/kernel/sys/process/process.c +++ b/src/kernel/sys/process/process.c @@ -6,7 +6,12 @@ ppdtx bb() { // creates memory for new process PDT on OS heap + ptr_t cur_top = get_top(0); + ptr_t pad_size = (((cur_top >> 12) + 1) << 12) - cur_top; + void* padding = c_malloc(pad_size); ppdtx proc_pdt_mem = c_malloc(sizeof(pde) * 1024); + printf("[INFO] New process PDT: %x\n", proc_pdt_mem); + c_delete(padding, pad_size); if (!proc_pdt_mem) { printf("[EXC] alloc_pdt():1: not enough memory!\n"); @@ -19,7 +24,7 @@ ppdtx bb() ppdex cur_pdex = &proc_pdt_mem->pages[0u]; cur_pdex->present = 1u; cur_pdex->ignored1 = enabled_page_frame; - cur_pdex->frame_address = 1u; + cur_pdex->frame_address = 0u; cur_pdex->rw = 0u; cur_pdex->us = 0u; cur_pdex->cache_ctrl1 = 0u; @@ -29,7 +34,7 @@ ppdtx bb() cur_pdex->phys_address = 0u; cur_pdex->reserved = 0u; } - for(unsigned int i = 0u; i < 1024u; ++i) + for(unsigned int i = 1u; i < 1024u; ++i) { ppdex cur_pdex = &proc_pdt_mem->pages[i]; // 1024 - 256 == 768 @@ -67,10 +72,11 @@ ppdtx bb() // enable stack { - ptr_t pad_size = ((fix_heap_to_N(0) >> 22) + 1) - fix_heap_to_N(0); + ptr_t cur_top = get_top(0); + ptr_t pad_size = (((cur_top >> 22) + 1) << 22) - cur_top; void* padding = c_malloc(pad_size); void* stack_mem = c_malloc(1u << 22); - c_delete((void*)padding, pad_size); + c_delete(padding, pad_size); if (!stack_mem) { printf("[EXC] create stack: not enough memory!\n"); @@ -79,7 +85,7 @@ ppdtx bb() ppdex cur_pdex = &proc_pdt_mem->pages[768 - 1]; // 0xC0000000 - 4MB (ebp will be on 0xC0000000 - 1MB, so stack size = 3MB) cur_pdex->present = 1u; cur_pdex->ignored1 = enabled_page_frame; - cur_pdex->frame_address = (ptr_t)stack_mem; + cur_pdex->frame_address = ((ptr_t)stack_mem >> 22); } return proc_pdt_mem; diff --git a/src/kernel/sys/process/process.h b/src/kernel/sys/process/process.h index 30c9e8a..c66a6db 100644 --- a/src/kernel/sys/process/process.h +++ b/src/kernel/sys/process/process.h @@ -4,6 +4,7 @@ static unsigned char* process_stack_start = ((unsigned char*)(0xC0000000 - 0x100000 - 1)); // - 1MB static unsigned char* process_heap_start = ((unsigned char*)(0x40000000)); // 1GB +static unsigned int MAX_ARGS = ((unsigned int)(10)); // max args to pass to process's entry function // creates new space (virtual memory pages) for new processes // big bang diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 5ab402b..74a325a 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -6,51 +6,96 @@ #define va_list char* #define va_start(x) (char*)(&x + 1) +unsigned int ntasks = 0u; +node* tasks = nullptr; +node* os_task = nullptr; + +extern void exec(); +extern void mov_esp_ebp(void); +extern ptr_t get_ebp(void); +extern ptr_t get_esp(void); +extern void set_ebp_esp(ptr_t ebp, ptr_t esp); + void reg_os() { - os_task = c_malloc(sizeof(node)) + 0xC0000000; - os_task->data3 = 0xC0100000; + node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); + *os_task_hh = os_malloc(sizeof(node)); + (*os_task_hh)->data = get_ebp(); + (*os_task_hh)->data2 = get_esp(); + (*os_task_hh)->data3 = 0x100000; +} + +void copy_rev(unsigned int *restrict dest, + unsigned int const *restrict src, + unsigned int n) +{ + unsigned int i, x; + for (i = 0; i < n; ++i) { + x = src[i]; + x = (x >> 16) | (x << 16); + x = ((x >> 8) & 0x00ff00ffU) | ((x & 0x00ff00ffU) << 8); + x = ((x >> 4) & 0x0f0f0f0fU) | ((x & 0x0f0f0f0fU) << 4); + x = ((x >> 2) & 0x33333333U) | ((x & 0x33333333U) << 2); + x = ((x >> 1) & 0x55555555U) | ((x & 0x555555555) << 1); + dest[n-1-i] = x; + } } -void add_task(task_t type, void (*entry)(void), unsigned int sz_args, ...) +void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) { va_list VAARGS = va_start(sz_args); - ptask new_task = c_malloc(sizeof(task)); - tasks = add_after_n(tasks, (uint32_t)new_task, ntasks); - node* cur_node = find_n(tasks, ntasks); + ptask new_task = os_malloc(sizeof(task)); + tasks = add_after_n(tasks, (uint32_t)new_task, 0u); + node* cur_node = find_n(tasks, 1u); if (ntasks == 0u) { tasks->next = tasks; - ++ntasks; } - new_task->entry = entry; + ++ntasks; + new_task->entry = (ptr_t*)((ptr_t)entry + 0xC0000000); new_task->type = type; new_task->state = INIT; - new_task->ctx->ebp = (uint32_t)process_stack_start; - new_task->ctx->esp = (uint32_t)process_stack_start; + crop_context* proc_ctx = os_malloc(sizeof(crop_context)); + new_task->ctx = proc_ctx; ppdtx new_space = bb(); - tasks->data3 = (uint32_t)new_space; - int args[sz_args]; + cur_node->data3 = (uint32_t)new_space; + unsigned int args[MAX_ARGS]; + sz_args = (sz_args < MAX_ARGS) ? sz_args : MAX_ARGS; if (sz_args > 0) { for(unsigned int i = 0u; i < sz_args; ++i) { - args[i] = va_arg(VAARGS, int); + args[i] = va_arg(VAARGS, unsigned int); } } - int* process_stack_esp_int = (int*)new_task->ctx->esp; - load_page_directory((void*)new_space); + ptr_t* process_stack_esp_int = (ptr_t*)process_stack_start; + node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); + ptr_t* process_stack_ebp_int = nullptr; + load_page_directory(new_space); if (sz_args > 0) { - for(unsigned int i = 0u; i < sz_args; ++i, --process_stack_esp_int) - { - *process_stack_esp_int = args[sz_args - i - 1]; - } + process_stack_esp_int -= sz_args; + copy_rev(process_stack_esp_int, args, sz_args * sizeof(int)); + printf("COPYING ARGS : to %x from %x\n", process_stack_esp_int, args); + asm("xchgw %bx, %bx"); } - load_page_directory((void*)os_task->data3); - *process_stack_esp_int = (ptr_t)&self_destruct; + *process_stack_esp_int = ((ptr_t)&self_destruct + 0xC0000000); + --process_stack_esp_int; + process_stack_ebp_int = process_stack_esp_int; --process_stack_esp_int; + load_page_directory((void*)(*os_task_hh)->data3); new_task->ctx->esp = (uint32_t)process_stack_esp_int; + new_task->ctx->ebp = (uint32_t)process_stack_ebp_int; + printf("ESP : EBP = %x : %x\n", new_task->ctx->esp, new_task->ctx->ebp); + asm("xchgw %bx, %bx"); +} + +void load_proc_ebp_pdt(ptr_t ebp, ptr_t pdt, ptr_t entry) +{ + node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); + ebp = (ptr_t)((ptask)(tasks_hh->data))->ctx->ebp; + pdt = (ptr_t)tasks_hh->data3; + entry = (ptr_t)((ptask)(tasks_hh->data))->entry; } void terminate_task() @@ -60,23 +105,32 @@ void terminate_task() void self_destruct() { - load_page_directory((void*)os_task->data3); - ((ptask)tasks->data)->state = DEAD; + node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); + node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); + load_page_directory((void*)os_task_hh->data3); + ((ptask)(tasks_hh->data))->state = DEAD; while(1) {} } -void switch_to_next(context* cur_ctx) +void switch_to_next(crop_context* cur_ctx) { - load_page_directory((void*)os_task->data3); - node* old_task_node = tasks; - ptask old_task = (ptask)tasks->data; - old_task->state = ON_PAUSE; - *(old_task->ctx) = *cur_ctx; // memcpy action + node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); + node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); + unsigned int ntasks_hh = (unsigned int)*(unsigned int*)((ptr_t)&ntasks + 0xC0000000); + node* old_task_node = tasks_hh; + ptask old_task = (ptask)tasks_hh->data; + if (old_task->state == ACTIVE) + { + if (ntasks_hh < 2) return; + old_task->state = ON_PAUSE; + *old_task->ctx = *cur_ctx; // memcpy action + } - tasks = tasks->next; - ptask next_task = (ptask)tasks->data; + tasks_hh = tasks_hh->next; + ptask next_task = (ptask)tasks_hh->data; do { + printf("[SWITCH_TO_NEXT] : case %x\n", next_task->state); switch (next_task->state) { case ACTIVE: @@ -86,43 +140,88 @@ void switch_to_next(context* cur_ctx) asm("xchgw %bx, %bx"); asm("xchgw %bx, %bx"); asm("xchgw %bx, %bx"); + asm("xchgw %bx, %bx"); + // load_page_directory((void*)tasks_hh->data3); return; } case ON_PAUSE: { + *cur_ctx = *next_task->ctx; // memcpy action next_task->state = ACTIVE; - *cur_ctx = *(next_task->ctx); // memcpy action + asm("xchgw %bx, %bx"); + // load_page_directory((void*)tasks_hh->data3); return; } case INIT: { - *cur_ctx = *(next_task->ctx); // memcpy action - cur_ctx->eip = (uint32_t)next_task->entry; - load_page_directory((void*)tasks->data3); + printf("CHANGE EBP, EIP : %x\n", cur_ctx); + asm("xchgw %bx, %bx"); + cur_ctx->ebp = (ptr_t)(os_task_hh->data2); + cur_ctx->eip = (uint32_t)((uint32_t*)&exec + 0xC0000000); + asm("xchgw %bx, %bx"); + next_task->state = ACTIVE; + + unsigned int current_eflags; + asm("pushf\n\t" + "pop %0" + : "=g"(current_eflags) + : + : "memory" + ); + printf("S[INFO] Interrupt %x, interrupted process context:\n" + " EAX = %x ECX = %x EDX = %x EBX = %x\n" + " ESP = %x EBP = %x ESI = %x EDI = %x\n" + " DS = %x ES = %x FS = %x GS = %x\n" + " CS = %x SS = %x EIP = %x\n" + " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + " error code = %x\n", + ((context*)cur_ctx)->vector, + ((context*)cur_ctx)->eax, ((context*)cur_ctx)->ecx, ((context*)cur_ctx)->edx, ((context*)cur_ctx)->ebx, + ((context*)cur_ctx)->esp, ((context*)cur_ctx)->ebp, ((context*)cur_ctx)->esi, ((context*)cur_ctx)->edi, + ((context*)cur_ctx)->ds, ((context*)cur_ctx)->es, ((context*)cur_ctx)->fs, ((context*)cur_ctx)->gs, + ((context*)cur_ctx)->cs, ((context*)cur_ctx)->ss, ((context*)cur_ctx)->eip, + ((context*)cur_ctx)->eflags, current_eflags, + ((context*)cur_ctx)->error_code); + + // load_page_directory((void*)tasks_hh->data3); + asm("xchgw %bx, %bx"); return; } case DEAD: { + asm("xchgw %bx, %bx"); for(unsigned int i = 1u; i < 768u; ++i) { - c_delete((void*)(((ppdtx)tasks->data3)->pages[i].frame_address << 22), 1u << 22); + os_delete((void*)(((ppdtx)tasks_hh->data3)->pages[i].frame_address << 22), 1u << 22); } - c_delete((void*)tasks->data3, sizeof(pde) * 1024); - c_delete((void*)tasks->data, sizeof(task)); + os_delete((void*)tasks_hh->data3, sizeof(pde) * 1024); + os_delete((void*)tasks_hh->data, sizeof(task)); // manual remove head (cyclic list case) - if (ntasks > 1) + if (ntasks_hh > 1) { - old_task_node->next = tasks->next; + old_task_node->next = (node*)*(node**)((ptr_t)tasks_hh->next + 0xC0000000); + os_delete((void*)tasks_hh, sizeof(node)); + } + else + { + os_delete((void*)tasks_hh, sizeof(node)); + return; } - c_delete((void*)tasks, sizeof(node)); + } + default: + { + printf("[SWITCH_TO_NEXT] : default!\n"); + asm("xchgw %bx, %bx"); + return; } } } while(1); } -context* switch_to_os(context* cur_ctx) +void switch_to_os() { - // ... + node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); + load_page_directory((void*)os_task_hh->data3); } task_t get_type() diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index 852ddcb..8b66d68 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -4,9 +4,9 @@ #include "../memory/alloc.h" #include "../context/context.h" -static unsigned int ntasks = 0u; -static node* tasks = nullptr; -static node* os_task = nullptr; +extern unsigned int ntasks; +extern node* tasks; +extern node* os_task; typedef enum task_type { @@ -29,16 +29,16 @@ typedef struct task_struct { task_t type; state_t state; - void (*entry)(void); - context* ctx; + ptr_t* entry; + crop_context* ctx; } task, *ptask; #pragma pack(pop) void reg_os(); -void add_task(task_t type, void (*entry)(void), unsigned int sz_args, ...); +void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...); void terminate_task(); void self_destruct(); -void switch_to_next(context* cur_ctx); -context* switch_to_os(context* cur_ctx); +void switch_to_next(crop_context* cur_ctx); +void switch_to_os(); task_t get_type(); \ No newline at end of file From 65d69c6ce7817a028ebc662fbb3c275de95052d4 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 18 May 2024 19:02:23 +0700 Subject: [PATCH 76/88] purgatory --- src/kernel/sys/interruption.c | 42 +++++------ src/kernel/sys/process/scheduler.c | 107 ++++++++++++++++------------- 2 files changed, 79 insertions(+), 70 deletions(-) diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 30482da..9c292c6 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -81,27 +81,27 @@ void timer_interrupt(context* ctx) { // printf("\n%d ", counter); if (counter == 10) { - unsigned int current_eflags; - asm("pushf\n\t" - "pop %0" - : "=g"(current_eflags) - : - : "memory" - ); - printf("T[INFO] Interrupt %x, interrupted process context:\n" - " EAX = %x ECX = %x EDX = %x EBX = %x\n" - " ESP = %x EBP = %x ESI = %x EDI = %x\n" - " DS = %x ES = %x FS = %x GS = %x\n" - " CS = %x SS = %x EIP = %x\n" - " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" - " error code = %x\n", - ctx->vector, - ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, - ctx->esp, ctx->ebp, ctx->esi, ctx->edi, - ctx->ds, ctx->es, ctx->fs, ctx->gs, - ctx->cs, ctx->ss, ctx->eip, - ctx->eflags, current_eflags, - ctx->error_code); + // unsigned int current_eflags; + // asm("pushf\n\t" + // "pop %0" + // : "=g"(current_eflags) + // : + // : "memory" + // ); + // printf("T[INFO] Interrupt %x, interrupted process context:\n" + // " EAX = %x ECX = %x EDX = %x EBX = %x\n" + // " ESP = %x EBP = %x ESI = %x EDI = %x\n" + // " DS = %x ES = %x FS = %x GS = %x\n" + // " CS = %x SS = %x EIP = %x\n" + // " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + // " error code = %x\n", + // ctx->vector, + // ctx->eax, ctx->ecx, ctx->edx, ctx->ebx, + // ctx->esp, ctx->ebp, ctx->esi, ctx->edi, + // ctx->ds, ctx->es, ctx->fs, ctx->gs, + // ctx->cs, ctx->ss, ctx->eip, + // ctx->eflags, current_eflags, + // ctx->error_code); switch_to_next((crop_context*)ctx); counter = 0u; } diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 74a325a..7f793f4 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -57,6 +57,7 @@ void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) new_task->state = INIT; crop_context* proc_ctx = os_malloc(sizeof(crop_context)); new_task->ctx = proc_ctx; + new_task->ctx->ebp = (uint32_t)process_stack_start; ppdtx new_space = bb(); cur_node->data3 = (uint32_t)new_space; unsigned int args[MAX_ARGS]; @@ -70,22 +71,24 @@ void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) } ptr_t* process_stack_esp_int = (ptr_t*)process_stack_start; node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); - ptr_t* process_stack_ebp_int = nullptr; load_page_directory(new_space); if (sz_args > 0) { - process_stack_esp_int -= sz_args; - copy_rev(process_stack_esp_int, args, sz_args * sizeof(int)); + // process_stack_esp_int -= sz_args; + // copy_rev(process_stack_esp_int, args, sz_args * sizeof(int)); + for(unsigned int i = 0u; i < sz_args; ++i, --process_stack_esp_int) + { + *process_stack_esp_int = args[sz_args - i - 1]; + } printf("COPYING ARGS : to %x from %x\n", process_stack_esp_int, args); asm("xchgw %bx, %bx"); } *process_stack_esp_int = ((ptr_t)&self_destruct + 0xC0000000); --process_stack_esp_int; - process_stack_ebp_int = process_stack_esp_int; - --process_stack_esp_int; + *process_stack_esp_int = (uint32_t)process_stack_start; load_page_directory((void*)(*os_task_hh)->data3); new_task->ctx->esp = (uint32_t)process_stack_esp_int; - new_task->ctx->ebp = (uint32_t)process_stack_ebp_int; + new_task->ctx->ebp = (uint32_t)process_stack_esp_int; printf("ESP : EBP = %x : %x\n", new_task->ctx->esp, new_task->ctx->ebp); asm("xchgw %bx, %bx"); } @@ -114,23 +117,27 @@ void self_destruct() void switch_to_next(crop_context* cur_ctx) { - node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); - node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); - unsigned int ntasks_hh = (unsigned int)*(unsigned int*)((ptr_t)&ntasks + 0xC0000000); - node* old_task_node = tasks_hh; - ptask old_task = (ptask)tasks_hh->data; + node** tasks_hh = (node**)((ptr_t)&tasks + 0xC0000000); + node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); + unsigned int* ntasks_hh = (unsigned int*)((ptr_t)&ntasks + 0xC0000000); + node* old_task_node = (*tasks_hh); + ptask old_task = (ptask)((*tasks_hh)->data); if (old_task->state == ACTIVE) { - if (ntasks_hh < 2) return; + if ((*ntasks_hh) < 2) return; old_task->state = ON_PAUSE; *old_task->ctx = *cur_ctx; // memcpy action } - tasks_hh = tasks_hh->next; - ptask next_task = (ptask)tasks_hh->data; + // printf("[SWITCH_TO_NEXT] : orig tasks %x\n", &tasks); + // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", (*tasks_hh), (*tasks_hh)->next); + *tasks_hh = (*tasks_hh)->next; + // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", tasks_hh, (*tasks_hh)->next); + ptask next_task = (ptask)((*tasks_hh)->data); do { printf("[SWITCH_TO_NEXT] : case %x\n", next_task->state); + // printf("[SWITCH_TO_NEXT] : old_task %x, next_task %x\n", old_task, next_task); switch (next_task->state) { case ACTIVE: @@ -141,7 +148,7 @@ void switch_to_next(crop_context* cur_ctx) asm("xchgw %bx, %bx"); asm("xchgw %bx, %bx"); asm("xchgw %bx, %bx"); - // load_page_directory((void*)tasks_hh->data3); + // load_page_directory((void*)(*tasks_hh)->data3); return; } case ON_PAUSE: @@ -149,41 +156,41 @@ void switch_to_next(crop_context* cur_ctx) *cur_ctx = *next_task->ctx; // memcpy action next_task->state = ACTIVE; asm("xchgw %bx, %bx"); - // load_page_directory((void*)tasks_hh->data3); + // load_page_directory((void*)(*tasks_hh)->data3); return; } case INIT: { - printf("CHANGE EBP, EIP : %x\n", cur_ctx); - asm("xchgw %bx, %bx"); - cur_ctx->ebp = (ptr_t)(os_task_hh->data2); + // printf("CHANGE EBP, EIP : %x\n", cur_ctx); + // asm("xchgw %bx, %bx"); + cur_ctx->ebp = (ptr_t)((*os_task_hh)->data2); cur_ctx->eip = (uint32_t)((uint32_t*)&exec + 0xC0000000); - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); next_task->state = ACTIVE; - unsigned int current_eflags; - asm("pushf\n\t" - "pop %0" - : "=g"(current_eflags) - : - : "memory" - ); - printf("S[INFO] Interrupt %x, interrupted process context:\n" - " EAX = %x ECX = %x EDX = %x EBX = %x\n" - " ESP = %x EBP = %x ESI = %x EDI = %x\n" - " DS = %x ES = %x FS = %x GS = %x\n" - " CS = %x SS = %x EIP = %x\n" - " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" - " error code = %x\n", - ((context*)cur_ctx)->vector, - ((context*)cur_ctx)->eax, ((context*)cur_ctx)->ecx, ((context*)cur_ctx)->edx, ((context*)cur_ctx)->ebx, - ((context*)cur_ctx)->esp, ((context*)cur_ctx)->ebp, ((context*)cur_ctx)->esi, ((context*)cur_ctx)->edi, - ((context*)cur_ctx)->ds, ((context*)cur_ctx)->es, ((context*)cur_ctx)->fs, ((context*)cur_ctx)->gs, - ((context*)cur_ctx)->cs, ((context*)cur_ctx)->ss, ((context*)cur_ctx)->eip, - ((context*)cur_ctx)->eflags, current_eflags, - ((context*)cur_ctx)->error_code); + // unsigned int current_eflags; + // asm("pushf\n\t" + // "pop %0" + // : "=g"(current_eflags) + // : + // : "memory" + // ); + // printf("S[INFO] Interrupt %x, interrupted process context:\n" + // " EAX = %x ECX = %x EDX = %x EBX = %x\n" + // " ESP = %x EBP = %x ESI = %x EDI = %x\n" + // " DS = %x ES = %x FS = %x GS = %x\n" + // " CS = %x SS = %x EIP = %x\n" + // " EFLAGS (interrupted) = %x " "EFLAGS (current) = %x\n" + // " error code = %x\n", + // ((context*)cur_ctx)->vector, + // ((context*)cur_ctx)->eax, ((context*)cur_ctx)->ecx, ((context*)cur_ctx)->edx, ((context*)cur_ctx)->ebx, + // ((context*)cur_ctx)->esp, ((context*)cur_ctx)->ebp, ((context*)cur_ctx)->esi, ((context*)cur_ctx)->edi, + // ((context*)cur_ctx)->ds, ((context*)cur_ctx)->es, ((context*)cur_ctx)->fs, ((context*)cur_ctx)->gs, + // ((context*)cur_ctx)->cs, ((context*)cur_ctx)->ss, ((context*)cur_ctx)->eip, + // ((context*)cur_ctx)->eflags, current_eflags, + // ((context*)cur_ctx)->error_code); - // load_page_directory((void*)tasks_hh->data3); + // load_page_directory((void*)(*tasks_hh)->data3); asm("xchgw %bx, %bx"); return; } @@ -192,19 +199,21 @@ void switch_to_next(crop_context* cur_ctx) asm("xchgw %bx, %bx"); for(unsigned int i = 1u; i < 768u; ++i) { - os_delete((void*)(((ppdtx)tasks_hh->data3)->pages[i].frame_address << 22), 1u << 22); + os_delete((void*)(((ppdtx)(*tasks_hh)->data3)->pages[i].frame_address << 22), 1u << 22); } - os_delete((void*)tasks_hh->data3, sizeof(pde) * 1024); - os_delete((void*)tasks_hh->data, sizeof(task)); + os_delete((void*)(*tasks_hh)->data3, sizeof(pde) * 1024); + os_delete((void*)(*tasks_hh)->data, sizeof(task)); // manual remove head (cyclic list case) - if (ntasks_hh > 1) + if ((*ntasks_hh) > 1) { - old_task_node->next = (node*)*(node**)((ptr_t)tasks_hh->next + 0xC0000000); - os_delete((void*)tasks_hh, sizeof(node)); + old_task_node->next = (node*)*(node**)((ptr_t)(*tasks_hh)->next + 0xC0000000); + os_delete((void*)(*tasks_hh), sizeof(node)); + *ntasks_hh = *ntasks_hh - 1; } else { - os_delete((void*)tasks_hh, sizeof(node)); + os_delete((void*)(*tasks_hh), sizeof(node)); + *ntasks_hh = *ntasks_hh - 1; return; } } From 889fb15bea52321faca6a9132721b82193797299 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sat, 18 May 2024 20:01:18 +0700 Subject: [PATCH 77/88] purificatory --- src/kernel/kernel.c | 10 ++++++-- src/kernel/sys/interruption.c | 4 +-- src/kernel/sys/process/exec.asm | 40 +++++++++++++++++++++++++++--- src/kernel/sys/process/scheduler.c | 35 ++++++++++++++++++-------- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index edbe5f9..7f7bf0d 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -24,8 +24,12 @@ void task1(int i, int j, int k) { static unsigned int counter = 0u; printf("Hello from process #1 : %d %d %d;\n", i, j, k); + int* temp = os_malloc(sizeof(int) * 100); + temp[5] = 5; + asm("xchgw %bx, %bx"); + while(counter < 1000) { if (!(counter % 10)) { printf("\n"); } printf("%d ", counter++); } + printf("End of the proc #1\n"); asm("xchgw %bx, %bx"); - while(counter < 100000) { if (!(counter % 10)) { printf("\n"); } printf("%d ", counter++); } } void task2(int i, int j, int k, int l) @@ -33,7 +37,9 @@ void task2(int i, int j, int k, int l) static unsigned int counter = 0u; printf("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); asm("xchgw %bx, %bx"); - while(counter < 100000) { printf("-+"); ++counter; } + while(counter < 1000) { printf("-+"); ++counter; } + printf("End of the proc #2\n"); + asm("xchgw %bx, %bx"); } void ostask1() diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 9c292c6..28990e6 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -261,14 +261,14 @@ void page_fault_pse(context* ctx) ptr_t* cr2 = nullptr; ppdtx cr3 = nullptr; get_cr2_and_cr3(&cr2, &cr3); - // printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS %x\n", cr2); + printf("[PAGE FAULT HANDLING] CAUSE-ADDRESS %x\n", cr2); int found = 0; for(unsigned int i = 0u; i < 1023u; ++i) { if ((cr3->pages[i].frame_address << 22) <= (ptr_t)cr2 && (i == 767 || i == 1023 || (cr3->pages[i + 1].frame_address << 22) > (ptr_t)cr2)) { - // printf("[PAGE FAULT HANDLING] PDE FOUND : %d\n", i); + printf("[PAGE FAULT HANDLING] PDE FOUND : %d\n", i); switch(cr3->pages[i].ignored1) { case not_stated: diff --git a/src/kernel/sys/process/exec.asm b/src/kernel/sys/process/exec.asm index f92b73d..0916c8c 100644 --- a/src/kernel/sys/process/exec.asm +++ b/src/kernel/sys/process/exec.asm @@ -1,16 +1,19 @@ [BITS 32] [GLOBAL exec] +[GLOBAL continue_exec] [GLOBAL get_ebp] [GLOBAL get_esp] [GLOBAL set_ebp_esp] -[EXTERN load_proc_ebp_pdt] +[EXTERN load_proc_ebp_pdt_entry_esp] +[EXTERN load_proc_esp_pdt_eip_ebp_esp] exec: - add esp, 4 xchg bx, bx + add esp, 4 mov esp, ebp + push eax push ebx push eax mov eax, 0x100000 @@ -18,7 +21,7 @@ exec: push eax push eax push eax - call load_proc_ebp_pdt + call load_proc_ebp_pdt_entry_esp pop eax mov ebp, eax pop eax @@ -28,9 +31,38 @@ exec: mov esp, ebp push eax mov esp, ebx - pop eax pop ebx + pop eax + pop esp + ret + +continue_exec: + xchg bx, bx + add esp, 4 mov esp, ebp + push eax + push eax + push ebx + push eax + mov eax, 0x100000 + mov cr3, eax + push eax + push eax + push eax + call load_proc_esp_pdt_eip_ebp_esp + pop eax + mov ebp, eax + pop eax + mov cr3, eax + pop eax + mov ebx, esp + mov esp, ebp + push eax + mov esp, ebx + pop ebx + pop eax + pop ebp + pop esp sub esp, 4 ret diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 7f793f4..0a3e2ee 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -10,6 +10,7 @@ unsigned int ntasks = 0u; node* tasks = nullptr; node* os_task = nullptr; +extern void continue_exec(); extern void exec(); extern void mov_esp_ebp(void); extern ptr_t get_ebp(void); @@ -80,25 +81,37 @@ void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) { *process_stack_esp_int = args[sz_args - i - 1]; } - printf("COPYING ARGS : to %x from %x\n", process_stack_esp_int, args); - asm("xchgw %bx, %bx"); + // printf("COPYING ARGS : to %x from %x\n", process_stack_esp_int, args); + // asm("xchgw %bx, %bx"); } *process_stack_esp_int = ((ptr_t)&self_destruct + 0xC0000000); - --process_stack_esp_int; - *process_stack_esp_int = (uint32_t)process_stack_start; + // --process_stack_esp_int; + // *process_stack_esp_int = (uint32_t)process_stack_start; load_page_directory((void*)(*os_task_hh)->data3); - new_task->ctx->esp = (uint32_t)process_stack_esp_int; new_task->ctx->ebp = (uint32_t)process_stack_esp_int; - printf("ESP : EBP = %x : %x\n", new_task->ctx->esp, new_task->ctx->ebp); - asm("xchgw %bx, %bx"); + --process_stack_esp_int; + new_task->ctx->esp = (uint32_t)process_stack_esp_int; + // printf("ESP : EBP = %x : %x\n", new_task->ctx->esp, new_task->ctx->ebp); + // asm("xchgw %bx, %bx"); } -void load_proc_ebp_pdt(ptr_t ebp, ptr_t pdt, ptr_t entry) +void load_proc_ebp_pdt_entry_esp(ptr_t ebp, ptr_t pdt, ptr_t entry, ptr_t fake1, ptr_t fake2, ptr_t esp) { node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); ebp = (ptr_t)((ptask)(tasks_hh->data))->ctx->ebp; pdt = (ptr_t)tasks_hh->data3; entry = (ptr_t)((ptask)(tasks_hh->data))->entry; + esp = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; +} + +void load_proc_esp_pdt_eip_ebp_esp(ptr_t esp1, ptr_t pdt, ptr_t eip, ptr_t fake1, ptr_t fake2, ptr_t ebp, ptr_t esp2) +{ + node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); + ebp = (ptr_t)((ptask)(tasks_hh->data))->ctx->ebp; + pdt = (ptr_t)tasks_hh->data3; + eip = (ptr_t)((ptask)(tasks_hh->data))->ctx->eip; + esp1 = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; + esp2 = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; } void terminate_task() @@ -153,9 +166,11 @@ void switch_to_next(crop_context* cur_ctx) } case ON_PAUSE: { - *cur_ctx = *next_task->ctx; // memcpy action + *cur_ctx = *next_task->ctx; + cur_ctx->ebp = (ptr_t)((*os_task_hh)->data2); + cur_ctx->eip = (uint32_t)((uint32_t*)&continue_exec + 0xC0000000); next_task->state = ACTIVE; - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); // load_page_directory((void*)(*tasks_hh)->data3); return; } From f220001a5598bde6b570b9c5383eaa13b2870208 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 19 May 2024 04:09:17 +0700 Subject: [PATCH 78/88] sync --- Makefile | 10 ++-- src/kernel/kernel.c | 27 ++++++++-- src/kernel/sync/sync.asm | 38 ++++++++++++++ src/kernel/sync/sync.h | 4 ++ src/kernel/sys/interruption.c | 9 +++- src/kernel/sys/io/printf.c | 11 +++- src/kernel/sys/io/printf.h | 2 + src/kernel/sys/process/exec.asm | 28 +++++++++- src/kernel/sys/process/scheduler.c | 83 +++++++++++++++++++----------- src/kernel/sys/process/scheduler.h | 4 +- 10 files changed, 173 insertions(+), 43 deletions(-) create mode 100644 src/kernel/sync/sync.asm create mode 100644 src/kernel/sync/sync.h diff --git a/Makefile b/Makefile index adbac91..bb265a3 100644 --- a/Makefile +++ b/Makefile @@ -77,13 +77,17 @@ $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o: $(KERNEL_SOURCE_DIR)/sys/process/exe $(info [MAKE] building: $@) $(ASM) -f elf $< -o $@ +$(KERNEL_BUILD_DIR)/sync/sync.asm.o: $(KERNEL_SOURCE_DIR)/sync/sync.asm $(KERNEL_SOURCE_DIR)/sync/sync.h + $(info [MAKE] building: $@) + $(ASM) -f elf $< -o $@ + $(KERNEL_BUILD_DIR)/sys/pic/pic.o: $(KERNEL_SOURCE_DIR)/sys/pic/pic.c \ $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ $(KERNEL_BUILD_DIR)/sys/io/printf.o: $(KERNEL_SOURCE_DIR)/sys/io/printf.c \ - $(KERNEL_SOURCE_DIR)/sys/io/printf.h + $(KERNEL_SOURCE_DIR)/sys/io/printf.h $(KERNEL_SOURCE_DIR)/sync/sync.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ @@ -133,7 +137,7 @@ $(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c $(KERNEL_SOURCE_DIR)/sys/context/tramplinsWContext.asm \ $(KERNEL_SOURCE_DIR)/sys/context/context.h $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h \ $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h $(KERNEL_SOURCE_DIR)/sys/pic/pic.h \ - $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h + $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(KERNEL_SOURCE_DIR)/sync/sync.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ @@ -155,7 +159,7 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(TRAMP_WC_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o $(KERNEL_BUILD_DIR)/misc/list.o \ $(KERNEL_BUILD_DIR)/sys/process/process.o $(KERNEL_BUILD_DIR)/sys/process/scheduler.o \ - $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o + $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o $(KERNEL_BUILD_DIR)/sync/sync.asm.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(LD) -m i386pe -T $(KERNEL_SOURCE_DIR)/link.ld $^ -o $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7f7bf0d..0aefe90 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -27,8 +27,15 @@ void task1(int i, int j, int k) int* temp = os_malloc(sizeof(int) * 100); temp[5] = 5; asm("xchgw %bx, %bx"); - while(counter < 1000) { if (!(counter % 10)) { printf("\n"); } printf("%d ", counter++); } - printf("End of the proc #1\n"); + while(counter < 200000) + { + if (!(counter % 10)) + { + printf("\n"); + } + printf("%d ", counter++); + } + printf("End of the proc #1, counter=%d\n", counter); asm("xchgw %bx, %bx"); } @@ -36,9 +43,18 @@ void task2(int i, int j, int k, int l) { static unsigned int counter = 0u; printf("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); + int* temp = os_malloc(sizeof(int) * 150); + temp[126] = 16; asm("xchgw %bx, %bx"); - while(counter < 1000) { printf("-+"); ++counter; } - printf("End of the proc #2\n"); + while(counter < 200000) + { + if (!(counter % 10)) + { + printf("\n"); + } + printf("%d ", counter++); + } + printf("End of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); } @@ -187,6 +203,7 @@ void entry_point() //----------------------------------------------------------------------- asm("sti"); + irq_on = 1; // while(1) { // printf("*"); // } @@ -198,5 +215,5 @@ void entry_point() // printf("%d", 1/0); // 0x0 // asm("sti"); - while (1) {} + while (1) { asm("hlt"); } } \ No newline at end of file diff --git a/src/kernel/sync/sync.asm b/src/kernel/sync/sync.asm new file mode 100644 index 0000000..8aaf0f4 --- /dev/null +++ b/src/kernel/sync/sync.asm @@ -0,0 +1,38 @@ +[BITS 32] + +[EXTERN lock_irq] +[EXTERN unlock_irq] + +lock_irq: + cli + push ebp + mov ebp, esp + push eax + push ebx + mov ebx, 0x1 +.start_lock: + xor eax, eax + lock cmpxchg [ebp + 8], ebx + cmp eax, 0x0 + je locked_irq + jmp .start_lock + +locked_irq: + pop ebx + pop eax + mov esp, ebp + pop ebp + ret + +unlock_irq: + push ebp + mov ebp, esp + push eax + + mov eax, [ebp + 8] + lock xor [ebp + 8], eax + + pop eax + mov esp, ebp + pop ebp + ret \ No newline at end of file diff --git a/src/kernel/sync/sync.h b/src/kernel/sync/sync.h new file mode 100644 index 0000000..875a87a --- /dev/null +++ b/src/kernel/sync/sync.h @@ -0,0 +1,4 @@ +#pragma once + +extern void lock_irq(ptr_t lock); +extern void unlock_irq(ptr_t lock); \ No newline at end of file diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 28990e6..6381801 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -1,6 +1,7 @@ #include "./interruption.h" #include "./paging/paging.h" #include "./process/scheduler.h" +#include "../sync/sync.h" void kernel_panic(char* str, int type) { @@ -76,11 +77,14 @@ void interrupt_handler(context* ctx) { } } +static ptr_t lock_scheduler = 0; + void timer_interrupt(context* ctx) { static unsigned int counter = 0u; // printf("\n%d ", counter); if (counter == 10) { + lock_irq(lock_scheduler); // unsigned int current_eflags; // asm("pushf\n\t" // "pop %0" @@ -103,6 +107,7 @@ void timer_interrupt(context* ctx) { // ctx->eflags, current_eflags, // ctx->error_code); switch_to_next((crop_context*)ctx); + unlock_irq(lock_scheduler); counter = 0u; } ++counter; @@ -341,8 +346,6 @@ void default_panic_handler(context* ctx) { : "memory" ); - - printf("Kernel panic: unhandled interrupt %x, interrupted process context:\n" " EAX = %x ECX = %x EDX = %x EBX = %x\n" " ESP = %x EBP = %x ESI = %x EDI = %x\n" @@ -358,6 +361,8 @@ void default_panic_handler(context* ctx) { ctx->eflags, current_eflags, ctx->error_code); + printf("Process #%x", tasks); + if (ctx->vector >= 0x20) { asm("sti"); } diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 14bd10c..aedd356 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -1,4 +1,5 @@ #include "printf.h" +#include "../../sync/sync.h" static int current_x; static int current_y; @@ -55,10 +56,13 @@ void print_string(char* str, int color) { } } +ptr_t irq_on = 0; +static ptr_t lock_printf = 0; + void printf(const char* fmt, ...) { va_list a = va_start(fmt); // asm("xchgw %bx, %bx"); - + if (irq_on) lock_irq(lock_printf); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -180,4 +184,9 @@ void printf(const char* fmt, ...) { print_char(*i, WHITE); } } + if (irq_on) + { + unlock_irq(lock_printf); + asm("sti"); + } } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 10ba047..9eadbe8 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -27,6 +27,8 @@ enum color { _LAST }; +extern ptr_t irq_on; + int string_compare(const char* first, const char* second, int n); void vga_print_char(char symbol, int x, int y, int foreground_color); void vga_clear_screen(); diff --git a/src/kernel/sys/process/exec.asm b/src/kernel/sys/process/exec.asm index 0916c8c..31eae72 100644 --- a/src/kernel/sys/process/exec.asm +++ b/src/kernel/sys/process/exec.asm @@ -2,15 +2,18 @@ [GLOBAL exec] [GLOBAL continue_exec] +[GLOBAL purgatory] [GLOBAL get_ebp] [GLOBAL get_esp] [GLOBAL set_ebp_esp] +[GLOBAL lock_irq] +[GLOBAL unlock_irq] [EXTERN load_proc_ebp_pdt_entry_esp] [EXTERN load_proc_esp_pdt_eip_ebp_esp] exec: - xchg bx, bx + ; xchg bx, bx add esp, 4 mov esp, ebp push eax @@ -30,14 +33,20 @@ exec: mov ebx, esp mov esp, ebp push eax + cmp eax, 0xC0000000 + jge .lol + xchg bx, bx +.lol: mov esp, ebx pop ebx pop eax pop esp + sti + ; xchg bx, bx ret continue_exec: - xchg bx, bx + ; xchg bx, bx add esp, 4 mov esp, ebp push eax @@ -58,14 +67,29 @@ continue_exec: mov ebx, esp mov esp, ebp push eax + cmp eax, 0xC0000000 + jge .lol + xchg bx, bx +.lol: mov esp, ebx pop ebx pop eax pop ebp pop esp sub esp, 4 + sti + ; xchg bx, bx ret +purgatory: + mov esp, ebp + mov eax, 0x100000 + mov cr3, eax +AGAIN: + hlt +before_we_gone: + jmp AGAIN + get_ebp: mov eax, ebp ret diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 0a3e2ee..0647f90 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -10,12 +10,13 @@ unsigned int ntasks = 0u; node* tasks = nullptr; node* os_task = nullptr; -extern void continue_exec(); extern void exec(); +extern void continue_exec(); +extern void purgatory(); extern void mov_esp_ebp(void); extern ptr_t get_ebp(void); extern ptr_t get_esp(void); -extern void set_ebp_esp(ptr_t ebp, ptr_t esp); +extern void set_ebp_esp(ptr_t ebp); void reg_os() { @@ -42,6 +43,15 @@ void copy_rev(unsigned int *restrict dest, } } +void self_destruct() +{ + node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); + node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); + printf("SELF-DESTRUCTION ON GOING FOR PROCESS #%x\n", *tasks_hh); + ((ptask)(tasks_hh->data))->state = DEAD; + while(1) { asm("hlt"); } +} + void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) { va_list VAARGS = va_start(sz_args); @@ -119,15 +129,6 @@ void terminate_task() // ... } -void self_destruct() -{ - node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); - node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); - load_page_directory((void*)os_task_hh->data3); - ((ptask)(tasks_hh->data))->state = DEAD; - while(1) {} -} - void switch_to_next(crop_context* cur_ctx) { node** tasks_hh = (node**)((ptr_t)&tasks + 0xC0000000); @@ -141,14 +142,14 @@ void switch_to_next(crop_context* cur_ctx) old_task->state = ON_PAUSE; *old_task->ctx = *cur_ctx; // memcpy action } - - // printf("[SWITCH_TO_NEXT] : orig tasks %x\n", &tasks); - // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", (*tasks_hh), (*tasks_hh)->next); - *tasks_hh = (*tasks_hh)->next; - // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", tasks_hh, (*tasks_hh)->next); - ptask next_task = (ptask)((*tasks_hh)->data); + do { + // printf("[SWITCH_TO_NEXT] : orig tasks %x\n", &tasks); + // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", (*tasks_hh), (*tasks_hh)->next); + *tasks_hh = (*tasks_hh)->next; + // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", tasks_hh, (*tasks_hh)->next); + ptask next_task = (ptask)((*tasks_hh)->data); printf("[SWITCH_TO_NEXT] : case %x\n", next_task->state); // printf("[SWITCH_TO_NEXT] : old_task %x, next_task %x\n", old_task, next_task); switch (next_task->state) @@ -206,42 +207,66 @@ void switch_to_next(crop_context* cur_ctx) // ((context*)cur_ctx)->error_code); // load_page_directory((void*)(*tasks_hh)->data3); - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); return; } case DEAD: { - asm("xchgw %bx, %bx"); - for(unsigned int i = 1u; i < 768u; ++i) - { - os_delete((void*)(((ppdtx)(*tasks_hh)->data3)->pages[i].frame_address << 22), 1u << 22); - } - os_delete((void*)(*tasks_hh)->data3, sizeof(pde) * 1024); - os_delete((void*)(*tasks_hh)->data, sizeof(task)); + cur_ctx->ebp = (ptr_t)((*os_task_hh)->data2); + cur_ctx->eip = (uint32_t)((uint32_t*)&purgatory + 0xC0000000); + // for(unsigned int i = 1u; i < 768u; ++i) + // { + // os_delete((void*)(((ppdtx)(*tasks_hh)->data3)->pages[i].frame_address << 22), 1u << 22); + // } + // os_delete((void*)(*tasks_hh)->data3, sizeof(pde) * 1024); + // os_delete((void*)(*tasks_hh)->data, sizeof(task)); + // manual remove head (cyclic list case) if ((*ntasks_hh) > 1) { - old_task_node->next = (node*)*(node**)((ptr_t)(*tasks_hh)->next + 0xC0000000); - os_delete((void*)(*tasks_hh), sizeof(node)); + old_task_node->next = (*tasks_hh)->next; + // os_delete((void*)(*tasks_hh), sizeof(node)); *ntasks_hh = *ntasks_hh - 1; } else { - os_delete((void*)(*tasks_hh), sizeof(node)); + // os_delete((void*)(*tasks_hh), sizeof(node)); *ntasks_hh = *ntasks_hh - 1; + printf("LAST TASK GONE!"); + asm("xchgw %bx, %bx"); return; } } default: { printf("[SWITCH_TO_NEXT] : default!\n"); - asm("xchgw %bx, %bx"); return; } } } while(1); } +// static int IRQ_disable_counter = 0; +// void unblock_scheduler() +// { +// --IRQ_disable_counter; +// if(IRQ_disable_counter == 0) +// { +// asm("sti"); +// } +// } + +// void block_scheduler() +// { +// asm("cli"); +// ++IRQ_disable_counter; +// } + +// int is_solo_owning() +// { +// return IRQ_disable_counter; +// } + void switch_to_os() { node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index 8b66d68..d9f6305 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -38,7 +38,9 @@ typedef struct task_struct void reg_os(); void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...); void terminate_task(); -void self_destruct(); void switch_to_next(crop_context* cur_ctx); +// void unblock_scheduler(); +// void block_scheduler(); +// int is_solo_owning(); void switch_to_os(); task_t get_type(); \ No newline at end of file From 67c4f08dd0c819e10dc8857000642ade611fe9d5 Mon Sep 17 00:00:00 2001 From: qq Date: Sun, 19 May 2024 04:23:58 +0700 Subject: [PATCH 79/88] init console --- src/kernel/libc/io/console.c | 67 ++++++++++++++++++++++++++++++ src/kernel/libc/io/console.h | 31 ++++++++++++++ src/kernel/libc/io/printer.c | 0 src/kernel/libc/io/printer.h | 4 ++ src/kernel/sys/process/scheduler.c | 7 +++- 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/kernel/libc/io/console.c create mode 100644 src/kernel/libc/io/console.h create mode 100644 src/kernel/libc/io/printer.c create mode 100644 src/kernel/libc/io/printer.h diff --git a/src/kernel/libc/io/console.c b/src/kernel/libc/io/console.c new file mode 100644 index 0000000..f917965 --- /dev/null +++ b/src/kernel/libc/io/console.c @@ -0,0 +1,67 @@ +#include +#include <../sys/memory/alloc.h> +#include <../sys/io/io.h> + + +static pair get_pair(int x, int y) +{ + pair p; + p.x = x; + p.y = y; + return p; +} + +static void frame_draw(console* cnsl, char* process_name) +{ + pair size = cnsl->size; + pair start = cnsl->start; + + vga_print_char("╔", start.x, start.y, WHITE); + vga_print_char("═", start.x + 1, start.y, WHITE); + + int i = 0; + for (; process_name[i]; i++) + { + vga_print_char(process_name[i], start.x + 1 + i + 1, start.y, WHITE); + } + + for(; i < size.x; i++) + { + vga_print_char("═", start.x + 1 + i + 1, start.y, WHITE); + } + + vga_print_char("╗", start.x + size.x - 1, start.y, WHITE); + + for (i = 1; i < size.y - 2; i++) + { + vga_print_char("║", start.x, start.y + i, WHITE); + vga_print_char("║", start.x + size.x - 1, start.y + i, WHITE); + } + + vga_print_char("╚", start.x, start.y + size.y - 1, WHITE); + for(i = 1; i < size.x - 2; i++) + { + vga_print_char("═", start.x + i, start.y + size.y - 1, WHITE); + } + vga_print_char("╝", start.x + size.x - 1, start.y + size.y - 1, WHITE); +} + +console* init_console(char* process_name, + int start_x, + int start_y, + int size_x, + int size_y) +{ + console* cnsl = c_malloc(sizeof(console)); + cnsl->start = get_pair(start_x, start_y); + cnsl->size = get_pair(size_x , size_y ); + cnsl->current_pos = get_pair(1 , 1 ); + frame_draw(cnsl, process_name); + return cnsl; +} + +void delete_console(console* cnsl) +{ + +} + diff --git a/src/kernel/libc/io/console.h b/src/kernel/libc/io/console.h new file mode 100644 index 0000000..f64e296 --- /dev/null +++ b/src/kernel/libc/io/console.h @@ -0,0 +1,31 @@ +#pragma once + +typedef struct +{ + int x; + int y; +} pair; + +typedef struct +{ + // The beginning of the console is considered to be the upper right corner of the frame. + pair start; + + // To be precise, the size of the terminal includes its frames, + // so the text will be displayed on the size plane (size_x - 2, size_y - 2). + pair size; + + // The current position is calculated taking into account the frame. + // Therefore, the starting character is placed at position (1, 1). + pair current_pos; +} console; + + +console* init_console(char* process_name, + int start_x, + int start_y, + int size_x, + int size_y); + +void delete_console(console* cnsl); + diff --git a/src/kernel/libc/io/printer.c b/src/kernel/libc/io/printer.c new file mode 100644 index 0000000..e69de29 diff --git a/src/kernel/libc/io/printer.h b/src/kernel/libc/io/printer.h new file mode 100644 index 0000000..20b1548 --- /dev/null +++ b/src/kernel/libc/io/printer.h @@ -0,0 +1,4 @@ +#pragma once + +#include + diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 0647f90..621c9f4 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -1,6 +1,7 @@ #include "scheduler.h" #include "process.h" #include "../paging/paging.h" +#include "../../libc/io/console.h" #define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] #define va_list char* @@ -52,7 +53,10 @@ void self_destruct() while(1) { asm("hlt"); } } -void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) +void add_task(task_t type, char* process_name, int start_x, + int start_y, + int size_x, + int size_y, ptr_t* entry, unsigned int sz_args, ...) { va_list VAARGS = va_start(sz_args); ptask new_task = os_malloc(sizeof(task)); @@ -70,6 +74,7 @@ void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...) new_task->ctx = proc_ctx; new_task->ctx->ebp = (uint32_t)process_stack_start; ppdtx new_space = bb(); + cur_node->data2 = init_console(process_name, start_x, start_y, size_x, size_y); cur_node->data3 = (uint32_t)new_space; unsigned int args[MAX_ARGS]; sz_args = (sz_args < MAX_ARGS) ? sz_args : MAX_ARGS; From 849975ac223b644c3ff5758377d1205ba0ff7442 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 19 May 2024 05:05:00 +0700 Subject: [PATCH 80/88] fwevvarverv --- Makefile | 20 +++++++++++++++++--- src/kernel/kernel.c | 7 ++++--- src/kernel/libc/io/printer.c | 0 src/kernel/libc/io/printer.h | 4 ---- src/kernel/sys/io/printf.c | 19 +++++++++---------- src/kernel/sys/process/scheduler.c | 8 ++++---- src/kernel/sys/process/scheduler.h | 5 ++++- src/{kernel => }/libc/io/console.c | 28 ++++++++++++++-------------- src/{kernel => }/libc/io/console.h | 0 src/libc/io/printer.c | 2 ++ src/libc/io/printer.h | 2 ++ 11 files changed, 56 insertions(+), 39 deletions(-) delete mode 100644 src/kernel/libc/io/printer.c delete mode 100644 src/kernel/libc/io/printer.h rename src/{kernel => }/libc/io/console.c (57%) rename src/{kernel => }/libc/io/console.h (100%) create mode 100644 src/libc/io/printer.c create mode 100644 src/libc/io/printer.h diff --git a/Makefile b/Makefile index bb265a3..361aeae 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,11 @@ BUILD_DIR = ./build BOOTLOADER_SOURCE_DIR = $(SOURCE_DIR)/bootloader KERNEL_SOURCE_DIR = $(SOURCE_DIR)/kernel +LIBC_SOURCE_DIR = $(SOURCE_DIR)/libc BOOTLOADER_BUILD_DIR = $(BUILD_DIR)/bootloader KERNEL_BUILD_DIR = $(BUILD_DIR)/kernel +LIBC_BUILD_DIR = $(BUILD_DIR)/libc BOOTLOADER_ASM = $(BOOTLOADER_SOURCE_DIR)/boot.asm KERNEL_C = $(KERNEL_SOURCE_DIR)/kernel.c @@ -128,7 +130,8 @@ $(KERNEL_BUILD_DIR)/sys/process/process.o: $(KERNEL_SOURCE_DIR)/sys/process/proc $(KERNEL_BUILD_DIR)/sys/process/scheduler.o: $(KERNEL_SOURCE_DIR)/sys/process/scheduler.c \ $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(KERNEL_SOURCE_DIR)/misc/list.h \ $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h $(KERNEL_SOURCE_DIR)/sys/context/context.h \ - $(KERNEL_SOURCE_DIR)/sys/process/process.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h + $(KERNEL_SOURCE_DIR)/sys/process/process.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h \ + $(LIBC_SOURCE_DIR)/io/console.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ @@ -141,10 +144,20 @@ $(KERNEL_BUILD_DIR)/sys/interruption.o: $(KERNEL_SOURCE_DIR)/sys/interruption.c $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ +$(LIBC_BUILD_DIR)/io/console.o: $(LIBC_SOURCE_DIR)/io/console.c $(LIBC_SOURCE_DIR)/io/console.h \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + +$(LIBC_BUILD_DIR)/io/printer.o: $(LIBC_SOURCE_DIR)/io/printer.c $(LIBC_SOURCE_DIR)/io/printer.h \ + $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/memory/alloc.h + $(info [MAKE] building: $@) + $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ + $(KERNEL_O): $(KERNEL_C) $(KERNEL_SOURCE_DIR)/sys/io/io.h $(KERNEL_SOURCE_DIR)/sys/io/printf.h \ $(KERNEL_SOURCE_DIR)/sys/memory/memory.h $(KERNEL_SOURCE_DIR)/sys/interruption.h \ $(KERNEL_SOURCE_DIR)/sys/pic/pic.h $(KERNEL_SOURCE_DIR)/sys/paging/paging.h \ - $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h + $(KERNEL_SOURCE_DIR)/sys/process/scheduler.h $(LIBC_SOURCE_DIR)/io/printer.h $(info [MAKE] building: $@) $(GCC) -Wall -m32 -mno-sse -fno-pie -ffreestanding -nostdlib -fno-stack-protector -c $< -o $@ # -m32 @@ -159,7 +172,8 @@ $(KERNEL_BIN): $(KERNEL_ENTRY_O) $(KERNEL_O) \ $(TRAMP_WC_O) $(KERNEL_BUILD_DIR)/sys/context/exp.o \ $(KERNEL_BUILD_DIR)/sys/paging/paging.asm.o $(KERNEL_BUILD_DIR)/sys/paging/paging.o $(KERNEL_BUILD_DIR)/misc/list.o \ $(KERNEL_BUILD_DIR)/sys/process/process.o $(KERNEL_BUILD_DIR)/sys/process/scheduler.o \ - $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o $(KERNEL_BUILD_DIR)/sync/sync.asm.o + $(KERNEL_BUILD_DIR)/sys/process/exec.asm.o $(KERNEL_BUILD_DIR)/sync/sync.asm.o $(LIBC_BUILD_DIR)/io/console.o \ + $(LIBC_BUILD_DIR)/io/printer.o $(info [MAKE] building: $@) $(LD) -m i386pe -Ttext $(KERNEL_ENTRY_ADDRESS) $^ -o $(KERNEL_TMP) # $(LD) -m i386pe -T $(KERNEL_SOURCE_DIR)/link.ld $^ -o $(KERNEL_TMP) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 0aefe90..f331efa 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -7,6 +7,7 @@ extern void experiment(); #include "./sys/pic/pic.h" #include "./sys/paging/paging.h" #include "./sys/process/scheduler.h" +#include "../libc/io/printer.h" // compare [first, first_end) and second (until '\0') @@ -52,7 +53,7 @@ void task2(int i, int j, int k, int l) { printf("\n"); } - printf("%d ", counter++); + printf("+-%d ", counter++); } printf("End of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); @@ -145,8 +146,8 @@ void entry_point() //----------------------------------------------------------------------- TEST ZONE : PROCESSES reg_os(); - add_task(ARBITRARY, (ptr_t*)task1, 3u, 1337, 228, 322); - add_task(ARBITRARY, (ptr_t*)task2, 4u, 5, 10, 15, 20); + add_task((ptr_t*)task1, ARBITRARY, "#1", 0, 0, 40, 24, 3u, 1337, 228, 322); + add_task((ptr_t*)task2, ARBITRARY, "#2", 40, 0, 40, 24, 4u, 5, 10, 15, 20); reg_os(); //----------------------------------------------------------------------- diff --git a/src/kernel/libc/io/printer.c b/src/kernel/libc/io/printer.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/kernel/libc/io/printer.h b/src/kernel/libc/io/printer.h deleted file mode 100644 index 20b1548..0000000 --- a/src/kernel/libc/io/printer.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include - diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index aedd356..d6d10d9 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -10,10 +10,19 @@ int string_compare(const char* first, const char* second, int n) { return !res; } +ptr_t irq_on = 0; +static ptr_t lock_print = 0; + void vga_print_char(char symbol, int x, int y, int foreground_color) { // if (current_x >= 80) end_line(); + if (irq_on) lock_irq(lock_print); *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; + if (irq_on) + { + unlock_irq(lock_print); + asm("sti"); + } } void vga_clear_screen() { @@ -56,13 +65,8 @@ void print_string(char* str, int color) { } } -ptr_t irq_on = 0; -static ptr_t lock_printf = 0; - void printf(const char* fmt, ...) { va_list a = va_start(fmt); - // asm("xchgw %bx, %bx"); - if (irq_on) lock_irq(lock_printf); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -184,9 +188,4 @@ void printf(const char* fmt, ...) { print_char(*i, WHITE); } } - if (irq_on) - { - unlock_irq(lock_printf); - asm("sti"); - } } \ No newline at end of file diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 621c9f4..9f8cb43 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -1,7 +1,7 @@ #include "scheduler.h" #include "process.h" #include "../paging/paging.h" -#include "../../libc/io/console.h" +#include "../../../libc/io/console.h" #define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] #define va_list char* @@ -53,10 +53,10 @@ void self_destruct() while(1) { asm("hlt"); } } -void add_task(task_t type, char* process_name, int start_x, +void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, int start_y, int size_x, - int size_y, ptr_t* entry, unsigned int sz_args, ...) + int size_y, unsigned int sz_args, ...) { va_list VAARGS = va_start(sz_args); ptask new_task = os_malloc(sizeof(task)); @@ -74,7 +74,7 @@ void add_task(task_t type, char* process_name, int start_x, new_task->ctx = proc_ctx; new_task->ctx->ebp = (uint32_t)process_stack_start; ppdtx new_space = bb(); - cur_node->data2 = init_console(process_name, start_x, start_y, size_x, size_y); + cur_node->data2 = (uint32_t)init_console(process_name, start_x, start_y, size_x, size_y); cur_node->data3 = (uint32_t)new_space; unsigned int args[MAX_ARGS]; sz_args = (sz_args < MAX_ARGS) ? sz_args : MAX_ARGS; diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index d9f6305..41860e1 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -36,7 +36,10 @@ typedef struct task_struct #pragma pack(pop) void reg_os(); -void add_task(task_t type, ptr_t* entry, unsigned int sz_args, ...); +void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, + int start_y, + int size_x, + int size_y, unsigned int sz_args, ...); void terminate_task(); void switch_to_next(crop_context* cur_ctx); // void unblock_scheduler(); diff --git a/src/kernel/libc/io/console.c b/src/libc/io/console.c similarity index 57% rename from src/kernel/libc/io/console.c rename to src/libc/io/console.c index f917965..b0c3343 100644 --- a/src/kernel/libc/io/console.c +++ b/src/libc/io/console.c @@ -1,7 +1,6 @@ -#include -#include <../sys/memory/alloc.h> -#include <../sys/io/io.h> - +#include "console.h" +#include "../../kernel/sys/memory/alloc.h" +#include "../../kernel/sys/io/io.h" static pair get_pair(int x, int y) { @@ -16,8 +15,8 @@ static void frame_draw(console* cnsl, char* process_name) pair size = cnsl->size; pair start = cnsl->start; - vga_print_char("╔", start.x, start.y, WHITE); - vga_print_char("═", start.x + 1, start.y, WHITE); + vga_print_char('╔', start.x, start.y, WHITE); + vga_print_char('═', start.x + 1, start.y, WHITE); int i = 0; for (; process_name[i]; i++) @@ -27,23 +26,23 @@ static void frame_draw(console* cnsl, char* process_name) for(; i < size.x; i++) { - vga_print_char("═", start.x + 1 + i + 1, start.y, WHITE); + vga_print_char('═', start.x + 1 + i + 1, start.y, WHITE); } - vga_print_char("╗", start.x + size.x - 1, start.y, WHITE); + vga_print_char('╗', start.x + size.x - 1, start.y, WHITE); for (i = 1; i < size.y - 2; i++) { - vga_print_char("║", start.x, start.y + i, WHITE); - vga_print_char("║", start.x + size.x - 1, start.y + i, WHITE); + vga_print_char('║', start.x, start.y + i, WHITE); + vga_print_char('║', start.x + size.x - 1, start.y + i, WHITE); } - vga_print_char("╚", start.x, start.y + size.y - 1, WHITE); + vga_print_char('╚', start.x, start.y + size.y - 1, WHITE); for(i = 1; i < size.x - 2; i++) { - vga_print_char("═", start.x + i, start.y + size.y - 1, WHITE); + vga_print_char('═', start.x + i, start.y + size.y - 1, WHITE); } - vga_print_char("╝", start.x + size.x - 1, start.y + size.y - 1, WHITE); + vga_print_char('╝', start.x + size.x - 1, start.y + size.y - 1, WHITE); } console* init_console(char* process_name, @@ -52,11 +51,12 @@ console* init_console(char* process_name, int size_x, int size_y) { - console* cnsl = c_malloc(sizeof(console)); + console* cnsl = os_malloc(sizeof(console)); cnsl->start = get_pair(start_x, start_y); cnsl->size = get_pair(size_x , size_y ); cnsl->current_pos = get_pair(1 , 1 ); frame_draw(cnsl, process_name); + asm("xchgw %bx, %bx"); return cnsl; } diff --git a/src/kernel/libc/io/console.h b/src/libc/io/console.h similarity index 100% rename from src/kernel/libc/io/console.h rename to src/libc/io/console.h diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c new file mode 100644 index 0000000..1cf1835 --- /dev/null +++ b/src/libc/io/printer.c @@ -0,0 +1,2 @@ +#include "printer.h" +#include "console.h" \ No newline at end of file diff --git a/src/libc/io/printer.h b/src/libc/io/printer.h new file mode 100644 index 0000000..3f59c93 --- /dev/null +++ b/src/libc/io/printer.h @@ -0,0 +1,2 @@ +#pragma once + From c13c0e120f2104ce6b74c5b0b94b78a453f2b254 Mon Sep 17 00:00:00 2001 From: qq Date: Sun, 19 May 2024 07:09:49 +0700 Subject: [PATCH 81/88] console+printer first ver. maybe this commit destroy everything. Shit happens. Will it happen this time? --- src/kernel/kernel.c | 29 ++++--- src/kernel/sys/io/printf.c | 8 ++ src/kernel/sys/io/printf.h | 1 + src/libc/io/console.c | 37 +++++--- src/libc/io/printer.c | 170 ++++++++++++++++++++++++++++++++++++- src/libc/io/printer.h | 1 + 6 files changed, 222 insertions(+), 24 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index f331efa..64075d8 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -24,7 +24,7 @@ void str_reverse(char* str_start, char* str_end) { void task1(int i, int j, int k) { static unsigned int counter = 0u; - printf("Hello from process #1 : %d %d %d;\n", i, j, k); + print("Hello from process #1 : %d %d %d;\n", i, j, k); int* temp = os_malloc(sizeof(int) * 100); temp[5] = 5; asm("xchgw %bx, %bx"); @@ -32,18 +32,19 @@ void task1(int i, int j, int k) { if (!(counter % 10)) { - printf("\n"); + print("\n"); } - printf("%d ", counter++); + print("%d ", counter++); } - printf("End of the proc #1, counter=%d\n", counter); + print("End of the proc #1, counter=%d\n", counter); asm("xchgw %bx, %bx"); + while(1) {} } void task2(int i, int j, int k, int l) { static unsigned int counter = 0u; - printf("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); + print("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); int* temp = os_malloc(sizeof(int) * 150); temp[126] = 16; asm("xchgw %bx, %bx"); @@ -51,12 +52,13 @@ void task2(int i, int j, int k, int l) { if (!(counter % 10)) { - printf("\n"); + print("\n"); } - printf("+-%d ", counter++); + print("+-%d ", counter++); } - printf("End of the proc #2, counter=%d\n", counter); + print("End of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); + while(1) {} } void ostask1() @@ -64,6 +66,12 @@ void ostask1() while(1) {} } +void task3() { + while (1) { + print("*"); + } +} + void entry_point() { init_printer(); @@ -146,8 +154,9 @@ void entry_point() //----------------------------------------------------------------------- TEST ZONE : PROCESSES reg_os(); - add_task((ptr_t*)task1, ARBITRARY, "#1", 0, 0, 40, 24, 3u, 1337, 228, 322); - add_task((ptr_t*)task2, ARBITRARY, "#2", 40, 0, 40, 24, 4u, 5, 10, 15, 20); + add_task((ptr_t*)task1, ARBITRARY, "Process #1", 0, 0, 40, 14, 3u, 1337, 228, 322); + add_task((ptr_t*)task2, ARBITRARY, "Process #2", 40, 0, 40, 25, 4u, 5, 10, 15, 20); + add_task((ptr_t*)task3, ARBITRARY, "Process #3", 0, 14, 40, 11, 0); reg_os(); //----------------------------------------------------------------------- diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index d6d10d9..a5a3807 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -10,11 +10,18 @@ int string_compare(const char* first, const char* second, int n) { return !res; } +void vga_print_char_raw(unsigned short symbol, int x, int y, unsigned short foreground_color) { + // if (current_x >= 80) end_line(); + // *((short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; + *((unsigned short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; +} + ptr_t irq_on = 0; static ptr_t lock_print = 0; void vga_print_char(char symbol, int x, int y, int foreground_color) { // if (current_x >= 80) end_line(); + short sy = ((short) foreground_color) << 8 | (short) symbol; if (irq_on) lock_irq(lock_print); *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; @@ -66,6 +73,7 @@ void print_string(char* str, int color) { } void printf(const char* fmt, ...) { + return; va_list a = va_start(fmt); for (const char* i = fmt; *i; ++i) { if (*i == '%') { diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 9eadbe8..68e69e4 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -30,6 +30,7 @@ enum color { extern ptr_t irq_on; int string_compare(const char* first, const char* second, int n); +void vga_print_char_raw(unsigned short symbol, int x, int y, unsigned short foreground_color); void vga_print_char(char symbol, int x, int y, int foreground_color); void vga_clear_screen(); void vga_print_string(char* str, int x, int y, int text_color); diff --git a/src/libc/io/console.c b/src/libc/io/console.c index b0c3343..f28c999 100644 --- a/src/libc/io/console.c +++ b/src/libc/io/console.c @@ -14,35 +14,40 @@ static void frame_draw(console* cnsl, char* process_name) { pair size = cnsl->size; pair start = cnsl->start; + int startx = start.x; + int starty = start.y; - vga_print_char('╔', start.x, start.y, WHITE); - vga_print_char('═', start.x + 1, start.y, WHITE); + vga_print_char_raw(201, start.x, start.y, WHITE); // 201 = ╔ + vga_print_char_raw(205, start.x + 1, start.y, WHITE); // 205 = '═' int i = 0; for (; process_name[i]; i++) { - vga_print_char(process_name[i], start.x + 1 + i + 1, start.y, WHITE); + vga_print_char_raw(process_name[i], start.x + 1 + i + 1, start.y, WHITE); } - for(; i < size.x; i++) + for(; i < size.x - 2; i++) { - vga_print_char('═', start.x + 1 + i + 1, start.y, WHITE); + vga_print_char_raw(205, start.x + 1 + i + 1, start.y, WHITE); // 205 = '═' } - vga_print_char('╗', start.x + size.x - 1, start.y, WHITE); + vga_print_char_raw(187, start.x + size.x - 1, start.y, WHITE); // 187 = ╗ - for (i = 1; i < size.y - 2; i++) + for (i = 1; i < size.y - 1; i++) { - vga_print_char('║', start.x, start.y + i, WHITE); - vga_print_char('║', start.x + size.x - 1, start.y + i, WHITE); + vga_print_char_raw(186, start.x, start.y + i, WHITE); // 186 = ║ + for(int j = 1; j < size.x - 1; j++) { + vga_print_char_raw('\0', start.x + j, start.y + i, WHITE); + } + vga_print_char_raw(186, start.x + size.x - 1, start.y + i, WHITE); // 186 = ║ } - vga_print_char('╚', start.x, start.y + size.y - 1, WHITE); - for(i = 1; i < size.x - 2; i++) + vga_print_char_raw(200, start.x, start.y + size.y - 1, WHITE); // 200 = ╚ + for(i = 1; i < size.x - 1; i++) { - vga_print_char('═', start.x + i, start.y + size.y - 1, WHITE); + vga_print_char_raw(205, start.x + i, start.y + size.y - 1, WHITE); // 205 = '═' } - vga_print_char('╝', start.x + size.x - 1, start.y + size.y - 1, WHITE); + vga_print_char_raw(188, start.x + size.x - 1, start.y + size.y - 1, WHITE); // 188 = ╝ } console* init_console(char* process_name, @@ -55,7 +60,13 @@ console* init_console(char* process_name, cnsl->start = get_pair(start_x, start_y); cnsl->size = get_pair(size_x , size_y ); cnsl->current_pos = get_pair(1 , 1 ); + asm("xchgw %bx, %bx"); frame_draw(cnsl, process_name); + // for(unsigned short i = 180; i < 256; i++) { + // printf("%d", i); + // asm("xchgw %bx, %bx"); + // vga_print_char_raw(i,i - 180, 0, WHITE); + // } asm("xchgw %bx, %bx"); return cnsl; } diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c index 1cf1835..8c772d4 100644 --- a/src/libc/io/printer.c +++ b/src/libc/io/printer.c @@ -1,2 +1,170 @@ #include "printer.h" -#include "console.h" \ No newline at end of file +#include "console.h" +#include "../../kernel/sys/process/scheduler.h" +#include "../../kernel/sys/io/io.h" + +static void shift_up_console(console* cnsl) { + asm("xchgw %bx, %bx"); + for (int i = cnsl->start.y + 1; i < cnsl->start.y + cnsl->size.y - 2; i++) { + memcpy((char*) VGA_START + (cnsl->start.x + 1) * sizeof(short) + 80 * i * sizeof(short), + (char*) VGA_START + (cnsl->start.x + 1) * sizeof(short) + 80 * (i + 1) * sizeof(short), + sizeof(short) * (cnsl->size.x - 2)); + } + memzero((char*) VGA_START + (cnsl->start.x + 1) * sizeof(short) + 80 * (cnsl->start.y + cnsl->size.y - 2) * sizeof(short), + sizeof(short) * (cnsl->size.x - 2)); + // memcpy((char*) VGA_START, (char*) VGA_START + sizeof(short) * 80, sizeof(short) * 24 * 80); + // memzero((char*) VGA_START + sizeof(short) * 80 * 24, sizeof(short) * 80); +} + +static void end_line_console(console* cnsl) { + // asm("xchgw %bx, %bx"); + if (cnsl->current_pos.y >= cnsl->size.y - 1 - 1) shift_up_console(cnsl); + else ++cnsl->current_pos.y; + cnsl->current_pos.x = 1; +} + +static void print_char_console(console* cnsl, char s, int color) { + // asm("xchgw %bx, %bx"); + if (cnsl->current_pos.y > cnsl->size.y - 1 - 1) { + shift_up_console(cnsl); + cnsl->current_pos.y--; + } + vga_print_char_raw(s, cnsl->start.x + cnsl->current_pos.x, cnsl->start.y + cnsl->current_pos.y, color); + if (++cnsl->current_pos.x >= cnsl->size.x - 1) end_line_console(cnsl); +} + +static void print_string_console(console *cnsl, char* str, int color) { + while (*str) { + print_char_console(cnsl, *str, color); + ++str; + } +} + + +void print(const char* fmt, ...) { + va_list a = va_start(fmt); + + console* current_console = (console*) ((node*)*(node**)((ptr_t)&tasks + 0xC0000000))->data2; + + for (const char* i = fmt; *i; ++i) { + if (*i == '%') { + if (*(i + 1) == '%') { + print_char_console(current_console, '%', WHITE); + ++i; + } else { + int color = WHITE; + + // mode select + char mode = *(i++ + 1); + + // color select (iff defined) + if (*(i + 1) == '_') { + i += 2; + const char* start = i; + const char* stop = i; + while (*i != '!') { + if (!*i) return; + stop = i++; + } + + if (start != stop) { + char* color_names = "BLACK\0BLUE\0GREEN\0CYAN\0RED\0PURPLE\0" + "BROWN\0GRAY\0DARK_GRAY\0LIGHT_BLUE\0LIGHT_GREEN\0" + "LIGHT_CYAN\0LIGHT_RED\0LIGHT_PURPLE\0YELLOW\0WHITE"; + char* cur_color = color_names; + for (int i = 0; i < _LAST; ++i) { + if (string_compare(start, cur_color, stop - start)) { + color = i; + break; + } + while (*cur_color++); + } + } + } + + // print + if (mode == 's') { + char* b = &va_arg(a, char); + print_string_console(current_console, b, color); + } else if (mode == 'd') { + unsigned b = (unsigned) va_arg(a, int); +#define _MAX_N_DIGITS_FOR_DEC_CASE (int)(12) + if (b != 0) { + char buffer[_MAX_N_DIGITS_FOR_DEC_CASE] = {0}; + + int counter = _MAX_N_DIGITS_FOR_DEC_CASE - 1; +#undef _MAX_N_DIGITS_FOR_DEC_CASE + int is_neg = 0; + if (b & (1 << sizeof(int) * 8 - 1)) + { + is_neg = 1; + b = ~b + 1; + } + while (b) { + buffer[--counter] = b % 10 + '0'; + b /= 10; + } + if (is_neg) + { + buffer[--counter] = '-'; + } + + print_string_console(current_console, buffer + counter, color); + } else { + print_char_console(current_console, '0', color); + } + } else if (mode == 'x' || mode == 'X') { + unsigned int b = (unsigned int) va_arg(a, int); + print_string_console(current_console, "0x", color); + if (b != 0) { + char buffer[sizeof(int) + 1] = {0}; + + int counter = 3; + if (b < 0) { + b *= -1; + } + while (b) { + int temp = b % 16; + + char symbol; + if (temp > 9) { + symbol = temp - 10 + (mode == 'X' ? 'A' : 'a'); + } else { + symbol = temp + '0'; + } + buffer[counter--] = symbol; + b >>= 4; + } + + print_string_console(current_console, buffer + counter + 1, color); + } else { + print_char_console(current_console, '0', color); + } + } else if (mode == 'b') { + unsigned b = (unsigned) va_arg(a, unsigned); + print_string_console(current_console, "0b", color); + char buffer[sizeof(unsigned) * 8 + 1] = {0}; + + int counter = sizeof(unsigned) * 8 - 1; + while (b) { + buffer[counter--] = (char)((b & 1) + '0'); + b >>= 1; + } + + print_string_console(current_console, buffer + counter + 1, color); + } + } + } else if (*i == '\n') { + // end_line_console(); + current_console->current_pos.y++; + current_console->current_pos.x = 1; + + } else if (*i == '\t' || *i == " ") { + // end_line_console(); + while(current_console->current_pos.x++ % 4) {} + + } else { + print_char_console(current_console, *i, WHITE); + } + } +} \ No newline at end of file diff --git a/src/libc/io/printer.h b/src/libc/io/printer.h index 3f59c93..81b03a4 100644 --- a/src/libc/io/printer.h +++ b/src/libc/io/printer.h @@ -1,2 +1,3 @@ #pragma once +void print(const char* fmt, ...); \ No newline at end of file From a77cca02ed05ff50b45ec722abc3e1cb9834db42 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 19 May 2024 14:34:48 +0700 Subject: [PATCH 82/88] sync printer --- src/kernel/kernel.c | 9 +-------- src/kernel/sync/sync.asm | 8 +++++--- src/kernel/sys/io/printf.c | 18 +++++++++--------- src/libc/io/printer.c | 5 +++++ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 64075d8..ba159ba 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -38,7 +38,6 @@ void task1(int i, int j, int k) } print("End of the proc #1, counter=%d\n", counter); asm("xchgw %bx, %bx"); - while(1) {} } void task2(int i, int j, int k, int l) @@ -58,12 +57,6 @@ void task2(int i, int j, int k, int l) } print("End of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); - while(1) {} -} - -void ostask1() -{ - while(1) {} } void task3() { @@ -156,7 +149,7 @@ void entry_point() reg_os(); add_task((ptr_t*)task1, ARBITRARY, "Process #1", 0, 0, 40, 14, 3u, 1337, 228, 322); add_task((ptr_t*)task2, ARBITRARY, "Process #2", 40, 0, 40, 25, 4u, 5, 10, 15, 20); - add_task((ptr_t*)task3, ARBITRARY, "Process #3", 0, 14, 40, 11, 0); + add_task((ptr_t*)task3, ARBITRARY, "Process #3", 0, 14, 40, 11, 0u); reg_os(); //----------------------------------------------------------------------- diff --git a/src/kernel/sync/sync.asm b/src/kernel/sync/sync.asm index 8aaf0f4..ac1f680 100644 --- a/src/kernel/sync/sync.asm +++ b/src/kernel/sync/sync.asm @@ -14,8 +14,7 @@ lock_irq: xor eax, eax lock cmpxchg [ebp + 8], ebx cmp eax, 0x0 - je locked_irq - jmp .start_lock + jne .start_lock locked_irq: pop ebx @@ -28,10 +27,13 @@ unlock_irq: push ebp mov ebp, esp push eax - +.start_unlock: mov eax, [ebp + 8] lock xor [ebp + 8], eax + cmp dword [ebp + 8], 0x0 + jne .start_unlock +unocked_irq: pop eax mov esp, ebp pop ebp diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index a5a3807..0c0b3b7 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -16,20 +16,11 @@ void vga_print_char_raw(unsigned short symbol, int x, int y, unsigned short fore *((unsigned short *)(VGA_START + 2 * (y * 80 + x))) = ((short) foreground_color) << 8 | (short) symbol; } -ptr_t irq_on = 0; -static ptr_t lock_print = 0; - void vga_print_char(char symbol, int x, int y, int foreground_color) { // if (current_x >= 80) end_line(); short sy = ((short) foreground_color) << 8 | (short) symbol; - if (irq_on) lock_irq(lock_print); *((short *)(VGA_START + 2 * (current_y * 80 + current_x))) = ((short) foreground_color) << 8 | (short) symbol; - if (irq_on) - { - unlock_irq(lock_print); - asm("sti"); - } } void vga_clear_screen() { @@ -72,9 +63,13 @@ void print_string(char* str, int color) { } } +ptr_t irq_on = 0; +static ptr_t lock_printf = 0; + void printf(const char* fmt, ...) { return; va_list a = va_start(fmt); + if (irq_on) lock_irq(lock_printf); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -196,4 +191,9 @@ void printf(const char* fmt, ...) { print_char(*i, WHITE); } } + if (irq_on) + { + unlock_irq(lock_printf); + asm("sti"); + } } \ No newline at end of file diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c index 8c772d4..cb2cd33 100644 --- a/src/libc/io/printer.c +++ b/src/libc/io/printer.c @@ -2,6 +2,7 @@ #include "console.h" #include "../../kernel/sys/process/scheduler.h" #include "../../kernel/sys/io/io.h" +#include "../../kernel/sync/sync.h" static void shift_up_console(console* cnsl) { asm("xchgw %bx, %bx"); @@ -40,12 +41,14 @@ static void print_string_console(console *cnsl, char* str, int color) { } } +static ptr_t lock_cprint = 0; void print(const char* fmt, ...) { va_list a = va_start(fmt); console* current_console = (console*) ((node*)*(node**)((ptr_t)&tasks + 0xC0000000))->data2; + lock_irq(lock_cprint); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -167,4 +170,6 @@ void print(const char* fmt, ...) { print_char_console(current_console, *i, WHITE); } } + unlock_irq(lock_cprint); + asm("sti"); } \ No newline at end of file From 098eff93903ff9b1f6c5520a82682fa8746d06e1 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Sun, 19 May 2024 21:22:43 +0700 Subject: [PATCH 83/88] wtf --- src/kernel/kernel.c | 3 +- src/kernel/sync/sync.asm | 80 +++++++++++++++++++++++++++----- src/kernel/sync/sync.h | 8 +++- src/kernel/sys/context/context.h | 4 +- src/kernel/sys/interruption.c | 6 +-- src/kernel/sys/io/printf.c | 11 ++--- src/kernel/sys/io/printf.h | 2 +- src/libc/io/console.c | 4 +- src/libc/io/printer.c | 9 ++-- 9 files changed, 91 insertions(+), 36 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index ba159ba..0be540e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -126,7 +126,7 @@ void entry_point() fix_heap_to_N(0x200000); // lab №7, #1 - experiment(); + // experiment(); //----------------------------------------------------------------------- TEST ZONE : C_MALLOC, C_DELETE // int* temp = c_malloc(sizeof(int) * 10); @@ -206,7 +206,6 @@ void entry_point() //----------------------------------------------------------------------- asm("sti"); - irq_on = 1; // while(1) { // printf("*"); // } diff --git a/src/kernel/sync/sync.asm b/src/kernel/sync/sync.asm index ac1f680..60b42ac 100644 --- a/src/kernel/sync/sync.asm +++ b/src/kernel/sync/sync.asm @@ -1,39 +1,97 @@ [BITS 32] -[EXTERN lock_irq] -[EXTERN unlock_irq] +[EXTERN lock_32_irq] +[EXTERN lock_32] +[EXTERN unlock_32_irq] +[EXTERN unlock_32] -lock_irq: +lock_32_irq: + push ebp + mov ebp, esp + push eax + push ebx + push ecx + mov ebx, 0x1 + ; xchg bx, bx + ; lea ecx, [ebp + 8] + mov ecx, dword [ebp + 8] +.start_lock: + xor eax, eax + lock cmpxchg dword [ecx], ebx + cmp eax, 0x0 + jne .start_lock cli + +locked_32_irq: + pop ecx + pop ebx + pop eax + mov esp, ebp + pop ebp + ret + +lock_32: push ebp mov ebp, esp push eax push ebx + push ecx mov ebx, 0x1 + ; xchg bx, bx + ; lea ecx, [ebp + 8] + mov ecx, dword [ebp + 8] .start_lock: xor eax, eax - lock cmpxchg [ebp + 8], ebx + lock cmpxchg dword [ecx], ebx cmp eax, 0x0 jne .start_lock -locked_irq: +locked_32: + pop ecx pop ebx pop eax mov esp, ebp pop ebp ret -unlock_irq: +unlock_32_irq: push ebp mov ebp, esp push eax + push ebx + ; xchg bx, bx + ; lea eax, [ebp + 8] + mov ebx, dword [ebp + 8] +.start_unlock: + mov eax, dword [ebx] + lock xor dword [ebx], eax + cmp eax, 0x0 + je .start_unlock + +unlocked_32_irq: + pop ebx + pop eax + mov esp, ebp + pop ebp + sti + ret + +unlock_32: + push ebp + mov ebp, esp + push eax + push ebx + ; xchg bx, bx + ; lea eax, [ebp + 8] + mov ebx, dword [ebp + 8] .start_unlock: - mov eax, [ebp + 8] - lock xor [ebp + 8], eax - cmp dword [ebp + 8], 0x0 - jne .start_unlock + mov eax, dword [ebx] + lock xor dword [ebx], eax + cmp eax, 0x0 + je .start_unlock -unocked_irq: +unlocked_32: + pop ebx pop eax mov esp, ebp pop ebp diff --git a/src/kernel/sync/sync.h b/src/kernel/sync/sync.h index 875a87a..4a1e53e 100644 --- a/src/kernel/sync/sync.h +++ b/src/kernel/sync/sync.h @@ -1,4 +1,8 @@ #pragma once -extern void lock_irq(ptr_t lock); -extern void unlock_irq(ptr_t lock); \ No newline at end of file +typedef int mutex; + +extern void lock_32_irq(mutex* lock); +extern void lock_32(mutex* lock); +extern void unlock_32_irq(mutex* lock); +extern void unlock_32(mutex* lock); \ No newline at end of file diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index 53d5616..5ecd566 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -8,7 +8,7 @@ typedef struct uint32_t edi; uint32_t esi; uint32_t ebp; - uint32_t esp; + uint32_t esp_on_handling; uint32_t ebx; uint32_t edx; uint32_t ecx; @@ -27,7 +27,7 @@ typedef struct uint16_t cs; uint16_t padding_5; uint32_t eflags; - uint32_t prev_esp; + uint32_t esp; uint16_t ss; uint16_t padding_6; diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 6381801..4ecfb0f 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -77,14 +77,14 @@ void interrupt_handler(context* ctx) { } } -static ptr_t lock_scheduler = 0; +static mutex lock_scheduler = 0; void timer_interrupt(context* ctx) { static unsigned int counter = 0u; // printf("\n%d ", counter); if (counter == 10) { - lock_irq(lock_scheduler); + lock_32_irq(&lock_scheduler); // unsigned int current_eflags; // asm("pushf\n\t" // "pop %0" @@ -107,7 +107,7 @@ void timer_interrupt(context* ctx) { // ctx->eflags, current_eflags, // ctx->error_code); switch_to_next((crop_context*)ctx); - unlock_irq(lock_scheduler); + unlock_32(&lock_scheduler); // need unlock_32(), NOT unlock_32_irq(). STI will be called later, in assembler code in exec.asm counter = 0u; } ++counter; diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index 0c0b3b7..c656140 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -63,13 +63,12 @@ void print_string(char* str, int color) { } } -ptr_t irq_on = 0; -static ptr_t lock_printf = 0; +static mutex lock_printf = 0; void printf(const char* fmt, ...) { return; va_list a = va_start(fmt); - if (irq_on) lock_irq(lock_printf); + lock_32(&lock_printf); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -191,9 +190,5 @@ void printf(const char* fmt, ...) { print_char(*i, WHITE); } } - if (irq_on) - { - unlock_irq(lock_printf); - asm("sti"); - } + unlock_32(&lock_printf); } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 68e69e4..43cf102 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -27,7 +27,7 @@ enum color { _LAST }; -extern ptr_t irq_on; +extern int irq_on; int string_compare(const char* first, const char* second, int n); void vga_print_char_raw(unsigned short symbol, int x, int y, unsigned short foreground_color); diff --git a/src/libc/io/console.c b/src/libc/io/console.c index f28c999..44b9b29 100644 --- a/src/libc/io/console.c +++ b/src/libc/io/console.c @@ -60,14 +60,14 @@ console* init_console(char* process_name, cnsl->start = get_pair(start_x, start_y); cnsl->size = get_pair(size_x , size_y ); cnsl->current_pos = get_pair(1 , 1 ); - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); frame_draw(cnsl, process_name); // for(unsigned short i = 180; i < 256; i++) { // printf("%d", i); // asm("xchgw %bx, %bx"); // vga_print_char_raw(i,i - 180, 0, WHITE); // } - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); return cnsl; } diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c index cb2cd33..c37ef6b 100644 --- a/src/libc/io/printer.c +++ b/src/libc/io/printer.c @@ -5,7 +5,7 @@ #include "../../kernel/sync/sync.h" static void shift_up_console(console* cnsl) { - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); for (int i = cnsl->start.y + 1; i < cnsl->start.y + cnsl->size.y - 2; i++) { memcpy((char*) VGA_START + (cnsl->start.x + 1) * sizeof(short) + 80 * i * sizeof(short), (char*) VGA_START + (cnsl->start.x + 1) * sizeof(short) + 80 * (i + 1) * sizeof(short), @@ -41,14 +41,14 @@ static void print_string_console(console *cnsl, char* str, int color) { } } -static ptr_t lock_cprint = 0; +static mutex lock_cprint = 0; void print(const char* fmt, ...) { va_list a = va_start(fmt); console* current_console = (console*) ((node*)*(node**)((ptr_t)&tasks + 0xC0000000))->data2; - lock_irq(lock_cprint); + lock_32(&lock_cprint); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -170,6 +170,5 @@ void print(const char* fmt, ...) { print_char_console(current_console, *i, WHITE); } } - unlock_irq(lock_cprint); - asm("sti"); + unlock_32(&lock_cprint); } \ No newline at end of file From 8c8b98faab33ffd7b439ce2818ce7ea14154a805 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 20 May 2024 00:42:11 +0700 Subject: [PATCH 84/88] blyat --- src/kernel/kernel.c | 22 +++++++++++++++------- src/kernel/sys/context/context.h | 6 +++--- src/kernel/sys/interruption.c | 26 +++++++++++++++++++++----- src/kernel/sys/io/printf.c | 7 +++---- src/kernel/sys/io/printf.h | 3 ++- src/kernel/sys/process/exec.asm | 18 ++++++++++++++---- src/kernel/sys/process/scheduler.c | 22 ++++++++++++++-------- src/kernel/sys/process/scheduler.h | 10 ++++++++-- src/libc/io/printer.c | 3 +-- src/libc/io/printer.h | 3 +++ 10 files changed, 84 insertions(+), 36 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 0be540e..b3fd2b7 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -24,17 +24,20 @@ void str_reverse(char* str_start, char* str_end) { void task1(int i, int j, int k) { static unsigned int counter = 0u; + static unsigned int ebp; print("Hello from process #1 : %d %d %d;\n", i, j, k); int* temp = os_malloc(sizeof(int) * 100); temp[5] = 5; - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); while(counter < 200000) { + ebp = get_esp(); if (!(counter % 10)) { - print("\n"); + print("%d\n", ebp); } - print("%d ", counter++); + print("%d ", ebp); + ++counter; } print("End of the proc #1, counter=%d\n", counter); asm("xchgw %bx, %bx"); @@ -43,25 +46,30 @@ void task1(int i, int j, int k) void task2(int i, int j, int k, int l) { static unsigned int counter = 0u; + static unsigned int ebp; print("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); int* temp = os_malloc(sizeof(int) * 150); temp[126] = 16; - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); while(counter < 200000) { + ebp = get_esp(); if (!(counter % 10)) { - print("\n"); + print("%d\n", ebp); } - print("+-%d ", counter++); + print("+-%d ", ebp); + ++counter; } print("End of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); } void task3() { + static unsigned int ebp; while (1) { - print("*"); + ebp = get_esp(); + print("*%d", ebp); } } diff --git a/src/kernel/sys/context/context.h b/src/kernel/sys/context/context.h index 5ecd566..093c754 100644 --- a/src/kernel/sys/context/context.h +++ b/src/kernel/sys/context/context.h @@ -8,7 +8,7 @@ typedef struct uint32_t edi; uint32_t esi; uint32_t ebp; - uint32_t esp_on_handling; + uint32_t esp; uint32_t ebx; uint32_t edx; uint32_t ecx; @@ -27,7 +27,7 @@ typedef struct uint16_t cs; uint16_t padding_5; uint32_t eflags; - uint32_t esp; + uint32_t esp_before; uint16_t ss; uint16_t padding_6; @@ -38,7 +38,7 @@ typedef struct uint32_t edi; uint32_t esi; uint32_t ebp; - uint32_t esp; + uint32_t esp_on_handling; uint32_t ebx; uint32_t edx; uint32_t ecx; diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 4ecfb0f..21e7297 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -2,6 +2,7 @@ #include "./paging/paging.h" #include "./process/scheduler.h" #include "../sync/sync.h" +#include "../../libc/io/printer.h" void kernel_panic(char* str, int type) { @@ -77,14 +78,26 @@ void interrupt_handler(context* ctx) { } } -static mutex lock_scheduler = 0; - void timer_interrupt(context* ctx) { static unsigned int counter = 0u; // printf("\n%d ", counter); if (counter == 10) { lock_32_irq(&lock_scheduler); + int printf_was_locked = 0; + int cprint_was_locked = 0; + if (lock_printf) + { + printf("[SWITCH TO NEXT] PRINTF WAS LOCKED\n"); + unlock_32(&lock_printf); + printf_was_locked = 1; + } + if (lock_cprint) + { + printf("[SWITCH TO NEXT] CPRINT WAS LOCKED\n"); + unlock_32(&lock_cprint); + cprint_was_locked = 1; + } // unsigned int current_eflags; // asm("pushf\n\t" // "pop %0" @@ -106,9 +119,12 @@ void timer_interrupt(context* ctx) { // ctx->cs, ctx->ss, ctx->eip, // ctx->eflags, current_eflags, // ctx->error_code); - switch_to_next((crop_context*)ctx); - unlock_32(&lock_scheduler); // need unlock_32(), NOT unlock_32_irq(). STI will be called later, in assembler code in exec.asm + // asm("xchgw %bx, %bx"); + switch_to_next(ctx); + if (printf_was_locked) lock_32(&lock_printf); + if (cprint_was_locked) lock_32(&lock_cprint); counter = 0u; + // asm("xchgw %bx, %bx"); } ++counter; } @@ -366,5 +382,5 @@ void default_panic_handler(context* ctx) { if (ctx->vector >= 0x20) { asm("sti"); } - asm("xchgw %bx, %bx"); + // asm("xchgw %bx, %bx"); } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.c b/src/kernel/sys/io/printf.c index c656140..3f2eea4 100644 --- a/src/kernel/sys/io/printf.c +++ b/src/kernel/sys/io/printf.c @@ -1,5 +1,4 @@ #include "printf.h" -#include "../../sync/sync.h" static int current_x; static int current_y; @@ -63,12 +62,12 @@ void print_string(char* str, int color) { } } -static mutex lock_printf = 0; +mutex lock_printf = 0; void printf(const char* fmt, ...) { return; va_list a = va_start(fmt); - lock_32(&lock_printf); + // lock_32(&lock_printf); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { @@ -190,5 +189,5 @@ void printf(const char* fmt, ...) { print_char(*i, WHITE); } } - unlock_32(&lock_printf); + // unlock_32(&lock_printf); } \ No newline at end of file diff --git a/src/kernel/sys/io/printf.h b/src/kernel/sys/io/printf.h index 43cf102..aa824e7 100644 --- a/src/kernel/sys/io/printf.h +++ b/src/kernel/sys/io/printf.h @@ -1,6 +1,7 @@ #pragma once #include "../memory/memory.h" +#include "../../sync/sync.h" #define VGA_START (int)(0xb8000) #define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] @@ -27,7 +28,7 @@ enum color { _LAST }; -extern int irq_on; +extern mutex lock_printf; int string_compare(const char* first, const char* second, int n); void vga_print_char_raw(unsigned short symbol, int x, int y, unsigned short foreground_color); diff --git a/src/kernel/sys/process/exec.asm b/src/kernel/sys/process/exec.asm index 31eae72..45f6cf7 100644 --- a/src/kernel/sys/process/exec.asm +++ b/src/kernel/sys/process/exec.asm @@ -6,11 +6,11 @@ [GLOBAL get_ebp] [GLOBAL get_esp] [GLOBAL set_ebp_esp] -[GLOBAL lock_irq] -[GLOBAL unlock_irq] [EXTERN load_proc_ebp_pdt_entry_esp] [EXTERN load_proc_esp_pdt_eip_ebp_esp] +[EXTERN unlock_32_irq] +[EXTERN lock_scheduler] exec: ; xchg bx, bx @@ -41,7 +41,12 @@ exec: pop ebx pop eax pop esp - sti + push eax + mov eax, lock_scheduler + push eax + call unlock_32_irq + pop eax + pop eax ; xchg bx, bx ret @@ -77,7 +82,12 @@ continue_exec: pop ebp pop esp sub esp, 4 - sti + push eax + mov eax, lock_scheduler + push eax + call unlock_32_irq + pop eax + pop eax ; xchg bx, bx ret diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 9f8cb43..752c97d 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -10,13 +10,12 @@ unsigned int ntasks = 0u; node* tasks = nullptr; node* os_task = nullptr; +mutex lock_scheduler = 0; extern void exec(); extern void continue_exec(); extern void purgatory(); extern void mov_esp_ebp(void); -extern ptr_t get_ebp(void); -extern ptr_t get_esp(void); extern void set_ebp_esp(ptr_t ebp); void reg_os() @@ -58,6 +57,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, int size_x, int size_y, unsigned int sz_args, ...) { + lock_32_irq(&lock_scheduler); va_list VAARGS = va_start(sz_args); ptask new_task = os_malloc(sizeof(task)); tasks = add_after_n(tasks, (uint32_t)new_task, 0u); @@ -70,7 +70,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, new_task->entry = (ptr_t*)((ptr_t)entry + 0xC0000000); new_task->type = type; new_task->state = INIT; - crop_context* proc_ctx = os_malloc(sizeof(crop_context)); + context* proc_ctx = os_malloc(sizeof(crop_context)); new_task->ctx = proc_ctx; new_task->ctx->ebp = (uint32_t)process_stack_start; ppdtx new_space = bb(); @@ -108,6 +108,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, new_task->ctx->esp = (uint32_t)process_stack_esp_int; // printf("ESP : EBP = %x : %x\n", new_task->ctx->esp, new_task->ctx->ebp); // asm("xchgw %bx, %bx"); + unlock_32_irq(&lock_scheduler); } void load_proc_ebp_pdt_entry_esp(ptr_t ebp, ptr_t pdt, ptr_t entry, ptr_t fake1, ptr_t fake2, ptr_t esp) @@ -119,13 +120,13 @@ void load_proc_ebp_pdt_entry_esp(ptr_t ebp, ptr_t pdt, ptr_t entry, ptr_t fake1, esp = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; } -void load_proc_esp_pdt_eip_ebp_esp(ptr_t esp1, ptr_t pdt, ptr_t eip, ptr_t fake1, ptr_t fake2, ptr_t ebp, ptr_t esp2) +void load_proc_esp_pdt_eip_ebp_esp(ptr_t esp, ptr_t pdt, ptr_t eip, ptr_t fake1, ptr_t fake2, ptr_t ebp, ptr_t esp2) { node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); ebp = (ptr_t)((ptask)(tasks_hh->data))->ctx->ebp; pdt = (ptr_t)tasks_hh->data3; eip = (ptr_t)((ptask)(tasks_hh->data))->ctx->eip; - esp1 = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; + esp = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; esp2 = (ptr_t)((ptask)(tasks_hh->data))->ctx->esp; } @@ -134,7 +135,7 @@ void terminate_task() // ... } -void switch_to_next(crop_context* cur_ctx) +void switch_to_next(context* cur_ctx) { node** tasks_hh = (node**)((ptr_t)&tasks + 0xC0000000); node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); @@ -145,8 +146,11 @@ void switch_to_next(crop_context* cur_ctx) { if ((*ntasks_hh) < 2) return; old_task->state = ON_PAUSE; - *old_task->ctx = *cur_ctx; // memcpy action + *(crop_context*)old_task->ctx = *(crop_context*)cur_ctx; // memcpy action + old_task->ctx->esp = (uint32_t)((ptr_t*)&(cur_ctx->eflags) + 2); } + // printf("CUR_CTX ON %x, esp on %x, eflags on %x, old.esp on %x\n", cur_ctx, cur_ctx->esp, cur_ctx->eflags, old_task->ctx->esp); + // asm("xchgw %bx, %bx"); do { @@ -172,8 +176,10 @@ void switch_to_next(crop_context* cur_ctx) } case ON_PAUSE: { - *cur_ctx = *next_task->ctx; + // asm("xchgw %bx, %bx"); + *(crop_context*)cur_ctx = *(crop_context*)next_task->ctx; cur_ctx->ebp = (ptr_t)((*os_task_hh)->data2); + cur_ctx->esp = next_task->ctx->esp; cur_ctx->eip = (uint32_t)((uint32_t*)&continue_exec + 0xC0000000); next_task->state = ACTIVE; // asm("xchgw %bx, %bx"); diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index 41860e1..141f143 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -3,6 +3,7 @@ #include "../../misc/list.h" #include "../memory/alloc.h" #include "../context/context.h" +#include "../../sync/sync.h" extern unsigned int ntasks; extern node* tasks; @@ -30,18 +31,23 @@ typedef struct task_struct task_t type; state_t state; ptr_t* entry; - crop_context* ctx; + context* ctx; } task, *ptask; #pragma pack(pop) +extern mutex lock_scheduler; + +extern ptr_t get_ebp(void); +extern ptr_t get_esp(void); + void reg_os(); void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, int start_y, int size_x, int size_y, unsigned int sz_args, ...); void terminate_task(); -void switch_to_next(crop_context* cur_ctx); +void switch_to_next(context* cur_ctx); // void unblock_scheduler(); // void block_scheduler(); // int is_solo_owning(); diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c index c37ef6b..6404f0b 100644 --- a/src/libc/io/printer.c +++ b/src/libc/io/printer.c @@ -2,7 +2,6 @@ #include "console.h" #include "../../kernel/sys/process/scheduler.h" #include "../../kernel/sys/io/io.h" -#include "../../kernel/sync/sync.h" static void shift_up_console(console* cnsl) { // asm("xchgw %bx, %bx"); @@ -41,7 +40,7 @@ static void print_string_console(console *cnsl, char* str, int color) { } } -static mutex lock_cprint = 0; +mutex lock_cprint = 0; void print(const char* fmt, ...) { va_list a = va_start(fmt); diff --git a/src/libc/io/printer.h b/src/libc/io/printer.h index 81b03a4..78e1fd7 100644 --- a/src/libc/io/printer.h +++ b/src/libc/io/printer.h @@ -1,3 +1,6 @@ #pragma once +#include "../../kernel/sync/sync.h" + +extern mutex lock_cprint; void print(const char* fmt, ...); \ No newline at end of file From a295c4382094dd01df3069d8f1356131dd95cc64 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Mon, 20 May 2024 01:09:01 +0700 Subject: [PATCH 85/88] fix --- src/kernel/sync/sync.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/sync/sync.asm b/src/kernel/sync/sync.asm index 60b42ac..674539e 100644 --- a/src/kernel/sync/sync.asm +++ b/src/kernel/sync/sync.asm @@ -6,6 +6,7 @@ [EXTERN unlock_32] lock_32_irq: + cli push ebp mov ebp, esp push eax @@ -20,7 +21,6 @@ lock_32_irq: lock cmpxchg dword [ecx], ebx cmp eax, 0x0 jne .start_lock - cli locked_32_irq: pop ecx From 5b92cf3bebeedcf0f9833ec41c1c7ce4cd7c8bc9 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 22 May 2024 19:47:36 +0700 Subject: [PATCH 86/88] some fixes, add lockword to scheduler --- src/kernel/kernel.c | 45 ++++++++++++++++++------------ src/kernel/sys/interruption.c | 18 +++++++----- src/kernel/sys/process/scheduler.c | 23 +++++++++------ src/kernel/sys/process/scheduler.h | 4 ++- src/libc/io/printer.c | 2 +- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index b3fd2b7..380e8f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -24,53 +24,61 @@ void str_reverse(char* str_start, char* str_end) { void task1(int i, int j, int k) { static unsigned int counter = 0u; - static unsigned int ebp; print("Hello from process #1 : %d %d %d;\n", i, j, k); int* temp = os_malloc(sizeof(int) * 100); temp[5] = 5; // asm("xchgw %bx, %bx"); while(counter < 200000) { - ebp = get_esp(); if (!(counter % 10)) { - print("%d\n", ebp); + print("1\n"); } - print("%d ", ebp); + print("1 "); ++counter; } - print("End of the proc #1, counter=%d\n", counter); + print("\nEnd of the proc #1, counter=%d\n", counter); asm("xchgw %bx, %bx"); } void task2(int i, int j, int k, int l) { static unsigned int counter = 0u; - static unsigned int ebp; print("Hello from process #2 : %d %d %d %d;\n", i, j, k, l); int* temp = os_malloc(sizeof(int) * 150); temp[126] = 16; // asm("xchgw %bx, %bx"); while(counter < 200000) { - ebp = get_esp(); if (!(counter % 10)) { - print("%d\n", ebp); + print("2\n"); } - print("+-%d ", ebp); + print("2 "); ++counter; } - print("End of the proc #2, counter=%d\n", counter); + print("\nEnd of the proc #2, counter=%d\n", counter); asm("xchgw %bx, %bx"); } -void task3() { - static unsigned int ebp; - while (1) { - ebp = get_esp(); - print("*%d", ebp); +void task3(int i, int j) +{ + static unsigned int counter = 0u; + print("Hello from process #%d : %d %d;\n", i, i, j); + int* temp = os_malloc(sizeof(int) * 77); + temp[126] = 16; + // asm("xchgw %bx, %bx"); + while(counter < 200000) + { + if (!(counter % 10)) + { + print("%d\n", i); + } + print("%d ", i); + ++counter; } + print("\nEnd of the proc #%d, counter=%d\n", i, counter); + asm("xchgw %bx, %bx"); } void entry_point() @@ -155,9 +163,10 @@ void entry_point() //----------------------------------------------------------------------- TEST ZONE : PROCESSES reg_os(); - add_task((ptr_t*)task1, ARBITRARY, "Process #1", 0, 0, 40, 14, 3u, 1337, 228, 322); - add_task((ptr_t*)task2, ARBITRARY, "Process #2", 40, 0, 40, 25, 4u, 5, 10, 15, 20); - add_task((ptr_t*)task3, ARBITRARY, "Process #3", 0, 14, 40, 11, 0u); + add_task((ptr_t*)task1, ARBITRARY, "Process #1", 0, 0, 40, 14, 3u, 1, 228, 322); + add_task((ptr_t*)task2, ARBITRARY, "Process #2", 40, 0, 40, 11, 4u, 2, 10, 15, 20); + add_task((ptr_t*)task3, ARBITRARY, "Process #3", 0, 14, 40, 11, 2u, 3, 228); + // add_task((ptr_t*)task3, ARBITRARY, "Process #4", 40, 11, 40, 14, 2u, 4, 322); reg_os(); //----------------------------------------------------------------------- diff --git a/src/kernel/sys/interruption.c b/src/kernel/sys/interruption.c index 21e7297..7abcf6a 100644 --- a/src/kernel/sys/interruption.c +++ b/src/kernel/sys/interruption.c @@ -79,11 +79,12 @@ void interrupt_handler(context* ctx) { } void timer_interrupt(context* ctx) { - static unsigned int counter = 0u; + static int counter = 0u; // printf("\n%d ", counter); - if (counter == 10) + lock_32_irq(&lock_scheduler); + if (counter == 100) { - lock_32_irq(&lock_scheduler); + counter = 0u; int printf_was_locked = 0; int cprint_was_locked = 0; if (lock_printf) @@ -120,12 +121,15 @@ void timer_interrupt(context* ctx) { // ctx->eflags, current_eflags, // ctx->error_code); // asm("xchgw %bx, %bx"); - switch_to_next(ctx); - if (printf_was_locked) lock_32(&lock_printf); - if (cprint_was_locked) lock_32(&lock_cprint); - counter = 0u; + int lockword = switch_to_next(ctx, (!!printf_was_locked << 0) | (!!cprint_was_locked << 1)); + if (lockword & (1u << 0)) lock_32(&lock_printf); + if (lockword & (1u << 1)) lock_32(&lock_cprint); // asm("xchgw %bx, %bx"); } + else + { + unlock_32_irq(&lock_scheduler); + } ++counter; } diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 752c97d..4bca557 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -2,6 +2,7 @@ #include "process.h" #include "../paging/paging.h" #include "../../../libc/io/console.h" +#include "../../../libc/io/printer.h" #define va_arg(list, type) ( (type*) ( list = (char*) list + sizeof(type) ) )[-1] #define va_list char* @@ -70,6 +71,8 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, new_task->entry = (ptr_t*)((ptr_t)entry + 0xC0000000); new_task->type = type; new_task->state = INIT; + new_task->reserved = nullptr; + new_task->lockword = 0; context* proc_ctx = os_malloc(sizeof(crop_context)); new_task->ctx = proc_ctx; new_task->ctx->ebp = (uint32_t)process_stack_start; @@ -135,7 +138,7 @@ void terminate_task() // ... } -void switch_to_next(context* cur_ctx) +int switch_to_next(context* cur_ctx, int cur_lockword) { node** tasks_hh = (node**)((ptr_t)&tasks + 0xC0000000); node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); @@ -144,10 +147,11 @@ void switch_to_next(context* cur_ctx) ptask old_task = (ptask)((*tasks_hh)->data); if (old_task->state == ACTIVE) { - if ((*ntasks_hh) < 2) return; + if ((*ntasks_hh) < 2) return old_task->lockword; old_task->state = ON_PAUSE; *(crop_context*)old_task->ctx = *(crop_context*)cur_ctx; // memcpy action old_task->ctx->esp = (uint32_t)((ptr_t*)&(cur_ctx->eflags) + 2); + old_task->lockword = cur_lockword; } // printf("CUR_CTX ON %x, esp on %x, eflags on %x, old.esp on %x\n", cur_ctx, cur_ctx->esp, cur_ctx->eflags, old_task->ctx->esp); // asm("xchgw %bx, %bx"); @@ -159,7 +163,8 @@ void switch_to_next(context* cur_ctx) *tasks_hh = (*tasks_hh)->next; // printf("[SWITCH_TO_NEXT] : tasks %x, tasks->next %x\n", tasks_hh, (*tasks_hh)->next); ptask next_task = (ptask)((*tasks_hh)->data); - printf("[SWITCH_TO_NEXT] : case %x\n", next_task->state); + printf("[SWITCH_TO_NEXT]:case %x\n", next_task->state); + printf("[SWITCH_TO_NEXT]:tasks %x\n", tasks); // printf("[SWITCH_TO_NEXT] : old_task %x, next_task %x\n", old_task, next_task); switch (next_task->state) { @@ -172,7 +177,7 @@ void switch_to_next(context* cur_ctx) asm("xchgw %bx, %bx"); asm("xchgw %bx, %bx"); // load_page_directory((void*)(*tasks_hh)->data3); - return; + return next_task->lockword; } case ON_PAUSE: { @@ -182,9 +187,10 @@ void switch_to_next(context* cur_ctx) cur_ctx->esp = next_task->ctx->esp; cur_ctx->eip = (uint32_t)((uint32_t*)&continue_exec + 0xC0000000); next_task->state = ACTIVE; + printf("[SWITCH_TO_NEXT]:ON_PAUSE:next.state %x\n", next_task->state); // asm("xchgw %bx, %bx"); // load_page_directory((void*)(*tasks_hh)->data3); - return; + return next_task->lockword; } case INIT: { @@ -194,6 +200,7 @@ void switch_to_next(context* cur_ctx) cur_ctx->eip = (uint32_t)((uint32_t*)&exec + 0xC0000000); // asm("xchgw %bx, %bx"); next_task->state = ACTIVE; + printf("[SWITCH_TO_NEXT]:INIT:next.state %x\n", next_task->state); // unsigned int current_eflags; // asm("pushf\n\t" @@ -219,7 +226,7 @@ void switch_to_next(context* cur_ctx) // load_page_directory((void*)(*tasks_hh)->data3); // asm("xchgw %bx, %bx"); - return; + return next_task->lockword; } case DEAD: { @@ -245,13 +252,13 @@ void switch_to_next(context* cur_ctx) *ntasks_hh = *ntasks_hh - 1; printf("LAST TASK GONE!"); asm("xchgw %bx, %bx"); - return; + return 0; } } default: { printf("[SWITCH_TO_NEXT] : default!\n"); - return; + return next_task->lockword; } } } while(1); diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index 141f143..46e3e95 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -32,6 +32,8 @@ typedef struct task_struct state_t state; ptr_t* entry; context* ctx; + void* reserved; + int lockword; } task, *ptask; #pragma pack(pop) @@ -47,7 +49,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, int size_x, int size_y, unsigned int sz_args, ...); void terminate_task(); -void switch_to_next(context* cur_ctx); +int switch_to_next(context* cur_ctx, int cur_lockword); // void unblock_scheduler(); // void block_scheduler(); // int is_solo_owning(); diff --git a/src/libc/io/printer.c b/src/libc/io/printer.c index 6404f0b..3bc9ab5 100644 --- a/src/libc/io/printer.c +++ b/src/libc/io/printer.c @@ -45,9 +45,9 @@ mutex lock_cprint = 0; void print(const char* fmt, ...) { va_list a = va_start(fmt); + lock_32(&lock_cprint); console* current_console = (console*) ((node*)*(node**)((ptr_t)&tasks + 0xC0000000))->data2; - lock_32(&lock_cprint); for (const char* i = fmt; *i; ++i) { if (*i == '%') { if (*(i + 1) == '%') { From 617cc74819a2a9099add4427d10f72a5ed937f48 Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Thu, 23 May 2024 03:11:15 +0700 Subject: [PATCH 87/88] fixes --- src/kernel/sys/process/scheduler.c | 6 +++--- src/kernel/sys/process/scheduler.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 4bca557..0e04a5d 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -72,7 +72,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, new_task->type = type; new_task->state = INIT; new_task->reserved = nullptr; - new_task->lockword = 0; + new_task->lockword = 0u; context* proc_ctx = os_malloc(sizeof(crop_context)); new_task->ctx = proc_ctx; new_task->ctx->ebp = (uint32_t)process_stack_start; @@ -138,7 +138,7 @@ void terminate_task() // ... } -int switch_to_next(context* cur_ctx, int cur_lockword) +unsigned int switch_to_next(context* cur_ctx, unsigned int cur_lockword) { node** tasks_hh = (node**)((ptr_t)&tasks + 0xC0000000); node** os_task_hh = (node**)((ptr_t)&os_task + 0xC0000000); @@ -252,7 +252,7 @@ int switch_to_next(context* cur_ctx, int cur_lockword) *ntasks_hh = *ntasks_hh - 1; printf("LAST TASK GONE!"); asm("xchgw %bx, %bx"); - return 0; + return 0u; } } default: diff --git a/src/kernel/sys/process/scheduler.h b/src/kernel/sys/process/scheduler.h index 46e3e95..e6c8f10 100644 --- a/src/kernel/sys/process/scheduler.h +++ b/src/kernel/sys/process/scheduler.h @@ -33,7 +33,7 @@ typedef struct task_struct ptr_t* entry; context* ctx; void* reserved; - int lockword; + unsigned int lockword; } task, *ptask; #pragma pack(pop) @@ -49,7 +49,7 @@ void add_task(ptr_t* entry, task_t type, char* process_name, int start_x, int size_x, int size_y, unsigned int sz_args, ...); void terminate_task(); -int switch_to_next(context* cur_ctx, int cur_lockword); +unsigned int switch_to_next(context* cur_ctx, unsigned int cur_lockword); // void unblock_scheduler(); // void block_scheduler(); // int is_solo_owning(); From fdadd808b7e583dc48557173285bb649965a2bbc Mon Sep 17 00:00:00 2001 From: Evgeny Shibanov Date: Wed, 29 May 2024 16:15:13 +0700 Subject: [PATCH 88/88] upd --- bx_enh_dbg.ini | 26 ++++++++++++++++++++++++++ src/kernel/kernel.c | 2 +- src/kernel/sys/process/scheduler.c | 10 ++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 bx_enh_dbg.ini diff --git a/bx_enh_dbg.ini b/bx_enh_dbg.ini new file mode 100644 index 0000000..0d0a5e1 --- /dev/null +++ b/bx_enh_dbg.ini @@ -0,0 +1,26 @@ +# bx_enh_dbg_ini +SeeReg[0] = TRUE +SeeReg[1] = TRUE +SeeReg[2] = TRUE +SeeReg[3] = TRUE +SeeReg[4] = FALSE +SeeReg[5] = FALSE +SeeReg[6] = TRUE +SeeReg[7] = FALSE +SingleCPU = FALSE +ShowIOWindows = TRUE +ShowButtons = TRUE +SeeRegColors = TRUE +ignoreNxtT = TRUE +ignSSDisasm = TRUE +UprCase = 0 +DumpInAsciiMode = 3 +isLittleEndian = TRUE +DefaultAsmLines = 512 +DumpWSIndex = 0 +DockOrder = 0x123 +ListWidthPix[0] = 381 +ListWidthPix[1] = 468 +ListWidthPix[2] = 466 +MainWindow = 0, 0, 878, 500 +FontName = Normal diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 380e8f9..4d55dac 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -234,5 +234,5 @@ void entry_point() // printf("%d", 1/0); // 0x0 // asm("sti"); - while (1) { asm("hlt"); } + while (1) { print("INT FROM MAIN\n"); asm("hlt"); } } \ No newline at end of file diff --git a/src/kernel/sys/process/scheduler.c b/src/kernel/sys/process/scheduler.c index 0e04a5d..5ec239f 100644 --- a/src/kernel/sys/process/scheduler.c +++ b/src/kernel/sys/process/scheduler.c @@ -46,10 +46,12 @@ void copy_rev(unsigned int *restrict dest, void self_destruct() { + asm("xchgw %bx, %bx"); node* tasks_hh = (node*)*(node**)((ptr_t)&tasks + 0xC0000000); node* os_task_hh = (node*)*(node**)((ptr_t)&os_task + 0xC0000000); - printf("SELF-DESTRUCTION ON GOING FOR PROCESS #%x\n", *tasks_hh); + print("SELF-DESTRUCTION HAS BEEN ENGAGED FOR PROCESS #%x\n", *tasks_hh); ((ptask)(tasks_hh->data))->state = DEAD; + asm("xchgw %bx, %bx"); while(1) { asm("hlt"); } } @@ -250,15 +252,15 @@ unsigned int switch_to_next(context* cur_ctx, unsigned int cur_lockword) { // os_delete((void*)(*tasks_hh), sizeof(node)); *ntasks_hh = *ntasks_hh - 1; - printf("LAST TASK GONE!"); + print("LAST TASK GONE!"); asm("xchgw %bx, %bx"); return 0u; } } default: { - printf("[SWITCH_TO_NEXT] : default!\n"); - return next_task->lockword; + print("[SWITCH_TO_NEXT] : default!\n"); + return next_task->lockword; } } } while(1);