Skip to content

Commit 812e794

Browse files
committed
Add a simple /proc/cpuinfo that includes some info from CPUID.
1 parent 90ddbca commit 812e794

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

Kernel/MemoryManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void MemoryManager::flushEntireTLB()
241241

242242
void MemoryManager::flushTLB(LinearAddress laddr)
243243
{
244-
asm volatile("invlpg %0": :"m" (*(char*)laddr.get()));
244+
asm volatile("invlpg %0": :"m" (*(char*)laddr.get()) : "memory");
245245
}
246246

247247
void MemoryManager::map_region_at_address(PageDirectory* page_directory, Region& region, LinearAddress laddr, bool user_allowed)

Kernel/ProcFileSystem.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "system.h"
55
#include "MemoryManager.h"
66
#include "StdLib.h"
7+
#include "i386.h"
78

89
static ProcFileSystem* s_the;
910

@@ -177,6 +178,71 @@ ByteBuffer procfs$mounts()
177178
return buffer;
178179
}
179180

181+
ByteBuffer procfs$cpuinfo()
182+
{
183+
auto buffer = ByteBuffer::createUninitialized(256);
184+
char* ptr = (char*)buffer.pointer();
185+
{
186+
CPUID cpuid(0);
187+
ptr += ksprintf(ptr, "cpuid: ");
188+
auto emit_dword = [&] (dword value) {
189+
ptr += ksprintf(ptr, "%c%c%c%c",
190+
value & 0xff,
191+
(value >> 8) & 0xff,
192+
(value >> 16) & 0xff,
193+
(value >> 24) & 0xff);
194+
};
195+
emit_dword(cpuid.ebx());
196+
emit_dword(cpuid.edx());
197+
emit_dword(cpuid.ecx());
198+
ptr += ksprintf(ptr, "\n");
199+
}
200+
{
201+
CPUID cpuid(1);
202+
dword stepping = cpuid.eax() & 0xf;
203+
dword model = (cpuid.eax() >> 4) & 0xf;
204+
dword family = (cpuid.eax() >> 8) & 0xf;
205+
dword type = (cpuid.eax() >> 12) & 0x3;
206+
dword extended_model = (cpuid.eax() >> 16) & 0xf;
207+
dword extended_family = (cpuid.eax() >> 20) & 0xff;
208+
dword display_model;
209+
dword display_family;
210+
if (family == 15) {
211+
display_family = family + extended_family;
212+
display_model = model + (extended_model << 4);
213+
} else if (family == 6) {
214+
display_family = family;
215+
display_model = model + (extended_model << 4);
216+
} else {
217+
display_family = family;
218+
display_model = model;
219+
}
220+
ptr += ksprintf(ptr, "family: %u\n", display_family);
221+
ptr += ksprintf(ptr, "model: %u\n", display_model);
222+
ptr += ksprintf(ptr, "stepping: %u\n", stepping);
223+
ptr += ksprintf(ptr, "type: %u\n", type);
224+
}
225+
{
226+
// FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
227+
// and verifying that the returned eax>=0x80000004.
228+
char buffer[48];
229+
dword* bufptr = reinterpret_cast<dword*>(buffer);
230+
auto copy_brand_string_part_to_buffer = [&] (dword i) {
231+
CPUID cpuid(0x80000002 + i);
232+
*bufptr++ = cpuid.eax();
233+
*bufptr++ = cpuid.ebx();
234+
*bufptr++ = cpuid.ecx();
235+
*bufptr++ = cpuid.edx();
236+
};
237+
copy_brand_string_part_to_buffer(0);
238+
copy_brand_string_part_to_buffer(1);
239+
copy_brand_string_part_to_buffer(2);
240+
ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
241+
}
242+
buffer.trim(ptr - (char*)buffer.pointer());
243+
return buffer;
244+
}
245+
180246
ByteBuffer procfs$kmalloc()
181247
{
182248
InterruptDisabler disabler;
@@ -235,6 +301,7 @@ bool ProcFileSystem::initialize()
235301
addFile(createGeneratedFile("mounts", procfs$mounts));
236302
addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
237303
addFile(createGeneratedFile("summary", procfs$summary));
304+
addFile(createGeneratedFile("cpuinfo", procfs$cpuinfo));
238305
return true;
239306
}
240307

Kernel/VirtualConsole.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ VirtualConsole::VirtualConsole(unsigned index, InitialContents initial_contents)
2323
{
2424
s_consoles[index] = this;
2525
m_buffer = (byte*)kmalloc_eternal(80 * 25 * 2);
26-
dbgprintf("VirtualConsole %u @ %p, m_buffer = %p\n", index, this, m_buffer);
2726
if (initial_contents == AdoptCurrentVGABuffer) {
2827
memcpy(m_buffer, s_vga_buffer, 80 * 25 * 2);
2928
auto vgaCursor = vga_get_cursor();

Kernel/i386.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,3 @@ void handleIRQ()
447447
s_irqHandler[irq]->handleIRQ();
448448
PIC::eoi(irq);
449449
}
450-

Kernel/i386.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,16 @@ inline constexpr dword pageBaseOf(dword address)
175175
return address & 0xfffff000;
176176
}
177177

178+
class CPUID {
179+
public:
180+
CPUID(dword function) { asm volatile("cpuid" : "=a" (m_eax), "=b" (m_ebx), "=c" (m_ecx), "=d" (m_edx) : "a" (function), "c" (0)); }
181+
dword eax() const { return m_eax; }
182+
dword ebx() const { return m_ebx; }
183+
dword ecx() const { return m_ecx; }
184+
dword edx() const { return m_edx; }
185+
private:
186+
dword m_eax { 0xffffffff };
187+
dword m_ebx { 0xffffffff };
188+
dword m_ecx { 0xffffffff };
189+
dword m_edx { 0xffffffff };
190+
};

0 commit comments

Comments
 (0)