Răsfoiți Sursa

add cpuinfo command

Johannes Zellner 8 luni în urmă
părinte
comite
498b49c575
3 a modificat fișierele cu 303 adăugiri și 3 ștergeri
  1. 5 2
      Makefile
  2. 276 0
      cpuinfo.c
  3. 22 1
      terminal.c

+ 5 - 2
Makefile

@@ -83,6 +83,9 @@ colors.o: colors.c fbtext.h io.h timer.h vga.h
 display.o: display.c io.h vga.h gpu.h ati.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+cpuinfo.o: cpuinfo.c io.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
@@ -98,7 +101,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -179,4 +182,4 @@ run-usb: usb.img root.img
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 276 - 0
cpuinfo.c

@@ -0,0 +1,276 @@
+/* cpuinfo command: print CPU vendor/model and estimated current clock speed. */
+
+#include "io.h"
+#include "timer.h"
+
+static void put_uint(unsigned int n)
+{
+	char buf[12];
+	char *p = buf + sizeof(buf) - 1;
+	*p = '\0';
+	if (n == 0) {
+		io_putchar('0');
+		return;
+	}
+	while (n) {
+		*--p = (char)('0' + (n % 10));
+		n /= 10;
+	}
+	while (*p)
+		io_putchar(*p++);
+}
+
+static void cpuid(unsigned int leaf, unsigned int subleaf,
+	unsigned int *eax, unsigned int *ebx,
+	unsigned int *ecx, unsigned int *edx)
+{
+	__asm__ volatile ("cpuid"
+		: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
+		: "a"(leaf), "c"(subleaf));
+}
+
+static unsigned int read_tsc_low(void)
+{
+	unsigned int lo, hi;
+	__asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
+	(void)hi;
+	return lo;
+}
+
+static void trim_right_spaces(char *s)
+{
+	unsigned int len = 0;
+	while (s[len])
+		len++;
+	while (len > 0 && s[len - 1] == ' ') {
+		s[len - 1] = '\0';
+		len--;
+	}
+}
+
+static void print_flag(const char *name, int *printed_any)
+{
+	if (*printed_any)
+		io_putchar(' ');
+	io_putstr(name);
+	*printed_any = 1;
+}
+
+static void print_features_leaf1(unsigned int ecx1, unsigned int edx1)
+{
+	int any = 0;
+	io_putstr("ext l1 ");
+	if (edx1 & (1u << 0))  print_flag("FPU", &any);
+	if (edx1 & (1u << 4))  print_flag("TSC", &any);
+	if (edx1 & (1u << 5))  print_flag("MSR", &any);
+	if (edx1 & (1u << 6))  print_flag("PAE", &any);
+	if (edx1 & (1u << 9))  print_flag("APIC", &any);
+	if (edx1 & (1u << 12)) print_flag("MTRR", &any);
+	if (edx1 & (1u << 15)) print_flag("CMOV", &any);
+	if (edx1 & (1u << 16)) print_flag("PAT", &any);
+	if (edx1 & (1u << 17)) print_flag("PSE36", &any);
+	if (edx1 & (1u << 23)) print_flag("MMX", &any);
+	if (edx1 & (1u << 24)) print_flag("FXSR", &any);
+	if (edx1 & (1u << 25)) print_flag("SSE", &any);
+	if (edx1 & (1u << 26)) print_flag("SSE2", &any);
+	if (edx1 & (1u << 28)) print_flag("HTT", &any);
+	if (ecx1 & (1u << 0))  print_flag("SSE3", &any);
+	if (ecx1 & (1u << 1))  print_flag("PCLMULQDQ", &any);
+	if (ecx1 & (1u << 9))  print_flag("SSSE3", &any);
+	if (ecx1 & (1u << 12)) print_flag("FMA", &any);
+	if (ecx1 & (1u << 13)) print_flag("CX16", &any);
+	if (ecx1 & (1u << 19)) print_flag("SSE4.1", &any);
+	if (ecx1 & (1u << 20)) print_flag("SSE4.2", &any);
+	if (ecx1 & (1u << 22)) print_flag("MOVBE", &any);
+	if (ecx1 & (1u << 23)) print_flag("POPCNT", &any);
+	if (ecx1 & (1u << 25)) print_flag("AES", &any);
+	if (ecx1 & (1u << 26)) print_flag("XSAVE", &any);
+	if (ecx1 & (1u << 27)) print_flag("OSXSAVE", &any);
+	if (ecx1 & (1u << 28)) print_flag("AVX", &any);
+	if (ecx1 & (1u << 29)) print_flag("F16C", &any);
+	if (ecx1 & (1u << 30)) print_flag("RDRAND", &any);
+	if (!any)
+		io_putstr("none");
+	io_putchar('\n');
+}
+
+static void print_features_leaf7(unsigned int max_leaf)
+{
+	unsigned int eax7, ebx7, ecx7, edx7;
+	int any = 0;
+	if (max_leaf < 7)
+		return;
+	cpuid(7, 0, &eax7, &ebx7, &ecx7, &edx7);
+	(void)eax7;
+	(void)ecx7;
+	(void)edx7;
+	io_putstr("ext l7 ");
+	if (ebx7 & (1u << 0))  print_flag("FSGSBASE", &any);
+	if (ebx7 & (1u << 3))  print_flag("BMI1", &any);
+	if (ebx7 & (1u << 5))  print_flag("AVX2", &any);
+	if (ebx7 & (1u << 8))  print_flag("BMI2", &any);
+	if (ebx7 & (1u << 9))  print_flag("ERMS", &any);
+	if (ebx7 & (1u << 18)) print_flag("RDSEED", &any);
+	if (ebx7 & (1u << 19)) print_flag("ADX", &any);
+	if (ebx7 & (1u << 20)) print_flag("SMAP", &any);
+	if (ebx7 & (1u << 23)) print_flag("CLFLUSHOPT", &any);
+	if (ebx7 & (1u << 29)) print_flag("SHA", &any);
+	if (!any)
+		io_putstr("none");
+	io_putchar('\n');
+}
+
+static void print_features_ext(unsigned int max_ext_leaf)
+{
+	unsigned int eax, ebx, ecx, edx;
+	int any = 0;
+	if (max_ext_leaf < 0x80000001u)
+		return;
+	cpuid(0x80000001u, 0, &eax, &ebx, &ecx, &edx);
+	(void)eax;
+	(void)ebx;
+	io_putstr("ext x ");
+	if (edx & (1u << 11)) print_flag("SYSCALL", &any);
+	if (edx & (1u << 20)) print_flag("NX", &any);
+	if (edx & (1u << 27)) print_flag("RDTSCP", &any);
+	if (edx & (1u << 29)) print_flag("LM", &any);
+	if (ecx & (1u << 0))  print_flag("LAHF_LM", &any);
+	if (ecx & (1u << 2))  print_flag("SVM", &any);
+	if (ecx & (1u << 5))  print_flag("ABM", &any);
+	if (ecx & (1u << 6))  print_flag("SSE4A", &any);
+	if (ecx & (1u << 8))  print_flag("PREFETCHW", &any);
+	if (!any)
+		io_putstr("none");
+	io_putchar('\n');
+}
+
+void program_cpuinfo_main(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+	unsigned int ecx1, edx1, ebx1;
+	char vendor[13];
+	char brand[49];
+	unsigned int max_leaf;
+	unsigned int max_ext_leaf;
+	unsigned int family, model, stepping;
+	unsigned int logical_cpus = 1;
+	unsigned int cores = 1;
+	unsigned int threads_per_core = 1;
+	unsigned int start_ms, end_ms, delta_ms;
+	unsigned int t0, t1, delta_cycles;
+	unsigned int mhz_x10;
+	unsigned int has_brand = 0;
+	unsigned int i;
+
+	cpuid(0, 0, &max_leaf, &ebx, &ecx, &edx);
+	((unsigned int *)vendor)[0] = ebx;
+	((unsigned int *)vendor)[1] = edx;
+	((unsigned int *)vendor)[2] = ecx;
+	vendor[12] = '\0';
+	cpuid(0x80000000u, 0, &max_ext_leaf, &ebx, &ecx, &edx);
+
+	io_putstr("vendor ");
+	io_putstr(vendor);
+	io_putchar('\n');
+	io_putstr("cpuid max ");
+	put_uint(max_leaf);
+	io_putstr(" ext max ");
+	put_uint(max_ext_leaf & 0xFFFFu);
+	io_putchar('\n');
+
+	cpuid(1, 0, &eax, &ebx, &ecx, &edx);
+	ebx1 = ebx;
+	ecx1 = ecx;
+	edx1 = edx;
+	stepping = eax & 0xF;
+	model = (eax >> 4) & 0xF;
+	family = (eax >> 8) & 0xF;
+	if (family == 0xF)
+		family += (eax >> 20) & 0xFF;
+	if (family == 0x6 || family == 0xF)
+		model += ((eax >> 16) & 0xF) << 4;
+
+	io_putstr("family ");
+	put_uint(family);
+	io_putstr(" model ");
+	put_uint(model);
+	io_putstr(" stepping ");
+	put_uint(stepping);
+	io_putchar('\n');
+
+	if (max_ext_leaf >= 0x80000004u) {
+		for (i = 0; i < 48; i++)
+			brand[i] = '\0';
+		for (i = 0; i < 3; i++) {
+			unsigned int *dst = (unsigned int *)(brand + i * 16);
+			cpuid(0x80000002u + i, 0, &eax, &ebx, &ecx, &edx);
+			dst[0] = eax;
+			dst[1] = ebx;
+			dst[2] = ecx;
+			dst[3] = edx;
+		}
+		brand[48] = '\0';
+		trim_right_spaces(brand);
+		if (brand[0] != '\0')
+			has_brand = 1;
+	}
+	if (has_brand) {
+		io_putstr("brand ");
+		io_putstr(brand);
+		io_putchar('\n');
+	}
+
+	/* Logical processors package-wide from leaf 1 EBX[23:16]. */
+	logical_cpus = (ebx1 >> 16) & 0xFF;
+	if (logical_cpus == 0)
+		logical_cpus = 1;
+
+	/* Physical core count (best effort across vendors/generations). */
+	if (max_leaf >= 4) {
+		cpuid(4, 0, &eax, &ebx, &ecx, &edx);
+		cores = ((eax >> 26) & 0x3F) + 1;
+	}
+	if (max_ext_leaf >= 0x80000008u) {
+		cpuid(0x80000008u, 0, &eax, &ebx, &ecx, &edx);
+		if ((ecx & 0xFFu) != 0)
+			cores = (ecx & 0xFFu) + 1;
+	}
+	if (cores == 0)
+		cores = 1;
+	if (logical_cpus >= cores && cores != 0)
+		threads_per_core = logical_cpus / cores;
+	if (threads_per_core == 0)
+		threads_per_core = 1;
+
+	io_putstr("topology ");
+	put_uint(logical_cpus);
+	io_putstr(" logical, ");
+	put_uint(cores);
+	io_putstr(" cores, ");
+	put_uint(threads_per_core);
+	io_putstr(" threads/core\n");
+
+	start_ms = get_ticks_ms();
+	t0 = read_tsc_low();
+	do {
+		__asm__ volatile ("hlt");
+		end_ms = get_ticks_ms();
+	} while (end_ms - start_ms < 200);
+	t1 = read_tsc_low();
+	delta_ms = end_ms - start_ms;
+	delta_cycles = t1 - t0;
+	if (delta_ms == 0)
+		delta_ms = 1;
+
+	/* MHz with one decimal: cycles / (ms * 100). */
+	mhz_x10 = delta_cycles / (delta_ms * 100);
+	io_putstr("clock ");
+	put_uint(mhz_x10 / 10);
+	io_putchar('.');
+	put_uint(mhz_x10 % 10);
+	io_putstr(" MHz (estimated)\n");
+
+	print_features_leaf1(ecx1, edx1);
+	print_features_leaf7(max_leaf);
+	print_features_ext(max_ext_leaf);
+}

+ 22 - 1
terminal.c

@@ -16,6 +16,7 @@ extern void program_run_main(const char *filename);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
+extern void program_cpuinfo_main(void);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
 
@@ -174,6 +175,22 @@ static int is_cmd_display(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 7 > line_len)
+		return 0;
+	if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
+	    line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
+		return 0;
+	i += 7;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_space(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -506,6 +523,9 @@ void terminal_run(void)
 					} else if (is_cmd_display(line, line_len)) {
 						if (process_create("display", program_display_main) < 0)
 							io_putstr("error: too many processes\n");
+					} else if (is_cmd_cpuinfo(line, line_len)) {
+						if (process_create("cpuinfo", program_cpuinfo_main) < 0)
+							io_putstr("error: too many processes\n");
 					} else if (is_cmd_drive(line, line_len, &drive_sel)) {
 						disk_set_drive(drive_sel);
 						io_putstr("drive ");
@@ -523,7 +543,8 @@ void terminal_run(void)
 						io_putstr("ls        cat <file>\n");
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
-						io_putstr("display   drive <0|1>\n");
+						io_putstr("display   cpuinfo\n");
+						io_putstr("drive <0|1>\n");
 						io_putstr("ps        kill <pid>\n");
 						io_putstr("help      clear\n");
 					} else if (is_cmd_clear(line, line_len)) {