* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / baremetal0.5.2 / os / syscalls / memory.asm
blob81da14e39e7a88892c24fd0daa69a56fc5edcff7
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
5 ; Memory functions
6 ; =============================================================================
8 align 16
9 db 'DEBUG: MEMORY '
10 align 16
13 ; -----------------------------------------------------------------------------
14 ; os_mem_allocate -- Allocates the requested number of 2 MiB pages
15 ; IN: RCX = Number of pages to allocate
16 ; OUT: RAX = Starting address
17 ; RCX = Number of pages allocated (Set to the value asked for or 0 on failure)
18 ; This function will only allocate continous pages
19 os_mem_allocate:
20 push rdi
21 push rsi
22 push rdx
23 push rbx
25 cmp rcx, 0
26 je os_mem_allocate_fail ; At least 1 page must be allocated
27 xor rax, rax
28 mov rsi, os_MemoryMap
29 mov ax, word [os_MemAmount]
30 shr ax, 1 ; Divide actual memory by 2
31 add rsi, rax ; RSI now points to the last page
32 sub rsi, 1
33 std ; Set direction flag to backward
35 os_mem_allocate_start: ; Find a free page of memory
36 lodsb
37 cmp rsi, os_MemoryMap ; We have hit the start of the memory map, no free pages
38 je os_mem_allocate_fail
39 cmp al, 1 ; If the byte is one then we found a free memory page
40 jne os_mem_allocate_start
41 mov rbx, rcx ; RBX is our temporary counter
42 sub rbx, 1 ; One free page was already found
43 cmp rbx, 0 ; Was only one page requested?
44 je os_mem_allocate_mark
46 os_mem_allocate_nextpage:
47 lodsb
48 cmp rsi, os_MemoryMap ; We have hit the start of the memory map, no more free pages
49 je os_mem_allocate_fail
50 cmp al, 1
51 jne os_mem_allocate_start
52 sub rbx, 1
53 cmp rbx, 0
54 jne os_mem_allocate_nextpage
56 os_mem_allocate_mark:
57 ; We have a suitable free series of pages. Allocate them.
58 cld ; Set direction flag to forward
59 mov rdi, rsi
60 add rdi, 1
61 mov rdx, rdi ; RDX points to the starting page
62 mov al, 2
63 push rcx
64 rep stosb
65 pop rcx
66 sub rdx, os_MemoryMap ; RDX now contains the memory page number
67 shl rdx, 21 ; Quick multiply by 2097152 (2 MiB) to get the starting memory address
68 mov rax, rdx ; Return the starting address in RAX
69 jmp os_mem_allocate_end
71 os_mem_allocate_fail:
72 cld ; Set direction flag to forward
73 xor rcx, rcx ; Failure so set RCX to 0 (No pages allocated)
74 xor rax, rax
76 os_mem_allocate_end:
77 pop rbx
78 pop rdx
79 pop rsi
80 pop rdi
81 ret
82 ; -----------------------------------------------------------------------------
85 ; -----------------------------------------------------------------------------
86 ; os_mem_release -- Frees the requested number of 2 MiB pages
87 ; IN: RAX = Starting address
88 ; RCX = Number of pages to free
89 ; OUT: RCX = Number of pages freed
90 os_mem_release:
91 push rdi
92 push rcx
93 push rax
95 shr rax, 21 ; Quick divide by 2097152 (2 MiB) to get the starting page number
96 add rax, os_MemoryMap
97 mov rdi, rax
98 mov al, 1
99 rep stosb
101 pop rax
102 pop rcx
103 pop rdi
105 ; -----------------------------------------------------------------------------
108 ; -----------------------------------------------------------------------------
109 ; os_mem_get_free -- Returns the number of 2 MiB pages that are available
110 ; IN: Nothing
111 ; OUT: RCX = Number of free 2 MiB pages
112 os_mem_get_free:
113 push rsi
114 push rbx
115 push rax
117 mov rsi, os_MemoryMap
118 xor rcx, rcx
119 xor rbx, rbx
121 os_mem_get_free_next:
122 lodsb
123 add rcx, 1
124 cmp rcx, 65536
125 je os_mem_get_free_end
126 cmp al, 1
127 jne os_mem_get_free_next
128 add rbx, 1
129 jmp os_mem_get_free_next
131 os_mem_get_free_end:
132 mov rcx, rbx
134 pop rax
135 pop rbx
136 pop rsi
138 ; -----------------------------------------------------------------------------
141 ; =============================================================================
142 ; EOF