Skip to content

Commit

Permalink
Merge pull request #73 from bassaer/develop
Browse files Browse the repository at this point in the history
[ver 1.15.1] fix context switch
  • Loading branch information
bassaer committed Mar 11, 2020
2 parents f9a7141 + b9c6278 commit 4caeb9f
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## ver 1.15.1
- fix context switch

## ver 1.15.0
- use linear list for timer

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ KERN_OBJ = kernel/main.o \
kernel/console.o \
kernel/intr.o \
kernel/keyboard.o \
kernel/sched.o \
kernel/timer.o

LIB_OBJ = lib/queue.o \
Expand Down
5 changes: 3 additions & 2 deletions bin/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <mm/memory.h>
#include <lib/string.h>

#define MB (1024*1024)

int free() {
char *fmt =
"total : %dMB\n"
Expand All @@ -16,7 +18,6 @@ int free() {
};
stats(&mem);

int mb = 1024*1024;
printf(fmt, mem.total_bytes/mb, mem.used_bytes/mb, mem.free_bytes/mb);
printf(fmt, mem.total_bytes/MB, mem.used_bytes/MB, mem.free_bytes/MB);
return EXIT_SUCCESS;
}
25 changes: 25 additions & 0 deletions bin/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include <console.h>
#include <exit.h>
#include <io.h>
#include <sched.h>
#include <tss.h>
#include <timer.h>
#include <lib/queue.h>
#include <lib/string.h>
#include <mm/memory.h>

void usage_sleep() {
printf("usage : sleep <timeout>\n");
Expand Down Expand Up @@ -33,13 +36,35 @@ int sleep(char *args[], int size) {
int timeout = atoi(args[1]) * 100;
set_timer(timer, timeout);

tss_t tss2;
tss2.ldtr = 0;
tss2.iomap = 0x40000000;
tss2.eip = (int) &task2_main;
tss2.eax = 0;
tss2.ecx = 0;
tss2.edx = 0;
tss2.ebx = 0;
tss2.esp = (int)alloc_single_block();
tss2.ebp = 0;
tss2.esi = 0;
tss2.edi = 0;
tss2.es = 1 * 8;
tss2.cs = 2 * 8;
tss2.ss = 1 * 8;
tss2.ds = 1 * 8;
tss2.fs = 1 * 8;
tss2.gs = 1 * 8;

debug("tss2 = %x", (int *)tss2.eip);

while(1) {
// 割り込み無効化
io_cli();
if (queue_status(&queue) != 0) {
// 割り込み有効化 + HLT
io_stihlt();
} else {
context_switch(tss2.eip);
// 割り込み有効化
io_sti();
break;
Expand Down
14 changes: 14 additions & 0 deletions include/dsctbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@
*/
#define AR_INTGATE32 0x008e

/**
* システム専用セグメント属性
* R: OK
* W: OK
* X: NG
*/
#define AR_DATA32_RW 0x4092

/**
* システム専用セグメント属性
* R: OK
* W: NG
* X: OK
*/
#define AR_CODE32_ER 0x409a
#define AR_TSS32 0x0089

#define KERN_ADDR 0x00000000
#define KERN_LIMIT 0xffffffff
Expand Down
15 changes: 13 additions & 2 deletions include/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ void io_cli(void);
* GDTR(global descriptor table register) : GDT設定用レジスタ
* GDT : 大域セグメント記述子, セグメンテーションを割り当てたテーブル
*/
void load_gdtr(int limit, int addr);
void set_gdtr(int limit, int addr);

/**
* IDTR(interrupt descriptor table register) : IDT設定用レジスタ
* IDT : 割り込みと割り込みハンドラの対応テーブル
*/
void load_idtr(int limit, int addr);
void set_idtr(int limit, int addr);

/**
* ポート指定でのレジスタ書き込み
Expand Down Expand Up @@ -71,4 +71,15 @@ int load_cr0(void);
*/
void store_cr0(int cr0);

/**
* TRは現在実行中のタスクを記憶するためのレジスタ
* ltrはTRを変更するための命令
*/
void set_tr(int tr);

/**
* 指定のアドレスにロングjumpする
*/
void context_switch();

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

void init_sched();

void set_tr(int tr);

void context_switch(int addr);

void task2_main();

#endif
57 changes: 57 additions & 0 deletions include/tss.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef MYOS_TSS_H
#define MYOS_TSS_H

/**
* 32bit TSS(Task Status Segment)
*/
typedef struct {
/**
* タスク設定の内容
*/
int backlink;
int esp0;
int ss0;
int esp1;
int ss1;
int esp2;
int ss2;
int cr3;

/**
* 32bitレジスタ
*/
int eip;
int eflags;
int eax;
int ecx;
int edx;
int ebx;
int esp;
int ebp;
int esi;
int edi;

/**
* 16bitレジスタ
*/
int es;
int cs;
int ss;
int ds;
int fs;
int gs;

/**
* タスク設定の内容
*
* 不正な値が入るとコンテキストスイッチが動作しないので
* 以下の初期値を設定する
* idtr = 0
* iomap = 0x400000000
*/
int ldtr;
int iomap;
} tss_t;

#endif

7 changes: 4 additions & 3 deletions kernel/dsctbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ void init_gdtidt() {
// メモリ全体
set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, AR_DATA32_RW);
// カーネル
set_segmdesc(gdt + 2, KERN_LIMIT, KERN_ADDR, AR_CODE32_ER);
load_gdtr(GDT_LIMIT, GDT_ADDR);
set_segmdesc(gdt + 2, 0xffffffff, 0x00000000, AR_CODE32_ER);
// GDTのリミットと番地をGDTR(48bitレジスタ)に設定
set_gdtr(GDT_LIMIT, GDT_ADDR);

for (i = 0; i <= IDT_LIMIT / 8; i++) {
set_gatedesc(idt + i, 0, 0, 0);
}
load_idtr(IDT_LIMIT, IDT_ADDR);
set_idtr(IDT_LIMIT, IDT_ADDR);

// IDTの設定
// 2*8 -> 2番目のセグメント
Expand Down
15 changes: 12 additions & 3 deletions kernel/func.s
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.code32
.global io_cli, io_sti, io_stihlt, io_hlt
.global load_gdtr, load_idtr, outb_p, io_in
.global set_gdtr, set_idtr, outb_p, io_in
.global asm_handle_intr20, asm_handle_intr21, asm_handle_intr27
.global io_load_eflags, io_store_eflags, load_cr0, store_cr0
.global set_tr, context_switch
.extern handle_intr20, handle_intr21, handle_intr27
.text

Expand All @@ -23,13 +24,13 @@ io_cli:
cli
ret

load_gdtr:
set_gdtr:
movw 4(%esp), %ax
movw %ax, 6(%esp)
lgdt 6(%esp)
ret

load_idtr:
set_idtr:
movw 4(%esp), %ax
movw %ax, 6(%esp)
lidt 6(%esp)
Expand Down Expand Up @@ -113,3 +114,11 @@ asm_handle_intr27:
pop %ds
pop %es
iret

set_tr:
ltr 4(%esp)
ret

context_switch:
ljmp *4(%esp)
ret
2 changes: 2 additions & 0 deletions kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <intr.h>
#include <io.h>
#include <keyboard.h>
#include <sched.h>
#include <timer.h>
#include <mm/memory.h>
#include <sh.h>
Expand All @@ -34,6 +35,7 @@ int main(void) {
init_keyboard(&queue);
init_mem_info();
init_shell();
init_sched();

while(1) {
io_cli(); // 割り込み無効化
Expand Down
38 changes: 38 additions & 0 deletions kernel/sched.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <sched.h>

#include <console.h>
#include <io.h>
#include <tss.h>
#include <dsctbl.h>
#include <mm/memory.h>

void init_sched() {
/*
tss_t tss1;
tss1.ldtr = 0;
tss1.iomap = 0x40000000;
*/


//segment_descriptor_t *gdt = (segment_descriptor_t *) GDT_ADDR;

//set_segmdesc(gdt + 3, 103, (int) &tss1, AR_TSS32);
//set_segmdesc(gdt + 4, 103, (int) &tss2, AR_TSS32);

//set_segmdesc(gdt + 3, 0xffffffff, 0x00000000, AR_TSS32);
//set_segmdesc(gdt + 4, 0xffffffff, 0x00000000, AR_TSS32);

//set_gdtr(GDT_LIMIT, GDT_ADDR);

//set_tr(3 * 8);
}

void task2_main() {
debug("task2_main!!");
/*
while(1) {
io_hlt();
}
*/
}

2 changes: 0 additions & 2 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ int init_pg_table() {
int map_page(unsigned long paddr, unsigned long vaddr) {
page_directory_entry *curr_pd = get_curr_pd();
if (curr_pd == NULL) {
debug("error at 94");
return MEM_ERROR;
}
page_directory_entry *pde = get_pde(curr_pd, vaddr);
Expand All @@ -107,7 +106,6 @@ int map_page(unsigned long paddr, unsigned long vaddr) {
} else {
pg_table = (page_table_entry *)alloc_single_block();
if (pg_table == NULL) {
debug("error at 102");
return MEM_ERROR;
}
//.ブロック初期化
Expand Down
4 changes: 2 additions & 2 deletions mm/pgtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ page_table_entry get_pde_ptaddr(page_directory_entry *pd) {
}

void set_pd(page_directory_entry *pde) {
__asm__("movl %%cr3, %0"::"r"(pde));
__asm__ volatile("movl %%cr3, %0"::"r"(pde));
}

void enable_paging() {
__asm__(
__asm__ volatile(
"pushl %eax;"
"movl %cr0, %eax;"
"OR 0x80000000, %eax;"
Expand Down

0 comments on commit 4caeb9f

Please sign in to comment.