Refactor main.c funcs into proper places, add cpuid
authorJack Miller <jack@codezen.org>
Sat, 5 Mar 2016 17:03:02 +0000 (11:03 -0600)
committerJack Miller <jack@codezen.org>
Sat, 19 Mar 2016 19:02:48 +0000 (14:02 -0500)
12 files changed:
asm/misc.asm
include/asm/misc.h
include/console.h [new file with mode: 0644]
include/cpuid.h [new file with mode: 0644]
include/kernel.h
include/string.h [new file with mode: 0644]
include/vga.h [new file with mode: 0644]
kernel/console.c [new file with mode: 0644]
kernel/cpuid.c [new file with mode: 0644]
kernel/main.c
kernel/string.c [new file with mode: 0644]
kernel/vga.c [new file with mode: 0644]

index 0de0fca..195168a 100644 (file)
@@ -6,3 +6,17 @@ reset_cr3:
     mov rax, cr3
     mov cr3, rax
     ret
+
+GLOBAL cpuid
+
+cpuid:
+    mov eax, edi
+    cpuid
+    mov [rsi], eax
+    add rsi, 4
+    mov [rsi], ebx
+    add rsi, 4
+    mov [rsi], ecx
+    add rsi, 4
+    mov [rsi], edx
+    ret
index f058a30..2802da7 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef ASM_MISC_H
 #define ASM_MISC_H
 
+#include "types.h"
+
 extern void reset_cr3(void);
+extern void cpuid(u32 eax, u32 *buf);
 
 #endif
diff --git a/include/console.h b/include/console.h
new file mode 100644 (file)
index 0000000..91a9d2a
--- /dev/null
@@ -0,0 +1,4 @@
+#pragma once
+
+void console_init(void);
+void printk(char *format, ...);
diff --git a/include/cpuid.h b/include/cpuid.h
new file mode 100644 (file)
index 0000000..2e3fc66
--- /dev/null
@@ -0,0 +1,3 @@
+#pragma once
+
+int cpuid_init(void);
index 9cbbb7d..d0a152c 100644 (file)
@@ -4,4 +4,6 @@
 #include <memmap.h>
 #include <types.h>
 
+void printk(char *format, ...);
+
 #endif
diff --git a/include/string.h b/include/string.h
new file mode 100644 (file)
index 0000000..9e0639b
--- /dev/null
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <stdarg.h>
+#include <types.h>
+
+void vsnprintf(char *buf, u32 size, char *format, va_list ap);
+int strlen(char *src);
+char *strncpy(char *dest, char *src, u32 size);
+void memcpy(void *dest, void *src, u32 size);
diff --git a/include/vga.h b/include/vga.h
new file mode 100644 (file)
index 0000000..d613274
--- /dev/null
@@ -0,0 +1,5 @@
+#pragma once
+
+void vga_init(void);
+void vga_clear(void);
+void vga_putchar(char c);
diff --git a/kernel/console.c b/kernel/console.c
new file mode 100644 (file)
index 0000000..cf18862
--- /dev/null
@@ -0,0 +1,57 @@
+#include <page_alloc.h>
+#include <memmap.h>
+#include <stdarg.h>
+#include <string.h>
+#include <vga.h>
+
+char *console_mem;
+u32 console_end = 0;
+u32 console_wrapped = 0;
+
+#define CONSOLE_ORDER 1
+#define CONSOLE_SIZE  (1 << (CONSOLE_ORDER + PAGE_SHIFT))
+
+void console_init(void)
+{
+       console_mem = page_alloc(CONSOLE_ORDER);
+}
+
+void console_flush(void)
+{
+
+}
+
+void console_putchar(char c)
+{
+       console_mem[console_end] = c;
+       console_end++;
+
+       if(console_end >= CONSOLE_SIZE) {
+               console_wrapped = 1;
+               console_end = 0;
+       }
+
+       vga_putchar(c);
+}
+
+void console_puts(char *string)
+{
+       int i;
+
+       for (i = 0; string[i]; i++)
+               console_putchar(string[i]);
+}
+
+void printk(char *format, ...)
+{
+       char buf[255];
+       va_list ap;
+
+       va_start(ap, format);
+
+       vsnprintf(buf, 255, format, ap);
+
+       va_end(ap);
+
+       console_puts(buf);
+}
diff --git a/kernel/cpuid.c b/kernel/cpuid.c
new file mode 100644 (file)
index 0000000..1a38e32
--- /dev/null
@@ -0,0 +1,40 @@
+/* vim: set ts=4 sw=4 sts=4 noet : */
+
+#include <kernel.h>
+
+#include "asm/misc.h"
+
+int cpuid_init(void)
+{
+       char vendor[13];
+       u32 regs[4];
+
+    cpuid(0x0, regs);
+
+       /* regs[0] - eax
+        * regs[1] - ebx
+        * regs[2] - ecx
+        * regs[3] - edx
+        */
+
+       vendor[0] = ((char *) &regs[1])[0];
+       vendor[1] = ((char *) &regs[1])[1];
+       vendor[2] = ((char *) &regs[1])[2];
+       vendor[3] = ((char *) &regs[1])[3];
+
+       vendor[4] = ((char *) &regs[3])[0];
+       vendor[5] = ((char *) &regs[3])[1];
+       vendor[6] = ((char *) &regs[3])[2];
+       vendor[7] = ((char *) &regs[3])[3];
+
+       vendor[8] = ((char *) &regs[2])[0];
+       vendor[9] = ((char *) &regs[2])[1];
+       vendor[10] = ((char *) &regs[2])[2];
+       vendor[11] = ((char *) &regs[2])[3];
+
+       vendor[12] = 0;
+
+       printk("%s\n", vendor);
+
+    return 0;
+}
index 1972ec2..ee508c4 100644 (file)
@@ -1,245 +1,9 @@
 /* vim: set ts=4 sw=4 sts=4 noet : */
 
-#include <kernel.h>
 #include <page_alloc.h>
-#include <stdarg.h>
-#include <vmalloc.h>
-#include <memmap.h>
-#include <map.h>
-
-char *vga_mem = NULL;
-u8 vga_x = 0;
-u8 vga_y = 0;
-
-u8 vga_max_x = 80;
-u8 vga_max_y = 25;
-
-void memcpy(void *dest, void *src, u32 size)
-{
-       int i;
-       for (i = 0; i < size; i++)
-               ((char *)dest)[i] = ((char *)src)[i];
-}
-
-void vga_putchar(char c)
-{
-       if ((c == '\n') || (vga_x >= vga_max_x))
-       {
-               vga_x = 0;
-               vga_y++;
-               return;
-       }
-
-       if (vga_y >= vga_max_y) {
-               memcpy(vga_mem, &vga_mem[vga_max_x], (vga_max_x * (vga_max_y - 1)));
-               vga_y--;
-       }
-
-       vga_mem[((vga_max_x * vga_y) + vga_x) * 2] = c;
-       vga_x++;
-}
-
-void vga_clear(void)
-{
-       int i;
-       for(i = 0; i < (vga_max_x * vga_max_y); i++) {
-               vga_mem[i * 2] = ' ';
-               vga_mem[(i * 2) + 1] = 0xf;
-       }
-}
-
-void vga_init(void)
-{
-       vga_mem = vmalloc("VGA mem", VGA_MEM_VIRTUAL, PAGE_SIZE);
-       map_page((u64) vga_mem, 0xB8000);
-       vga_clear();
-}
-
-char *console_mem;
-u32 console_end = 0;
-u32 console_wrapped = 0;
-
-#define CONSOLE_ORDER 1
-#define CONSOLE_SIZE  (1 << (CONSOLE_ORDER + PAGE_SHIFT))
-
-void console_init(void)
-{
-       console_mem = page_alloc(CONSOLE_ORDER);
-}
-
-void console_flush(void)
-{
-
-}
-
-void console_putchar(char c)
-{
-       console_mem[console_end] = c;
-       console_end++;
-
-       if(console_end >= CONSOLE_SIZE) {
-               console_wrapped = 1;
-               console_end = 0;
-       }
-
-       vga_putchar(c);
-}
-
-void console_puts(char *string)
-{
-       int i;
-
-       for (i = 0; string[i]; i++)
-               console_putchar(string[i]);
-}
-
-char *strncpy(char *dest, char *src, u32 size)
-{
-       int i;
-       for(i = 0; i < size && src[i]; i++)
-               dest[i] = src[i];
-       if (i < size)
-               src[i] = 0;
-       return dest;
-}
-
-int strlen(char *src)
-{
-       int i = 0;
-       while (src[i])
-               i++;
-       return i;
-}
-
-/* Convert a single value into ASCII */
-
-static int convert(char *buf, u32 rem, s32 fieldwidth, unsigned long long val, u32 base)
-{
-       char conv[] = "0123456789abcdef";
-       /* Largest possible ull in decimal is 39 digits */
-       char tmp[39];
-       int digits = 0;
-       int i = 0;
-
-       if (fieldwidth > -1 && fieldwidth < rem)
-               rem = fieldwidth;
-
-       /* Write the digits into tmp, backwards because it's easiest to calculate
-        * the smallest digits first */
-
-       do {
-               tmp[digits] = conv[val % base];
-               val /= base;
-               digits++;
-       } while (val > 0);
-
-       /* Pad out tmp to fieldwidth */
-
-       i = 0;
-       if (fieldwidth != -1) {
-               for(i = 0; i < (rem - digits); i++)
-                       buf[i] = '0';
-       }
-
-       /* Then reverse the digits into buf */
-
-       for( ; i <= (digits - 1) && rem; rem--, i++)
-               buf[i] = tmp[(digits - 1) - i];
-
-       return i;
-}
-
-void vsnprintf(char *buf, u32 size, char *format, va_list ap)
-{
-       char *str = buf;
-       char qualifier[2];
-       char *s;
-
-       unsigned long long val;
-       s32 fieldwidth = -1;
-       u32 escaped = 0;
-       u32 tmp;
-       u32 i;
-
-       /* size - 1, so we have room for the null byte regardless */
-
-       for(i = 0; i < (size - 1) && format[i]; i++) {
-               if (format[i] == '%') {
-                       if (escaped) {
-                               *str++ = '%';
-                               escaped = 0;
-                       }
-                       else
-                               escaped = 1;
-                       continue;
-               }
-
-               if (escaped) {
-                       switch(format[i]) {
-                               case 's': s = va_arg(ap, char *);
-                                                 if(fieldwidth == -1)
-                                                         fieldwidth = (size - i);
-                                                 while ((fieldwidth--) && (*s)) {
-                                                         *str++ = *s++;
-                                                         size--;
-                                                 }
-
-                                                 fieldwidth = -1;
-                                                 escaped = 0;
-                                                 size++;
-                                                 break;
-                               case 'l': if (qualifier[0] == 'l')
-                                                         qualifier[1] = 'l';
-                                                 else {
-                                                         qualifier[0] = 'l';
-                                                         qualifier[1] = 0;
-                                                 }
-
-                                                 size++;
-                                                 break;
-                               case 'd':
-                               case 'x': if (qualifier[0] == 'l') {
-                                                        if(qualifier[1] == 'l')
-                                                               val = va_arg(ap, unsigned long long);
-                                                        else
-                                                               val = va_arg(ap, unsigned long);
-                                                 } else {
-                                                         val = va_arg(ap, unsigned int);
-                                                 }
-                                                 if (format[i] == 'x')
-                                                         tmp = convert(str, (size - i), fieldwidth, val, 16);
-                                                 else
-                                                         tmp = convert(str, (size - i), fieldwidth, val, 10);
-                                                 str += tmp;
-
-                                                 /* tmp were printed, but the format string shouldn't
-                                                  * count against size */
-
-                                                 size -= (tmp - 1);
-
-                                                 escaped = 0;
-                                                 break;
-                       }
-               }
-               else
-                       *str++ = format[i];
-       }
-       *str = 0;
-}
-
-void printk(char *format, ...)
-{
-       char buf[255];
-       va_list ap;
-
-       va_start(ap, format);
-
-       vsnprintf(buf, 255, format, ap);
-
-       va_end(ap);
-
-       console_puts(buf);
-}
+#include <console.h>
+#include <cpuid.h>
+#include <vga.h>
 
 void main (void *info_phys, unsigned long end_structures)
 {
@@ -251,5 +15,7 @@ void main (void *info_phys, unsigned long end_structures)
 
        printk("Viridis %d.%d-g%s\n", __MAJOR_VERSION__,__MINOR_VERSION__, __GIT_VERSION__);
 
+       cpuid_init();
+
        asm ("hlt" : : );
 }
diff --git a/kernel/string.c b/kernel/string.c
new file mode 100644 (file)
index 0000000..52e62e5
--- /dev/null
@@ -0,0 +1,143 @@
+#include <types.h>
+#include <stdarg.h>
+
+void memcpy(void *dest, void *src, u32 size)
+{
+       int i;
+       for (i = 0; i < size; i++)
+               ((char *)dest)[i] = ((char *)src)[i];
+}
+
+char *strncpy(char *dest, char *src, u32 size)
+{
+       int i;
+       for(i = 0; i < size && src[i]; i++)
+               dest[i] = src[i];
+       if (i < size)
+               src[i] = 0;
+       return dest;
+}
+
+int strlen(char *src)
+{
+       int i = 0;
+       while (src[i])
+               i++;
+       return i;
+}
+
+/* Convert a single value into ASCII */
+
+static int convert(char *buf, u32 rem, s32 fieldwidth, unsigned long long val, u32 base)
+{
+       char conv[] = "0123456789abcdef";
+       /* Largest possible ull in decimal is 39 digits */
+       char tmp[39];
+       int digits = 0;
+       int i = 0;
+
+       if (fieldwidth > -1 && fieldwidth < rem)
+               rem = fieldwidth;
+
+       /* Write the digits into tmp, backwards because it's easiest to calculate
+        * the smallest digits first */
+
+       do {
+               tmp[digits] = conv[val % base];
+               val /= base;
+               digits++;
+       } while (val > 0);
+
+       /* Pad out tmp to fieldwidth */
+
+       i = 0;
+       if (fieldwidth != -1) {
+               for(i = 0; i < (rem - digits); i++)
+                       buf[i] = '0';
+       }
+
+       /* Then reverse the digits into buf */
+
+       for( ; i <= (digits - 1) && rem; rem--, i++)
+               buf[i] = tmp[(digits - 1) - i];
+
+       return i;
+}
+
+void vsnprintf(char *buf, u32 size, char *format, va_list ap)
+{
+       char *str = buf;
+       char qualifier[2];
+       char *s;
+
+       unsigned long long val;
+       s32 fieldwidth = -1;
+       u32 escaped = 0;
+       u32 tmp;
+       u32 i;
+
+       /* size - 1, so we have room for the null byte regardless */
+
+       for(i = 0; i < (size - 1) && format[i]; i++) {
+               if (format[i] == '%') {
+                       if (escaped) {
+                               *str++ = '%';
+                               escaped = 0;
+                       }
+                       else
+                               escaped = 1;
+                       continue;
+               }
+
+               if (escaped) {
+                       switch(format[i]) {
+                               case 's': s = va_arg(ap, char *);
+                                                 if(fieldwidth == -1)
+                                                         fieldwidth = (size - i);
+                                                 while ((fieldwidth--) && (*s)) {
+                                                         *str++ = *s++;
+                                                         size--;
+                                                 }
+
+                                                 fieldwidth = -1;
+                                                 escaped = 0;
+                                                 size++;
+                                                 break;
+                               case 'l': if (qualifier[0] == 'l')
+                                                         qualifier[1] = 'l';
+                                                 else {
+                                                         qualifier[0] = 'l';
+                                                         qualifier[1] = 0;
+                                                 }
+
+                                                 size++;
+                                                 break;
+                               case 'd':
+                               case 'x': if (qualifier[0] == 'l') {
+                                                        if(qualifier[1] == 'l')
+                                                               val = va_arg(ap, unsigned long long);
+                                                        else
+                                                               val = va_arg(ap, unsigned long);
+                                                 } else {
+                                                         val = va_arg(ap, unsigned int);
+                                                 }
+                                                 if (format[i] == 'x')
+                                                         tmp = convert(str, (size - i), fieldwidth, val, 16);
+                                                 else
+                                                         tmp = convert(str, (size - i), fieldwidth, val, 10);
+                                                 str += tmp;
+
+                                                 /* tmp were printed, but the format string shouldn't
+                                                  * count against size */
+
+                                                 size -= (tmp - 1);
+
+                                                 escaped = 0;
+                                                 break;
+                       }
+               }
+               else
+                       *str++ = format[i];
+       }
+       *str = 0;
+}
diff --git a/kernel/vga.c b/kernel/vga.c
new file mode 100644 (file)
index 0000000..c44fc0e
--- /dev/null
@@ -0,0 +1,45 @@
+#include <vmalloc.h>
+#include <memmap.h>
+#include <string.h>
+#include <map.h>
+
+char *vga_mem = NULL;
+u8 vga_x = 0;
+u8 vga_y = 0;
+
+u8 vga_max_x = 80;
+u8 vga_max_y = 25;
+
+void vga_putchar(char c)
+{
+       if ((c == '\n') || (vga_x >= vga_max_x))
+       {
+               vga_x = 0;
+               vga_y++;
+               return;
+       }
+
+       if (vga_y >= vga_max_y) {
+               memcpy(vga_mem, &vga_mem[vga_max_x], (vga_max_x * (vga_max_y - 1)));
+               vga_y--;
+       }
+
+       vga_mem[((vga_max_x * vga_y) + vga_x) * 2] = c;
+       vga_x++;
+}
+
+void vga_clear(void)
+{
+       int i;
+       for(i = 0; i < (vga_max_x * vga_max_y); i++) {
+               vga_mem[i * 2] = ' ';
+               vga_mem[(i * 2) + 1] = 0xf;
+       }
+}
+
+void vga_init(void)
+{
+       vga_mem = vmalloc("VGA mem", VGA_MEM_VIRTUAL, PAGE_SIZE);
+       map_page((u64) vga_mem, 0xB8000);
+       vga_clear();
+}