Physical memory refactor + update lib part + add arguments managements

master
Yann MAGNIN 3 years ago
parent 0a49df9a83
commit 61582ad9f9
  1. 2
      include/kernel/devices/earlyterm.h
  2. 4
      include/kernel/devices/tty.h
  3. 2
      include/kernel/drivers/keyboard.h
  4. 14
      include/kernel/fs/file.h
  5. 2
      include/kernel/fs/filesystem.h
  6. 2
      include/kernel/fs/gladfs.h
  7. 2
      include/kernel/fs/smemfs.h
  8. 2
      include/kernel/fs/vfs.h
  9. 14
      include/kernel/memory.h
  10. 6
      include/kernel/process.h
  11. 7
      include/kernel/syscall.h
  12. 13
      include/kernel/util/kmem.h
  13. 0
      include/lib/asm/types.h
  14. 11
      include/lib/asm/unistd_32.h
  15. 156
      include/lib/errno.h
  16. 20
      include/lib/fcntl.h
  17. 30
      include/lib/stdlib.h
  18. 3
      include/lib/string.h
  19. 44
      include/lib/sys/mman.h
  20. 34
      include/lib/unistd.h
  21. 4
      include/user/shell/builtin.h
  22. 11
      include/user/shell/util.h
  23. 9
      include/user/util.h
  24. 2
      src/kernel/Makefile
  25. 2
      src/kernel/bootstrap/mpu.c
  26. 2
      src/kernel/bootstrap/start.c
  27. 8
      src/kernel/devices/tty/operations/close.c
  28. 18
      src/kernel/devices/tty/operations/open.c
  29. 8
      src/kernel/devices/tty/operations/read.c
  30. 4
      src/kernel/fs/gladfs/superblock/alloc_fragdata.c
  31. 4
      src/kernel/fs/gladfs/superblock/alloc_inode.c
  32. 4
      src/kernel/fs/gladfs/superblock/destroy_fragdata.c
  33. 4
      src/kernel/fs/gladfs/superblock/destroy_inode.c
  34. 1
      src/kernel/fs/syscall/pread.c
  35. 1
      src/kernel/fs/syscall/pwrite.c
  36. 1
      src/kernel/fs/syscall/read.c
  37. 1
      src/kernel/fs/syscall/write.c
  38. 4
      src/kernel/fs/vfs/dentry/alloc.c
  39. 4
      src/kernel/fs/vfs/dentry/free.c
  40. 1
      src/kernel/fs/vfs/file/lseek.c
  41. 1
      src/kernel/loader/entry.c
  42. 7
      src/kernel/loader/reloc_sym.c
  43. 38
      src/kernel/memory/heap/alloc.c
  44. 56
      src/kernel/memory/heap/block.c
  45. 3
      src/kernel/memory/heap/debug.c
  46. 33
      src/kernel/memory/heap/free.c
  47. 108
      src/kernel/memory/heap/realloc.c
  48. 34
      src/kernel/memory/initialize.c
  49. 20
      src/kernel/memory/syscall/sys_proc_heap_alloc.c
  50. 18
      src/kernel/memory/syscall/sys_proc_heap_free.c
  51. 20
      src/kernel/memory/syscall/sys_proc_heap_realloc.c
  52. 4
      src/kernel/scheduler/process/alloc.c
  53. 11
      src/kernel/scheduler/process/create.c
  54. 43
      src/kernel/scheduler/syscall/sys_fexecve.c
  55. 7
      src/kernel/syscall/handler.c
  56. 14
      src/kernel/util/kmem/alloc.c
  57. 12
      src/kernel/util/kmem/debug.c
  58. 12
      src/kernel/util/kmem/free.c
  59. 25
      src/kernel/util/kmem/realloc.c
  60. 2
      src/lib/Makefile
  61. 3
      src/lib/display/dascii.c
  62. 2
      src/lib/display/dclear.c
  63. 2
      src/lib/display/dopen.c
  64. 2
      src/lib/display/dreverse.c
  65. 2
      src/lib/display/dscroll.c
  66. 2
      src/lib/display/font_default5x3.c
  67. 2
      src/lib/libc/stdio/dprintf.c
  68. 2
      src/lib/libc/stdio/internal/printf_actions.c
  69. 2
      src/lib/libc/stdio/internal/printf_common.c
  70. 2
      src/lib/libc/stdio/internal/printf_options.c
  71. 4
      src/lib/libc/stdio/printf.c
  72. 4
      src/lib/libc/stdio/putc.c
  73. 6
      src/lib/libc/stdio/puts.c
  74. 2
      src/lib/libc/stdio/snprintf.c
  75. 2
      src/lib/libc/stdio/sprintf.c
  76. 4
      src/lib/libc/stdio/vdprintf.c
  77. 2
      src/lib/libc/stdio/vsnprintf.c
  78. 2
      src/lib/libc/stdio/vsprintf.c
  79. 16
      src/lib/libc/stdlib/calloc.c
  80. 17
      src/lib/libc/stdlib/free.S
  81. 16
      src/lib/libc/stdlib/malloc.S
  82. 46
      src/lib/libc/stdlib/realloc.S
  83. 8
      src/lib/libc/stdlib/reallocarray.c
  84. 3
      src/lib/libc/string/memcpy.c
  85. 2
      src/lib/libc/string/memset.c
  86. 2
      src/lib/libc/string/strcat.c
  87. 2
      src/lib/libc/string/strchr.c
  88. 2
      src/lib/libc/string/strcmp.c
  89. 2
      src/lib/libc/string/strcpy.c
  90. 22
      src/lib/libc/string/strdup.c
  91. 2
      src/lib/libc/string/strlen.c
  92. 2
      src/lib/libc/unistd/close.S
  93. 2
      src/lib/libc/unistd/fexecve.S
  94. 2
      src/lib/libc/unistd/getpgid.S
  95. 2
      src/lib/libc/unistd/getpid.S
  96. 2
      src/lib/libc/unistd/getppid.S
  97. 2
      src/lib/libc/unistd/lseek.S
  98. 2
      src/lib/libc/unistd/open.S
  99. 2
      src/lib/libc/unistd/read.S
  100. 2
      src/lib/libc/unistd/setpgid.S
  101. Some files were not shown because too many files have changed in this diff Show More

@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include <lib/display.h>
#include <display.h>
// Wait for debug (dirty)
// TODO: use earlyterm_read() to wait key event ?

@ -3,9 +3,9 @@
#include <stddef.h>
#include <stdint.h>
#include <asm/types.h>
#include <kernel/drivers/screen.h>
#include <kernel/util/types.h>
#include <lib/display.h>
#include <display.h>
// Define default buffer size.
// TODO: remove me ?

@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include <kernel/util/types.h>
#include <asm/types.h>
#define KEYCODE_GEN(row, column) \
(((row & 0x0f) << 4) | ((column & 0x0f) << 0))

@ -3,24 +3,14 @@
#include <stdint.h>
#include <stddef.h>
#include <kernel/util/types.h>
#include <asm/types.h>
#include <kernel/fs/filesystem.h>
#ifndef FILE_OPEN_NUMBER
# define FILE_OPEN_NUMBER 4
#endif
// TODO: fix me
#ifndef __LIB_UNISTD_H__
# define O_RDONLY 0x00
# define O_WRONLY 0x01
# define O_RDWR 0x02
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
// FILE type
struct file_s
{
void *private;

@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include <kernel/util/types.h>
#include <asm/types.h>
// FS flags
#define FS_RDWR (0x01)

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stddef.h>
#include <kernel/util/types.h>
#include <asm/types.h>
# define GLADFS_INODE_NAME_LENGHT (16)

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stddef.h>
#include <kernel/util/types.h>
#include <asm/types.h>
#include <kernel/fs/file.h>
#define CASIO_SMEM_NAME_LENGHT 12

@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include <kernel/util/types.h>
#include <asm/types.h>
#include <kernel/fs/filesystem.h>
#include <kernel/fs/file.h>
#include <kernel/devices/device.h>

@ -105,14 +105,20 @@ struct memory_info
};
// Function
extern void *pm_alloc(size_t size);
extern void pm_free(void *ptr);
extern void pm_debug(void);
// "user" function
extern void *pm_heap_alloc(struct pm_heap_page **page, size_t size);
extern void *pm_heap_realloc(struct pm_heap_page **page, void *ptr, size_t size);
extern void pm_heap_debug(struct pm_heap_page *page);
extern void pm_heap_free(struct pm_heap_page *page, void *ptr);
// Page allocator
extern void *pm_pages_alloc(int nb_pages);
extern void pm_pages_free(void *addr);
// Internal helpers
extern void *pm_block_split(struct pm_heap_block *block, size_t size);
extern void pm_block_frontmerge(struct pm_heap_block *block, void *brk);
extern void pm_block_backmerge(struct pm_heap_block **block, struct pm_heap_block *parent);
#endif /*__KERNEL_MEMORY_H__*/

@ -6,7 +6,7 @@
#include <kernel/fs/file.h>
#include <kernel/fs/filesystem.h>
#include <kernel/context.h>
#include <kernel/util/types.h>
#include <asm/types.h>
#define PROCESS_NB_OPEN_FILE (4)
#define PROCESS_USER_STACK_SIZE (2 * 1024)
@ -78,7 +78,7 @@ struct process
void *start;
size_t size;
} exit;
struct heap *heap;
struct pm_heap_page *heap;
} memory;
@ -120,7 +120,7 @@ extern struct process *process_create(void);
extern int process_switch(pid_t pid);
// Internals
// Internals (kernel only)
extern struct process *process_alloc(void);
extern struct process *process_get(pid_t pid);
extern int process_free(struct process *process);

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stddef.h>
#include <kernel/util/types.h>
#include <asm/types.h>
// Process
extern pid_t sys_getpid(void);
@ -12,7 +12,7 @@ extern pid_t sys_getpgid(void);
extern pid_t sys_wait(int *wstatus);
extern int sys_setpgid(pid_t pid, pid_t pgid);
extern pid_t sys_waitpid(pid_t pid, int *wstatus, int options);
extern pid_t sys_fexecve(const char *pathname);
extern pid_t sys_fexecve(const char *pathname, char **argv, char **envp);
extern void sys_exit(int status);
// File
@ -28,5 +28,8 @@ extern int sys_close(int fd);
extern void *sys_mmap(void *addr, size_t length,
int prot, int flags, int fd, off_t offset);
extern int sys_munmap(void *addr, size_t length);
extern void *sys_proc_heap_alloc(size_t size);
extern void *sys_proc_heap_realloc(void *ptr, size_t size);
extern void sys_proc_heap_free(void *ptr);
#endif /*__SYSCALL_H__*/

@ -0,0 +1,13 @@
#ifndef __KERNEL_UTIL_KMEM_H__
# define __KERNEL_UTIL_KMEM_H__
#include <stddef.h>
#include <stdint.h>
// Kernel memomries allocation function wrapper
extern void *kmem_alloc(size_t size);
extern void *kmem_realloc(void *ptr, size_t size);
extern void kmem_free(void *ptr);
extern void kmem_debug(void);
#endif /*__KERNEL_UTIL_KMEM_H__*/

@ -1,8 +1,8 @@
#ifndef __KERNEL_UNISTD_32_H__
# define __KERNEL_UNISTD_32_H__
#ifndef __ASM_UNISTD_32_H__
# define __ASM_UNISTD_32_H__
// Define the number of syscall
#define __NR_MAX 18
#define __NR_MAX 21
// Kernel Test
#define __NR_test_syscall 0
@ -29,5 +29,8 @@
// Memory
#define __NR_mmap 16
#define __NR_munmap 17
#define __NR_proc_heap_alloc 18
#define __NR_proc_heap_free 19
#define __NR_proc_heap_realloc 20
#endif /*__KERNEL_UNISTD_32_H__*/
#endif /*__ASM_UNISTD_32_H__*/

@ -0,0 +1,156 @@
#ifndef __KERNEL_ERRNO_H__
# define __KERNEL_ERRNO_H__ 1
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOLCK 37 /* No record locks available */
/*
* This error code is special: arch syscall entry code will return
* -ENOSYS if users try to call a syscall that doesn't exist. To keep
* failures of syscalls that really do exist distinguishable from
* failures due to attempts to use a nonexistent syscall, syscall
* implementations should refrain from returning -ENOSYS.
*/
#define ENOSYS 38 /* Invalid system call number */
#define ENOTEMPTY 39 /* Directory not empty */
#define ELOOP 40 /* Too many symbolic links encountered */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#define ENOMSG 42 /* No message of desired type */
#define EIDRM 43 /* Identifier removed */
#define ECHRNG 44 /* Channel number out of range */
#define EL2NSYNC 45 /* Level 2 not synchronized */
#define EL3HLT 46 /* Level 3 halted */
#define EL3RST 47 /* Level 3 reset */
#define ELNRNG 48 /* Link number out of range */
#define EUNATCH 49 /* Protocol driver not attached */
#define ENOCSI 50 /* No CSI structure available */
#define EL2HLT 51 /* Level 2 halted */
#define EBADE 52 /* Invalid exchange */
#define EBADR 53 /* Invalid request descriptor */
#define EXFULL 54 /* Exchange full */
#define ENOANO 55 /* No anode */
#define EBADRQC 56 /* Invalid request code */
#define EBADSLT 57 /* Invalid slot */
#define EDEADLOCK EDEADLK
#define EBFONT 59 /* Bad font file format */
#define ENOSTR 60 /* Device not a stream */
#define ENODATA 61 /* No data available */
#define ETIME 62 /* Timer expired */
#define ENOSR 63 /* Out of streams resources */
#define ENONET 64 /* Machine is not on the network */
#define ENOPKG 65 /* Package not installed */
#define EREMOTE 66 /* Object is remote */
#define ENOLINK 67 /* Link has been severed */
#define EADV 68 /* Advertise error */
#define ESRMNT 69 /* Srmount error */
#define ECOMM 70 /* Communication error on send */
#define EPROTO 71 /* Protocol error */
#define EMULTIHOP 72 /* Multihop attempted */
#define EDOTDOT 73 /* RFS specific error */
#define EBADMSG 74 /* Not a data message */
#define EOVERFLOW 75 /* Value too large for defined data type */
#define ENOTUNIQ 76 /* Name not unique on network */
#define EBADFD 77 /* File descriptor in bad state */
#define EREMCHG 78 /* Remote address changed */
#define ELIBACC 79 /* Can not access a needed shared library */
#define ELIBBAD 80 /* Accessing a corrupted shared library */
#define ELIBSCN 81 /* .lib section in a.out corrupted */
#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
#define ELIBEXEC 83 /* Cannot exec a shared library directly */
#define EILSEQ 84 /* Illegal byte sequence */
#define ERESTART 85 /* Interrupted system call should be restarted */
#define ESTRPIPE 86 /* Streams pipe error */
#define EUSERS 87 /* Too many users */
#define ENOTSOCK 88 /* Socket operation on non-socket */
#define EDESTADDRREQ 89 /* Destination address required */
#define EMSGSIZE 90 /* Message too long */
#define EPROTOTYPE 91 /* Protocol wrong type for socket */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define ENOTSUP EOPNOTSUPP /* Not supported (may be the same value as [EOPNOTSUPP]) */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET 102 /* Network dropped connection because of reset */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS 109 /* Too many references: cannot splice */
#define ETIMEDOUT 110 /* Connection timed out */
#define ECONNREFUSED 111 /* Connection refused */
#define EHOSTDOWN 112 /* Host is down */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE 116 /* Stale file handle */
#define EUCLEAN 117 /* Structure needs cleaning */
#define ENOTNAM 118 /* Not a XENIX named type file */
#define ENAVAIL 119 /* No XENIX semaphores available */
#define EISNAM 120 /* Is a named type file */
#define EREMOTEIO 121 /* Remote I/O error */
#define EDQUOT 122 /* Quota exceeded */
#define ENOMEDIUM 123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#define ECANCELED 125 /* Operation Canceled */
#define ENOKEY 126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED 129 /* Key was rejected by service */
/* for robust mutexes */
#define EOWNERDEAD 130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */
#define ERFKILL 132 /* Operation not possible due to RF-kill */
#define EHWPOISON 133 /* Memory page has hardware error */
#endif /* __KERNEL_ERRNO_H__ */

@ -0,0 +1,20 @@
#ifndef __LIB_FCNTL_H__
# define __LIB_FCNTL_H__
#include <stddef.h>
#include <stdint.h>
//TODO: update me !!
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
//TODO: define also in <stdio.h>
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
// Functions
extern int open(const char *pathname, int flags, ...);
#endif /*__LIB_FCNTL_H__*/

@ -0,0 +1,30 @@
#ifndef __LIB_STDLIB_H__
# define __LIB_STDLIB_H__
#include <stddef.h>
#include <stdint.h>
#define HEAP_BLOCK_MIN (16)
struct heap_block
{
uint16_t status; /* status of the block (1=used,0=free) */
uint16_t size; /* size of the block (without block header) */
} __attribute__((packed, aligned(2)));
struct heap_page
{
void *brk; /* break (address of the limit area) */
size_t size; /* Page size (without header) */
struct heap_page *next; /* Next heap page */
struct heap_block heap; /* Start of block informations */
} __attribute__((packed, aligned(4)));
// Functions
extern void *malloc(size_t size);
extern void *calloc(size_t nmemb, size_t size);
extern void *realloc(void *ptr, size_t size);
extern void *reallocarray(void *ptr, size_t nmemb, size_t size);
extern void free(void *ptr);
#endif /*__LIB_STDLIB_H__*/

@ -26,4 +26,7 @@ extern size_t strlen(char const *str);
/* strrchr() - find the last occurent of a byte */
extern char *strrchr(const char *s, int c);
/* strdup() - dump string */
extern char *strdup(const char *s);
#endif /*__STRING_H__*/

@ -0,0 +1,44 @@
#ifndef __LIB_SYS_MMAN_H__
# define __LIB_SYS_MMAN_H__
#include <stddef.h>
#include <stdint.h>
#ifndef __KERNEL_MEMORY_H__
// mapping flags
// Protection (unused)
#define PROT_READ 0x1 /* Page can be read. */
#define PROT_WRITE 0x2 /* Page can be written. */
#define PROT_EXEC 0x4 /* Page can be executed. */
#define PROT_NONE 0x0 /* Page can not be accessed. */
// Mapping flags
#define MAP_SHARED 0x01 /* Share changes. */
#define MAP_PRIVATE 0x02 /* Changes are private. */
#define MAP_ANONYMOUS 0x20 /* Don't use a file. */
#define MAP_ANON MAP_ANONYMOUS /* Don't use a file. */
#define MAP_FILE 0 /* Ignored. */
#define MAP_GROWSDOWN 0x00100 /* Stack-like segment. */
#define MAP_DENYWRITE 0x00800 /* ETXTBSY. */
#define MAP_EXECUTABLE 0x01000 /* Mark it as an executable. */
#define MAP_LOCKED 0x02000 /* Lock the mapping. */
#define MAP_NORESERVE 0x04000 /* Don't check for reservations. */
#define MAP_POPULATE 0x08000 /* Populate (prefault) pagetables. */
#define MAP_NONBLOCK 0x10000 /* Do not block on IO. */
#define MAP_STACK 0x20000 /* Allocation is for a stack. */
#define MAP_HUGETLB 0x40000 /* Create huge page mapping. */
#define MAP_SYNC 0x80000 /* Perform synchronous page
faults for the mapping. */
#define MAP_FIXED_NOREPLACE 0x100000 /* MAP_FIXED but do not unmap
underlying mapping. */
#define MAP_UNINITIALIZED 0x200000 /* Don't clear anonymous page */
// Value returned when mmap value
#define MAP_FAILED ((void*) -1)
#endif
// Prototype
extern void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
extern int munmap(void *addr, size_t length);
#endif /*__LIB_SYS_MMAN_H__*/

@ -3,16 +3,18 @@
#include <stddef.h>
#include <stdint.h>
#include <kernel/util/types.h>
// Define syscall LIST
#include <kernel/util/unistd_32.h>
#include <asm/types.h>
// TODO: move me
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
//TODO: move me
#define _SC_PAGE_SIZE 0
#define _SC_PAGESIZE _SC_PAGE_SIZE
//TODO: move me
#define WNOHANG 0
#define WUNTRACED 1
@ -23,31 +25,21 @@
#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WEXITSTATUS(status) __WEXITSTATUS(status)
// Process part
extern pid_t getpid(void);
extern pid_t getpgid(void);
extern pid_t getppid(void);
extern int setpgid(pid_t pid, pid_t pgid);
extern pid_t waitpid(pid_t pid, int *wstatus, int options);
extern pid_t fexecve(const char *pathname);
// File syscall
// TODO: move me
// FIXME: find real value
#ifndef __FILE_H__
# define O_DIRECT 0
# define O_DIRECTORY 1
# define O_RDONLY 2
# define O_WRONLY 4
# define O_RDWR 8
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
extern int open(const char *pathname, int flags, ...);
extern pid_t fexecve(const char *pathname, char **argv, char **envp);
// File part
extern ssize_t write(int fd, const void *buf, size_t count);
extern ssize_t read(int fd, void *buf, size_t count);
extern off_t lseek(int fd, off_t offset, int whence);
extern int close(int fd);
// System part
extern long sysconf(int name);
#endif /*__LIB_UNISTD_H__*/

@ -8,7 +8,7 @@
// Generate a "command block" stored in ".cmd.cahce" section.
// We need to "hide" the block, so we generate anonyme name using
// __COUNTER__ macros.
#define gen_name(n) _##n
/*#define gen_name(n) _##n
#define anonym_name(n) gen_name(n)
#define VHEX_BUILTIN(builtin) \
static int builtin(int argc, char **argv); \
@ -18,6 +18,7 @@
.entry = builtin \
}; \
static int builtin(int argc, char **argv)
*/
// Define builtin struct.
struct builtin_s
@ -29,6 +30,7 @@ struct builtin_s
// Builtin list
extern int builtin_proc(void);
extern int builtin_ram(void);
extern int builtin_fxdb(int argc, char **argv);
#endif /*__USER_BUILTIN_H__*/

@ -0,0 +1,11 @@
#ifndef __USER_UTIL_H__
# define __USER_UTIL_H__
#include <stddef.h>
#include <stdint.h>
extern int check_builtin(int argc, char **argv);
extern int strtotab(int *argc, char ***argv, char const *str);
extern void strtotab_quit(int *argc, char ***argv);
#endif /*__USER_UTIL_H__*/

@ -1,9 +0,0 @@
#ifndef __USER_UTIL_H__
# define __USER_UTIL_H__
#include <stddef.h>
#include <stdint.h>
extern int check_builtin(char *cmd);
#endif /*__USER_UTIL_H__*/

@ -11,7 +11,7 @@ include ../../global.mk
##---
## Static variables
##--
HEADER := -I../../include
HEADER := -I../../include -I../../include/lib
BUILD := ../../build/kernel
OUTPUT := ../../output
DEBUG := ../../debug_bin

@ -1,4 +1,4 @@
#include <kernel/util/types.h>
#include <asm/types.h>
/* check_sh3 - Detecting sh3-based MPU */
static mpu_t check_sh3(uint16_t tplcr)

@ -1,7 +1,7 @@
#include <stdint.h>
#include <stddef.h>
// Internal helpers
#include <kernel/util/types.h>
#include <asm/types.h>
#include <kernel/util/atomic.h>
#include <kernel/util/casio.h>
// Modules

@ -1,5 +1,5 @@
#include <kernel/devices/tty.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
int tty_close(void *inode)
{
@ -13,10 +13,10 @@ int tty_close(void *inode)
// output buffer
line = tty->cursor.max.y;
while (line >= 0)
pm_free(tty->buffers.output[line]);
pm_free(tty->buffers.output);
kmem_free(tty->buffers.output[line]);
kmem_free(tty->buffers.output);
// Free'd tty object
pm_free(tty);
kmem_free(tty);
return (0);
}

@ -1,7 +1,7 @@
#include <kernel/devices/tty.h>
#include <kernel/devices/earlyterm.h>
#include <kernel/drivers/screen.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <lib/display.h>
#include <lib/string.h>
@ -15,14 +15,14 @@ void *tty_open(dev_t major, dev_t minor)
(void)minor;
// Try to allocate tty object
tty = (struct tty_s*)pm_alloc(sizeof(struct tty_s));
tty = (struct tty_s*)kmem_alloc(sizeof(struct tty_s));
if (tty == NULL)
return (NULL);
// Get the font used by the tty.
if (dopen(&tty->disp, "default") != 0)
{
pm_free(tty);
kmem_free(tty);
return (NULL);
}
@ -38,10 +38,10 @@ void *tty_open(dev_t major, dev_t minor)
tty->cursor.max.y = tty->cursor.max.y * 4;
// Try to alloc new tty output buffer
tty->buffers.output = (char **)pm_alloc(sizeof(char *) * tty->cursor.max.y);
tty->buffers.output = (char **)kmem_alloc(sizeof(char *) * tty->cursor.max.y);
if (tty->buffers.output == NULL)
{
pm_free(tty);
kmem_free(tty);
return (NULL);
}
@ -50,16 +50,16 @@ void *tty_open(dev_t major, dev_t minor)
while (--line >= 0)
{
// Try to alloc the line
tty->buffers.output[line] = (char*)pm_alloc(tty->cursor.max.x);
tty->buffers.output[line] = (char*)kmem_alloc(tty->cursor.max.x);
if (tty->buffers.output[line] != NULL) {
memset(tty->buffers.output[line], '\0', tty->cursor.max.x);
continue;
}
// Release all allocated space and return NULL
while (++line < tty->cursor.max.y)
pm_free(tty->buffers.output[line]);
pm_free(tty->buffers.output);
pm_free(tty);
kmem_free(tty->buffers.output[line]);
kmem_free(tty->buffers.output);
kmem_free(tty);
return (NULL);
}

@ -119,7 +119,13 @@ static int check_special(struct keyboard_obj_s *keyboard, key_t key)
return (1);
}
// Check DEL key.
// Check space key
if (key == KEY_DOT && (keyboard->mode & 1) == 0) {
buffer_insert(keyboard, ' ');
return (1);
}
// Check DEL key.
if (key == KEY_DEL)
{
// Check potential error.

@ -1,5 +1,5 @@
#include <kernel/fs/gladfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <kernel/util/atomic.h>
/* gladfs_alloc_fragdata() - Superblock primitive to alloc "empty" fragment data block (sync) */
@ -15,7 +15,7 @@ int gladfs_alloc_fragdata(struct gladfs_fragment_data_s **parent, int nb_block)
atomic_start();
// Try to alloc block
*parent = pm_alloc(gladfs_superblock.block_size * nb_block);
*parent = kmem_alloc(gladfs_superblock.block_size * nb_block);
if (*parent == NULL)
{
atomic_stop();

@ -1,5 +1,5 @@
#include <kernel/fs/gladfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <kernel/util/atomic.h>
#include <lib/string.h>
@ -12,7 +12,7 @@ struct gladfs_inode_s *gladfs_alloc_inode(const char *name, mode_t mode)
atomic_start();
// alloc memory
inode = pm_alloc(sizeof(struct gladfs_inode_s));
inode = kmem_alloc(sizeof(struct gladfs_inode_s));
if (inode == NULL)
{
atomic_stop();

@ -1,5 +1,5 @@
#include <kernel/fs/gladfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <kernel/util/atomic.h>
/* gladfs_destroy_fragdata() - Free'd allocated fragmented data (sync) */
@ -14,7 +14,7 @@ int gladfs_destroy_fragdata(struct gladfs_fragment_data_s *fragment)
atomic_start();
// Free'd allocated space
pm_free(fragment);
kmem_free(fragment);
// Stop atomic operation
atomic_stop();

@ -1,5 +1,5 @@
#include <kernel/fs/gladfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <kernel/util/atomic.h>
/* gladfs_destroy_inode() - Free'd allocated inode (sync) */
@ -29,7 +29,7 @@ int gladfs_destroy_inode(struct gladfs_inode_s *inode)
}
// Free inode
pm_free(inode);
kmem_free(inode);
// Stop atomic operations
atomic_stop();

@ -1,5 +1,6 @@
#include <kernel/fs/vfs.h>
#include <kernel/process.h>
#include <fcntl.h>
ssize_t sys_pread(int fd, void *buf, size_t count, off_t offset)
{

@ -1,5 +1,6 @@
#include <kernel/fs/vfs.h>
#include <kernel/process.h>
#include <fcntl.h>
ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t offset)
{

@ -1,5 +1,6 @@
#include <kernel/fs/vfs.h>
#include <kernel/process.h>
#include <fcntl.h>
ssize_t sys_read(int fd, void *buf, size_t count)
{

@ -1,5 +1,6 @@
#include <kernel/fs/vfs.h>
#include <kernel/process.h>
#include <fcntl.h>
ssize_t sys_write(int fd, const void *buf, size_t count)
{

@ -1,5 +1,5 @@
#include <kernel/fs/vfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
#include <lib/string.h>
/* vfs_dentry_alloc() - Allocate new "empty" dentry */
@ -8,7 +8,7 @@ struct dentry *vfs_dentry_alloc(const char *name, mode_t mode)
struct dentry *node;
// Try to create new dentry
node = pm_alloc(sizeof(struct dentry));
node = kmem_alloc(sizeof(struct dentry));
if (node == NULL)
return (NULL);

@ -1,5 +1,5 @@
#include <kernel/fs/vfs.h>
#include <kernel/memory.h>
#include <kernel/util/kmem.h>
/* vfs_dentry_free() - Free'd allocated dentry */
/* @note: *WARNING* no verification will be done, so do not use this primitive */
@ -10,5 +10,5 @@ void vfs_dentry_free(struct dentry *dentry)
return;
// Free'd allocated space
pm_free(dentry);
kmem_free(dentry);
}

@ -1,5 +1,6 @@
#include <kernel/fs/vfs.h>
#include <kernel/fs/file.h>
#include <fcntl.h>
/* vfs_lseek() - File cursor management */
off_t vfs_lseek(FILE *file, off_t offset, int whence)

@ -3,6 +3,7 @@
#include <kernel/fs/file.h>
#include <kernel/util/elf.h>
#include <kernel/devices/earlyterm.h>
#include <fcntl.h>
//
// TODO: write doc

@ -1,5 +1,6 @@
#include <kernel/loader.h>
#include <kernel/devices/earlyterm.h>
#include <kernel/util/kmem.h>
#include <kernel/memory.h>
#include <kernel/fs/vfs.h>
@ -19,7 +20,7 @@ static char *get_shstrtab(FILE *file, Elf32_Ehdr *header)
// Allocate dump area
earlyterm_write("Try to alloc strtab (%do)\n", shdr.sh_size);
shstrtab = (char*)pm_alloc(shdr.sh_size);
shstrtab = (char*)kmem_alloc(shdr.sh_size);
if (shstrtab == NULL)
{
earlyterm_write("relo_sym:mem (%d)\n", shdr.sh_size);
@ -30,7 +31,7 @@ static char *get_shstrtab(FILE *file, Elf32_Ehdr *header)
if (vfs_pread(file, shstrtab, shdr.sh_size, shdr.sh_offset) != (ssize_t)shdr.sh_size)
{
earlyterm_write("relo_sym: shstrtab size error\n");
pm_free(shstrtab);
kmem_free(shstrtab);
return (NULL);
}
return (shstrtab);
@ -102,6 +103,6 @@ int loader_reloc_sym(struct process *process, FILE *file, Elf32_Ehdr *header)
if (reloc_section(process, file, &shdr) != 0)
return (-3);
}
pm_free(shstrtab);
kmem_free(shstrtab);
return (0);
}

@ -2,35 +2,6 @@
#include <kernel/util/atomic.h>
#include <kernel/devices/earlyterm.h>
static void *pm_block_split(struct pm_heap_block *block, size_t size)
{
size_t rest_size;
void *ret;
// Check size
if (block->size < size)
return (NULL);
// Change current block status
block->status = 1;
// Check if we can not split
rest_size = block->size - (size + sizeof(struct pm_heap_block));
if (rest_size < PM_HEAP_BLOCK_MIN)
return (&block[1]);
// Update current block size and get the
// returned value
block->size = size;
ret = &block[1];
// Initialize seconde block
block = (void *)&block[1] + block->size;
block->status = 0;
block->size = rest_size;
return (ret);
}
static void *new_pages(struct pm_heap_page **page, size_t size)
{
int nb_page;
@ -57,7 +28,7 @@ static void *new_pages(struct pm_heap_page **page, size_t size)
return (pm_block_split(&(*page)->heap, size));
}
static void *pm_heap_alloc(struct pm_heap_page *page, size_t size)
static void *pm_heap_page_check(struct pm_heap_page *page, size_t size)
{
struct pm_heap_block *block;
size_t rest_size;
@ -85,10 +56,8 @@ static void *pm_heap_alloc(struct pm_heap_page *page, size_t size)
}
// Kernel malloc wrapper
void *pm_alloc(size_t size)
void *pm_heap_alloc(struct pm_heap_page **page, size_t size)
{
extern struct memory_info pmemory;
struct pm_heap_page **page;
void *ret;
// Force 4-align
@ -98,7 +67,6 @@ void *pm_alloc(size_t size)
atomic_start();
// Walk into each pages x_x
page = &pmemory.kheap;
while (*page != NULL)
{
// Check "page" size
@ -108,7 +76,7 @@ void *pm_alloc(size_t size)
}
// Try to find free space in the page
ret = pm_heap_alloc(*page, size);
ret = pm_heap_page_check(*page, size);
if (ret != NULL) {
atomic_stop();
return (ret);

@ -0,0 +1,56 @@
#include <kernel/memory.h>
void *pm_block_split(struct pm_heap_block *block, size_t size)
{
int rest_size;
void *ret;
// Check size
if (block->size < size)
return (NULL);
// Change current block status
block->status = 1;
// Check if we can not split
rest_size = block->size - (size + sizeof(struct pm_heap_block));
if (rest_size < PM_HEAP_BLOCK_MIN)
return (&block[1]);
// Update current block size and get the
// returned value
block->size = size;
ret = &block[1];
// Initialize seconde block
block = (void *)&block[1] + block->size;
block->status = 0;
block->size = rest_size;
return (ret);
}
void pm_block_backmerge(struct pm_heap_block **block, struct pm_heap_block *parent)
{
// Check back-merge possibility
if (parent == NULL || parent->status != 0)
return;
// Absorb current block
parent->size += (*block)->size + sizeof(struct pm_heap_block);
// Switch current block
*block = parent;
}
void pm_block_frontmerge(struct pm_heap_block *block, void *brk)
{
struct pm_heap_block *block_front;
// Check front-merge possibility
block_front = (void*)&block[1] + block->size;
if ((void*)block_front >= brk || block_front->status != 0)
return;
// Absorb front block
block->size += block_front->size + sizeof(struct pm_heap_block);
}

@ -1,11 +1,10 @@
#include <kernel/memory.h>
#include <kernel/devices/earlyterm.h>
void pm_debug(void)
void pm_heap_debug(struct pm_heap_page *page)
{
extern struct memory_info pmemory;
struct pm_heap_block *block;
struct pm_heap_page *page;
page = pmemory.kheap;
while (page != NULL)

@ -2,33 +2,6 @@
#include <kernel/util/atomic.h>
#include <kernel/devices/earlyterm.h>
static void pm_block_backmerge(struct pm_heap_block **block,
struct pm_heap_block *parent)
{
// Check back-merge possibility
if (parent == NULL || parent->status != 0)
return;
// Absorb current block
parent->size += (*block)->size + sizeof(struct pm_heap_block);
// Switch current block
*block = parent;
}
static void pm_block_frontmerge(struct pm_heap_block *block, void *brk)
{
struct pm_heap_block *block_front;
// Check front-merge possibility
block_front = (void*)&block[1] + block->size;
if ((void*)block_front >= brk || block_front->status != 0)
return;
// Absorb front block
block->size += block_front->size + sizeof(struct pm_heap_block);
}
static int pm_block_free(struct pm_heap_page *page, void *ptr)
{
struct pm_heap_block *block_parent;
@ -57,16 +30,12 @@ static int pm_block_free(struct pm_heap_page *page, void *ptr)
return (-1);
}
void pm_free(void *ptr)
void pm_heap_free(struct pm_heap_page *page, void *ptr)
{
extern struct memory_info pmemory;
struct pm_heap_page *page;
// Start atomic operations
atomic_start();
// Try to find the page
page = pmemory.kheap;
while (page != NULL)
{
// If is the page is found

@ -0,0 +1,108 @@
#include <kernel/memory.h>
#include <kernel/util/atomic.h>
#include <kernel/devices/earlyterm.h>
#include <string.h>
// Internal function
extern void *pm_block_split(struct pm_heap_block *block, size_t size);
extern void pm_block_backmerge(struct pm_heap_block **block, struct pm_heap_block *parent);
extern void pm_block_frontmerge(struct pm_heap_block *block, void *brk);
static int pm_block_update(struct pm_heap_block *block, size_t size, void *brk)
{
struct pm_heap_block *block_front;
// Check front block validity
block_front = (void*)&block[1] + block->size;
if ((void*)block_front >= brk || block_front->status != 0)
return (-1);
// Check size
if (block_front->size + sizeof(struct pm_heap_block) + block->size < size)
return (-1);
// merge front block and try to split the block
block->size += block_front->size + sizeof(struct pm_heap_block);
pm_block_split(block, size);
return (0);
}
static void *pm_block_realloc(struct pm_heap_page **page, void *ptr, size_t size)
{
struct pm_heap_block *block_parent;
struct pm_heap_block *block;
struct pm_heap_block *new;
block_parent = NULL;
block = &(*page)->heap;
while ((void*)block < (*page)->brk)
{
// check block validity
if ((void*)&block[1] != ptr)
{
block_parent = block;
block = (void*)&block[1] + block->size;
continue;
}
// Check useless action
if (block->size >= size || pm_block_update(block, size, (*page)->brk) == 0)
return (&block[1]);
// Manually alloc new node
new = pm_heap_alloc(page, size);
if (new == NULL)
return (&block[1]);
// Copie old place
memcpy(new, &block[1], block->size);
// Try back-merge / front merge current block
pm_block_backmerge(&block, block_parent);
pm_block_frontmerge(block, (*page)->brk);
// Update block status
block->status = 0;
return (new);
}
return (NULL);
}
void *pm_heap_realloc(struct pm_heap_page **page, void *ptr, size_t size)
{
void *ret;
// Force 4-align
size = (size + 3) >> 2 << 2;
// Start atomic operations
atomic_start();
// Try to find the page
while (page != NULL)
{