POK(kernelpart)
thread.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
23 #include <bsp.h>
24 #include <libc.h>
25 #include <errno.h>
26 #include <core/thread.h>
27 
28 #include "gdt.h"
29 
30 #include "thread.h"
31 
32 #ifdef POK_NEEDS_THREADS
33 
35  uint32_t stack_size,
36  uint32_t entry)
37 {
38  start_context_t* sp;
39  char* stack_addr;
40 
41  stack_addr = pok_bsp_mem_alloc (stack_size);
42 
43  sp = (start_context_t *) (stack_addr + stack_size - 4 - sizeof (start_context_t));
44 
45  memset (sp, 0, sizeof (start_context_t));
46 
47  sp->ctx.__esp = (uint32_t) (&sp->ctx.eip); /* for pusha */
48  sp->ctx.eip = (uint32_t) pok_thread_start;
49  sp->ctx.cs = GDT_CORE_CODE_SEGMENT << 3;
50  sp->ctx.eflags = 1 << 9;
51 
52  sp->entry = entry;
53  sp->id = thread_id;
54 
55  return ((uint32_t)sp);
56 }
57 
58 
59 void pok_context_switch (uint32_t* old_sp,
60  uint32_t new_sp);
61 asm (".global pok_context_switch \n"
62  "pok_context_switch: \n"
63  "pushf \n"
64  "pushl %cs \n"
65  "pushl $1f \n"
66  "pusha \n"
67  "movl 48(%esp), %ebx \n" /* 48(%esp) : &old_sp, 52(%esp) : new_sp */
68  "movl %esp, (%ebx) \n"
69  "movl 52(%esp), %esp \n"
70  "popa \n"
71  "iret \n"
72  "1: \n"
73  "ret"
74  );
75 
76 #endif