Skip to content

Commit

Permalink
memory paging
Browse files Browse the repository at this point in the history
  • Loading branch information
wxggg committed Oct 13, 2017
1 parent 5c358d9 commit 8c91aa0
Show file tree
Hide file tree
Showing 88 changed files with 52 additions and 6,985 deletions.
12 changes: 6 additions & 6 deletions kern/font/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <graphic.h>
#include <font.h>

static uint8_t* p_font_asc16_b = (uint8_t*)(FONT_ASC16_ADDR);
static uint8_t* p_font_asc16_b = (uint8_t*)(FONT_ASC16_ADDR+KERNBASE);

BOOL draw_asc16(char ch, point_t point, rgb_t c)
{
uint8_t * p_asc = p_font_asc16_b + (uint8_t)ch * 16;
uint8_t * p_asc = p_font_asc16_b + (uint8_t)ch * 16;

for(int y=0; y<ASC16_HEIGHT; y++){
uint8_t testbit = 1 << 7;
for(int x=0; x<ASC16_WIDTH; x++)
{
for(int x=0; x<ASC16_WIDTH; x++)
{
if(*p_asc & testbit)
setpixel(point.x+x, point.y+y, c);
setpixel(point.x+x, point.y+y, c);
testbit >>= 1;
}
p_asc ++;
Expand All @@ -31,4 +31,4 @@ BOOL draw_str16(char* ch, point_t point, rgb_t c)
p++;
}
return TRUE;
}
}
8 changes: 4 additions & 4 deletions kern/graphic/bitmap.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <bitmap.h>
#include <types.h>
#include <graphic.h>
#include <kerninfo.h>
#include <kerninfo.h>
#include <math.h>

static bitmap_t* p_bmp_icon = (bitmap_t*)(BMP_ICON_ADDR);
static bitmap_t* p_bmp_icon = (bitmap_t*)(BMP_ICON_ADDR+KERNBASE);

BOOL draw_bitmap(bitmap_t* p_bmp, int x0, int y0)
{
Expand All @@ -18,7 +18,7 @@ BOOL draw_bitmap(bitmap_t* p_bmp, int x0, int y0)
/* 图像的宽、髙 */
int bmp_cx = abs(p_bmp->info_head.bi_width);
int bmp_cy = abs(p_bmp->info_head.bi_height);

int nBpline = (((bmp_cx*p_bmp->info_head.bi_bit_count + 31) >> 5) << 2);

for(int j=0; j<bmp_cy; j++) {
Expand All @@ -35,4 +35,4 @@ BOOL draw_bitmap(bitmap_t* p_bmp, int x0, int y0)
void draw_bmp_test()
{
draw_bitmap(p_bmp_icon, 820, 20);
}
}
21 changes: 6 additions & 15 deletions kern/graphic/graphic.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#include <memlayout.h>
#include <stdio.h>
#include <string.h>
#include <kerninfo.h>

struct BOOTINFO *binfo = (struct BOOTINFO *)(ADR_BOOTINFO);

void draw_editbox(editbox_t eb);

struct BOOTINFO* get_bootinfo() {
struct BOOTINFO * get_bootinfo()
{
return binfo;
}

void draw_editbox(editbox_t eb);

inline BOOL is_pixel_valid(int32_t x, int32_t y)
{
if(x<0 || y<0 || (uint32_t)x >= binfo->scrnx || (uint32_t)y >= binfo->scrny)
Expand All @@ -32,11 +32,6 @@ inline BOOL setpixel(int32_t x, int32_t y, rgb_t c)

int nBppixel = binfo->bitspixel>>3;
uint8_t * pvram = (uint8_t*)(binfo->vram + nBppixel*binfo->scrnx*y + nBppixel*x);
// cprintf("setpixel: pvram:%x\n", *pvram);
// *pvram = c;

// cprintf("wtf:%x ---------------------------\n", *pvram);

*pvram = c.r;
*(pvram+1) = c.g;
*(pvram+2) = c.b;
Expand Down Expand Up @@ -65,7 +60,6 @@ rect_t _gGetScrnRect()

void graphic_init()
{
binfo->vram += KERNBASE;
cprintf("graphic_init\n");
init_screen8();

Expand Down Expand Up @@ -96,11 +90,8 @@ void graphic_init()

void init_screen8()
{
cprintf("wtf binfo:%x\n", binfo);
cprintf("scrnx:%d binfo:%x", binfo->scrnx, binfo);
setpixel(20,30,(rgb_t){0,0xff,0});
_gfillrect2((rgb_t){20,40,100}, (rect_t){0,0,binfo->scrnx,binfo->scrny});
cprintf("wtf");

_gdrawrect((rgb_t){100,100,100}, (rect_t){0, 0, 64, 700});

for(int i=0; i<10; i++)
Expand Down
2 changes: 1 addition & 1 deletion kern/graphic/graphic.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _KERN_GRAPHIC_H
#define _KERN_GRAPHIC_H
#define _KERN_GRAPHIC_H


#include <types.h>
Expand Down
4 changes: 3 additions & 1 deletion kern/init/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#include <kdebug.h>
#include <asm_tool.h>

struct BOOTINFO *binfo = (struct BOOTINFO *) (ADR_BOOTINFO+KERNBASE);

int kern_init(void) __attribute__((noreturn));

int
kern_init(void) {
extern char edata[], end[];
memset(edata, 0, end - edata);
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;

cons_init();

const char *message = "Sharix is loading ...";
Expand Down
5 changes: 4 additions & 1 deletion kern/libs/kerninfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _KERNINFO_H

#include <types.h>
#include <pmm.h>

#define FONT_ASC16_ADDR 0x11000
#define FONT_ASC16_SIZE (4096)
Expand All @@ -19,8 +20,9 @@ struct BOOTINFO
};


#define ADR_BOOTINFO (0x00000000 + KERNBASE)
#define ADR_BOOTINFO 0x00000000
#define BOOTINFO_SIZE (sizeof(BOOTINFO))
extern struct BOOTINFO* binfo;

struct CMD
{
Expand All @@ -31,4 +33,5 @@ struct CMD

};


#endif
39 changes: 16 additions & 23 deletions kern/mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>
#include <kdebug.h>
#include <kerninfo.h>

static struct taskstate ts = {0};

Expand Down Expand Up @@ -82,7 +83,6 @@ static void page_init(void)
// SetPageReserved(pages+i);

uintptr_t freemem = PADDR((uintptr_t)pages + sizeof(struct Page)*npage);
cprintf("freemem:%x", freemem);
for (int i=0; i<memmap->nr_map; ++i) {
begin = memmap->map[i].addr;
free_end = begin + memmap->map[i].size;
Expand All @@ -101,10 +101,6 @@ static void page_init(void)
}
}
}
cprintf("free_end:%x \n", free_end);

cprintf("maxpa:%x \n", maxpa);
cprintf("npage:%d pages:%x", npage, pages);
}

static void check_alloc_page()
Expand Down Expand Up @@ -151,19 +147,19 @@ gdt_init(void) {
ltr(GD_TSS);
}

static void boot_map_segment(uintptr_t *pgdir, uintptr_t la, size_t size, uintptr_t pa, uint32_t perm)
static void map_physical_memory(uintptr_t *pgdir, uintptr_t la, size_t size, uintptr_t pa, uint32_t perm)
{
size_t n = ROUND_UP(size + PG_OFF(la), PGSIZE) /PGSIZE;
la = ROUND_DOWN(la, PGSIZE);
pa = ROUND_DOWN(pa, PGSIZE);
for(; n>0; n--,la+=PGSIZE,pa+=PGSIZE)
{
uintptr_t *pte_p = get_pte(pgdir, la);
// cprintf("ptep:%x *ptep:%x n:%x pa:%x la:%x\n", pte_p, *pte_p, pa, la);

// cprintf("ptep:%x *ptep:%x n:%x pa:%x la:%x\n", pte_p, *pte_p, pa, la);
*pte_p = pa | PTE_P | perm;
}
}

void enable_paging(void)
{
lcr3(boot_cr3);
Expand All @@ -179,28 +175,23 @@ void pmm_init(void)
{
init_pmm_manager();
page_init();

check_alloc_page();

struct Page *p = alloc_page();
boot_pgdir = (uintptr_t*)page2va(p);
memset(boot_pgdir, 0, PGSIZE);
boot_cr3 = PADDR(boot_pgdir);

cprintf("boot_pgdir:%x cr3:%x \n", boot_pgdir, boot_cr3);

check_pgdir();

cprintf("boot_pgdir 0:%x 1:%x 2:%x\n", boot_pgdir[0], boot_pgdir[1], boot_pgdir[2]);

// recursively insert boot_pgdir in itself
// to form a virtual page table at virtual address VPT
boot_pgdir[PDX(VPT)] = PADDR(boot_pgdir) | PTE_P | PTE_W;

boot_map_segment(boot_pgdir, KERNBASE, KMEMSIZE, 0, PTE_W);

//boot_map_segment: map KERNBASE~KERNBASE+KMEMSIZE to 0~KMEMSIZE
map_physical_memory(boot_pgdir, KERNBASE, KMEMSIZE, 0, PTE_W);
//temporary map:
//virtual_addr 3G~3G+4M = linear_addr 0~4M = linear_addr 3G~3G+4M = phy_addr 0~4M
//virtual_addr 3G~3G+4M = linear_addr 0~4M = linear_addr 3G~3G+4M = phy_addr 0~4M
boot_pgdir[0] = boot_pgdir[PDX(KERNBASE)];

enable_paging();
Expand All @@ -210,13 +201,12 @@ void pmm_init(void)

check_boot_pgdir();

// for(int i=0;i<1024;i++) {
// cprintf("pgdir[%d]:%x\n", i, boot_pgdir[i]);
// }
//map vram memory to vram memory
int nBppixel = binfo->bitspixel>>3;
map_physical_memory(boot_pgdir, (uint32_t)binfo->vram, binfo->scrnx*binfo->scrny*nBppixel, (uint32_t)binfo->vram, PTE_W);

print_pgdir();
print_stackframe();

}

struct Page *alloc_pages(size_t n)
Expand All @@ -242,7 +232,7 @@ uintptr_t * get_pte(uintptr_t *pgdir, uintptr_t la)
{
uintptr_t *pde_p = &pgdir[PDX(la)];
if(!(*pde_p & PTE_P)) {
cprintf("alloc-");
// cprintf("la:%x get_pte alloc for pde_p\n", la);
struct Page *page = alloc_page();
uintptr_t pa = page2pa(page);
memset((void*)VADDR(pa), 0, PGSIZE);
Expand Down Expand Up @@ -299,7 +289,6 @@ static void check_pgdir(void)

uintptr_t *pte_p = get_pte(boot_pgdir, 0x0);
cprintf("*pte_p:%x pa2page(*pte_p):%x p1:%x \n", *pte_p, pa2page(*pte_p), p1);
cprintf("wtf\n");
cprintf("pte_p:%x newpte_p:%x\n", pte_p, get_pte(boot_pgdir, 0));

struct Page *p2 = alloc_page();
Expand Down Expand Up @@ -331,7 +320,6 @@ void check_boot_pgdir()
cprintf(" *pte_p:%x \n", *pte_p);
}

cprintf("%x %x\n", PDE_ADDR(boot_pgdir[PDX(VPT)]), PADDR(boot_pgdir));
cprintf("%x\n", boot_pgdir[0]);

struct Page *p;
Expand Down Expand Up @@ -359,6 +347,11 @@ void check_boot_pgdir()
cprintf("check_boot_pgdir() succeeded!\n");
}

void insert_someaddress()
{
// page_insert(boot_pgdir,)

}

//perm2str - use string 'u,r,w,-' to present the permission
static const char *
Expand Down
8 changes: 4 additions & 4 deletions kern/trap/trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ void idt_init(void) {
static void trap_dispatch(struct trapframe *tf) {
char c;
switch(tf->tf_trapno) {
// case T_PGFLT:
// cprintf("pgfault\n");
// break;
case T_PGFLT:
// cprintf("pgfault\n");
break;
case IRQ_OFFSET + IRQ_TIMER:
ticks ++;
if (ticks % TICK_NUM == 0) {
Expand All @@ -52,6 +52,6 @@ static void trap_dispatch(struct trapframe *tf) {
}

void trap(struct trapframe *tf) {
// cprintf("trap");
// cprintf("trap");
trap_dispatch(tf);
}
15 changes: 8 additions & 7 deletions kern/trap/trap.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef __KERN_TRAP_TRAP_H__
#define __KERN_TRAP_TRAP_H__
#pragma once

#include <types.h>

/* Trap Numbers */

/* Processor-defined: */
#define T_PGFLT 14

#define IRQ_TIMER 0
#define IRQ_KBD 1
#define IRQ_COM1 4
Expand All @@ -18,10 +22,10 @@ struct pushregs
uint32_t reg_ebx;
uint32_t reg_edx;
uint32_t reg_ecx;
uint32_t reg_eax;
uint32_t reg_eax;
};

struct trapframe
struct trapframe
{
struct pushregs tf_regs;
uint16_t tf_es;
Expand All @@ -42,6 +46,3 @@ struct trapframe
} __attribute__((packed));

void idt_init(void);

#endif /* !__KERN_TRAP_TRAP_H__ */

Loading

0 comments on commit 8c91aa0

Please sign in to comment.