raw
m_genesis.kv            1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
m_genesis.kv 2 ;; ;;
m_genesis.kv 3 ;; This file is part of 'M', a MIPS system emulator. ;;
m_genesis.kv 4 ;; ;;
m_genesis.kv 5 ;; (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) ;;
m_genesis.kv 6 ;; http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html ;;
m_genesis.kv 7 ;; ;;
m_genesis.kv 8 ;; You do not have, nor can you ever acquire the right to use, copy or ;;
m_genesis.kv 9 ;; distribute this software ; Should you use this software for any purpose, ;;
m_genesis.kv 10 ;; or copy and distribute it to anyone or in any manner, you are breaking ;;
m_genesis.kv 11 ;; the laws of whatever soi-disant jurisdiction, and you promise to ;;
m_genesis.kv 12 ;; continue doing so for the indefinite future. In any case, please ;;
m_genesis.kv 13 ;; always : read and understand any software ; verify any PGP signatures ;;
m_genesis.kv 14 ;; that you use - for any purpose. ;;
m_genesis.kv 15 ;; ;;
m_genesis.kv 16 ;; See also http://trilema.com/2015/a-new-software-licensing-paradigm . ;;
m_genesis.kv 17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
m_genesis.kv 18
m_genesis.kv 19 ;-----------------------------------------------------------------------------
m_genesis.kv 20 %macro PUSHA 0
m_genesis.kv 21 push rax
m_genesis.kv 22 push rbx
m_genesis.kv 23 push rcx
m_genesis.kv 24 push rdx
m_genesis.kv 25 push rbp
m_genesis.kv 26 push rdi
m_genesis.kv 27 push rsi
m_genesis.kv 28 push r8
m_genesis.kv 29 push r9
m_genesis.kv 30 push r10
m_genesis.kv 31 push r11
m_genesis.kv 32 push r12
m_genesis.kv 33 push r13
m_genesis.kv 34 push r14
m_genesis.kv 35 push r15
m_genesis.kv 36
m_genesis.kv 37 ; lea rsp,[rsp-16*4]
m_genesis.kv 38 ; movdqu [rsp+16*0], xmm0
m_genesis.kv 39 ; movdqu [rsp+16*1], xmm1
m_genesis.kv 40 ; movdqu [rsp+16*2], xmm2
m_genesis.kv 41 ; movdqu [rsp+16*3], xmm2
m_genesis.kv 42
m_genesis.kv 43 %endmacro
m_genesis.kv 44 ;-----------------------------------------------------------------------------
m_genesis.kv 45
m_genesis.kv 46 ;-----------------------------------------------------------------------------
m_genesis.kv 47 %macro POPA 0
m_genesis.kv 48
m_genesis.kv 49 ; movdqu xmm1,[rsp+16*3]
m_genesis.kv 50 ; movdqu xmm1,[rsp+16*2]
m_genesis.kv 51 ; movdqu xmm1,[rsp+16*1]
m_genesis.kv 52 ; movdqu xmm0,[rsp+16*0]
m_genesis.kv 53 ; lea rsp,[rsp+16*4]
m_genesis.kv 54
m_genesis.kv 55 pop r15
m_genesis.kv 56 pop r14
m_genesis.kv 57 pop r13
m_genesis.kv 58 pop r12
m_genesis.kv 59 pop r11
m_genesis.kv 60 pop r10
m_genesis.kv 61 pop r9
m_genesis.kv 62 pop r8
m_genesis.kv 63 pop rsi
m_genesis.kv 64 pop rdi
m_genesis.kv 65 pop rbp
m_genesis.kv 66 pop rdx
m_genesis.kv 67 pop rcx
m_genesis.kv 68 pop rbx
m_genesis.kv 69 pop rax
m_genesis.kv 70 %endmacro
m_genesis.kv 71 ;-----------------------------------------------------------------------------
m_genesis.kv 72
m_genesis.kv 73 ;-----------------------------------------------------------------------------
m_genesis.kv 74 ; x64 sys calls
m_genesis.kv 75 %define SYS_BRK 12
m_genesis.kv 76 %define SYS_EXIT 60
m_genesis.kv 77 %define SYS_EXIT_GROUP 231
m_genesis.kv 78 %define SYS_READ 0
m_genesis.kv 79 %define SYS_WRITE 1
m_genesis.kv 80 %define SYS_OPEN 2
m_genesis.kv 81 %define SYS_CLOSE 3
m_genesis.kv 82 %define SYS_FSTAT 5
m_genesis.kv 83 %define SYS_MMAP 9
m_genesis.kv 84 %define SYS_MUNMAP 11
m_genesis.kv 85 %define SYS_NANOSLEEP 35
m_genesis.kv 86 %define SYS_TIME 201
m_genesis.kv 87
m_genesis.kv 88 ; For threadism:
m_genesis.kv 89 %define SYS_CLONE 56
m_genesis.kv 90
m_genesis.kv 91 ; For tty cure:
m_genesis.kv 92 %define SYS_IOCTL 16
m_genesis.kv 93
m_genesis.kv 94 ; For sys_mmap
m_genesis.kv 95 %define PROT_READ 1
m_genesis.kv 96 %define PROT_WRITE 2
m_genesis.kv 97 %define MAP_PRIVATE 2
m_genesis.kv 98
m_genesis.kv 99 ; Output fd's:
m_genesis.kv 100 %define STDOUT 1
m_genesis.kv 101 %define STDERR 2
m_genesis.kv 102 ;-----------------------------------------------------------------------------
m_genesis.kv 103 ; For threadism:
m_genesis.kv 104 ;; sched.h
m_genesis.kv 105 %define CLONE_VM 0x00000100
m_genesis.kv 106 %define CLONE_FS 0x00000200
m_genesis.kv 107 %define CLONE_FILES 0x00000400
m_genesis.kv 108 %define CLONE_SIGHAND 0x00000800
m_genesis.kv 109 %define CLONE_PARENT 0x00008000
m_genesis.kv 110 %define CLONE_THREAD 0x00010000
m_genesis.kv 111 %define CLONE_IO 0x80000000
m_genesis.kv 112
m_genesis.kv 113 ;; sys/mman.h
m_genesis.kv 114 %define MAP_GROWSDOWN 0x0100
m_genesis.kv 115 %define MAP_ANONYMOUS 0x0020
m_genesis.kv 116 %define MAP_PRIVATE 0x0002
m_genesis.kv 117 %define PROT_READ 0x1
m_genesis.kv 118 %define PROT_WRITE 0x2
m_genesis.kv 119 %define PROT_EXEC 0x4
m_genesis.kv 120
m_genesis.kv 121 %define THREAD_FLAGS \
m_genesis.kv 122 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_PARENT | \
m_genesis.kv 123 CLONE_THREAD | CLONE_IO
m_genesis.kv 124
m_genesis.kv 125 ;-----------------------------------------------------------------------------
m_genesis.kv 126 ; Pill for linux's tty buffering retardation
m_genesis.kv 127 %define ICANON 2
m_genesis.kv 128 %define ECHO 8
m_genesis.kv 129 %define TCGETS 21505 ; to read back the termios config
m_genesis.kv 130 %define TCPUTS 21508 ; to set the termios config
m_genesis.kv 131 ;-----------------------------------------------------------------------------
m_genesis.kv 132
m_genesis.kv 133 ;-----------------------------------------------------------------------------
m_genesis.kv 134 ; For output of sys_fstat
m_genesis.kv 135 struc statbuf
m_genesis.kv 136 .st_dev resq 1
m_genesis.kv 137 .st_ino resq 1
m_genesis.kv 138 .st_nlink resq 1
m_genesis.kv 139 .st_mode resd 1
m_genesis.kv 140 .st_uid resd 1
m_genesis.kv 141 .st_gid resd 1
m_genesis.kv 142 .__pad0 resd 1
m_genesis.kv 143 .st_rdev resq 1
m_genesis.kv 144 .st_size resq 1 ; file size in bytes
m_genesis.kv 145 .st_blksize resq 1
m_genesis.kv 146 .st_blocks resq 1
m_genesis.kv 147 .st_atime resq 1
m_genesis.kv 148 .st_atime_nsec resq 1
m_genesis.kv 149 .st_mtime resq 1
m_genesis.kv 150 .st_mtime_nsec resq 1
m_genesis.kv 151 .st_ctime resq 1
m_genesis.kv 152 .st_ctime_nsec resq 1
m_genesis.kv 153 .__unused resq 3
m_genesis.kv 154 .size
m_genesis.kv 155 endstruc
m_genesis.kv 156 ;-----------------------------------------------------------------------------
m_genesis.kv 157
m_genesis.kv 158 ;-----------------------------------------------------------------------------
m_genesis.kv 159 ;; State
m_genesis.kv 160 ;-----------------------------------------------------------------------------
m_genesis.kv 161 section .bss
m_genesis.kv 162 linux_epoch resq 1 ; for _Get_Epoch_Time
m_genesis.kv 163 ;-----------------------------------------------------------------------------
m_genesis.kv 164
m_genesis.kv 165 section .text
m_genesis.kv 166
m_genesis.kv 167 ;-----------------------------------------------------------------------------
m_genesis.kv 168 ; For spawning slave threads:
m_genesis.kv 169 ;-----------------------------------------------------------------------------
m_genesis.kv 170 _Create_Thread:
m_genesis.kv 171 push rdi
m_genesis.kv 172 ;; Allocate stack for the new thread:
m_genesis.kv 173 mov rdi, 0
m_genesis.kv 174 mov rsi, SLAVE_STACK_SIZE
m_genesis.kv 175 mov rdx, PROT_WRITE | PROT_READ
m_genesis.kv 176 mov r10, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN
m_genesis.kv 177 mov rax, SYS_MMAP
m_genesis.kv 178 syscall ; invoke mmap
m_genesis.kv 179 ;; Verify whether we actually got our memory:
m_genesis.kv 180 cmp rax, 0
m_genesis.kv 181 jg .ok
m_genesis.kv 182 ;; ... if not:
m_genesis.kv 183 EGGOG "Could not allocate memory for slave stack!"
m_genesis.kv 184 .ok: ;; we got the memory;
m_genesis.kv 185 ;; Actually spawn the thread:
m_genesis.kv 186 lea rsi, [rax + SLAVE_STACK_SIZE - 8]
m_genesis.kv 187 pop qword [rsi]
m_genesis.kv 188 mov rdi, THREAD_FLAGS
m_genesis.kv 189 mov rax, SYS_CLONE
m_genesis.kv 190 syscall
m_genesis.kv 191 ret
m_genesis.kv 192 ;-----------------------------------------------------------------------------
m_genesis.kv 193
m_genesis.kv 194 ;-----------------------------------------------------------------------------
m_genesis.kv 195 ; Get Epoch Time from OS. Return in RDX.
m_genesis.kv 196 ;-----------------------------------------------------------------------------
m_genesis.kv 197 _Get_Epoch_Time:
m_genesis.kv 198 PUSHA
m_genesis.kv 199 mov rax, SYS_TIME ; sys_time
m_genesis.kv 200 mov rdi, linux_epoch ; where it will be written
m_genesis.kv 201 syscall ; invoke the call
m_genesis.kv 202 POPA
m_genesis.kv 203 mov rdx, qword [linux_epoch] ; return all 64 bits of epoch time
m_genesis.kv 204 ret
m_genesis.kv 205 ;-----------------------------------------------------------------------------
m_genesis.kv 206
m_genesis.kv 207 ;-----------------------------------------------------------------------------
m_genesis.kv 208 ; 'NanoSleep' (rax : pointer to TS structure)
m_genesis.kv 209 ;-----------------------------------------------------------------------------
m_genesis.kv 210 _Nano_Sleep:
m_genesis.kv 211 mov rdi, rax
m_genesis.kv 212 mov rax, SYS_NANOSLEEP ; nanosleep
m_genesis.kv 213 xor rsi, rsi
m_genesis.kv 214 syscall ; Invoke sleep
m_genesis.kv 215 ret
m_genesis.kv 216 ;-----------------------------------------------------------------------------
m_genesis.kv 217
m_genesis.kv 218 ;-----------------------------------------------------------------------------
m_genesis.kv 219 ; Terminate Current Thread Only
m_genesis.kv 220 ;-----------------------------------------------------------------------------
m_genesis.kv 221 _Stop:
m_genesis.kv 222 mov rax, SYS_EXIT ; Terminate (current thread only)
m_genesis.kv 223 mov rdi, 0 ; exit code (always 0, for now)
m_genesis.kv 224 syscall
m_genesis.kv 225 ;; no more after this, we're through
m_genesis.kv 226 ;-----------------------------------------------------------------------------
m_genesis.kv 227
m_genesis.kv 228 ;-----------------------------------------------------------------------------
m_genesis.kv 229 ; Hard Stop (kill self and all child threads)
m_genesis.kv 230 ;-----------------------------------------------------------------------------
m_genesis.kv 231 _Hard_Stop:
m_genesis.kv 232 mov rax, SYS_EXIT_GROUP ; Terminate Master and all slaves
m_genesis.kv 233 mov rdi, 0 ; exit code (always 0, for now)
m_genesis.kv 234 syscall
m_genesis.kv 235 ;; no more after this, we're through
m_genesis.kv 236 ;-----------------------------------------------------------------------------