Browse Source

commit before fetching upstream/master

CiD- 3 years ago
parent
commit
9ae566adcc
2 changed files with 91 additions and 42 deletions
  1. 4 4
      core/os/os2/file_linux.odin
  2. 87 38
      core/sys/unix/syscalls_linux.odin

+ 4 - 4
core/os/os2/file_linux.odin

@@ -331,12 +331,12 @@ _is_dir :: proc(fd: Handle) -> bool {
 // defined as 512, however, it is well known that paths can exceed that limit.
 // So, in theory you could have a path larger than the entire temp_allocator's
 // buffer.  Therefor any large paths will use context.allocator.
-_name_to_cstring :: proc(path: string) -> (cpath: cstring, allocated: bool) {
-	if len(path) > _CSTRING_NAME_HEAP_THRESHOLD {
-		cpath = strings.clone_to_cstring(path)
+_name_to_cstring :: proc(name: string) -> (cname: cstring, allocated: bool) {
+	if len(name) > _CSTRING_NAME_HEAP_THRESHOLD {
+		cname = strings.clone_to_cstring(name)
 		allocated = true
 		return
 	}
-	cpath = strings.clone_to_cstring(path, context.temp_allocator)
+	cname = strings.clone_to_cstring(name, context.temp_allocator)
 	return
 }

+ 87 - 38
core/sys/unix/syscalls_linux.odin

@@ -1518,11 +1518,43 @@ when ODIN_ARCH == .amd64 {
 	#panic("Unsupported architecture")
 }
 
+// syscall related constants
 AT_FDCWD            :: ~uintptr(99)
 AT_REMOVEDIR        :: uintptr(0x200)
 AT_SYMLINK_FOLLOW   :: uintptr(0x400)
 AT_SYMLINK_NOFOLLOW :: uintptr(0x100)
 
+PROT_NONE  :: 0x0
+PROT_READ  :: 0x1
+PROT_WRITE :: 0x2
+PROT_EXEC  :: 0x4
+PROT_GROWSDOWN :: 0x01000000
+PROT_GROWSUP :: 0x02000000
+
+MAP_FIXED     :: 0x1
+MAP_PRIVATE   :: 0x2
+MAP_SHARED    :: 0x4
+MAP_ANONYMOUS :: 0x20
+
+MADV_NORMAL      :: 0
+MADV_RANDOM      :: 1
+MADV_SEQUENTIAL  :: 2
+MADV_WILLNEED    :: 3
+MADV_DONTNEED    :: 4
+MADV_FREE        :: 8
+MADV_REMOVE      :: 9
+MADV_DONTFORK    :: 10
+MADV_DOFORK      :: 11
+MADV_MERGEABLE   :: 12
+MADV_UNMERGEABLE :: 13
+MADV_HUGEPAGE    :: 14
+MADV_NOHUGEPAGE  :: 15
+MADV_DONTDUMP    :: 16
+MADV_DODUMP      :: 17
+MADV_WIPEONFORK  :: 18
+MADV_KEEPONFORK  :: 19
+MADV_HWPOISON    :: 100
+
 sys_gettid :: proc "contextless" () -> int {
 	return cast(int)intrinsics.syscall(SYS_gettid)
 }
@@ -1531,7 +1563,7 @@ sys_getrandom :: proc "contextless" (buf: ^byte, buflen: int, flags: uint) -> in
 	return cast(int)intrinsics.syscall(SYS_getrandom, buf, cast(uintptr)(buflen), cast(uintptr)(flags))
 }
 
-sys_open :: proc(path: cstring, flags: int, mode: int = 0o000) -> int {
+sys_open :: proc "contextless" (path: cstring, flags: int, mode: int = 0o000) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_open, uintptr(rawptr(path)), uintptr(flags), uintptr(mode)))
 	} else { // NOTE: arm64 does not have open
@@ -1539,19 +1571,19 @@ sys_open :: proc(path: cstring, flags: int, mode: int = 0o000) -> int {
 	}
 }
 
-sys_openat :: proc(dfd: int, path: cstring, flags: int, mode: int = 0o000) -> int {
+sys_openat :: proc "contextless" (dfd: int, path: cstring, flags: int, mode: int = 0o000) -> int {
 	return int(intrinsics.syscall(SYS_openat, uintptr(dfd), uintptr(rawptr(path)), uintptr(flags), uintptr(mode)))
 }
 
-sys_close :: proc(fd: int) -> int {
+sys_close :: proc "contextless" (fd: int) -> int {
 	return int(intrinsics.syscall(SYS_close, uintptr(fd)))
 }
 
-sys_read :: proc(fd: int, buf: rawptr, size: uint) -> int {
+sys_read :: proc "contextless" (fd: int, buf: rawptr, size: uint) -> int {
 	return int(intrinsics.syscall(SYS_read, uintptr(fd), uintptr(buf), uintptr(size)))
 }
 
-sys_pread :: proc(fd: int, buf: rawptr, size: uint, offset: i64) -> int {
+sys_pread :: proc "contextless" (fd: int, buf: rawptr, size: uint, offset: i64) -> int {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return int(intrinsics.syscall(SYS_pread64, uintptr(fd), uintptr(buf), uintptr(size), uintptr(offset)))
 	} else {
@@ -1561,11 +1593,11 @@ sys_pread :: proc(fd: int, buf: rawptr, size: uint, offset: i64) -> int {
 	}
 }
 
-sys_write :: proc(fd: int, buf: rawptr, size: uint) -> int {
+sys_write :: proc "contextless" (fd: int, buf: rawptr, size: uint) -> int {
 	return int(intrinsics.syscall(SYS_write, uintptr(fd), uintptr(buf), uintptr(size)))
 }
 
-sys_pwrite :: proc(fd: int, buf: rawptr, size: uint, offset: i64) -> int {
+sys_pwrite :: proc "contextless" (fd: int, buf: rawptr, size: uint, offset: i64) -> int {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return int(intrinsics.syscall(SYS_pwrite64, uintptr(fd), uintptr(buf), uintptr(size), uintptr(offset)))
 	} else {
@@ -1575,7 +1607,7 @@ sys_pwrite :: proc(fd: int, buf: rawptr, size: uint, offset: i64) -> int {
 	}
 }
 
-sys_lseek :: proc(fd: int, offset: i64, whence: int) -> i64 {
+sys_lseek :: proc "contextless" (fd: int, offset: i64, whence: int) -> i64 {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return i64(intrinsics.syscall(SYS_lseek, uintptr(fd), uintptr(offset), uintptr(whence)))
 	} else {
@@ -1587,7 +1619,7 @@ sys_lseek :: proc(fd: int, offset: i64, whence: int) -> i64 {
 	}
 }
 
-sys_stat :: proc(path: cstring, stat: rawptr) -> int {
+sys_stat :: proc "contextless" (path: cstring, stat: rawptr) -> int {
 	when ODIN_ARCH == .amd64 {
 		return int(intrinsics.syscall(SYS_stat, uintptr(rawptr(path)), uintptr(stat)))
 	} else when ODIN_ARCH != .arm64 {
@@ -1597,7 +1629,7 @@ sys_stat :: proc(path: cstring, stat: rawptr) -> int {
 	}
 }
 
-sys_fstat :: proc(fd: int, stat: rawptr) -> int {
+sys_fstat :: proc "contextless" (fd: int, stat: rawptr) -> int {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return int(intrinsics.syscall(SYS_fstat, uintptr(fd), uintptr(stat)))
 	} else {
@@ -1605,7 +1637,7 @@ sys_fstat :: proc(fd: int, stat: rawptr) -> int {
 	}
 }
 
-sys_lstat :: proc(path: cstring, stat: rawptr) -> int {
+sys_lstat :: proc "contextless" (path: cstring, stat: rawptr) -> int {
 	when ODIN_ARCH == .amd64 {
 		return int(intrinsics.syscall(SYS_lstat, uintptr(rawptr(path)), uintptr(stat)))
 	} else when ODIN_ARCH != .arm64 {
@@ -1615,7 +1647,7 @@ sys_lstat :: proc(path: cstring, stat: rawptr) -> int {
 	}
 }
 
-sys_readlink :: proc(path: cstring, buf: rawptr, bufsiz: uint) -> int {
+sys_readlink :: proc "contextless" (path: cstring, buf: rawptr, bufsiz: uint) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_readlink, uintptr(rawptr(path)), uintptr(buf), uintptr(bufsiz)))
 	} else { // NOTE: arm64 does not have readlink
@@ -1623,7 +1655,7 @@ sys_readlink :: proc(path: cstring, buf: rawptr, bufsiz: uint) -> int {
 	}
 }
 
-sys_symlink :: proc(old_name: cstring, new_name: cstring) -> int {
+sys_symlink :: proc "contextless" (old_name: cstring, new_name: cstring) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_symlink, uintptr(rawptr(old_name)), uintptr(rawptr(new_name))))
 	} else { // NOTE: arm64 does not have symlink
@@ -1631,7 +1663,7 @@ sys_symlink :: proc(old_name: cstring, new_name: cstring) -> int {
 	}
 }
 
-sys_access :: proc(path: cstring, mask: int) -> int {
+sys_access :: proc "contextless" (path: cstring, mask: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_access, uintptr(rawptr(path)), uintptr(mask)))
 	} else { // NOTE: arm64 does not have access
@@ -1639,19 +1671,19 @@ sys_access :: proc(path: cstring, mask: int) -> int {
 	}
 }
 
-sys_getcwd :: proc(buf: rawptr, size: uint) -> int {
+sys_getcwd :: proc "contextless" (buf: rawptr, size: uint) -> int {
 	return int(intrinsics.syscall(SYS_getcwd, uintptr(buf), uintptr(size)))
 }
 
-sys_chdir :: proc(path: cstring) -> int {
+sys_chdir :: proc "contextless" (path: cstring) -> int {
 	return int(intrinsics.syscall(SYS_chdir, uintptr(rawptr(path))))
 }
 
-sys_fchdir :: proc(fd: int) -> int {
+sys_fchdir :: proc "contextless" (fd: int) -> int {
 	return int(intrinsics.syscall(SYS_fchdir, uintptr(fd)))
 }
 
-sys_chmod :: proc(path: cstring, mode: int) -> int {
+sys_chmod :: proc "contextless" (path: cstring, mode: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_chmod, uintptr(rawptr(path)), uintptr(mode)))
 	} else { // NOTE: arm64 does not have chmod
@@ -1659,11 +1691,11 @@ sys_chmod :: proc(path: cstring, mode: int) -> int {
 	}
 }
 
-sys_fchmod :: proc(fd: int, mode: int) -> int {
+sys_fchmod :: proc "contextless" (fd: int, mode: int) -> int {
 	return int(intrinsics.syscall(SYS_fchmod, uintptr(fd), uintptr(mode)))
 }
 
-sys_chown :: proc(path: cstring, user: int, group: int) -> int {
+sys_chown :: proc "contextless" (path: cstring, user: int, group: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_chown, uintptr(rawptr(path)), uintptr(user), uintptr(group)))
 	} else { // NOTE: arm64 does not have chown
@@ -1671,11 +1703,11 @@ sys_chown :: proc(path: cstring, user: int, group: int) -> int {
 	}
 }
 
-sys_fchown :: proc(fd: int, user: int, group: int) -> int {
+sys_fchown :: proc "contextless" (fd: int, user: int, group: int) -> int {
 	return int(intrinsics.syscall(SYS_fchown, uintptr(fd), uintptr(user), uintptr(group)))
 }
 
-sys_lchown :: proc(path: cstring, user: int, group: int) -> int {
+sys_lchown :: proc "contextless" (path: cstring, user: int, group: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_lchown, uintptr(rawptr(path)), uintptr(user), uintptr(group)))
 	} else { // NOTE: arm64 does not have lchown
@@ -1683,7 +1715,7 @@ sys_lchown :: proc(path: cstring, user: int, group: int) -> int {
 	}
 }
 
-sys_rename :: proc(old, new: cstring) -> int {
+sys_rename :: proc "contextless" (old, new: cstring) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_rename, uintptr(rawptr(old)), uintptr(rawptr(new))))
 	} else { // NOTE: arm64 does not have rename
@@ -1691,7 +1723,7 @@ sys_rename :: proc(old, new: cstring) -> int {
 	}
 }
 
-sys_link :: proc(old_name: cstring, new_name: cstring) -> int {
+sys_link :: proc "contextless" (old_name: cstring, new_name: cstring) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_link, uintptr(rawptr(old_name)), uintptr(rawptr(new_name))))
 	} else { // NOTE: arm64 does not have link
@@ -1699,7 +1731,7 @@ sys_link :: proc(old_name: cstring, new_name: cstring) -> int {
 	}
 }
 
-sys_unlink :: proc(path: cstring) -> int {
+sys_unlink :: proc "contextless" (path: cstring) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_unlink, uintptr(rawptr(path))))
 	} else { // NOTE: arm64 does not have unlink
@@ -1707,11 +1739,11 @@ sys_unlink :: proc(path: cstring) -> int {
 	}
 }
 
-sys_unlinkat :: proc(dfd: int, path: cstring, flag: int = 0) -> int {
+sys_unlinkat :: proc "contextless" (dfd: int, path: cstring, flag: int = 0) -> int {
 	return int(intrinsics.syscall(SYS_unlinkat, uintptr(dfd), uintptr(rawptr(path)), flag))
 }
 
-sys_rmdir :: proc(path: cstring) -> int {
+sys_rmdir :: proc "contextless" (path: cstring) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_rmdir, uintptr(rawptr(path))))
 	} else { // NOTE: arm64 does not have rmdir
@@ -1719,7 +1751,7 @@ sys_rmdir :: proc(path: cstring) -> int {
 	}
 }
 
-sys_mkdir :: proc(path: cstring, mode: int) -> int {
+sys_mkdir :: proc "contextless" (path: cstring, mode: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_mkdir, uintptr(rawptr(path)), uintptr(mode)))
 	} else { // NOTE: arm64 does not have mkdir
@@ -1727,11 +1759,11 @@ sys_mkdir :: proc(path: cstring, mode: int) -> int {
 	}
 }
 
-sys_mkdirat :: proc(dfd: int, path: cstring, mode: int) -> int {
+sys_mkdirat :: proc "contextless" (dfd: int, path: cstring, mode: int) -> int {
 	return int(intrinsics.syscall(SYS_mkdirat, uintptr(dfd), uintptr(rawptr(path)), uintptr(mode)))
 }
 
-sys_mknod :: proc(path: cstring, mode: int, dev: int) -> int {
+sys_mknod :: proc "contextless" (path: cstring, mode: int, dev: int) -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_mknod, uintptr(rawptr(path)), uintptr(mode), uintptr(dev)))
 	} else { // NOTE: arm64 does not have mknod
@@ -1739,11 +1771,11 @@ sys_mknod :: proc(path: cstring, mode: int, dev: int) -> int {
 	}
 }
 
-sys_mknodat :: proc(dfd: int, path: cstring, mode: int, dev: int) -> int {
+sys_mknodat :: proc "contextless" (dfd: int, path: cstring, mode: int, dev: int) -> int {
 	return int(intrinsics.syscall(SYS_mknodat, uintptr(dfd), uintptr(rawptr(path)), uintptr(mode), uintptr(dev)))
 }
 
-sys_truncate :: proc(path: cstring, length: i64) -> int {
+sys_truncate :: proc "contextless" (path: cstring, length: i64) -> int {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return int(intrinsics.syscall(SYS_truncate, uintptr(rawptr(path)), uintptr(length)))
 	} else {
@@ -1753,7 +1785,7 @@ sys_truncate :: proc(path: cstring, length: i64) -> int {
 	}
 }
 
-sys_ftruncate :: proc(fd: int, length: i64) -> int {
+sys_ftruncate :: proc "contextless" (fd: int, length: i64) -> int {
 	when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64 {
 		return int(intrinsics.syscall(SYS_ftruncate, uintptr(fd), uintptr(length)))
 	} else {
@@ -1763,15 +1795,15 @@ sys_ftruncate :: proc(fd: int, length: i64) -> int {
 	}
 }
 
-sys_fsync :: proc(fd: int) -> int {
+sys_fsync :: proc "contextless" (fd: int) -> int {
 	return int(intrinsics.syscall(SYS_fsync, uintptr(fd)))
 }
 
-sys_getdents64 :: proc(fd: int, dirent: rawptr, count: int) -> int {
+sys_getdents64 :: proc "contextless" (fd: int, dirent: rawptr, count: int) -> int {
 	return int(intrinsics.syscall(SYS_getdents64, uintptr(fd), uintptr(dirent), uintptr(count)))
 }
 
-sys_fork :: proc() -> int {
+sys_fork :: proc "contextless" () -> int {
 	when ODIN_ARCH != .arm64 {
 		return int(intrinsics.syscall(SYS_fork))
 	} else {
@@ -1779,13 +1811,30 @@ sys_fork :: proc() -> int {
 	}
 }
 
+sys_mmap :: proc "contextless" (addr: rawptr, length: uint, prot, flags, fd: int, offset: uintptr) -> int {
+	return int(intrinsics.syscall(unix.SYS_mmap, uintptr(addr), uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), offset))
+}
+
+sys_munmap :: proc "contextless" (addr: rawptr, length: uint) -> int {
+	return int(intrinsics.syscall(unix.SYS_munmap, uintptr(addr), uintptr(length)))
+}
+
+sys_mprotect :: proc "contextless" (addr: rawptr, length: uint, prot: int) -> int {
+	return int(intrinsics.syscall(unix.SYS_mprotect, uintptr(addr), uintptr(length), uintptr(prot)))
+}
+
+sys_madvise :: proc "contextless" (addr: rawptr, length: uint, advice: int) -> int {
+	return int(intrinsics.syscall(unix.SYS_madvise, uintptr(addr), uintptr(length), uintptr(advice)))
+}
+
+
 // NOTE: Unsure about if this works directly on 32 bit archs. It may need 32 bit version of the time struct.
 //       As of Linux 5.1, there is a utimensat_time64 function.  Maybe use this in the future?
-sys_utimensat :: proc(dfd: int, path: cstring, times: rawptr, flags: int) -> int {
+sys_utimensat :: proc "contextless" (dfd: int, path: cstring, times: rawptr, flags: int) -> int {
 	return int(intrinsics.syscall(SYS_utimensat, uintptr(dfd), uintptr(rawptr(path)), uintptr(times), uintptr(flags)))
 }
 
-get_errno :: proc(res: int) -> i32 {
+get_errno :: proc "contextless" (res: int) -> i32 {
 	if res < 0 && res > -4096 {
 		return i32(-res)
 	}