Skip to content

Commit

Permalink
Merge pull request #69 from bassaer/develop
Browse files Browse the repository at this point in the history
[ver 1.12.0] add sleep command
  • Loading branch information
bassaer committed Mar 2, 2020
2 parents ead04b9 + 3b06433 commit dab39d9
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 15 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.12.0
- add sleep command

## ver 1.11.0
- support paging

Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ KERN_OBJ = kernel/main.o \
kernel/dsctbl.o \
kernel/console.o \
kernel/intr.o \
kernel/keyboard.o
kernel/keyboard.o \
kernel/timer.o

LIB_OBJ = lib/queue.o \
lib/string.o
Expand All @@ -18,7 +19,8 @@ BIN_OBJ = bin/echo.o \
bin/free.o \
bin/ls.o \
bin/sh.o \
bin/shutdown.o
bin/shutdown.o \
bin/sleep.o

ARCH = arch/x86/boot
.PHONY: install img run package clean
Expand Down
6 changes: 6 additions & 0 deletions bin/include/sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MYOS_SLEEP_H
#define MYOS_SLEEP_H

int sleep(char *args[], int size);

#endif
3 changes: 3 additions & 0 deletions bin/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <keyboard.h>
#include <ls.h>
#include <shutdown.h>
#include <sleep.h>
#include <lib/queue.h>
#include <lib/string.h>

Expand Down Expand Up @@ -169,6 +170,8 @@ void exec_cmd() {
exit_status = free();
} else if (strcmp(cmd, "shutdown") == 0 || strcmp(cmd, "exit") == 0) {
exit_status = shutdown(args, split_count);
} else if(strcmp(cmd, "sleep") == 0) {
exit_status = sleep(args, split_count);
} else if (strcmp(cmd, "ls") == 0) {
exit_status = ls();
} else {
Expand Down
41 changes: 41 additions & 0 deletions bin/sleep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <sleep.h>

#include <console.h>
#include <exit.h>
#include <io.h>
#include <timer.h>
#include <lib/queue.h>
#include <lib/string.h>

void usage_sleep() {
printf("usage : sleep <timeout>\n");
}


int sleep(char *args[], int size) {
if (size != 2) {
usage_sleep();
return EXIT_FAILURE;
}

struct Queue queue;
unsigned char buf[8];
init_queue(&queue, 8, buf);
int timeout = atoi(args[1]) * 100;

set_timer(timeout, &queue, 1);

while(1) {
// 割り込み無効化
io_cli();
if (queue_status(&queue) != 0) {
// 割り込み有効化 + HLT
io_stihlt();
} else {
// 割り込み有効化
io_sti();
break;
}
}
return EXIT_SUCCESS;
}
5 changes: 2 additions & 3 deletions include/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ int io_in(int port);
/**
* 割り込みハンドラ
*/
void asm_handle_intr(void);
void asm_handle_intr20(void);
void asm_handle_intr21(void);
void asm_handle_intr27(void);

/**
Expand All @@ -70,6 +71,4 @@ int load_cr0(void);
*/
void store_cr0(int cr0);

void foobar(void);

#endif
2 changes: 2 additions & 0 deletions include/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ int split(char *src, char *dst[], char delim);
*/
void strcpy(char *src, char *dist);

int atoi(char *src);

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

#include <lib/queue.h>

struct timer_ctrl {
unsigned int count;
unsigned int timeout;
struct Queue *queue;
unsigned char data;
} timer;

void init_pit();

void handle_intr20(int *esp);

void set_timer(unsigned int timeout, struct Queue *queue, unsigned char data);

#endif
4 changes: 3 additions & 1 deletion kernel/dsctbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ void init_gdtidt() {
// IDTの設定
// 2*8 -> 2番目のセグメント
// セグメント番号の下位3bitを0にするため8倍
// タイマーはIRQ0なのでINT 0x20
// キーボードはIRQ1なのでINT 0x21
// 割り込み発生でasm_handle_intrをcall
set_gatedesc(idt + 0x21, (int) asm_handle_intr, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x20, (int) asm_handle_intr20, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x21, (int) asm_handle_intr21, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x27, (int) asm_handle_intr27, 2 * 8, AR_INTGATE32);
}

Expand Down
24 changes: 20 additions & 4 deletions kernel/func.s
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.code32
.global io_cli, io_sti, io_stihlt, io_hlt
.global load_gdtr, load_idtr, outb_p, io_in
.global asm_handle_intr, asm_handle_intr27
.global asm_handle_intr20, asm_handle_intr21, asm_handle_intr27
.global io_load_eflags, io_store_eflags, load_cr0, store_cr0
.extern handle_keyboard, handle_intr27
.extern handle_intr20, handle_intr21, handle_intr27
.text

io_hlt:
Expand Down Expand Up @@ -66,7 +66,7 @@ io_in:
inb %dx, %al
ret

asm_handle_intr:
asm_handle_intr20:
push %es
push %ds
pusha
Expand All @@ -75,7 +75,23 @@ asm_handle_intr:
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call handle_keyboard
call handle_intr20
pop %eax
popa
pop %ds
pop %es
iret

asm_handle_intr21:
push %es
push %ds
pusha
mov %esp, %eax
push %eax
mov %ss, %ax
mov %ax, %ds
mov %ax, %es
call handle_intr21
pop %eax
popa
pop %ds
Expand Down
2 changes: 1 addition & 1 deletion kernel/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void init_keyboard(struct Queue *q) {
* 割り込みハンドラ
* 入力をバッファに, 空き状態をフラグで保持
*/
void handle_keyboard(int *esp) {
void handle_intr21(int *esp) {
outb_p(PIC0_OCW2, 0x61); // IRQ-01受付完了をPICに通知
unsigned char code = io_in(PORT_KEYDAT);
enqueue(queue, code);
Expand Down
10 changes: 6 additions & 4 deletions kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <intr.h>
#include <io.h>
#include <keyboard.h>
#include <timer.h>
#include <mm/memory.h>
#include <sh.h>
#include <lib/queue.h>
Expand All @@ -24,16 +25,17 @@ int main(void) {
init_pic();

io_sti();
outb_p(PIC0_IMR, 0xf9);
outb_p(PIC0_IMR, 0xf8); // PIT, PIC1, キーボードを許可(11111000)

init_pit();

struct Queue queue;
unsigned char keybuf[KEYBUF_LIMIT];

init_queue(&queue, KEYBUF_LIMIT, keybuf);
init_keyboard(&queue);
if (init_mem_info() == MEM_SUCCESS) {
init_shell();
}
init_mem_info();
init_shell();

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

#include <intr.h>
#include <io.h>
#include <lib/queue.h>

#include <console.h>

#define PIT_CTRL 0x0043
#define PIT_CNT0 0x0040

/**
* (PIT)Programmable Interval Timer
* AT互換機ではPITに1.19318MHzのクロックが与えられている
*
* IRQ0の割り込み周期変更
* AL = 0x34; OUT(0x43, AL);
* AL = 割り込み周期の下位8bit; OUT(0x40, AL);
* AL = 割り込み周期の上位8bit; OUT(0x40, AL);
*/
void init_pit() {
/**
* IRQ0の割り込み周期変更
* 割り込み頻度は クロック/設定カウント値
*/
outb_p(PIT_CTRL, 0x34);
/**
* 11932を設定することで100Hzになる
* 11932=0x2e9c
*/
outb_p(PIT_CNT0, 0x9c);
outb_p(PIT_CNT0, 0x2e);
timer.count++;
}

/**
* IRQ0用割り込みハンドラ
*/
void handle_intr20(int *esp) {
outb_p(PIC0_OCW2, 0x60); // IRQ-00受付完了をPICに通知
timer.count++;
// タイムアウトが設定されている場合
if (timer.timeout > 0) {
timer.timeout--;
if (timer.timeout == 0 ){//&& queue_status(timer.queue) != 0) {
unsigned char buf[8];
dequeue(timer.queue, buf);
}
}
}

void set_timer(unsigned int timeout, struct Queue *queue, unsigned char data) {
// EFLAGSは32bit拡張のレジスタ
// キャリーフラグや割り込みフラグなどを保持する
int eflags = io_load_eflags();
// 設定中の割り込みを禁止する
io_cli();
timer.timeout = timeout;
timer.queue = queue;
timer.data = data;
enqueue(timer.queue, timer.data);
// 割り込み状態を元の状態に戻す
io_store_eflags(eflags);
}
10 changes: 10 additions & 0 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ void strcpy(char *src, char *dst) {
while ((*dst++ = *src++) != '\0');
*(dst++) = '\0';
}

int atoi(char *src) {
int num = 0;
while(*src != '\0') {
num += *src - 48;
num *= 10;
src++;
}
return num / 10;
}

0 comments on commit dab39d9

Please sign in to comment.