Skip to content

Commit

Permalink
early printk
Browse files Browse the repository at this point in the history
  • Loading branch information
BitInit committed Jan 31, 2024
1 parent 8fe3b46 commit df16823
Show file tree
Hide file tree
Showing 12 changed files with 565 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export TARGET_ISO=HelloOS.iso
export CONFIG_x86_64=y
export CC=gcc
export AS=as
export LD=ld
export HIDE=@
export DEBUG=-g

Expand All @@ -18,10 +19,10 @@ cleanall:
$(HIDE)make -C arch cleanall

run:
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -nographic
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -curses

debug:
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -s -S -nographic
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -s -S -curses

kill:
@ps aux | grep [q]emu | awk '{print $$2}' | xargs kill
2 changes: 1 addition & 1 deletion arch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $(TARGET_ISO): clean $(kernel_target)
$(HIDE)rm -rf build_dir

$(kernel_target): compile
$(HIDE)ld -b elf64-x86-64 -z muldefs -o $(kernel_target) -Map=./kernel.map -T $(lds) $(shell find $(SRCS) -name "*.o")
$(HIDE)$(LD) -b elf64-x86-64 -z muldefs -o $(kernel_target) -Map=./kernel.map -T $(lds) $(shell find $(SRCS) -name "*.o")

compile:
$(HIDE)for dir in $(SRCS); do \
Expand Down
34 changes: 29 additions & 5 deletions arch/x86/kernel/early_printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,37 @@
#include <os/kernel.h>

#define VGABASE (PAGE_OFFSET + 0xb8000)
static long max_ypos = 25, max_xpos = 80;
static long current_ypos = 25, current_xpos;

void early_vga_write(struct console *con, const char *str, unsigned n) {
unsigned short *dst = (unsigned short*)VGABASE;
for (int i = 0; i < 1000; i++) {
writew((0x7 << 8) | (unsigned short) 'h', dst);
dst++;
}
char c;
int i, k, j;

while ((c = *str++) != '\0' && n-- > 0) {
// 向上滚动一行
if (current_ypos >= max_ypos) {
for (k = 1, j = 0; k < max_ypos; k++, j++) {
for (i = 0; i < max_xpos; i++) {
writew(readw((void*)(VGABASE+2*(max_xpos*k+i))), (void*)(VGABASE + 2*(max_xpos*j + i)));
}
}
// 将最后一行设置空
for (i = 0; i < max_xpos; i++)
writew(0x720, (void*)(VGABASE + 2*(max_xpos*j + i)));
current_ypos = max_ypos-1;
}
if (c == '\n') {
current_xpos = 0;
current_ypos++;
} else if (c != '\r') {
writew(((0x7 << 8) | (unsigned short) c), (void*)(VGABASE + 2*(max_xpos*current_ypos + current_xpos++)));
if (current_xpos >= max_xpos) {
current_xpos = 0;
current_ypos++;
}
}
}
}

static struct console early_vga_console = {
Expand Down
9 changes: 6 additions & 3 deletions arch/x86/kernel/head64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
#include <os/kernel.h>

void clear_bss() {
// memset(__bss_start, 0,
// (unsigned long) __bss_stop - (unsigned long) __bss_start);
memset(__bss_start, 0,
(unsigned long) __bss_stop - (unsigned long) __bss_start);
}


void x86_64_start_kernel() {
early_printk("hello world");
early_printk("x86_64_start_kernel running...");

// bss 段清零
clear_bss();

start_kernel();
}
14 changes: 14 additions & 0 deletions include/asm/div64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _ASM_DIV64_H
#define _ASM_DIV64_H

#include <os/types.h>

# define do_div(n,base) ({ \
uint32_t __base = (base); \
uint32_t __rem; \
__rem = ((uint64_t)(n)) % __base; \
(n) = ((uint64_t)(n)) / __base; \
__rem; \
})

#endif
2 changes: 2 additions & 0 deletions include/asm/posix_types_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
typedef unsigned long __kernel_size_t;
typedef long __kernel_ssize_t;

typedef long __kernel_ptrdiff_t;

#endif
12 changes: 12 additions & 0 deletions include/os/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _OS_CTYPE_H
#define _OS_CTYPE_H

static inline int isdigit(char c) {
return c >= '0' && c <= '9';
}

static inline int isalnum(char c) {
return c >= 'a' && c <= 'z' || (c >= 'A' && c <= 'Z');
}

#endif
3 changes: 3 additions & 0 deletions include/os/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ extern void __attribute__((format(printf, 1, 2)))
extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
__attribute__ ((format (printf, 3, 0)));

extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
__attribute__ ((format (printf, 3, 0)));

#endif
3 changes: 3 additions & 0 deletions include/os/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
#ifndef __HAVE_ARCH_MEMSET
extern void *memset(void *,int,__kernel_size_t);
#endif
#ifndef __HAVE_ARCH_STRNLEN
extern __kernel_size_t strnlen(const char *,__kernel_size_t);
#endif

#endif
10 changes: 10 additions & 0 deletions include/os/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;
#endif

#ifndef _PTRDIFF_T
#define _PTRDIFF_T
typedef __kernel_ptrdiff_t ptrdiff_t;
#endif

typedef __u8 uint8_t;
typedef __u16 uint16_t;
typedef __u32 uint32_t;
typedef __u64 uint64_t;

#endif
13 changes: 11 additions & 2 deletions lib/string.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#include <os/string.h>

#ifndef __HAVE_ARCH_MEMSET
void *memset(void *s, int c, size_t count)
{
void *memset(void *s, int c, size_t count) {
char *xs = s;

while (count--)
*xs++ = c;
return s;
}
#endif

#ifndef __HAVE_ARCH_STRNLEN
__kernel_size_t strnlen(const char *s, __kernel_size_t count) {
const char *sc;

for (sc = s; count-- && *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
#endif
Loading

0 comments on commit df16823

Please sign in to comment.