-
+ 63181E522486B260324663A2C7CB928E8110114503A0711AC596F35176CB774BCA680F59C83B3723A3ABC4EB57A3FA3D10A657CA6E6BC79A4A3706325279068B
m/os/linux.asm
(0 . 0)(1 . 236)
4724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4725 ;; ;;
4726 ;; This file is part of 'M', a MIPS system emulator. ;;
4727 ;; ;;
4728 ;; (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) ;;
4729 ;; http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html ;;
4730 ;; ;;
4731 ;; You do not have, nor can you ever acquire the right to use, copy or ;;
4732 ;; distribute this software ; Should you use this software for any purpose, ;;
4733 ;; or copy and distribute it to anyone or in any manner, you are breaking ;;
4734 ;; the laws of whatever soi-disant jurisdiction, and you promise to ;;
4735 ;; continue doing so for the indefinite future. In any case, please ;;
4736 ;; always : read and understand any software ; verify any PGP signatures ;;
4737 ;; that you use - for any purpose. ;;
4738 ;; ;;
4739 ;; See also http://trilema.com/2015/a-new-software-licensing-paradigm . ;;
4740 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4741
4742 ;-----------------------------------------------------------------------------
4743 %macro PUSHA 0
4744 push rax
4745 push rbx
4746 push rcx
4747 push rdx
4748 push rbp
4749 push rdi
4750 push rsi
4751 push r8
4752 push r9
4753 push r10
4754 push r11
4755 push r12
4756 push r13
4757 push r14
4758 push r15
4759
4760 ; lea rsp,[rsp-16*4]
4761 ; movdqu [rsp+16*0], xmm0
4762 ; movdqu [rsp+16*1], xmm1
4763 ; movdqu [rsp+16*2], xmm2
4764 ; movdqu [rsp+16*3], xmm2
4765
4766 %endmacro
4767 ;-----------------------------------------------------------------------------
4768
4769 ;-----------------------------------------------------------------------------
4770 %macro POPA 0
4771
4772 ; movdqu xmm1,[rsp+16*3]
4773 ; movdqu xmm1,[rsp+16*2]
4774 ; movdqu xmm1,[rsp+16*1]
4775 ; movdqu xmm0,[rsp+16*0]
4776 ; lea rsp,[rsp+16*4]
4777
4778 pop r15
4779 pop r14
4780 pop r13
4781 pop r12
4782 pop r11
4783 pop r10
4784 pop r9
4785 pop r8
4786 pop rsi
4787 pop rdi
4788 pop rbp
4789 pop rdx
4790 pop rcx
4791 pop rbx
4792 pop rax
4793 %endmacro
4794 ;-----------------------------------------------------------------------------
4795
4796 ;-----------------------------------------------------------------------------
4797 ; x64 sys calls
4798 %define SYS_BRK 12
4799 %define SYS_EXIT 60
4800 %define SYS_EXIT_GROUP 231
4801 %define SYS_READ 0
4802 %define SYS_WRITE 1
4803 %define SYS_OPEN 2
4804 %define SYS_CLOSE 3
4805 %define SYS_FSTAT 5
4806 %define SYS_MMAP 9
4807 %define SYS_MUNMAP 11
4808 %define SYS_NANOSLEEP 35
4809 %define SYS_TIME 201
4810
4811 ; For threadism:
4812 %define SYS_CLONE 56
4813
4814 ; For tty cure:
4815 %define SYS_IOCTL 16
4816
4817 ; For sys_mmap
4818 %define PROT_READ 1
4819 %define PROT_WRITE 2
4820 %define MAP_PRIVATE 2
4821
4822 ; Output fd's:
4823 %define STDOUT 1
4824 %define STDERR 2
4825 ;-----------------------------------------------------------------------------
4826 ; For threadism:
4827 ;; sched.h
4828 %define CLONE_VM 0x00000100
4829 %define CLONE_FS 0x00000200
4830 %define CLONE_FILES 0x00000400
4831 %define CLONE_SIGHAND 0x00000800
4832 %define CLONE_PARENT 0x00008000
4833 %define CLONE_THREAD 0x00010000
4834 %define CLONE_IO 0x80000000
4835
4836 ;; sys/mman.h
4837 %define MAP_GROWSDOWN 0x0100
4838 %define MAP_ANONYMOUS 0x0020
4839 %define MAP_PRIVATE 0x0002
4840 %define PROT_READ 0x1
4841 %define PROT_WRITE 0x2
4842 %define PROT_EXEC 0x4
4843
4844 %define THREAD_FLAGS \
4845 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_PARENT | \
4846 CLONE_THREAD | CLONE_IO
4847
4848 ;-----------------------------------------------------------------------------
4849 ; Pill for linux's tty buffering retardation
4850 %define ICANON 2
4851 %define ECHO 8
4852 %define TCGETS 21505 ; to read back the termios config
4853 %define TCPUTS 21508 ; to set the termios config
4854 ;-----------------------------------------------------------------------------
4855
4856 ;-----------------------------------------------------------------------------
4857 ; For output of sys_fstat
4858 struc statbuf
4859 .st_dev resq 1
4860 .st_ino resq 1
4861 .st_nlink resq 1
4862 .st_mode resd 1
4863 .st_uid resd 1
4864 .st_gid resd 1
4865 .__pad0 resd 1
4866 .st_rdev resq 1
4867 .st_size resq 1 ; file size in bytes
4868 .st_blksize resq 1
4869 .st_blocks resq 1
4870 .st_atime resq 1
4871 .st_atime_nsec resq 1
4872 .st_mtime resq 1
4873 .st_mtime_nsec resq 1
4874 .st_ctime resq 1
4875 .st_ctime_nsec resq 1
4876 .__unused resq 3
4877 .size
4878 endstruc
4879 ;-----------------------------------------------------------------------------
4880
4881 ;-----------------------------------------------------------------------------
4882 ;; State
4883 ;-----------------------------------------------------------------------------
4884 section .bss
4885 linux_epoch resq 1 ; for _Get_Epoch_Time
4886 ;-----------------------------------------------------------------------------
4887
4888 section .text
4889
4890 ;-----------------------------------------------------------------------------
4891 ; For spawning slave threads:
4892 ;-----------------------------------------------------------------------------
4893 _Create_Thread:
4894 push rdi
4895 ;; Allocate stack for the new thread:
4896 mov rdi, 0
4897 mov rsi, SLAVE_STACK_SIZE
4898 mov rdx, PROT_WRITE | PROT_READ
4899 mov r10, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN
4900 mov rax, SYS_MMAP
4901 syscall ; invoke mmap
4902 ;; Verify whether we actually got our memory:
4903 cmp rax, 0
4904 jg .ok
4905 ;; ... if not:
4906 EGGOG "Could not allocate memory for slave stack!"
4907 .ok: ;; we got the memory;
4908 ;; Actually spawn the thread:
4909 lea rsi, [rax + SLAVE_STACK_SIZE - 8]
4910 pop qword [rsi]
4911 mov rdi, THREAD_FLAGS
4912 mov rax, SYS_CLONE
4913 syscall
4914 ret
4915 ;-----------------------------------------------------------------------------
4916
4917 ;-----------------------------------------------------------------------------
4918 ; Get Epoch Time from OS. Return in RDX.
4919 ;-----------------------------------------------------------------------------
4920 _Get_Epoch_Time:
4921 PUSHA
4922 mov rax, SYS_TIME ; sys_time
4923 mov rdi, linux_epoch ; where it will be written
4924 syscall ; invoke the call
4925 POPA
4926 mov rdx, qword [linux_epoch] ; return all 64 bits of epoch time
4927 ret
4928 ;-----------------------------------------------------------------------------
4929
4930 ;-----------------------------------------------------------------------------
4931 ; 'NanoSleep' (rax : pointer to TS structure)
4932 ;-----------------------------------------------------------------------------
4933 _Nano_Sleep:
4934 mov rdi, rax
4935 mov rax, SYS_NANOSLEEP ; nanosleep
4936 xor rsi, rsi
4937 syscall ; Invoke sleep
4938 ret
4939 ;-----------------------------------------------------------------------------
4940
4941 ;-----------------------------------------------------------------------------
4942 ; Terminate Current Thread Only
4943 ;-----------------------------------------------------------------------------
4944 _Stop:
4945 mov rax, SYS_EXIT ; Terminate (current thread only)
4946 mov rdi, 0 ; exit code (always 0, for now)
4947 syscall
4948 ;; no more after this, we're through
4949 ;-----------------------------------------------------------------------------
4950
4951 ;-----------------------------------------------------------------------------
4952 ; Hard Stop (kill self and all child threads)
4953 ;-----------------------------------------------------------------------------
4954 _Hard_Stop:
4955 mov rax, SYS_EXIT_GROUP ; Terminate Master and all slaves
4956 mov rdi, 0 ; exit code (always 0, for now)
4957 syscall
4958 ;; no more after this, we're through
4959 ;-----------------------------------------------------------------------------