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

Commit

Permalink
Merge pull request #55 from UteroOS/implement-tasks
Browse files Browse the repository at this point in the history
Implement tasks (but it's incomplete)
  • Loading branch information
noriyotcp authored Jun 30, 2017
2 parents 2da11ce + fd26c29 commit c1cf1e3
Show file tree
Hide file tree
Showing 28 changed files with 1,194 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kernel := build/kernel-$(arch).bin
iso := build/utero-$(arch).iso
top_dir := $(shell pwd)
INCLUDE := -I$(top_dir)/src/c_kernel/include -I$(top_dir)/src/arch/x86_64/c/include
CFLAGS := -ffreestanding -nostdinc -Wno-implicit
CFLAGS := -O2 -ffreestanding -nostdinc -Wno-implicit

libcr := src/musl/lib/libcr.a
libu := build/arch/$(arch)/c/libu.a
Expand Down Expand Up @@ -61,7 +61,7 @@ cleanobjs:
@rm -rf target/

run: $(iso)
@qemu-system-$(arch) -cdrom $(iso)
@qemu-system-$(arch) -cdrom $(iso) -monitor stdio

iso: $(iso)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is the *work in progress*.

## Requirements

* Crystal 0.22.0
* Crystal 0.23.0
* llvm
* Please see [All required libraries](https://github.com/crystal-lang/crystal/wiki/All-required-libraries)
* nasm
Expand Down
108 changes: 108 additions & 0 deletions src/arch/x86_64/c/include/asm/processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2016-2017 Utero OS Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// The part of this file was taken from:
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/processor.h

#ifndef ASM_PROCESSOR_H
#define ASM_PROCESSOR_H

#include <asm/stddef.h>

// rdtsc (Read-time stamp counter)
// return the 64-bit time stamp value
inline static uint64_t
rdtsc(void)
{
uint64_t lo, hi;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (hi << 32 | lo);
}

// wbinvd asm instruction(Write back and invalidate cache)
inline static void
flush_cache(void)
{
asm volatile("wbinvd" ::: "memory");
}

// invd asm instruction (Invalidate internal caches - without writing back)
inline static void
invalid_cache(void)
{
asm volatile("invd");
}

// NOTE: mb, rmb, wmb will be moved to arch/x86_64/c/processor.c
// and used via extern func_memory_barrier (mb|rmb|wmb)

// mb (Memory barrier)
// mfence asm instruction (Memory Fence - Serializes load and store operations)
inline static void
mb(void)
{
asm volatile("mfence" ::: "memory");
}

// rmb (Read memory barrier)
// lfence asm instruction (Load Fence - Serializes load operations)
inline static void
rmb(void)
{
asm volatile("lfence" ::: "memory");
}

// wmb (Write memory barrier)
// sfence asm instruction (Store Fence - Serializes store operations)
inline static void
wmb(void)
{
asm volatile("sfence" ::: "memory");
}

// search the most significant bit
// bsr (Bit Scan Reverse) asm instruction
static inline size_t
msb(size_t i)
{
size_t ret;

if (!i) {
return (sizeof(size_t) * 8);
}
asm volatile("bsr %1, %0" : "=r"(ret) : "r"(i) : "cc");

return ret;
}

// search the least significant bit
// bsf (Bit Scan Forward) asm instruction
static inline size_t
lsb(size_t i)
{
size_t ret;

if (!i) {
return (sizeof(size_t) * 8);
}
asm volatile("bsf %1, %0" : "=r"(ret) : "r"(i) : "cc");

return ret;
}

// A one-instruction-do-nothing
#define NOP1 asm volatile("nop")
// A two-instruction-do-nothing
#define NOP2 asm volatile("nop;nop")
// A four-instruction-do-nothing
#define NOP4 asm volatile("nop;nop;nop;nop")
// A eight-instruction-do-nothing
#define NOP8 asm volatile("nop;nop;nop;nop;nop;nop;nop;nop")

#endif /* end of include guard: ASM_PROCESSOR_H */
9 changes: 5 additions & 4 deletions src/arch/x86_64/c/include/asm/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// The part of this file was taken from:
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/stddef.h

#ifndef STDDEF_H
#define STDDEF_H
#ifndef ASM_STDDEF_H
#define ASM_STDDEF_H

// Unsigned 64 bit integer
typedef unsigned long long uint64_t;
Expand All @@ -34,7 +34,8 @@ typedef char int8_t;
typedef unsigned long long size_t;

// This defines what the stack looks like after the task context is saved.
struct state {
struct state
{
// R15 register
uint64_t r15;
// R14 register
Expand Down Expand Up @@ -80,4 +81,4 @@ struct state {
uint64_t ss;
};

#endif /* end of include guard: STDDEF_H */
#endif /* end of include guard: ASM_STDDEF_H */
31 changes: 31 additions & 0 deletions src/arch/x86_64/c/include/asm/tasks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2016-2017 Utero OS Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// The part of this file was taken from:
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/tasks.h

#ifndef ASM_TASKS_H
#define ASM_TASKS_H

#include <stddef.h>

// Switch the current task
// stack: Pointer to the old stack pointer
void switch_context(size_t** stack);

// Setup a default frame for a new task
// task: Pointer to the task structure
// ep: The entry point for code execution
// arg: Arguments list pointer for the task's stack
// return
// - 0 on success
// - -EINVAL (-22) on failure
int create_default_frame(task_t* task, entry_point_t ep, void* arg);

#endif /* end of include guard: ASM_TASKS_H */
16 changes: 16 additions & 0 deletions src/arch/x86_64/c/include/asm/tasks_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// The part of this file was taken from:
// https://github.com/RWTH-OS/eduOS/blob/master/arch/x86/include/asm/tasks_types.h

#ifndef ASM_TASKS_TYPES_H
#define ASM_TASKS_TYPES_H

#include <asm/processor.h>
#include <stddef.h>

#endif /* end of include guard: ASM_TASKS_TYPES_H */
Loading

0 comments on commit c1cf1e3

Please sign in to comment.