Skip to content

Commit

Permalink
finish step10 : add memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
sat0ken committed Jan 6, 2024
1 parent 265e2b5 commit 700953b
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/os/Makefile
Expand Up @@ -21,7 +21,7 @@ H8WRITE_SERDEV = /dev/ttyUSB0

OBJS = startup.o main.o interrupt.o
OBJS += lib.o serial.o
OBJS += kozos.o syscall.o test09_1.o test09_2.o test09_3.o
OBJS += kozos.o syscall.o memory.o test10_1.o

TARGET = kozos

Expand Down
Binary file modified src/os/kozos
Binary file not shown.
22 changes: 22 additions & 0 deletions src/os/kozos.c
Expand Up @@ -4,6 +4,7 @@
#include "interrupt.h"
#include "syscall.h"
#include "lib.h"
#include "memory.h"

#define THREAD_NUM 6
#define THREAD_NAME_SIZE 15
Expand Down Expand Up @@ -223,6 +224,19 @@ static int thread_chpri(int priority)
return old;
}

static void *thread_kmalloc(int size)
{
putcurrent();
return kzmem_alloc(size);
}

static int thread_kmfree(char *p)
{
kzmem_free(p);
putcurrent();
return 0;
}

// 割り込みハンドラの登録
static int setintr(softvec_type_t type, kz_handler_t handler)
{
Expand Down Expand Up @@ -262,6 +276,12 @@ static void call_functions(kz_syscall_type_t type, kz_syscall_param_t *param)
case KZ_SYSCALL_TYPE_CHPRI:
param->un.chpri.ret = thread_chpri(param->un.chpri.priority);
break;
case KZ_SYSCALL_TYPE_KMALLOC:
param->un.kmalloc.ret = thread_kmalloc(param->un.kmalloc.size);
break;
case KZ_SYSCALL_TYPE_KMFREE:
param->un.kmfree.ret = thread_kmfree(param->un.kmfree.p);
break;
default:
break;
}
Expand Down Expand Up @@ -329,6 +349,8 @@ static void thread_intr(softvec_type_t type, unsigned long sp)
// 初期スレッドを起動しOSの動作を開始
void kz_start(kz_func_t func, char *name, int priority, int stacksize, int argc, char *argv[])
{
kzmem_init();

current = NULL;
// 各種データの初期化
memset(readyque, 0, sizeof(readyque));
Expand Down
9 changes: 6 additions & 3 deletions src/os/kozos.h
Expand Up @@ -27,12 +27,15 @@ int kz_sleep(void);
int kz_wakeup(kz_thread_id_t id);
kz_thread_id_t kz_getid(void);
int kz_chpri(int priority);
void *kz_kmalloc(int size);
int kz_kmfree(void *p);

// ユーザスレッド
//int test08_1_main(int argc, char *argv[]);
int test09_1_main(int argc, char *argv[]);
int test09_2_main(int argc, char *argv[]);
int test09_3_main(int argc, char *argv[]);
//int test09_1_main(int argc, char *argv[]);
//int test09_2_main(int argc, char *argv[]);
//int test09_3_main(int argc, char *argv[]);
int test10_1_main(int argc, char *argv[]);
extern kz_thread_id_t test09_1_id;
extern kz_thread_id_t test09_2_id;
extern kz_thread_id_t test09_3_id;
Expand Down
4 changes: 4 additions & 0 deletions src/os/ld.scr
Expand Up @@ -48,6 +48,10 @@ SECTIONS
. = ALIGN(4);
_end = . ;

.freearea : {
_freearea = . ;
} > ram

.userstack : {
_userstack = .;
} > userstack
Expand Down
16 changes: 9 additions & 7 deletions src/os/main.c
Expand Up @@ -3,15 +3,17 @@
#include "interrupt.h"
#include "lib.h"

kz_thread_id_t test09_1_id;
kz_thread_id_t test09_2_id;
kz_thread_id_t test09_3_id;
//kz_thread_id_t test09_1_id;
//kz_thread_id_t test09_2_id;
//kz_thread_id_t test09_3_id;

static int start_threads(int argc, char *argv[])
{
test09_1_id = kz_run(test09_1_main, "test09_1", 1, 0x100, 0, NULL);
test09_2_id = kz_run(test09_2_main, "test09_2", 2, 0x100, 0, NULL);
test09_3_id = kz_run(test09_3_main, "test09_3", 3, 0x100, 0, NULL);
// test09_1_id = kz_run(test09_1_main, "test09_1", 1, 0x100, 0, NULL);
// test09_2_id = kz_run(test09_2_main, "test09_2", 2, 0x100, 0, NULL);
// test09_3_id = kz_run(test09_3_main, "test09_3", 3, 0x100, 0, NULL);

kz_run(test10_1_main, "test10_1", 1, 0x100, 0,NULL);

kz_chpri(15);
INTR_ENABLE;
Expand All @@ -29,7 +31,7 @@ int main(void)

// OSの動作開始
// 初期スレッドを優先度0で起動する
kz_start(start_threads, "idel", 0,0x100, 0, NULL);
kz_start(start_threads, "idle", 0,0x100, 0, NULL);

return 0;
}
Expand Down
97 changes: 97 additions & 0 deletions src/os/memory.c
@@ -0,0 +1,97 @@
#include "defines.h"
#include "kozos.h"
#include "lib.h"
#include "memory.h"

typedef struct _kzmem_block {
struct _kzmem_block *next;
int size;
} kzmem_block;

typedef struct _kzmem_pool {
int size;
int num;
kzmem_block *free;
} kzmem_pool;

static kzmem_pool pool[] = {
{16, 8, NULL},
{32, 8, NULL},
{64, 4, NULL},
};

#define MEMORY_AREA_NUM (sizeof(pool) / sizeof(*pool))

static int kzmem_init_pool(kzmem_pool *p)
{
int i;
kzmem_block *mp;
kzmem_block **mpp;
extern char freearea;
static char *area = &freearea;

mp = (kzmem_block *)area;

mpp = &p->free;
for (i = 0; i < p->num; i++) {
*mpp = mp;
memset(mp, 0, sizeof(*mp));
mp->size = p->size;
mpp = &(mp->next);
mp = (kzmem_block *)((char *)mp + p->size);
area += p->size;
}

return 0;
}

int kzmem_init(void)
{
int i;
for (i = 0; i < MEMORY_AREA_NUM; i++) {
kzmem_init_pool(&pool[i]);
}
return 0;
}

void *kzmem_alloc(int size)
{
kzmem_block *mp;
kzmem_pool *p;
int i;

for (i = 0; i < MEMORY_AREA_NUM; i++) {
p = &pool[i];
if (size <= p->size - sizeof(kzmem_block)) {
if (p->free == NULL) {
kz_sysdown();
return NULL;
}
mp = p->free;
p->free = p->free->next;
mp->next = NULL;
return mp + 1;
}
}
kz_sysdown();
return NULL;
}

void kzmem_free(void *mem)
{
kzmem_block *mp;
kzmem_pool *p;
int i;

mp = ((kzmem_block *)mem -1);

for (i = 0; i < MEMORY_AREA_NUM; i++) {
p = &pool[i];
if (mp->size == p->size) {
mp->next = p->free;
p->free = mp;
return;
}
}
kz_sysdown();
}
8 changes: 8 additions & 0 deletions src/os/memory.h
@@ -0,0 +1,8 @@
#ifndef _KOZOS_MEMORY_H_INCLUDED_
#define _KOZOS_MEMORY_H_INCLUDED_

int kzmem_init(void);
void *kzmem_alloc(int size);
void kzmem_free(void *mem);

#endif
16 changes: 16 additions & 0 deletions src/os/syscall.c
Expand Up @@ -59,3 +59,19 @@ int kz_chpri(int priority)
kz_syscall(KZ_SYSCALL_TYPE_CHPRI, &param);
return param.un.chpri.ret;
}

void *kz_kmalloc(int size)
{
kz_syscall_param_t param;
param.un.kmalloc.size = size;
kz_syscall(KZ_SYSCALL_TYPE_KMALLOC, &param);
return param.un.kmalloc.ret;
}

int kz_kmfree(void *p)
{
kz_syscall_param_t param;
param.un.kmfree.p = p;
kz_syscall(KZ_SYSCALL_TYPE_KMFREE, &param);
return param.un.kmfree.ret;
}
10 changes: 10 additions & 0 deletions src/os/syscall.h
Expand Up @@ -12,6 +12,8 @@ typedef enum {
KZ_SYSCALL_TYPE_WAKEUP,
KZ_SYSCALL_TYPE_GETID,
KZ_SYSCALL_TYPE_CHPRI,
KZ_SYSCALL_TYPE_KMALLOC,
KZ_SYSCALL_TYPE_KMFREE,
} kz_syscall_type_t;

// システムコール呼び出し時のパラメータ格納用構造体
Expand Down Expand Up @@ -46,6 +48,14 @@ typedef struct {
int priority;
int ret;
} chpri;
struct {
int size;
void *ret;
} kmalloc;
struct {
char *p;
int ret;
} kmfree;
} un;
} kz_syscall_param_t;

Expand Down
35 changes: 35 additions & 0 deletions src/os/test10_1.c
@@ -0,0 +1,35 @@
#include "defines.h"
#include "kozos.h"
#include "lib.h"

int test10_1_main(int argc, char *argv[])
{
char *p1, *p2;
int i, j;
puts("test10_1 started.\n");

for (i = 4; i <= 56; i += 4) {
p1 = kz_kmalloc(i);
p2 = kz_kmalloc(i);
for (j = 0; j < i - 1; j++) {
p1[j] = 'a';
p2[j] = 'b';
}
p1[j] = '\0';
p2[j] = '\0';

putxval((unsigned long)p1, 8);
puts(" ");
puts(p1);
puts("\n");
putxval((unsigned long)p2, 8);
puts(" ");
puts(p2);
puts("\n");

kz_kmfree(p1);
kz_kmfree(p2);
}
puts("test10_1 exit.\n");
return 0;
}

0 comments on commit 700953b

Please sign in to comment.