0493c0a645561df7964b5ed8cd3608e6a7168d46
[monolithium.git] / kernel / src / helpers.asm
1 ;
2 ; helpers.asm
3 ;
4 ; Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
5 ;
6 ; This program is free software: you can redistribute it and/or modify
7 ; it under the terms of the GNU Affero General Public License as
8 ; published by the Free Software Foundation, either version 3 of the
9 ; License, or (at your option) any later version.
10 ;
11 ; This program is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ; GNU Affero General Public License for more details.
15 ;
16 ; You should have received a copy of the GNU Affero General Public License
17 ; along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 ;
19
20 CONTEXT_SWITCH_MAGIC    EQU 0xDEADBEEF
21
22 bits 32
23
24 global init_cpu_gdt
25 global vm86_start
26 global exception_return
27 global save_kernel_handler
28 global syscall_function
29 global reschedule
30
31 extern set_exception_handler
32 extern set_kernel_esp
33 extern scheduler
34
35 gdtr                    DW 0
36                         DD 0
37
38 ;********************************************************************************
39 ; void init_cpu_gdt(gdt_descriptor_t *table,
40 ;                   size_t table_size,
41 ;                   word_t code_selector,
42 ;                   word_t data_selector,
43 ;                   word_t tss_selector)
44 ;********************************************************************************
45 init_cpu_gdt:           push ebp
46                         mov ebp, esp
47                         pushfd
48                         cli
49                         mov eax, dword [ebp + 8]
50                         mov dword [gdtr + 2], eax
51                         mov eax, dword [ebp + 12]
52                         dec eax
53                         mov word [gdtr], ax
54                         mov eax, dword [ebp + 16]
55                         mov word [.code_seg], ax
56                         mov eax, dword [ebp + 20]
57                         lgdt [gdtr]
58                         DB 0xEA
59                         DD .continue
60 .code_seg:              DW 0
61 .continue:              mov es, ax
62                         mov ss, ax
63                         mov ds, ax
64                         mov fs, ax
65                         mov gs, ax
66                         mov eax, dword [ebp + 24]
67                         ltr ax
68                         popfd
69                         leave
70                         ret
71
72 ;********************************************************************************
73 ; void vm86_start(vm86_registers_t regs)
74 ;********************************************************************************
75 vm86_start:             pushfd
76                         push cs
77                         push dword [esp + 8]
78                         push 0
79                         pushad
80                         push ds
81                         push esp
82                         call set_kernel_esp
83                         cli
84                         add esp, 60
85                         popad
86                         iret
87
88 ;********************************************************************************
89 ; void exception_return(register_t regs)
90 ;********************************************************************************
91 exception_return:       lea esi, [esp + 0x04]
92                         mov edi, dword [esp + 0x14]
93                         sub edi, 13 * 4
94                         mov esp, edi
95                         mov ecx, 13
96                         cld
97                         rep movsd
98                         add esp, 4
99                         popad
100                         add esp, 4
101                         iret
102
103 ;********************************************************************************
104 ; void save_kernel_handler(exception_handler_t *old_handler)
105 ;********************************************************************************
106 save_kernel_handler:    pushfd
107                         push cs
108                         push dword [esp + 8]
109                         push 0
110                         pushad
111                         add dword [esp + 12], 16
112                         push ds
113                         push dword [esp + 56]
114                         push 0
115                         lea eax, [esp + 8]
116                         push eax
117                         call set_exception_handler
118                         add esp, 64
119                         xor eax, eax
120                         ret
121
122 ;********************************************************************************
123 ; qword_t syscall_function(void *function,
124 ;                          dword_t *parameters,
125 ;                          size_t parameters_size)
126 ;********************************************************************************
127 syscall_function:       push ebp
128                         mov ebp, esp
129                         mov eax, dword [ebp + 0x08]
130                         mov esi, dword [ebp + 0x0C]
131                         mov ecx, dword [ebp + 0x10]
132                         or ecx, ecx
133                         jz .no_params
134                         sub esp, ecx
135                         mov edi, esp
136                         cld
137                         rep movsb
138 .no_params:             call eax
139                         add esp, dword [ebp + 0x10]
140                         pop ebp
141                         ret
142
143 ;********************************************************************************
144 ; void reschedule(void)
145 ;********************************************************************************
146 reschedule:             push cs
147                         push dword [esp + 4]
148                         push 0
149                         pushad
150                         pushfd
151                         cli
152                         pop eax
153                         mov dword [esp + 44], eax
154                         add dword [esp + 12], 16
155                         push ds
156                         push esp
157                         call scheduler
158                         add esp, 4
159                         cmp dword [esp + 0x24], CONTEXT_SWITCH_MAGIC
160                         jnz .no_stack_switch
161                         mov esp, dword [esp + 0x10]
162 .no_stack_switch:       pop eax
163                         mov ds, ax
164                         mov es, ax
165                         mov fs, ax
166                         mov gs, ax
167                         popad
168                         add esp, 4
169                         iret