2010-07-31 15:53:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
|
|
|
|
* RWTH Aachen University
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of MetalSVM.
|
|
|
|
*/
|
|
|
|
|
2012-07-17 12:44:18 -07:00
|
|
|
#include <metalsvm/stdio.h>
|
2010-07-31 15:53:30 +00:00
|
|
|
#include <metalsvm/string.h>
|
2010-08-09 11:47:51 +00:00
|
|
|
#include <metalsvm/stdlib.h>
|
|
|
|
#include <metalsvm/tasks.h>
|
2010-12-10 06:16:58 +00:00
|
|
|
#include <metalsvm/errno.h>
|
2011-03-02 13:49:36 +01:00
|
|
|
#include <metalsvm/processor.h>
|
2010-09-10 22:18:55 +00:00
|
|
|
#include <asm/gdt.h>
|
2010-08-09 11:47:51 +00:00
|
|
|
#include <asm/tss.h>
|
2011-02-21 08:36:06 +01:00
|
|
|
#include <asm/page.h>
|
2010-07-31 15:53:30 +00:00
|
|
|
|
2010-12-10 06:16:58 +00:00
|
|
|
gdt_ptr_t gp;
|
2012-07-19 00:17:31 +02:00
|
|
|
static tss_t task_state_segments[MAX_CORES] __attribute__ ((aligned (PAGE_SIZE)));
|
2010-12-10 06:16:58 +00:00
|
|
|
// currently, our kernel has full access to the ioports
|
|
|
|
static gdt_entry_t gdt[GDT_ENTRIES] = {[0 ... GDT_ENTRIES-1] = {0, 0, 0, 0, 0, 0}};
|
2010-07-31 15:53:30 +00:00
|
|
|
|
|
|
|
/*
|
2011-07-18 09:14:28 +02:00
|
|
|
* This is defined in entry.asm. We use this to properly reload
|
2010-07-31 15:53:30 +00:00
|
|
|
* the new segment registers
|
|
|
|
*/
|
|
|
|
extern void gdt_flush(void);
|
|
|
|
|
2012-05-21 15:04:05 +02:00
|
|
|
size_t* get_current_stack(void)
|
|
|
|
{
|
|
|
|
task_t* curr_task = per_core(current_task);
|
|
|
|
|
2012-07-19 00:17:31 +02:00
|
|
|
// determine and set esp0
|
2012-07-19 00:31:08 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2012-07-19 22:07:59 +02:00
|
|
|
task_state_segments[CORE_ID].esp0 = (size_t) curr_task->stack + KERNEL_STACK_SIZE - 16; // => stack is 16byte aligned
|
2012-07-19 00:31:08 +02:00
|
|
|
#else
|
2012-07-19 22:07:59 +02:00
|
|
|
task_state_segments[CORE_ID].rsp0 = (size_t) curr_task->stack + KERNEL_STACK_SIZE - 16; // => stack is 16byte aligned
|
2012-07-19 00:31:08 +02:00
|
|
|
#endif
|
2012-07-19 00:17:31 +02:00
|
|
|
|
|
|
|
// use new page table
|
2012-05-21 15:04:05 +02:00
|
|
|
write_cr3(virt_to_phys((size_t)curr_task->pgd));
|
|
|
|
|
2012-07-19 22:07:59 +02:00
|
|
|
return curr_task->last_stack_pointer;
|
2010-08-09 11:47:51 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 13:49:36 +01:00
|
|
|
int arch_fork(task_t* task)
|
|
|
|
{
|
2012-05-21 15:04:05 +02:00
|
|
|
struct state* state;
|
2011-03-02 13:49:36 +01:00
|
|
|
task_t* curr_task = per_core(current_task);
|
2012-07-17 12:44:18 -07:00
|
|
|
size_t esp, state_size;
|
2011-03-02 13:49:36 +01:00
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!task, 0))
|
|
|
|
return -EINVAL;
|
2012-07-19 22:07:59 +02:00
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!task->stack, 0))
|
|
|
|
return -EINVAL;
|
2011-03-02 13:49:36 +01:00
|
|
|
|
2012-07-17 12:44:18 -07:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
state_size = sizeof(struct state) - 2*sizeof(size_t);
|
|
|
|
#else
|
|
|
|
state_size = sizeof(struct state);
|
|
|
|
#endif
|
|
|
|
|
2011-04-18 12:18:59 -07:00
|
|
|
// copy kernel stack of the current task
|
2012-07-16 23:05:46 +02:00
|
|
|
mb();
|
2012-07-19 22:07:59 +02:00
|
|
|
memcpy(task->stack, curr_task->stack, KERNEL_STACK_SIZE);
|
2011-04-18 12:18:59 -07:00
|
|
|
|
2012-05-24 10:49:45 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2012-07-17 12:44:18 -07:00
|
|
|
asm volatile ("mov %%esp, %0" : "=m"(esp));
|
2012-07-19 22:07:59 +02:00
|
|
|
esp -= (size_t) curr_task->stack;
|
|
|
|
esp += (size_t) task->stack;
|
2012-05-21 15:04:05 +02:00
|
|
|
|
2012-07-17 12:44:18 -07:00
|
|
|
state = (struct state*) (esp - state_size);
|
2012-07-19 00:17:31 +02:00
|
|
|
//memset(state, 0x00, state_size);
|
2011-03-02 23:08:01 +01:00
|
|
|
|
2012-07-17 12:44:18 -07:00
|
|
|
asm volatile ("pusha; pop %0" : "=m"(state->edi));
|
|
|
|
asm volatile ("pop %0" : "=m"(state->esi));
|
|
|
|
asm volatile ("pop %0" : "=m"(state->ebp));
|
2011-03-02 23:08:01 +01:00
|
|
|
asm volatile ("add $4, %%esp" ::: "%esp");
|
2012-07-17 12:44:18 -07:00
|
|
|
asm volatile ("pop %0" : "=m"(state->ebx));
|
|
|
|
asm volatile ("pop %0" : "=m"(state->edx));
|
|
|
|
asm volatile ("pop %0" : "=m"(state->ecx));
|
|
|
|
asm volatile ("pop %0" : "=m"(state->eax));
|
|
|
|
|
|
|
|
state->esp = esp;
|
2012-07-19 22:07:59 +02:00
|
|
|
task->last_stack_pointer = (size_t*) state;
|
2012-05-21 15:04:05 +02:00
|
|
|
state->int_no = 0xB16B00B5;
|
|
|
|
state->error = 0xC03DB4B3;
|
2012-07-19 00:17:31 +02:00
|
|
|
state->cs = 0x08;
|
2012-08-24 14:42:37 +02:00
|
|
|
state->ds = state->es = 0x10;
|
2011-08-06 15:55:34 +02:00
|
|
|
// store the current EFLAGS
|
2012-07-17 12:44:18 -07:00
|
|
|
asm volatile ("pushf; pop %0" : "=m"(state->eflags));
|
2012-05-21 15:04:05 +02:00
|
|
|
// enable interrupts
|
|
|
|
state->eflags |= (1 << 9);
|
2012-07-17 12:44:18 -07:00
|
|
|
// This will be the entry point for the new task. read_ip cleanups the stack
|
|
|
|
asm volatile ("push %0; call read_ip" :: "r"(&state->eip) : "%eax");
|
2012-05-24 10:49:45 +02:00
|
|
|
#else
|
|
|
|
#warning Currently, not supported!
|
|
|
|
return -1;
|
|
|
|
#endif
|
2011-03-02 13:49:36 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-24 10:49:45 +02:00
|
|
|
int create_default_frame(task_t* task, entry_point_t ep, void* arg)
|
2010-08-09 11:47:51 +00:00
|
|
|
{
|
2012-06-10 08:05:24 +02:00
|
|
|
size_t *stack;
|
2012-04-14 00:34:09 +02:00
|
|
|
struct state *stptr;
|
2012-06-10 08:05:24 +02:00
|
|
|
size_t state_size;
|
2012-04-14 00:34:09 +02:00
|
|
|
|
2010-08-09 11:47:51 +00:00
|
|
|
if (BUILTIN_EXPECT(!task, 0))
|
2011-03-02 13:49:36 +01:00
|
|
|
return -EINVAL;
|
2010-08-09 11:47:51 +00:00
|
|
|
|
2012-07-19 22:07:59 +02:00
|
|
|
if (BUILTIN_EXPECT(!task->stack, 0))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(task->stack, 0xCD, KERNEL_STACK_SIZE);
|
2012-04-14 00:34:09 +02:00
|
|
|
|
2012-04-14 09:57:18 +02:00
|
|
|
/* The difference between setting up a task for SW-task-switching
|
|
|
|
* and not for HW-task-switching is setting up a stack and not a TSS.
|
|
|
|
* This is the stack which will be activated and popped off for iret later.
|
|
|
|
*/
|
2012-07-19 22:07:59 +02:00
|
|
|
stack = (size_t*) (task->stack + KERNEL_STACK_SIZE - 16); // => stack is 16byte aligned
|
2012-04-14 09:57:18 +02:00
|
|
|
|
|
|
|
/* The next three things on the stack are a marker for debugging purposes, ... */
|
2012-04-14 00:34:09 +02:00
|
|
|
*stack-- = 0xDEADBEEF;
|
2012-06-10 08:05:24 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2012-04-14 09:57:18 +02:00
|
|
|
/* the first-function-to-be-called's arguments, ... */
|
2012-05-21 15:04:05 +02:00
|
|
|
*stack-- = (size_t) arg;
|
2012-06-10 08:05:24 +02:00
|
|
|
#endif
|
2012-04-14 09:57:18 +02:00
|
|
|
/* and the "caller" we shall return to.
|
|
|
|
* This procedure cleans the task after exit. */
|
2012-05-21 15:04:05 +02:00
|
|
|
*stack = (size_t) leave_kernel_task;
|
2012-04-14 09:57:18 +02:00
|
|
|
|
|
|
|
/* Next bunch on the stack is the initial register state.
|
|
|
|
* The stack must look like the stack of a task which was
|
|
|
|
* scheduled away previously. */
|
|
|
|
|
2012-06-10 08:05:24 +02:00
|
|
|
/* In 64bit mode, he stack pointer (SS:RSP) is pushed unconditionally on interrupts.
|
|
|
|
* In legacy modes, this push is conditional and based on a change in current privilege level (CPL).*/
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
state_size = sizeof(struct state) - 2*sizeof(size_t);
|
|
|
|
#else
|
|
|
|
state_size = sizeof(struct state);
|
|
|
|
#endif
|
|
|
|
stack = (size_t*) ((size_t) stack - state_size);
|
2012-04-14 00:34:09 +02:00
|
|
|
|
2012-05-21 15:04:05 +02:00
|
|
|
stptr = (struct state *) stack;
|
2012-06-10 08:05:24 +02:00
|
|
|
memset(stptr, 0x00, state_size);
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
stptr->esp = (size_t)stack + state_size;
|
|
|
|
#else
|
|
|
|
stptr->rsp = (size_t)stack + state_size;
|
|
|
|
/* the first-function-to-be-called's arguments, ... */
|
|
|
|
stptr->rdi = (size_t) arg;
|
|
|
|
#endif
|
2012-04-14 00:34:09 +02:00
|
|
|
stptr->int_no = 0xB16B00B5;
|
|
|
|
stptr->error = 0xC03DB4B3;
|
|
|
|
|
2012-04-14 09:57:18 +02:00
|
|
|
/* The instruction pointer shall be set on the first function to be called
|
|
|
|
* after IRETing */
|
2012-06-10 08:05:24 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
stptr->eip = (size_t)ep;
|
|
|
|
#else
|
|
|
|
stptr->rip = (size_t)ep;
|
|
|
|
#endif
|
2012-07-19 22:07:59 +02:00
|
|
|
stptr->cs = 0x08;
|
2012-06-10 08:05:24 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2012-05-21 15:04:05 +02:00
|
|
|
stptr->eflags = 0x1202;
|
2012-08-24 14:42:37 +02:00
|
|
|
stptr->ds = stptr->es = 0x10;
|
2012-05-21 15:04:05 +02:00
|
|
|
// the creation of a kernel tasks didn't change the IOPL level
|
|
|
|
// => useresp & ss is not required
|
2012-06-10 08:05:24 +02:00
|
|
|
#else
|
|
|
|
stptr->rflags = 0x1202;
|
|
|
|
stptr->ss = 0x10;
|
|
|
|
stptr->userrsp = stptr->rsp;
|
|
|
|
#endif
|
2012-04-14 00:34:09 +02:00
|
|
|
|
2012-05-21 15:04:05 +02:00
|
|
|
/* Set the task's stack pointer entry to the stack we have crafted right now. */
|
2012-07-19 22:07:59 +02:00
|
|
|
task->last_stack_pointer = (size_t*)stack;
|
2011-02-08 18:37:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-13 09:36:28 +02:00
|
|
|
/* Setup a descriptor in the Global Descriptor Table */
|
2010-07-31 15:53:30 +00:00
|
|
|
static void gdt_set_gate(int num, unsigned long base, unsigned long limit,
|
2012-06-13 09:36:28 +02:00
|
|
|
unsigned char access, unsigned char gran)
|
|
|
|
{
|
|
|
|
configure_gdt_entry(&gdt[num], base, limit, access, gran);
|
|
|
|
}
|
|
|
|
|
|
|
|
void configure_gdt_entry(gdt_entry_t *dest_entry, unsigned long base, unsigned long limit,
|
|
|
|
unsigned char access, unsigned char gran)
|
2011-04-19 18:51:59 +02:00
|
|
|
{
|
2010-07-31 15:53:30 +00:00
|
|
|
/* Setup the descriptor base address */
|
2012-06-13 09:36:28 +02:00
|
|
|
dest_entry->base_low = (base & 0xFFFF);
|
|
|
|
dest_entry->base_middle = (base >> 16) & 0xFF;
|
|
|
|
dest_entry->base_high = (base >> 24) & 0xFF;
|
2010-07-31 15:53:30 +00:00
|
|
|
|
|
|
|
/* Setup the descriptor limits */
|
2012-06-13 09:36:28 +02:00
|
|
|
dest_entry->limit_low = (limit & 0xFFFF);
|
|
|
|
dest_entry->granularity = ((limit >> 16) & 0x0F);
|
2010-07-31 15:53:30 +00:00
|
|
|
|
|
|
|
/* Finally, set up the granularity and access flags */
|
2012-06-13 09:36:28 +02:00
|
|
|
dest_entry->granularity |= (gran & 0xF0);
|
|
|
|
dest_entry->access = access;
|
2010-07-31 15:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will setup the special GDT
|
|
|
|
* pointer, set up the entries in our GDT, and then
|
|
|
|
* finally call gdt_flush() in our assembler file in order
|
|
|
|
* to tell the processor where the new GDT is and update the
|
|
|
|
* new segment registers
|
|
|
|
*/
|
|
|
|
void gdt_install(void)
|
|
|
|
{
|
2012-06-11 16:16:35 +02:00
|
|
|
unsigned int i;
|
|
|
|
unsigned long mode, limit;
|
2010-08-09 11:47:51 +00:00
|
|
|
|
2012-04-14 00:34:09 +02:00
|
|
|
memset(task_state_segments, 0x00, MAX_CORES*sizeof(tss_t));
|
2010-08-09 11:47:51 +00:00
|
|
|
|
2012-06-10 08:05:24 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
mode = GDT_FLAG_32_BIT;
|
2012-06-11 16:16:35 +02:00
|
|
|
limit = 0xFFFFFFFF;
|
2012-06-10 08:05:24 +02:00
|
|
|
#elif defined(CONFIG_X86_64)
|
|
|
|
mode = GDT_FLAG_64_BIT;
|
2012-06-11 16:16:35 +02:00
|
|
|
limit = 0;
|
2012-06-10 08:05:24 +02:00
|
|
|
#else
|
|
|
|
#error invalid mode
|
|
|
|
#endif
|
|
|
|
|
2010-07-31 15:53:30 +00:00
|
|
|
/* Setup the GDT pointer and limit */
|
2010-08-09 11:47:51 +00:00
|
|
|
gp.limit = (sizeof(gdt_entry_t) * GDT_ENTRIES) - 1;
|
2012-06-10 08:05:24 +02:00
|
|
|
gp.base = (size_t) &gdt;
|
2010-07-31 15:53:30 +00:00
|
|
|
|
|
|
|
/* Our NULL descriptor */
|
|
|
|
gdt_set_gate(0, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The second entry is our Code Segment. The base address
|
|
|
|
* is 0, the limit is 4 GByte, it uses 4KByte granularity,
|
|
|
|
* uses 32-bit opcodes, and is a Code Segment descriptor.
|
|
|
|
*/
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(1, 0, limit,
|
2010-09-10 22:18:55 +00:00
|
|
|
GDT_FLAG_RING0 | GDT_FLAG_SEGMENT | GDT_FLAG_CODESEG | GDT_FLAG_PRESENT,
|
2012-06-10 08:05:24 +02:00
|
|
|
GDT_FLAG_4K_GRAN | mode);
|
2010-07-31 15:53:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The third entry is our Data Segment. It's EXACTLY the
|
|
|
|
* same as our code segment, but the descriptor type in
|
|
|
|
* this entry's access byte says it's a Data Segment
|
|
|
|
*/
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(2, 0, limit,
|
2010-09-10 22:18:55 +00:00
|
|
|
GDT_FLAG_RING0 | GDT_FLAG_SEGMENT | GDT_FLAG_DATASEG | GDT_FLAG_PRESENT,
|
2012-06-10 08:05:24 +02:00
|
|
|
GDT_FLAG_4K_GRAN | mode);
|
2010-07-31 15:53:30 +00:00
|
|
|
|
2010-08-05 12:41:38 +00:00
|
|
|
/*
|
|
|
|
* Create code segement for userspace applications (ring 3)
|
|
|
|
*/
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(3, 0, limit,
|
2010-08-24 05:43:39 +00:00
|
|
|
GDT_FLAG_RING3 | GDT_FLAG_SEGMENT | GDT_FLAG_CODESEG | GDT_FLAG_PRESENT,
|
2012-06-10 08:05:24 +02:00
|
|
|
GDT_FLAG_4K_GRAN | mode);
|
2010-08-05 12:41:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create data segement for userspace applications (ring 3)
|
|
|
|
*/
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(4, 0, limit,
|
2010-08-24 05:43:39 +00:00
|
|
|
GDT_FLAG_RING3 | GDT_FLAG_SEGMENT | GDT_FLAG_DATASEG | GDT_FLAG_PRESENT,
|
2012-06-10 08:05:24 +02:00
|
|
|
GDT_FLAG_4K_GRAN | mode);
|
2010-08-05 12:41:38 +00:00
|
|
|
|
2010-08-09 11:47:51 +00:00
|
|
|
/*
|
2011-02-08 18:37:56 +00:00
|
|
|
* Create TSS for each task at ring0 (we use these segments for task switching)
|
2010-09-10 22:18:55 +00:00
|
|
|
*/
|
2012-04-14 00:34:09 +02:00
|
|
|
for(i=0; i<MAX_CORES; i++) {
|
2012-06-10 08:05:24 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2012-05-21 15:04:05 +02:00
|
|
|
/* set default values */
|
|
|
|
task_state_segments[i].eflags = 0x1202;
|
|
|
|
task_state_segments[i].ss0 = 0x10; // data segment
|
|
|
|
task_state_segments[i].esp0 = 0xDEADBEEF; // invalid pseudo address
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(5+i, (unsigned long) (task_state_segments+i), sizeof(tss_t)-1,
|
|
|
|
GDT_FLAG_PRESENT | GDT_FLAG_TSS | GDT_FLAG_RING0, mode);
|
2012-06-10 08:05:24 +02:00
|
|
|
#elif defined(CONFIG_X86_64)
|
|
|
|
task_state_segments[i].rsp0 = 0xDEADBEEF; // invalid pseudo address
|
2012-06-11 16:16:35 +02:00
|
|
|
gdt_set_gate(5+i*2, (unsigned long) (task_state_segments+i), sizeof(tss_t)-1,
|
|
|
|
GDT_FLAG_PRESENT | GDT_FLAG_TSS | GDT_FLAG_RING0, mode);
|
2012-06-10 08:05:24 +02:00
|
|
|
#endif
|
2010-08-24 05:43:39 +00:00
|
|
|
}
|
2010-08-09 11:47:51 +00:00
|
|
|
|
2010-07-31 15:53:30 +00:00
|
|
|
/* Flush out the old GDT and install the new changes! */
|
|
|
|
gdt_flush();
|
|
|
|
}
|