如何查看计算机的内存容量?
我正在尝试用C语言制作一个操作系统。我查了很多次,但找不到答案。如何检查计算机有多少 RAM(总计、未释放或已使用)(以 MB 为单位)。我使用了 CodePulse 的教程,如果有人关心的话,这是文件结构:fs
I am trying to make an operating system in C. I've looked it up many times, but couldn't find the answer. How do I check how much RAM a computer has (total, not free or used) in MB. I used a tutorial from CodePulse Here is the file structure if anyone cares: fs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说这是相当复杂的。我目前正在开发一个业余爱好 x64 操作系统,我倾向于说您正在查看的教程并不是最好的。如果我是你,我会转而学习引导加载程序的工作原理,而不是依赖 GRUB 和多重引导。看看 如何从我自己的引导加载程序跳转到我自己的内核? 它显示了一个完整的传统引导加载程序与 BIOS 一起加载最小的 C 内核。另外,看看现代操作系统如何工作以及如何从头开始制作一个操作系统?了解这种引导加载程序将教您什么以及它将如何改进您的知识能够了解有关 x86-64 计算机如何工作的基本知识。
为了回答您的问题,以 Linux 为例,现代操作系统不会向 BIOS 询问安装了 UEFI 固件的 RAM 量。它们要么解析 SMBIOS 表(通常根本不存在),要么可靠地解析包含 DSDT 之类的一些表的 ACPI 表(请参阅 https://wiki.osdev.org/DSDT)。解析此 AML 编码的 ACPI 表将允许检测所有计算机的外围设备,包括 RAM 控制器以及总共有多少实际 RAM。
可以肯定的是,大部分 RAM 是无法访问的。例如,如果 DSDT 告诉您安装了 8GB RAM,您就不能依赖它并盲目地在操作系统中使用所有 RAM。您需要向 UEFI 固件(或旧版 BIOS)询问 RAM 的内存映射,以确定哪些 RAM 可用,哪些不可用。大多数内存映射部分可以在退出固件界面后被回收。例如,可以回收 UEFI 代码和数据来使用它,因为您不再依赖 UEFI。此外,大多数内存映射可以通过使用其他技术来检索,例如读取 MCFG 并确定不同的 PCI 配置空间在哪里,以及解析 ACPI 以确定您不应该接触的不同 ACPI 表在哪里,因为您在执行过程中会需要它们。执行您的操作系统。
It is quite complex I would say. I'm currently working on a hobby x64 operating-system and I would tend to say that the tutorial you are looking at isn't the best out there. If I were you I'd switch to learning how the bootloader works instead of relying on GRUB and multiboot. Have a look at How do I jump from my own bootloader to my own kernel? which shows a complete legacy bootloader working with the BIOS to load a minimal C kernel. Also, have a look at How does a modern operating system work and how to make one from scratch? to have info on what this kind of bootloader will teach you and how it will improve your knowledge to be able to understand basic things about how x86-64 computers work.
To answer your question taking example on Linux, modern operating-systems don't ask the BIOS for the amount of RAM installed neither UEFI firmware. They either parse the SMBIOS table (which is often not even present) or they, reliably, parse the ACPI tables that have some tables like the DSDT (see https://wiki.osdev.org/DSDT). Parsing this AML encoded ACPI table will allow to detect all the computer's peripherals including the RAM controller and how much actual RAM you have in total.
It is certain that big portions of RAM isn't accessible. For example, if the DSDT tells there is 8GB of RAM installed, you can't rely on that and blindly use all of it in your OS. You need to ask the UEFI firmware (or the legacy BIOS) for a memory map of RAM to determine what is usable RAM and what isn't. Most of the memory map portions can be reclaimed after exiting the firmware interface. For example, the UEFI code and data can be reclaimed to use it because you don't rely on UEFI anymore. Also, most of the memory map can be retrieved by using other techniques such as reading the MCFG and determining where the different PCI configuration spaces are and parsing ACPI to determine where are the different ACPI tables that you should not touch because you will need them during execution of your OS.