Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit c1cf1e3

Browse files
authored
Merge pull request #55 from UteroOS/implement-tasks
Implement tasks (but it's incomplete)
2 parents 2da11ce + fd26c29 commit c1cf1e3

File tree

28 files changed

+1194
-81
lines changed

28 files changed

+1194
-81
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ kernel := build/kernel-$(arch).bin
1616
iso := build/utero-$(arch).iso
1717
top_dir := $(shell pwd)
1818
INCLUDE := -I$(top_dir)/src/c_kernel/include -I$(top_dir)/src/arch/x86_64/c/include
19-
CFLAGS := -ffreestanding -nostdinc -Wno-implicit
19+
CFLAGS := -O2 -ffreestanding -nostdinc -Wno-implicit
2020

2121
libcr := src/musl/lib/libcr.a
2222
libu := build/arch/$(arch)/c/libu.a
@@ -61,7 +61,7 @@ cleanobjs:
6161
@rm -rf target/
6262

6363
run: $(iso)
64-
@qemu-system-$(arch) -cdrom $(iso)
64+
@qemu-system-$(arch) -cdrom $(iso) -monitor stdio
6565

6666
iso: $(iso)
6767

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is the *work in progress*.
77

88
## Requirements
99

10-
* Crystal 0.22.0
10+
* Crystal 0.23.0
1111
* llvm
1212
* Please see [All required libraries](https://github.com/crystal-lang/crystal/wiki/All-required-libraries)
1313
* nasm
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) 2016-2017 Utero OS Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
//
10+
// The part of this file was taken from:
11+
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/processor.h
12+
13+
#ifndef ASM_PROCESSOR_H
14+
#define ASM_PROCESSOR_H
15+
16+
#include <asm/stddef.h>
17+
18+
// rdtsc (Read-time stamp counter)
19+
// return the 64-bit time stamp value
20+
inline static uint64_t
21+
rdtsc(void)
22+
{
23+
uint64_t lo, hi;
24+
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
25+
return (hi << 32 | lo);
26+
}
27+
28+
// wbinvd asm instruction(Write back and invalidate cache)
29+
inline static void
30+
flush_cache(void)
31+
{
32+
asm volatile("wbinvd" ::: "memory");
33+
}
34+
35+
// invd asm instruction (Invalidate internal caches - without writing back)
36+
inline static void
37+
invalid_cache(void)
38+
{
39+
asm volatile("invd");
40+
}
41+
42+
// NOTE: mb, rmb, wmb will be moved to arch/x86_64/c/processor.c
43+
// and used via extern func_memory_barrier (mb|rmb|wmb)
44+
45+
// mb (Memory barrier)
46+
// mfence asm instruction (Memory Fence - Serializes load and store operations)
47+
inline static void
48+
mb(void)
49+
{
50+
asm volatile("mfence" ::: "memory");
51+
}
52+
53+
// rmb (Read memory barrier)
54+
// lfence asm instruction (Load Fence - Serializes load operations)
55+
inline static void
56+
rmb(void)
57+
{
58+
asm volatile("lfence" ::: "memory");
59+
}
60+
61+
// wmb (Write memory barrier)
62+
// sfence asm instruction (Store Fence - Serializes store operations)
63+
inline static void
64+
wmb(void)
65+
{
66+
asm volatile("sfence" ::: "memory");
67+
}
68+
69+
// search the most significant bit
70+
// bsr (Bit Scan Reverse) asm instruction
71+
static inline size_t
72+
msb(size_t i)
73+
{
74+
size_t ret;
75+
76+
if (!i) {
77+
return (sizeof(size_t) * 8);
78+
}
79+
asm volatile("bsr %1, %0" : "=r"(ret) : "r"(i) : "cc");
80+
81+
return ret;
82+
}
83+
84+
// search the least significant bit
85+
// bsf (Bit Scan Forward) asm instruction
86+
static inline size_t
87+
lsb(size_t i)
88+
{
89+
size_t ret;
90+
91+
if (!i) {
92+
return (sizeof(size_t) * 8);
93+
}
94+
asm volatile("bsf %1, %0" : "=r"(ret) : "r"(i) : "cc");
95+
96+
return ret;
97+
}
98+
99+
// A one-instruction-do-nothing
100+
#define NOP1 asm volatile("nop")
101+
// A two-instruction-do-nothing
102+
#define NOP2 asm volatile("nop;nop")
103+
// A four-instruction-do-nothing
104+
#define NOP4 asm volatile("nop;nop;nop;nop")
105+
// A eight-instruction-do-nothing
106+
#define NOP8 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop")
107+
108+
#endif /* end of include guard: ASM_PROCESSOR_H */

src/arch/x86_64/c/include/asm/stddef.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
// The part of this file was taken from:
1111
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/stddef.h
1212

13-
#ifndef STDDEF_H
14-
#define STDDEF_H
13+
#ifndef ASM_STDDEF_H
14+
#define ASM_STDDEF_H
1515

1616
// Unsigned 64 bit integer
1717
typedef unsigned long long uint64_t;
@@ -34,7 +34,8 @@ typedef char int8_t;
3434
typedef unsigned long long size_t;
3535

3636
// This defines what the stack looks like after the task context is saved.
37-
struct state {
37+
struct state
38+
{
3839
// R15 register
3940
uint64_t r15;
4041
// R14 register
@@ -80,4 +81,4 @@ struct state {
8081
uint64_t ss;
8182
};
8283

83-
#endif /* end of include guard: STDDEF_H */
84+
#endif /* end of include guard: ASM_STDDEF_H */
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2016-2017 Utero OS Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
//
10+
// The part of this file was taken from:
11+
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/tasks.h
12+
13+
#ifndef ASM_TASKS_H
14+
#define ASM_TASKS_H
15+
16+
#include <stddef.h>
17+
18+
// Switch the current task
19+
// stack: Pointer to the old stack pointer
20+
void switch_context(size_t** stack);
21+
22+
// Setup a default frame for a new task
23+
// task: Pointer to the task structure
24+
// ep: The entry point for code execution
25+
// arg: Arguments list pointer for the task's stack
26+
// return
27+
// - 0 on success
28+
// - -EINVAL (-22) on failure
29+
int create_default_frame(task_t* task, entry_point_t ep, void* arg);
30+
31+
#endif /* end of include guard: ASM_TASKS_H */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4+
// option. This file may not be copied, modified, or distributed
5+
// except according to those terms.
6+
//
7+
// The part of this file was taken from:
8+
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/tasks_types.h
9+
10+
#ifndef ASM_TASKS_TYPES_H
11+
#define ASM_TASKS_TYPES_H
12+
13+
#include <asm/processor.h>
14+
#include <stddef.h>
15+
16+
#endif /* end of include guard: ASM_TASKS_TYPES_H */

0 commit comments

Comments
 (0)