{"id":18716441,"url":"https://github.com/seaswalker/tiny-os","last_synced_at":"2025-04-06T10:11:32.456Z","repository":{"id":42327199,"uuid":"141647730","full_name":"seaswalker/tiny-os","owner":"seaswalker","description":"《操作系统真象还原》一书实现的系统代码","archived":false,"fork":false,"pushed_at":"2024-05-08T07:13:22.000Z","size":424,"stargazers_count":280,"open_issues_count":6,"forks_count":78,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T08:11:04.505Z","etag":null,"topics":["learning-by-doing","operating-system"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seaswalker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-20T01:24:05.000Z","updated_at":"2025-03-24T10:05:39.000Z","dependencies_parsed_at":"2024-12-16T11:44:07.503Z","dependency_job_id":null,"html_url":"https://github.com/seaswalker/tiny-os","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seaswalker%2Ftiny-os","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seaswalker%2Ftiny-os/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seaswalker%2Ftiny-os/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seaswalker%2Ftiny-os/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seaswalker","download_url":"https://codeload.github.com/seaswalker/tiny-os/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247464222,"owners_count":20942970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["learning-by-doing","operating-system"],"created_at":"2024-11-07T13:12:43.048Z","updated_at":"2025-04-06T10:11:32.421Z","avatar_url":"https://github.com/seaswalker.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tiny-os\n## 环境准备\n\n### Bochs\n\n在Mac上使用以下命令安装:\n\n```shell\nbrew install bochs\n```\n\n### 配置\n\n每个章节下的bios配置部分应视bochs的具体版本而定，可自行修改：\n\n```html\n# 对应真实机器的bios\nromimage: file=/usr/local/Cellar/bochs/2.6.9_2/share/bochs/BIOS-bochs-latest\n# 对应真实机器的VGA bios\nvgaromimage: file=/usr/local/Cellar/bochs/2.6.9_2/share/bochs/VGABIOS-lgpl-latest\n```\n\n## 说明\n\n每个章节下的代码均可以独立运行，在对应的目录下执行以下命令即可启动：\n\n```shell\n./build.sh\n```\n\n即可启动执行。完毕之后可执行以下命令清理环境：\n\n```shell\n./clean.sh\n```\n\n## 运行\n\n### 第三章\n\n![结果](images/chapter_3_result.png)\n\n### 第四章\n\n![结果](images/chapter_4_result.png)\n\n有以下几点需要注意:\n\n1. 书中的源码boot.inc的DESC_LIMIT_VIDEO2定义可能有误，应修改为:\n\n   ```assembly\n   DESC_LIMIT_VIDEO2 equ 00000000000000000000000000001011b\n   ```\n\n   原因是保护模式的基地址是0xb8000，所以最后8位应该是b，而不是0，这样才能正确显示字母'P'。\n\n   第164页的图4-11同样有问题，第4个GDT表项(显存)的base应该等于0xb8000，因为如果是图中的0xc00b8000，那么对应的物理内存地址是3072MB处，明显不合理。\n\n2. Mac上的nasm并不支持数字中间以下划线分割的写法，会出现编译错误。\n\n3. 第161页代码4-3的21行为:\n\n   ```assembly\n   times 60 dq 0\n   ```\n\n   Apple版本的nasm这样写会报错，原因是不能把int型的0赋给dq。稍加变通即可:\n\n   ```assembly\n   times 120 dd 0\n   ```\n\n### 第五章\n\n#### 内存检测\n\n这里对书中源码进行了改造，只使用e820一种方式，检测失败时会在第一行显示字符串: 'failed'，成功将在第二行显示: 'done'，如下图:\n\n![内存检测结果](images/chapter_5_detect_memory.png)\n\n内存检测的结果通过命令: `x /4wx 0xb00`查看，如下图:\n\n![检测结果](images/chapter_5_memory_size.png)\n\n结果正是我们设置的内存大小: 32MB，无误。\n\n#### 内存分页\n\n![内存分页结果](images/chapter_5_page_memory.png)\n\n这一节注意要和前面的保护模式、内存检测部分结合起来。\n\n#### 加载内核\n\n由于此时没有可以用于打印的手段，所以正确性需要到下一章节验证。此部分需要使用Linux交叉编译器中的ld命令才可以正常链接，Mac自带的无法使用，Mac上的安装参考:\n\n[11-kernel-crosscompiler](https://github.com/cfenollosa/os-tutorial/tree/master/11-kernel-crosscompiler)\n\n### 第六章\n\n#### 打印字符串\n\n![打印字符](images/chapter_6_put_str.png)\n\n这里是打印字符和打印字符串两者结合的效果，这里遇到了一个奇怪的问题，如果在main.c中存在除main之外的其它函数，那么实验结果将不正确，原因未知。\n\n#### 打印数字\n\n![打印数字](images/chapter_6_put_int.png)\n\n### 第七章\n\n#### ASM\n\n![汇编中断实现](images/chapter_7_with_asm.png)\n\n#### 改进\n\n![改进](images/chapter_7_improve.png)\n\n#### Timer\n\n![timer](images/chapter_7_timer.png)\n\n实验中的中断号与书中不同。\n\n### 第八章\n\n#### Assert\n\n![Assert](images/chapter_8_assert.png)\n\n注意, 在64位Linux系统上编译时有几点需要注意:\n\n- GCC加上参数:\n\n  ```bash\n  -m32 -fno-stack-protector\n  ```\n\n- LD增加参数:\n\n  ```bash\n  -m elf_i386\n  ```\n\n- 在Ubuntu上使用apt安装的bximage版本较老, 创建镜像的命令为:\n\n  ```bash\n  bximage -hd -mode=flat -size=10 -q disk.img\n  ```\n\n\n#### 内存池\n\n![内存池](images/chapter_8_memory_pool.png)\n\n#### 内存分配\n\n![内存分配](images/chapter_8_malloc.png)\n\n注：上一节中内存池初始化代码有一个bug：用户内存池其实地址应该是0xc009a1e0，已在此节修正。\n\n### 第九章\n\n#### 线程启动\n\n![线程启动](images/chapter_9_thread_start.png)\n\n#### 线程调度\n\n![线程调度](images/chapter_9_thread_schedule.png)\n\n这部分两次上下文切换的过程较难理解，整理成如下的时序图:\n\n![时序图](images/thread_schedule_graph.png)\n\n注意:\n\n- 如果被调度线程是第一次执行，那么转到线程对应的函数\n- 如果不是第一次被调度，那么走时序图的下半部分，原因是switch_to中保存的起始地址其实是此函数的返回地址\n- 虚线流程由第二次调度触发\n\n### 第十章\n\n#### 锁\n\n![锁](images/chapter_10_with_lock.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseaswalker%2Ftiny-os","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseaswalker%2Ftiny-os","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseaswalker%2Ftiny-os/lists"}