Subject | Hash | Author | Date (UTC) |
---|---|---|---|
initial commit | 5310fde2d021e3505f8f1d5d9091726c236967ec | Sylvain BERTRAND | 2011-12-07 11:29:18 |
File README added (mode: 100644) (index 0000000..467ddb5) | |||
1 | Configure in conf.mk, then run make and follow the instruction. | ||
2 | |||
3 | The scsi_scan_wait, is a module that will hold while loading until the scsi | ||
4 | async scan is complete. You can remove it once done. | ||
5 | |||
6 | coding style is c99 compact: | ||
7 | - indent is 2 spaces | ||
8 | - 80 columns | ||
9 | - comments use // |
File conf.mk added (mode: 100644) (index 0000000..0fb75b9) | |||
1 | export | ||
2 | DEBUG?=1 | ||
3 | SYSROOT?= | ||
4 | KERNEL_VERSION?=$(shell uname -r) | ||
5 | ARCH?=$(shell uname -m | sed -e s/i.86/i386/ -e s/parisc64/parisc/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/sh.*/sh/) | ||
6 | |||
7 | BUILD_DIR?=build | ||
8 | OBJ_DIR?=$(BUILD_DIR)/obj | ||
9 | CPIO_DIR?=$(BUILD_DIR)/cpio | ||
10 | MODULES_DIR?=$(CPIO_DIR)/modules | ||
11 | |||
12 | LD:=ld | ||
13 | CC:=gcc | ||
14 | |||
15 | CFLAGS?=-Wall -Wextra -std=gnu99 -O3 -march=native -I./ | ||
16 | LDFLAGS?=-O -nostdlib -Bstatic --strip-all | ||
17 | |||
18 | ifdef DEBUG | ||
19 | CFLAGS+= -DDEBUG | ||
20 | endif | ||
21 | |||
22 | #customized your module list (scsi_wait is dealed with) | ||
23 | MODULES?=ext4 ahci sd_mod sg | ||
24 | SCSI_WAIT?=1 | ||
25 | ifdef SCSI_WAIT | ||
26 | MODULES+= scsi_wait_scan | ||
27 | CFLAGS+= -DSCSI_WAIT_SCAN | ||
28 | endif |
File globals.h added (mode: 100644) (index 0000000..02f31ff) | |||
1 | #ifndef GLOBALS_H | ||
2 | #define GLOBALS_H | ||
3 | //****************************************************************************** | ||
4 | //*this code is protected by the GNU affero GPLv3 | ||
5 | //*author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com> | ||
6 | //* <digital.ragnarok AT gmail dot com> | ||
7 | //****************************************************************************** | ||
8 | #ifdef DEBUG | ||
9 | #define PRE "initramfs:" | ||
10 | extern k_i g_console; | ||
11 | extern k_u8 *g_dprintf_buf; | ||
12 | #define OUT(f,...) u_a_dprintf(g_console,g_dprintf_buf,K_PAGE_SIZE,(k_u8*)f,\ | ||
13 | __VA_ARGS__) | ||
14 | #define OUTC(s) sysc(write,3,g_console,s,sizeof(s)) | ||
15 | #else | ||
16 | #define PRE | ||
17 | #define OUT(f,...) (void)0; | ||
18 | #define OUTC(s) (void)0; | ||
19 | #endif | ||
20 | #endif |
File init.c added (mode: 100644) (index 0000000..b8a86e9) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com> | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #include <ulinux/compiler_types.h> | ||
7 | #include <ulinux/arch/sysc.h> | ||
8 | #ifdef DEBUG | ||
9 | #include <ulinux/arch/types.h> | ||
10 | #include <ulinux/error.h> | ||
11 | #include <ulinux/file.h> | ||
12 | #include <ulinux/stat.h> | ||
13 | #include <ulinux/mmap.h> | ||
14 | |||
15 | #include <stdarg.h> | ||
16 | #include <ulinux/utils/ascii/string/vsprintf.h> | ||
17 | #endif | ||
18 | |||
19 | #include "modules.h" | ||
20 | #include "globals.h" | ||
21 | |||
22 | #ifdef DEBUG | ||
23 | k_u8 *g_dprintf_buf; | ||
24 | k_i g_console; | ||
25 | #endif | ||
26 | |||
27 | void _start(void) | ||
28 | { | ||
29 | # ifdef DEBUG | ||
30 | k_l r=sysc(open,3,"/dev/console",K_O_WRONLY,0); | ||
31 | if(K_ISERR(r)) sysc(exit_group,1,-1); | ||
32 | g_console=(k_i)r; | ||
33 | r=sysc(mmap,6,0, K_PAGE_SIZE, K_PROT_READ|K_PROT_WRITE, | ||
34 | K_MAP_PRIVATE|K_MAP_ANONYMOUS|K_MAP_POPULATE,0,0); | ||
35 | if(K_ISERR(r)){ | ||
36 | OUTC(PRE "ERROR: unable to mmap memory for formatted output\n"); | ||
37 | sysc(exit_group,1,-1); | ||
38 | } | ||
39 | g_dprintf_buf=(k_u8*)r;//just one page should be enough for formatted output | ||
40 | # endif | ||
41 | modules_load(); | ||
42 | sysc(exit_group,1,0); | ||
43 | } |
File makefile added (mode: 100644) (index 0000000..3e39461) | |||
1 | include conf.mk | ||
2 | |||
3 | .PHONY:all dirs clean am_i_root help | ||
4 | |||
5 | DIRS=\ | ||
6 | $(OBJ_DIR) \ | ||
7 | $(MODULES_DIR) \ | ||
8 | $(CPIO_DIR)/dev | ||
9 | |||
10 | OBJS=\ | ||
11 | $(OBJ_DIR)/init.o \ | ||
12 | $(OBJ_DIR)/modules.o | ||
13 | |||
14 | ifdef DEBUG | ||
15 | ULINUX_DEBUG_OBJS=\ | ||
16 | $(OBJ_DIR)/mem.o \ | ||
17 | $(OBJ_DIR)/string.o \ | ||
18 | $(OBJ_DIR)/vsprintf.o | ||
19 | |||
20 | OBJS+= $(ULINUX_DEBUG_OBJS) | ||
21 | endif | ||
22 | |||
23 | help: | ||
24 | @echo "targets are 'all', 'help'(this output), 'clean'" | ||
25 | @echo -e "you can configure the build with the following variables:\\n\ | ||
26 | SYSROOT, KERNEL_VERSION, ARCH, MODULES, SCSI_WAIT, CC, LD, CFLAGS, LDFLAGS\\n\ | ||
27 | (you can tune the conf.mk file)" | ||
28 | |||
29 | all:am_i_root dirs ulinux/arch $(BUILD_DIR)/initramfs.cpio.xz | ||
30 | |||
31 | am_i_root: | ||
32 | @if [ $$(whoami) != root ];then echo *ERROR* must be root to build;false;fi | ||
33 | |||
34 | dirs:$(DIRS) | ||
35 | $(DIRS): | ||
36 | -mkdir -p $@ | ||
37 | |||
38 | ulinux/arch: | ||
39 | ln -s archs/$(ARCH) ulinux/arch | ||
40 | |||
41 | #=============================================================================== | ||
42 | ifdef DEBUG | ||
43 | $(OBJ_DIR)/mem.o:ulinux/arch/utils/mem.c | ||
44 | $(CC) $(CFLAGS) -c $< -o $@ | ||
45 | |||
46 | $(OBJ_DIR)/string.o:ulinux/utils/ascii/string/string.c | ||
47 | $(CC) $(CFLAGS) -c $< -o $@ | ||
48 | |||
49 | $(OBJ_DIR)/vsprintf.o:ulinux/utils/ascii/string/vsprintf.c | ||
50 | $(CC) $(CFLAGS) -c $< -o $@ | ||
51 | endif | ||
52 | #=============================================================================== | ||
53 | |||
54 | $(CPIO_DIR)/dev/console: | ||
55 | mknod --mode=0600 $(CPIO_DIR)/dev/console c 5 1 | ||
56 | |||
57 | $(BUILD_DIR)/initramfs.cpio:$(CPIO_DIR)/init $(CPIO_DIR)/dev/console | ||
58 | chown -R 0:0 $(CPIO_DIR) | ||
59 | DEST_FILE=$$(realpath $(BUILD_DIR))/initramfs.cpio;\ | ||
60 | cd $(CPIO_DIR);\ | ||
61 | find . -depth -print | cpio --format=newc --create >$${DEST_FILE} | ||
62 | |||
63 | $(BUILD_DIR)/initramfs.cpio.xz:$(BUILD_DIR)/initramfs.cpio | ||
64 | xz --force --check=crc32 --extreme $< | ||
65 | |||
66 | $(OBJ_DIR)/modules.o:modules.c modules_list.h | ||
67 | $(CC) $(CFLAGS) -c $< -o $@ | ||
68 | |||
69 | $(OBJ_DIR)/init.o:init.c modules.h | ||
70 | $(CC) $(CFLAGS) -c $< -o $@ | ||
71 | |||
72 | $(CPIO_DIR)/init:$(OBJS) | ||
73 | $(LD) $(LDFLAGS) $(OBJS) --output $@ | ||
74 | |||
75 | modules_list.h: | ||
76 | ./script/modules_list.sh >$@ | ||
77 | |||
78 | clean: | ||
79 | -rm -f ulinux/arch | ||
80 | -rm -Rf $(BUILD_DIR) | ||
81 | -rm -f modules_list.h |
File modules.c added (mode: 100644) (index 0000000..5c828b2) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com> | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #include <ulinux/compiler_types.h> | ||
7 | #include <ulinux/arch/types.h> | ||
8 | #include <ulinux/arch/sysc.h> | ||
9 | |||
10 | #include <ulinux/stat.h> | ||
11 | #include <ulinux/mmap.h> | ||
12 | #include <ulinux/error.h> | ||
13 | #include <ulinux/file.h> | ||
14 | |||
15 | #ifdef DEBUG | ||
16 | #include <stdarg.h> | ||
17 | #include <ulinux/utils/ascii/string/vsprintf.h> | ||
18 | #endif | ||
19 | |||
20 | #include "globals.h" | ||
21 | #include "modules_list.h" | ||
22 | |||
23 | static void module_load(k_u8 *m) | ||
24 | { | ||
25 | k_l fd=sysc(open,3,m,K_O_RDONLY,0); | ||
26 | if(K_ISERR(fd)){ | ||
27 | OUTC(PRE " unable to open module\n"); | ||
28 | goto err; | ||
29 | } | ||
30 | |||
31 | struct k_stat m_stat; | ||
32 | k_l r=sysc(fstat,2,fd,&m_stat); | ||
33 | if(K_ISERR(r)){ | ||
34 | OUTC(PRE " unable to stat module\n"); | ||
35 | goto err; | ||
36 | } | ||
37 | |||
38 | k_l addr=sysc(mmap,6,0,m_stat.st_size,K_PROT_READ, | ||
39 | K_MAP_PRIVATE|K_MAP_POPULATE,fd,0); | ||
40 | if(K_ISERR(addr)){ | ||
41 | OUTC(PRE " unable to mmap module\n"); | ||
42 | goto err; | ||
43 | } | ||
44 | return; | ||
45 | |||
46 | err: | ||
47 | sysc(exit_group,1,-1); | ||
48 | } | ||
49 | |||
50 | void modules_load(void) | ||
51 | { | ||
52 | k_u8 **m=modules_list; | ||
53 | |||
54 | while(*m!=0){ | ||
55 | OUT(PRE "loading module '%s'...\n",*m); | ||
56 | module_load(*m); | ||
57 | OUTC(PRE "module loaded"); | ||
58 | ++m; | ||
59 | } | ||
60 | } |
File modules.h added (mode: 100644) (index 0000000..f5e162a) | |||
1 | #ifndef MODULES_H | ||
2 | #define MODULES_H | ||
3 | //****************************************************************************** | ||
4 | //*this code is protected by the GNU affero GPLv3 | ||
5 | //*author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com> | ||
6 | //* <digital.ragnarok AT gmail dot com> | ||
7 | //****************************************************************************** | ||
8 | void modules_load(void); | ||
9 | #endif |
File script/modules_list.sh added (mode: 100755) (index 0000000..69181cd) | |||
1 | #!/bin/sh | ||
2 | |||
3 | uniqify() | ||
4 | { | ||
5 | local i= result= | ||
6 | for i in ${MODULES_ORDERED}; do | ||
7 | case " $result " in | ||
8 | *" $i "*);; | ||
9 | *) result="$result $i";; | ||
10 | esac | ||
11 | done | ||
12 | MODULES_ORDERED="${result# *}" | ||
13 | } | ||
14 | |||
15 | modules_ordered_build() | ||
16 | { | ||
17 | local deps IFS m deps_spaced | ||
18 | for m; do | ||
19 | deps=$(modinfo -b "${SYSROOT}" -k "${KERNEL_VERSION}" --field depends $m) | ||
20 | if [ -n "$deps" ]; then | ||
21 | IFS="," deps_spaced= | ||
22 | deps_spaced=$(echo $deps) | ||
23 | IFS=" " | ||
24 | modules_ordered_build $deps_spaced | ||
25 | fi | ||
26 | MODULES_ORDERED="${MODULES_ORDERED} $m" | ||
27 | done | ||
28 | } | ||
29 | |||
30 | modules_copy() | ||
31 | { | ||
32 | local path_src path_dst path_cpio | ||
33 | for m in ${MODULES_ORDERED}; do | ||
34 | path_src=$(modinfo -b "${SYSROOT}" -k "${KERNEL_VERSION}" --field filename $m) | ||
35 | path_dst=${MODULES_DIR}/$(basename $path_src) | ||
36 | cp -f "$path_src" "$path_dst" | ||
37 | path_cpio=/modules/$(basename $path_src) | ||
38 | echo "(k_u8*)\"$path_cpio\"," | ||
39 | done | ||
40 | } | ||
41 | |||
42 | echo "\ | ||
43 | #ifndef MODULES_LIST_H | ||
44 | #define MODULES_LIST_H | ||
45 | k_u8 *modules_list[]={" | ||
46 | modules_ordered_build ${MODULES} | ||
47 | uniqify | ||
48 | modules_copy | ||
49 | echo "\ | ||
50 | 0}; | ||
51 | #endif" |
File ulinux/README added (mode: 100644) (index 0000000..881c6f0) | |||
1 | NAMESPACES: | ||
2 | - k_ generic namespace for ulinux | ||
3 | - u_ generic namespace for utils | ||
4 | * u_a utils for ascii namespace | ||
5 | |||
6 | Why no magic include files: then you have the information of "what's in". | ||
7 | This is not dirty RAD and it will take time to reach enough stability to justify shared objects. | ||
8 | Why override the compiler types: because they are long, and name the short ones the same way. | ||
9 | |||
10 | Compiler types (scalability) with some semantics: | ||
11 | - size semantics: | ||
12 | * no range constraint:unsigned long... do not mistake it with linux k_sz | ||
13 | * range constraint:take the smallest compiler (C standard) unsigned integer type garanteed to fit the range | ||
14 | - bool semantics:unsigned tiny (mapped to minimum 8 bits compiler type... unsigned char). | ||
15 | may do some typedef, one day. | ||
16 | |||
17 | |||
18 | CUSTOMARY ABBREVIATIONS: | ||
19 | blk block | ||
20 | buf buffer | ||
21 | cmts comments | ||
22 | dec decimal | ||
23 | fmt format | ||
24 | ptr pointer | ||
25 | str string | ||
26 | sz size |
File ulinux/TODO added (mode: 100644) (index 0000000..bdbe8ea) | |||
1 | x86/x86_64 important notes: | ||
2 | there is an infrastructure in the linux kernel to select proper code path based | ||
3 | on cpuinfo flags. Namely, for cross compilation, we would need to inject more | ||
4 | than just the architecture in order to select the right code path. |
File ulinux/arch added (mode: 120000) (index 0000000..6e4e80d) | |||
1 | archs/x86_64 |
File ulinux/archs/x86_64/epoll.h added (mode: 100644) (index 0000000..1bfc80f) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_EPOLL_H | ||
6 | #define ULINUX_ARCH_EPOLL_H | ||
7 | union k_epoll_data{ | ||
8 | void *ptr; | ||
9 | k_i fd; | ||
10 | k_u32 u32; | ||
11 | k_u64 u64; | ||
12 | }; | ||
13 | |||
14 | struct k_epoll_event{ | ||
15 | k_u32 events; | ||
16 | union k_epoll_data data; | ||
17 | } __attribute__((packed));//packed is specific to x86_64 | ||
18 | #endif |
File ulinux/archs/x86_64/mmap.h added (mode: 100644) (index 0000000..6b006b3) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_MMAP_H | ||
6 | #define ULINUX_ARCH_MMAP_H | ||
7 | #define K_PAGE_SIZE 4096 | ||
8 | #endif |
File ulinux/archs/x86_64/signal/handler.c added (mode: 100644) (index 0000000..4bb4f1c) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #define ULINUX_ARCH_C | ||
6 | //signal handler setup is arch depend | ||
7 | //on x86_84, a syscall restorer must be called and that must be rt_sigreturn | ||
8 | //in order to get a usable stack-->naked syscall 15, namely rt_sigreturn | ||
9 | //gdb won't be able to deal with the signal handler | ||
10 | extern void restore_rt(void) asm ("__restore_rt"); | ||
11 | asm( | ||
12 | ".text\n" | ||
13 | "__restore_rt:\n" | ||
14 | " movq $15,%rax\n" | ||
15 | " syscall\n"); | ||
16 | |||
17 | k_l arch_sigaction(k_l num,struct k_sigaction *kact,struct k_sigaction *okact) | ||
18 | { | ||
19 | kact->sa_flags|=SA_RESTORER;//*MUST* have a restorer on x86_64, see above | ||
20 | kact->sa_restorer=restore_rt;//bear rt_sigreturn syscall | ||
21 | return sysc(rt_sigaction,4,num,kact,okact,sizeof(k_ul)); | ||
22 | } |
File ulinux/archs/x86_64/signal/handler.h added (mode: 100644) (index 0000000..a10a93c) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_SIGNAL_HANDLER_H | ||
6 | #define ULINUX_ARCH_SIGNAL_HANDLER_H | ||
7 | #define K_SA_RESTORER 0x04000000 | ||
8 | extern k_l arch_sigaction(k_l num,struct k_sigaction *kact,struct k_sigaction *okact); | ||
9 | #endif |
File ulinux/archs/x86_64/signal/signal.h added (mode: 100644) (index 0000000..58a3d19) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_SIGNAL_SIGNAL_H | ||
6 | #define ULINUX_ARCH_SIGNAL_SIGNAL_H | ||
7 | #define K_SIGHUP 1 | ||
8 | #define K_SIGINT 2 | ||
9 | #define K_SIGQUIT 3 | ||
10 | #define K_SIGILL 4 | ||
11 | #define K_SIGTRAP 5 | ||
12 | #define K_SIGABRT 6 | ||
13 | #define K_SIGIOT 6 | ||
14 | #define K_SIGBUS 7 | ||
15 | #define K_SIGFPE 8 | ||
16 | #define K_SIGKILL 9 | ||
17 | #define K_SIGUSR1 10 | ||
18 | #define K_SIGSEGV 11 | ||
19 | #define K_SIGUSR2 12 | ||
20 | #define K_SIGPIPE 13 | ||
21 | #define K_SIGALRM 14 | ||
22 | #define K_SIGTERM 15 | ||
23 | #define K_SIGSTKFLT 16 | ||
24 | #define K_SIGCHLD 17 | ||
25 | #define K_SIGCONT 18 | ||
26 | #define K_SIGSTOP 19 | ||
27 | #define K_SIGTSTP 20 | ||
28 | #define K_SIGTTIN 21 | ||
29 | #define K_SIGTTOU 22 | ||
30 | #define K_SIGURG 23 | ||
31 | #define K_SIGXCPU 24 | ||
32 | #define K_SIGXFSZ 25 | ||
33 | #define K_SIGVTALRM 26 | ||
34 | #define K_SIGPROF 27 | ||
35 | #define K_SIGWINCH 28 | ||
36 | #define K_SIGIO 29 | ||
37 | #define K_SIGPOLL K_SIGIO | ||
38 | #define K_SIGPWR 30 | ||
39 | #define K_SIGSYS 31 | ||
40 | #define K_SIGUNUSED 31 | ||
41 | #endif |
File ulinux/archs/x86_64/stat.h added (mode: 100644) (index 0000000..7de7a09) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #ifndef ULINUX_ARCH_STAT_H | ||
7 | #define ULINUX_ARCH_STAT_H | ||
8 | struct k_stat{ | ||
9 | k_ul st_dev; | ||
10 | k_ul st_ino; | ||
11 | k_ul st_nlink; | ||
12 | |||
13 | k_u st_mode; | ||
14 | k_u st_uid; | ||
15 | k_u st_gid; | ||
16 | k_u __pad0; | ||
17 | k_ul st_rdev; | ||
18 | k_l st_size; | ||
19 | k_l st_blksize; | ||
20 | k_l st_blocks;//Number 512-byte blocks allocated. | ||
21 | |||
22 | k_ul st_atime; | ||
23 | k_ul st_atime_nsec; | ||
24 | k_ul st_mtime; | ||
25 | k_ul st_mtime_nsec; | ||
26 | k_ul st_ctime; | ||
27 | k_ul st_ctime_nsec; | ||
28 | k_l __unused[3]; | ||
29 | }; | ||
30 | #endif |
File ulinux/archs/x86_64/sysc.h added (mode: 100644) (index 0000000..3c51dd1) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_SYSC_H | ||
6 | #define ULINUX_SYSC_H | ||
7 | |||
8 | # define sysc(name, nr, args...) \ | ||
9 | ({ \ | ||
10 | k_ul resultvar; \ | ||
11 | SYSC_LOAD_ARGS_##nr (args) \ | ||
12 | SYSC_LOAD_REGS_##nr \ | ||
13 | asm volatile ( \ | ||
14 | "syscall\n\t" \ | ||
15 | : "=a" (resultvar) \ | ||
16 | : "0" (__K_NR_##name) SYSC_ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \ | ||
17 | (long int) resultvar; }) | ||
18 | |||
19 | # define SYSC_LOAD_ARGS_0() | ||
20 | # define SYSC_LOAD_REGS_0 | ||
21 | # define SYSC_ASM_ARGS_0 | ||
22 | |||
23 | # define SYSC_LOAD_ARGS_1(a1) \ | ||
24 | k_l __arg1 = (k_l) (a1); \ | ||
25 | SYSC_LOAD_ARGS_0 () | ||
26 | # define SYSC_LOAD_REGS_1 \ | ||
27 | register k_l _a1 asm ("rdi") = __arg1; \ | ||
28 | SYSC_LOAD_REGS_0 | ||
29 | # define SYSC_ASM_ARGS_1 SYSC_ASM_ARGS_0, "r" (_a1) | ||
30 | |||
31 | # define SYSC_LOAD_ARGS_2(a1, a2) \ | ||
32 | k_l __arg2 = (k_l) (a2); \ | ||
33 | SYSC_LOAD_ARGS_1 (a1) | ||
34 | # define SYSC_LOAD_REGS_2 \ | ||
35 | register k_l _a2 asm ("rsi") = __arg2; \ | ||
36 | SYSC_LOAD_REGS_1 | ||
37 | # define SYSC_ASM_ARGS_2 SYSC_ASM_ARGS_1, "r" (_a2) | ||
38 | |||
39 | # define SYSC_LOAD_ARGS_3(a1, a2, a3) \ | ||
40 | k_l __arg3 = (k_l) (a3); \ | ||
41 | SYSC_LOAD_ARGS_2 (a1, a2) | ||
42 | # define SYSC_LOAD_REGS_3 \ | ||
43 | register k_l _a3 asm ("rdx") = __arg3; \ | ||
44 | SYSC_LOAD_REGS_2 | ||
45 | # define SYSC_ASM_ARGS_3 SYSC_ASM_ARGS_2, "r" (_a3) | ||
46 | |||
47 | # define SYSC_LOAD_ARGS_4(a1, a2, a3, a4) \ | ||
48 | k_l __arg4 = (k_l) (a4); \ | ||
49 | SYSC_LOAD_ARGS_3 (a1, a2, a3) | ||
50 | # define SYSC_LOAD_REGS_4 \ | ||
51 | register k_l _a4 asm ("r10") = __arg4; \ | ||
52 | SYSC_LOAD_REGS_3 | ||
53 | # define SYSC_ASM_ARGS_4 SYSC_ASM_ARGS_3, "r" (_a4) | ||
54 | |||
55 | # define SYSC_LOAD_ARGS_5(a1, a2, a3, a4, a5) \ | ||
56 | k_l __arg5 = (k_l) (a5); \ | ||
57 | SYSC_LOAD_ARGS_4 (a1, a2, a3, a4) | ||
58 | # define SYSC_LOAD_REGS_5 \ | ||
59 | register k_l _a5 asm ("r8") = __arg5; \ | ||
60 | SYSC_LOAD_REGS_4 | ||
61 | # define SYSC_ASM_ARGS_5 SYSC_ASM_ARGS_4, "r" (_a5) | ||
62 | |||
63 | # define SYSC_LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \ | ||
64 | k_l __arg6 = (k_l) (a6); \ | ||
65 | SYSC_LOAD_ARGS_5 (a1, a2, a3, a4, a5) | ||
66 | # define SYSC_LOAD_REGS_6 \ | ||
67 | register k_l _a6 asm ("r9") = __arg6; \ | ||
68 | SYSC_LOAD_REGS_5 | ||
69 | # define SYSC_ASM_ARGS_6 SYSC_ASM_ARGS_5, "r" (_a6) | ||
70 | |||
71 | //This file contains the system call numbers. | ||
72 | //Note: holes are not allowed. | ||
73 | //at least 8 syscall per cacheline | ||
74 | #define __K_NR_read 0 | ||
75 | #define __K_NR_write 1 | ||
76 | #define __K_NR_open 2 | ||
77 | #define __K_NR_close 3 | ||
78 | #define __K_NR_stat 4 | ||
79 | #define __K_NR_fstat 5 | ||
80 | #define __K_NR_lstat 6 | ||
81 | #define __K_NR_poll 7 | ||
82 | |||
83 | #define __K_NR_lseek 8 | ||
84 | #define __K_NR_mmap 9 | ||
85 | #define __K_NR_mprotect 10 | ||
86 | #define __K_NR_munmap 11 | ||
87 | #define __K_NR_brk 12 | ||
88 | #define __K_NR_rt_sigaction 13 | ||
89 | #define __K_NR_rt_sigprocmask 14 | ||
90 | #define __K_NR_rt_sigreturn 15 | ||
91 | |||
92 | #define __K_NR_ioctl 16 | ||
93 | #define __K_NR_pread64 17 | ||
94 | #define __K_NR_pwrite64 18 | ||
95 | #define __K_NR_readv 19 | ||
96 | #define __K_NR_writev 20 | ||
97 | #define __K_NR_access 21 | ||
98 | #define __K_NR_pipe 22 | ||
99 | #define __K_NR_select 23 | ||
100 | |||
101 | #define __K_NR_sched_yield 24 | ||
102 | #define __K_NR_mremap 25 | ||
103 | #define __K_NR_msync 26 | ||
104 | #define __K_NR_mincore 27 | ||
105 | #define __K_NR_madvise 28 | ||
106 | #define __K_NR_shmget 29 | ||
107 | #define __K_NR_shmat 30 | ||
108 | #define __K_NR_shmctl 31 | ||
109 | |||
110 | #define __K_NR_dup 32 | ||
111 | #define __K_NR_dup2 33 | ||
112 | #define __K_NR_pause 34 | ||
113 | #define __K_NR_nanosleep 35 | ||
114 | #define __K_NR_getitimer 36 | ||
115 | #define __K_NR_alarm 37 | ||
116 | #define __K_NR_setitimer 38 | ||
117 | #define __K_NR_getpid 39 | ||
118 | |||
119 | #define __K_NR_sendfilei 40 | ||
120 | #define __K_NR_socket 41 | ||
121 | #define __K_NR_connect 42 | ||
122 | #define __K_NR_accept 43 | ||
123 | #define __K_NR_sendto 44 | ||
124 | #define __K_NR_recvfrom 45 | ||
125 | #define __K_NR_sendmsg 46 | ||
126 | #define __K_NR_recvmsg 47 | ||
127 | |||
128 | #define __K_NR_shutdown 48 | ||
129 | #define __K_NR_bind 49 | ||
130 | #define __K_NR_listen 50 | ||
131 | #define __K_NR_getsockname 51 | ||
132 | #define __K_NR_getpeername 52 | ||
133 | #define __K_NR_socketpair 53 | ||
134 | #define __K_NR_setsockopt 54 | ||
135 | #define __K_NR_getsockopt 55 | ||
136 | |||
137 | #define __K_NR_clone 56 | ||
138 | #define __K_NR_fork 57 | ||
139 | #define __K_NR_vfork 58 | ||
140 | #define __K_NR_execve 59 | ||
141 | #define __K_NR_exit 60 | ||
142 | #define __K_NR_wait4 61 | ||
143 | #define __K_NR_kill 62 | ||
144 | #define __K_NR_uname 63 | ||
145 | |||
146 | #define __K_NR_semget 64 | ||
147 | #define __K_NR_semop 65 | ||
148 | #define __K_NR_semctl 66 | ||
149 | #define __K_NR_shmdt 67 | ||
150 | #define __K_NR_msgget 68 | ||
151 | #define __K_NR_msgsnd 69 | ||
152 | #define __K_NR_msgrcv 70 | ||
153 | #define __K_NR_msgctl 71 | ||
154 | |||
155 | #define __K_NR_fcntl 72 | ||
156 | #define __K_NR_flock 73 | ||
157 | #define __K_NR_fsync 74 | ||
158 | #define __K_NR_fdatasync 75 | ||
159 | #define __K_NR_truncate 76 | ||
160 | #define __K_NR_ftruncate 77 | ||
161 | #define __K_NR_getdents 78 | ||
162 | #define __K_NR_getcwd 79 | ||
163 | |||
164 | #define __K_NR_chdir 80 | ||
165 | #define __K_NR_fchdir 81 | ||
166 | #define __K_NR_rename 82 | ||
167 | #define __K_NR_mkdir 83 | ||
168 | #define __K_NR_rmdir 84 | ||
169 | #define __K_NR_creat 85 | ||
170 | #define __K_NR_link 86 | ||
171 | #define __K_NR_unlink 87 | ||
172 | |||
173 | #define __K_NR_symlink 88 | ||
174 | #define __K_NR_readlink 89 | ||
175 | #define __K_NR_chmod 90 | ||
176 | #define __K_NR_fchmod 91 | ||
177 | #define __K_NR_chown 92 | ||
178 | #define __K_NR_fchown 93 | ||
179 | #define __K_NR_lchown 94 | ||
180 | #define __K_NR_umask 95 | ||
181 | |||
182 | #define __K_NR_gettimeofday 96 | ||
183 | #define __K_NR_getrlimit 97 | ||
184 | #define __K_NR_getrusage 98 | ||
185 | #define __K_NR_sysinfo 99 | ||
186 | #define __K_NR_times 100 | ||
187 | #define __K_NR_ptrace 101 | ||
188 | #define __K_NR_getuid 102 | ||
189 | #define __K_NR_syslog 103 | ||
190 | |||
191 | //at the very end the stuff that never runs during the benchmarks | ||
192 | #define __K_NR_getgid 104 | ||
193 | #define __K_NR_setuid 105 | ||
194 | #define __K_NR_setgid 106 | ||
195 | #define __K_NR_geteuid 107 | ||
196 | #define __K_NR_getegid 108 | ||
197 | #define __K_NR_setpgid 109 | ||
198 | #define __K_NR_getppid 110 | ||
199 | #define __K_NR_getpgrp 111 | ||
200 | |||
201 | #define __K_NR_setsid 112 | ||
202 | #define __K_NR_setreuid 113 | ||
203 | #define __K_NR_setregid 114 | ||
204 | #define __K_NR_getgroups 115 | ||
205 | #define __K_NR_setgroups 116 | ||
206 | #define __K_NR_setresuid 117 | ||
207 | #define __K_NR_getresuid 118 | ||
208 | #define __K_NR_setresgid 119 | ||
209 | |||
210 | #define __K_NR_getresgid 120 | ||
211 | #define __K_NR_getpgid 121 | ||
212 | #define __K_NR_setfsuid 122 | ||
213 | #define __K_NR_setfsgid 123 | ||
214 | #define __K_NR_getsid 124 | ||
215 | #define __K_NR_capget 125 | ||
216 | #define __K_NR_capset 126 | ||
217 | |||
218 | #define __K_NR_rt_sigpending 127 | ||
219 | #define __K_NR_rt_sigtimedwait 128 | ||
220 | #define __K_NR_rt_sigqueueinfo 129 | ||
221 | #define __K_NR_rt_sigsuspend 130 | ||
222 | #define __K_NR_sigaltstack 131 | ||
223 | #define __K_NR_utime 132 | ||
224 | #define __K_NR_mknod 133 | ||
225 | |||
226 | //only needed for a.out | ||
227 | #define __K_NR_uselib 134 | ||
228 | #define __K_NR_personality 135 | ||
229 | |||
230 | #define __K_NR_ustat 136 | ||
231 | #define __K_NR_statfs 137 | ||
232 | #define __K_NR_fstatfs 138 | ||
233 | #define __K_NR_sysfs 139 | ||
234 | |||
235 | #define __K_NR_getpriority 140 | ||
236 | #define __K_NR_setpriority 141 | ||
237 | #define __K_NR_sched_setparam 142 | ||
238 | #define __K_NR_sched_getparam 143 | ||
239 | #define __K_NR_sched_setscheduler 144 | ||
240 | #define __K_NR_sched_getscheduler 145 | ||
241 | #define __K_NR_sched_get_priority_max 146 | ||
242 | #define __K_NR_sched_get_priority_min 147 | ||
243 | #define __K_NR_sched_rr_get_interval 148 | ||
244 | |||
245 | #define __K_NR_mlock 149 | ||
246 | #define __K_NR_munlock 150 | ||
247 | #define __K_NR_mlockall 151 | ||
248 | #define __K_NR_munlockall 152 | ||
249 | |||
250 | #define __K_NR_vhangup 153 | ||
251 | |||
252 | #define __K_NR_modify_ldt 154 | ||
253 | |||
254 | #define __K_NR_pivot_root 155 | ||
255 | |||
256 | #define __K_NR__sysctl 156 | ||
257 | |||
258 | #define __K_NR_prctl 157 | ||
259 | #define __K_NR_arch_prctl 158 | ||
260 | |||
261 | #define __K_NR_adjtimex 159 | ||
262 | |||
263 | #define __K_NR_setrlimit 160 | ||
264 | |||
265 | #define __K_NR_chroot 161 | ||
266 | |||
267 | #define __K_NR_sync 162 | ||
268 | |||
269 | #define __K_NR_acct 163 | ||
270 | |||
271 | #define __K_NR_settimeofday 164 | ||
272 | |||
273 | #define __K_NR_mount 165 | ||
274 | #define __K_NR_umount2 166 | ||
275 | |||
276 | #define __K_NR_swapon 167 | ||
277 | #define __K_NR_swapoff 168 | ||
278 | |||
279 | #define __K_NR_reboot 169 | ||
280 | |||
281 | #define __K_NR_sethostname 170 | ||
282 | #define __K_NR_setdomainname 171 | ||
283 | |||
284 | #define __K_NR_iopl 172 | ||
285 | #define __K_NR_ioperm 173 | ||
286 | |||
287 | #define __K_NR_create_module 174 | ||
288 | #define __K_NR_init_module 175 | ||
289 | #define __K_NR_delete_module 176 | ||
290 | #define __K_NR_get_kernel_syms 177 | ||
291 | #define __K_NR_query_module 178 | ||
292 | |||
293 | #define __K_NR_quotactl 179 | ||
294 | |||
295 | #define __K_NR_nfsservctl 180 | ||
296 | |||
297 | //reserved fo rLiS/STREAMS | ||
298 | #define __K_NR_getpmsg 181 | ||
299 | #define __K_NR_putpmsg 182 | ||
300 | |||
301 | //reserved fori AFS | ||
302 | #define __K_NR_afs_syscall 183 | ||
303 | |||
304 | //reserved for tux | ||
305 | #define __K_NR_tuxcall 184 | ||
306 | |||
307 | #define __K_NR_security 185 | ||
308 | |||
309 | #define __K_NR_gettid 186 | ||
310 | |||
311 | #define __K_NR_readahead 187 | ||
312 | #define __K_NR_setxattr 188 | ||
313 | #define __K_NR_lsetxattr 189 | ||
314 | #define __K_NR_fsetxattr 190 | ||
315 | #define __K_NR_getxattr 191 | ||
316 | #define __K_NR_lgetxattr 192 | ||
317 | #define __K_NR_fgetxattr 193 | ||
318 | #define __K_NR_listxattr 194 | ||
319 | #define __K_NR_llistxattr 195 | ||
320 | #define __K_NR_flistxattr 196 | ||
321 | #define __K_NR_removexattr 197 | ||
322 | #define __K_NR_lremovexattr 198 | ||
323 | #define __K_NR_fremovexattr 199 | ||
324 | #define __K_NR_tkill 200 | ||
325 | #define __K_NR_time 201 | ||
326 | #define __K_NR_futex 202 | ||
327 | #define __K_NR_sched_setaffinity 203 | ||
328 | #define __K_NR_sched_getaffinity 204 | ||
329 | #define __K_NR_set_thread_area 205 | ||
330 | #define __K_NR_io_setup 206 | ||
331 | #define __K_NR_io_destroy 207 | ||
332 | #define __K_NR_io_getevents 208 | ||
333 | #define __K_NR_io_submit 209 | ||
334 | #define __K_NR_io_cancel 210 | ||
335 | #define __K_NR_get_thread_area 211 | ||
336 | #define __K_NR_lookup_dcookie 212 | ||
337 | #define __K_NR_epoll_create 213 | ||
338 | #define __K_NR_epoll_ctl_old 214 | ||
339 | #define __K_NR_epoll_wait_oldi 215 | ||
340 | #define __K_NR_remap_file_pages 216 | ||
341 | #define __K_NR_getdents64 217 | ||
342 | #define __K_NR_set_tid_address 218 | ||
343 | #define __K_NR_restart_syscall 219 | ||
344 | #define __K_NR_semtimedop 220 | ||
345 | #define __K_NR_fadvise64 221 | ||
346 | #define __K_NR_timer_create 222 | ||
347 | #define __K_NR_timer_settime 223 | ||
348 | #define __K_NR_timer_gettime 224 | ||
349 | #define __K_NR_timer_getoverrun 225 | ||
350 | #define __K_NR_timer_delete 226 | ||
351 | #define __K_NR_clock_settime 227 | ||
352 | #define __K_NR_clock_gettime 228 | ||
353 | #define __K_NR_clock_getres 229 | ||
354 | #define __K_NR_clock_nanosleep 230 | ||
355 | #define __K_NR_exit_group 231 | ||
356 | #define __K_NR_epoll_wait 232 | ||
357 | #define __K_NR_epoll_ctl 233 | ||
358 | #define __K_NR_tgkill 234 | ||
359 | #define __K_NR_utimes 235 | ||
360 | #define __K_NR_vserver 236 | ||
361 | #define __K_NR_mbind 237 | ||
362 | #define __K_NR_set_mempolicy 238 | ||
363 | #define __K_NR_get_mempolicy 239 | ||
364 | #define __K_NR_mq_open 240 | ||
365 | #define __K_NR_mq_unlink 241 | ||
366 | #define __K_NR_mq_timedsend 242 | ||
367 | #define __K_NR_mq_timedreceive 243 | ||
368 | #define __K_NR_mq_notify 244 | ||
369 | #define __K_NR_mq_getsetattr 245 | ||
370 | #define __K_NR_kexec_load 246 | ||
371 | #define __K_NR_waitid 247 | ||
372 | #define __K_NR_add_key 248 | ||
373 | #define __K_NR_request_key 249 | ||
374 | #define __K_NR_keyctl 250 | ||
375 | #define __K_NR_ioprio_set 251 | ||
376 | #define __K_NR_ioprio_get 252 | ||
377 | #define __K_NR_inotify_init 253 | ||
378 | #define __K_NR_inotify_add_watch 254 | ||
379 | #define __K_NR_inotify_rm_watchi 255 | ||
380 | #define __K_NR_migrate_pages 256 | ||
381 | #define __K_NR_openat 257 | ||
382 | #define __K_NR_mkdirat 258 | ||
383 | #define __K_NR_mknodat 259 | ||
384 | #define __K_NR_fchownat 260 | ||
385 | #define __K_NR_futimesat 261 | ||
386 | #define __K_NR_newfstatat 262 | ||
387 | #define __K_NR_unlinkat 263 | ||
388 | #define __K_NR_renameat 264 | ||
389 | #define __K_NR_linkat 265 | ||
390 | #define __K_NR_symlinkat 266 | ||
391 | #define __K_NR_readlinkat 267 | ||
392 | #define __K_NR_fchmodat 268 | ||
393 | #define __K_NR_faccessat 269 | ||
394 | #define __K_NR_pselect6 270 | ||
395 | #define __K_NR_ppoll 271 | ||
396 | #define __K_NR_unshare 272 | ||
397 | #define __K_NR_set_robust_list 273 | ||
398 | #define __K_NR_get_robust_list 274 | ||
399 | #define __K_NR_splice 275 | ||
400 | #define __K_NR_tee 276 | ||
401 | #define __K_NR_sync_file_range 277 | ||
402 | #define __K_NR_vmsplice 278 | ||
403 | #define __K_NR_move_pages 279 | ||
404 | #define __K_NR_utimensat 280 | ||
405 | #define __K_IGNORE_getcpu//implemented as a vsyscall | ||
406 | #define __K_NR_epoll_pwait 281 | ||
407 | #define __K_NR_signalfd 282 | ||
408 | #define __K_NR_timerfd_create 283 | ||
409 | #define __K_NR_eventfd 284 | ||
410 | #define __K_NR_fallocate 285 | ||
411 | #define __K_NR_timerfd_settime 286 | ||
412 | #define __K_NR_timerfd_gettime 287 | ||
413 | #define __K_NR_accept4 288 | ||
414 | #define __K_NR_signalfd4 289 | ||
415 | #define __K_NR_eventfd2 290 | ||
416 | #define __K_NR_epoll_create1 291 | ||
417 | #define __K_NR_dup3 292 | ||
418 | #define __K_NR_pipe2 293 | ||
419 | #define __K_NR_inotify_init1 294 | ||
420 | #define __K_NR_preadv 295 | ||
421 | #define __K_NR_pwritev 296 | ||
422 | #define __K_NR_rt_tgsigqueueinfo 297 | ||
423 | #define __K_NR_perf_event_open 298 | ||
424 | #define __K_NR_recvmmsg 299 | ||
425 | |||
426 | #endif |
File ulinux/archs/x86_64/types.h added (mode: 100644) (index 0000000..0e35640) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_TYPES_H | ||
6 | #define ULINUX_ARCH_TYPES_H | ||
7 | typedef k_t k_s8; | ||
8 | typedef k_ut k_u8; | ||
9 | |||
10 | typedef k_s k_s16; | ||
11 | typedef k_us k_u16; | ||
12 | |||
13 | typedef k_i k_s32; | ||
14 | typedef k_u k_u32; | ||
15 | |||
16 | typedef k_l k_s64; | ||
17 | typedef k_ul k_u64; | ||
18 | |||
19 | typedef k_ul k_sz;//linux says unsigned long for its size type on x86_64 | ||
20 | typedef k_s64 k_ptrdiff;//pointers are 64 bits, keep in mind overflow when working on large mmaped block | ||
21 | #endif |
File ulinux/archs/x86_64/utils/ascii.h added (mode: 100644) (index 0000000..6d68d18) | |||
1 | #ifndef ULINUX_ARCH_UTILS_ASCII_H | ||
2 | #define ULINUX_ARCH_UTILS_ASCII_H | ||
3 | #define u_a_strict_dec2ut_blk u_a_strict_dec2u8 | ||
4 | #define u_a_strict_dec2us_blk u_a_strict_dec2u16_blk | ||
5 | #define u_a_strict_dec2us u_a_strict_dec2u16 | ||
6 | #define u_a_strict_dec2ul_blk u_a_strict_dec2u64_blk | ||
7 | #define u_a_strict_dec2ul u_a_strict_dec2u64 | ||
8 | #define u_a_strict_dec2ull_blk u_a_strict_dec2u64_blk | ||
9 | #define u_a_strict_dec2ull u_a_strict_dec2ull | ||
10 | #endif |
File ulinux/archs/x86_64/utils/div.h added (mode: 100644) (index 0000000..f32d3b2) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_UTILS_DIV_H | ||
6 | #define ULINUX_ARCH_UTILS_DIV_H | ||
7 | #define u_do_div(n,base)({ \ | ||
8 | k_u32 __base=(base); \ | ||
9 | k_u32 __rem; \ | ||
10 | __rem=((k_u64)(n))%__base; \ | ||
11 | (n)=((k_u64)(n))/__base; \ | ||
12 | __rem; \ | ||
13 | }) | ||
14 | #endif |
File ulinux/archs/x86_64/utils/endian.h added (mode: 100644) (index 0000000..7bf440b) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_UTILS_ENDIAN_H | ||
6 | #define ULINUX_ARCH_UTILS_ENDIAN_H | ||
7 | |||
8 | static inline k_u16 u_cpu2be16(k_u16 v) | ||
9 | { | ||
10 | return (k_u16)((((k_u16)(v)&(k_u16)0x00ffU)<<8)|(((k_u16)(v)&(k_u16)0xff00U)>>8)); | ||
11 | } | ||
12 | #define u_be162cpu(v) u_cpu2be16(v) | ||
13 | |||
14 | static inline k_u32 u_cpu2be32(k_u32 v) | ||
15 | { | ||
16 | asm("bswapl %0":"=r" (v):"0" (v)); | ||
17 | return v; | ||
18 | } | ||
19 | #define u_be322cpu(v) u_cpu2be32(v) | ||
20 | |||
21 | static inline k_u64 u_cpu2be64(k_u64 v) | ||
22 | { | ||
23 | asm("bswapq %0":"=r" (v):"0" (v)); | ||
24 | return v; | ||
25 | } | ||
26 | #define u_be642cpu(v) u_cpu2be64(v) | ||
27 | #endif |
File ulinux/archs/x86_64/utils/ipv6.h added (mode: 100644) (index 0000000..f6b6143) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ARCH_UTILS_IPV6 | ||
6 | #define ULINUX_ARCH_UTILS_IPV6 | ||
7 | static inline k_ut u_ipv6_addr_loopback(struct k_in6_addr *a) | ||
8 | { | ||
9 | return ((a->s6_addr32[0]|a->s6_addr32[1]| | ||
10 | a->s6_addr32[2]|(a->s6_addr32[3]^u_cpu2be32(1)))==0); | ||
11 | } | ||
12 | |||
13 | static inline k_ut u_ipv6_addr_v4mapped(struct k_in6_addr *a) | ||
14 | { | ||
15 | return ((a->s6_addr32[0]|a->s6_addr32[1]| | ||
16 | (a->s6_addr32[2]^u_cpu2be32(0x0000ffff)))==0); | ||
17 | } | ||
18 | |||
19 | static inline k_ut u_ipv6_addr_is_isatap(struct k_in6_addr *addr) | ||
20 | { | ||
21 | return ((addr->s6_addr32[2]|u_cpu2be32(0x02000000))==u_cpu2be32(0x02005EFE)); | ||
22 | } | ||
23 | #endif |
File ulinux/archs/x86_64/utils/mem.c added (mode: 100644) (index 0000000..9196740) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com> | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #include <ulinux/compiler_types.h> | ||
7 | #include <ulinux/arch/types.h> | ||
8 | #include <stddef.h> | ||
9 | |||
10 | void u_memcpy(void *d,void *s,k_ul len) | ||
11 | { | ||
12 | __builtin_memcpy(d,s,len);//use gcc builtin for this arch | ||
13 | } | ||
14 | |||
15 | //a memcpy implementation is needed anyway if gcc decides not to use its builtin | ||
16 | void *memcpy(void *to,const void *from,size_t len) | ||
17 | { | ||
18 | k_u8 *d=to; | ||
19 | k_u8 *s=(k_u8*)from; | ||
20 | while(len--) *d++=*s++; | ||
21 | return to; | ||
22 | } |
File ulinux/archs/x86_64/utils/mem.h added (mode: 100644) (index 0000000..86449f9) | |||
1 | #ifndef ULINUX_ARCH_UTILS_MEM_H | ||
2 | #define ULINUX_ARCH_UTILS_MEM_H | ||
3 | #define ULINUX_ARCH_HAS_MEMCPY | ||
4 | #define ULINUX_ARCH_HAS_MEMSET | ||
5 | void u_memcpy(void *d,void *s,k_ul len); | ||
6 | #define u_memset __builtin_memset | ||
7 | #endif |
File ulinux/compiler_types.h added (mode: 100644) (index 0000000..01062c8) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_COMPILER_TYPES_H | ||
6 | #define ULINUX_COMPILER_TYPES_H | ||
7 | //with GCC | ||
8 | //'char' is not a 'signed char' neither it is a 'unsigned char' | ||
9 | //but 'char' is signed on x86 and unsigned on ARM... feel the difference | ||
10 | typedef signed char k_t;//t stands for tiny | ||
11 | typedef short k_s; | ||
12 | typedef int k_i; | ||
13 | typedef long k_l; | ||
14 | typedef long long k_ll; | ||
15 | |||
16 | typedef unsigned char k_ut;//t stands for tiny | ||
17 | typedef unsigned short k_us; | ||
18 | typedef unsigned k_u; | ||
19 | typedef unsigned long k_ul; | ||
20 | typedef unsigned long long k_ull; | ||
21 | #endif |
File ulinux/epoll.h added (mode: 100644) (index 0000000..e550c2e) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_EPOLL_H | ||
6 | #define ULINUX_EPOLL_H | ||
7 | #define K_EPOLL_CTL_ADD 1 | ||
8 | #define K_EPOLL_CTL_DEL 2 | ||
9 | #define K_EPOLL_CTL_MOD 3 | ||
10 | |||
11 | #define K_EPOLLIN 0x0001 | ||
12 | #define K_EPOLLPRI 0x0002 | ||
13 | #define K_EPOLLOUT 0x0004 | ||
14 | #define K_EPOLLERR 0x0008 | ||
15 | #define K_EPOLLHUP 0x0010 | ||
16 | #define K_EPOLLNVAL 0x0020 | ||
17 | #define K_EPOLLRDHUP 0x2000 | ||
18 | #define K_EPOLLONESHOT (1 << 30) | ||
19 | #define K_EPOLLET (1 << 31) | ||
20 | |||
21 | #include <ulinux/arch/epoll.h> | ||
22 | #endif |
File ulinux/error.h added (mode: 100644) (index 0000000..ed6132d) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ERROR_H | ||
6 | #define ULINUX_ERROR_H | ||
7 | |||
8 | #define K_ISERR(x) (-4095<=(x)&&(x)<=-1) | ||
9 | #define K_EPERM 1//Operation not permitted | ||
10 | #define K_ENOENT 2//No such file or directory | ||
11 | #define K_ESRCH 3//No such process | ||
12 | #define K_EINTR 4//Interrupted system call | ||
13 | #define K_EIO 5//I/O error | ||
14 | #define K_ENXIO 6//No such device or address | ||
15 | #define K_E2BIG 7//Argument list too long | ||
16 | #define K_ENOEXEC 8//Exec format error | ||
17 | #define K_EBADF 9//Bad file number | ||
18 | #define K_ECHILD 10//No child processes | ||
19 | #define K_EAGAIN 11//Try again | ||
20 | #define K_ENOMEM 12//Out of memory | ||
21 | #define K_EACCES 13//Permission denied | ||
22 | #define K_EFAULT 14//Bad address | ||
23 | #define K_ENOTBLK 15//Block device required | ||
24 | #define K_EBUSY 16//Device or resource busy | ||
25 | #define K_EEXIST 17//File exists | ||
26 | #define K_EXDEV 18//Cross-device link | ||
27 | #define K_ENODEV 19//No such device | ||
28 | #define K_ENOTDIR 20//Not a directory | ||
29 | #define K_EISDIR 21//Is a directory | ||
30 | #define K_EINVAL 22//Invalid argument | ||
31 | #define K_ENFILE 23//File table overflow | ||
32 | #define K_EMFILE 24//Too many open files | ||
33 | #define K_ENOTTY 25//Not a typewriter | ||
34 | #define K_ETXTBSY 26//Text file busy | ||
35 | #define K_EFBIG 27//File too large | ||
36 | #define K_ENOSPC 28//No space left on device | ||
37 | #define K_ESPIPE 29//Illegal seek | ||
38 | #define K_EROFS 30//Read-only file system | ||
39 | #define K_EMLINK 31//Too many links | ||
40 | #define K_EPIPE 32//Broken pipe | ||
41 | #define K_EDOM 33//Math argument out of domain of func | ||
42 | #define K_ERANGE 34//Math result not representable | ||
43 | #define K_EDEADLK 35//Resource deadlock would occur | ||
44 | #define K_ENAMETOOLONG 36//File name too long | ||
45 | #define K_ENOLCK 37//No record locks available | ||
46 | #define K_ENOSYS 38//Function not implemented | ||
47 | #define K_ENOTEMPTY 39//Directory not empty | ||
48 | #define K_ELOOP 40//Too many symbolic links encountered | ||
49 | #define K_EWOULDBLOCK K_EAGAIN//Operation would block | ||
50 | #define K_ENOMSG 42//No message of desired type | ||
51 | #define K_EIDRM 43//Identifier removed | ||
52 | #define K_ECHRNG 44//Channel number out of range | ||
53 | #define K_EL2NSYNC 45//Level 2 not synchronized | ||
54 | #define K_EL3HLT 46//Level 3 halted | ||
55 | #define K_EL3RST 47//Level 3 reset | ||
56 | #define K_ELNRNG 48//Link number out of range | ||
57 | #define K_EUNATCH 49//Protocol driver not attached | ||
58 | #define K_ENOCSI 50//No CSI structure available | ||
59 | #define K_EL2HLT 51//Level 2 halted | ||
60 | #define K_EBADE 52//Invalid exchange | ||
61 | #define K_EBADR 53//Invalid request descriptor | ||
62 | #define K_EXFULL 54//Exchange full | ||
63 | #define K_ENOANO 55//No anode | ||
64 | #define K_EBADRQC 56//Invalid request code | ||
65 | #define K_EBADSLT 57//Invalid slot | ||
66 | |||
67 | #define K_EDEADLOCK K_EDEADLK | ||
68 | |||
69 | #define K_EBFONT 59//Bad font file format | ||
70 | #define K_ENOSTR 60//Device not a stream | ||
71 | #define K_ENODATA 61//No data available | ||
72 | #define K_ETIME 62//Timer expired | ||
73 | #define K_ENOSR 63//Out of streams resources | ||
74 | #define K_ENONET 64//Machine is not on the network | ||
75 | #define K_ENOPKG 65//Package not installed | ||
76 | #define K_EREMOTE 66//Object is remote | ||
77 | #define K_ENOLINK 67//Link has been severed | ||
78 | #define K_EADV 68//Advertise error | ||
79 | #define K_ESRMNT 69//Srmount error | ||
80 | #define K_ECOMM 70//Communication error on send | ||
81 | #define K_EPROTO 71//Protocol error | ||
82 | #define K_EMULTIHOP 72//Multihop attempted | ||
83 | #define K_EDOTDOT 73//RFS specific error | ||
84 | #define K_EBADMSG 74//Not a data message | ||
85 | #define K_EOVERFLOW 75//Value too large for defined data type | ||
86 | #define K_ENOTUNIQ 76//Name not unique on network | ||
87 | #define K_EBADFD 77//File descriptor in bad state | ||
88 | #define K_EREMCHG 78//Remote address changed | ||
89 | #define K_ELIBACC 79//Can not access a needed shared library | ||
90 | #define K_ELIBBAD 80//Accessing a corrupted shared library | ||
91 | #define K_ELIBSCN 81//.lib section in a.out corrupted | ||
92 | #define K_ELIBMAX 82//Attempting to link in too many shared libraries | ||
93 | #define K_ELIBEXEC 83//Cannot exec a shared library directly | ||
94 | #define K_EILSEQ 84//Illegal byte sequence | ||
95 | #define K_ERESTART 85//Interrupted system call should be restarted | ||
96 | #define K_ESTRPIPE 86//Streams pipe error | ||
97 | #define K_EUSERS 87//Too many users | ||
98 | #define K_ENOTSOCK 88//Socket operation on non-socket | ||
99 | #define K_EDESTADDRREQ 89//Destination address required | ||
100 | #define K_EMSGSIZE 90//Message too long | ||
101 | #define K_EPROTOTYPE 91//Protocol wrong type for socket | ||
102 | #define K_ENOPROTOOPT 92//Protocol not available | ||
103 | #define K_EPROTONOSUPPORT 93//Protocol not supported | ||
104 | #define K_ESOCKTNOSUPPORT 94//Socket type not supported | ||
105 | #define K_EOPNOTSUPP 95//Operation not supported on transport endpoint | ||
106 | #define K_EPFNOSUPPORT 96//Protocol family not supported | ||
107 | #define K_EAFNOSUPPORT 97//Address family not supported by protocol | ||
108 | #define K_EADDRINUSE 98//Address already in use | ||
109 | #define K_EADDRNOTAVAIL 99//Cannot assign requested address | ||
110 | #define K_ENETDOWN 100//Network is down | ||
111 | #define K_ENETUNREACH 101//Network is unreachable | ||
112 | #define K_ENETRESET i 102//Network dropped connection because of reset | ||
113 | #define K_ECONNABORTED 103//Software caused connection abort | ||
114 | #define K_ECONNRESET 104//Connection reset by peer | ||
115 | #define K_ENOBUFS 105//No buffer space available | ||
116 | #define K_EISCONN 106//Transport endpoint is already connected | ||
117 | #define K_ENOTCONN 107//Transport endpoint is not connected | ||
118 | #define K_ESHUTDOWN 108//Cannot send after transport endpoint shutdown | ||
119 | #define K_ETOOMANYREFS 109//Too many references: cannot splice | ||
120 | #define K_ETIMEDOUT 110//Connection timed out | ||
121 | #define K_ECONNREFUSED 111//Connection refused | ||
122 | #define K_EHOSTDOWN 112//Host is down | ||
123 | #define K_EHOSTUNREACH 113//No route to host | ||
124 | #define K_EALREADY 114//Operation already in progress | ||
125 | #define K_EINPROGRESS 115//Operation now in progress | ||
126 | #define K_ESTALE 116//Stale NFS file handle | ||
127 | #define K_EUCLEAN 117//Structure needs cleaning | ||
128 | #define K_ENOTNAM 118//Not a XENIX named type file | ||
129 | #define K_ENAVAIL 119//No XENIX semaphores available | ||
130 | #define K_EISNAM 120//Is a named type file | ||
131 | #define K_EREMOTEIO 121//Remote I/O error | ||
132 | #define K_EDQUOT 122//Quota exceeded | ||
133 | |||
134 | #define K_ENOMEDIUM 123//No medium found | ||
135 | #define K_EMEDIUMTYPE 124//Wrong medium type | ||
136 | #define K_ECANCELED 125//Operation Canceled | ||
137 | #define K_ENOKEY 126//Required key not available | ||
138 | #define K_EKEYEXPIRED 127//Key has expired | ||
139 | #define K_EKEYREVOKED 128//Key has been revoked | ||
140 | #define K_EKEYREJECTED 129//Key was rejected by service | ||
141 | |||
142 | //for robust mutexes | ||
143 | #define K_EOWNERDEAD 130//Owner died | ||
144 | #define K_ENOTRECOVERABLE 131//State not recoverable | ||
145 | |||
146 | #define K_ERFKILL 132//Operation not possible due to RF-kill | ||
147 | |||
148 | #endif |
File ulinux/file.h added (mode: 100644) (index 0000000..4238ddb) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #ifndef ULINUX_FILE_H | ||
7 | #define ULINUX_FILE_H | ||
8 | #define K_O_ACCMODE 00000003 | ||
9 | #define K_O_RDONLY 00000000 | ||
10 | #define K_O_WRONLY 00000001 | ||
11 | #define K_O_RDWR 00000002 | ||
12 | #define K_O_CREAT 00000100//not fcntl | ||
13 | #define K_O_EXCL 00000200//not fcntl | ||
14 | #define K_O_NOCTTY 00000400//not fcntl | ||
15 | #define K_O_TRUNC 00001000//not fcntl | ||
16 | #define K_O_APPEND 00002000 | ||
17 | #define K_O_NONBLOCK 00004000 | ||
18 | #define K_O_DSYNC 00010000//used to be O_SYNC, see below | ||
19 | #define K_FASYNC 00020000//fcntl, for BSD compatibility | ||
20 | #define K_O_DIRECT 00040000//direct disk access hint | ||
21 | #define K_O_LARGEFILE 00100000 | ||
22 | #define K_O_DIRECTORY 00200000//must be a directory | ||
23 | #define K_O_NOFOLLOW 00400000//don't follow links | ||
24 | #define K_O_NOATIME 01000000 | ||
25 | #define K_O_CLOEXEC 02000000//set close_on_exec | ||
26 | |||
27 | #define __K_O_SYNC 04000000 | ||
28 | #define K_O_SYNC (__K_O_SYNC|K_O_DSYNC) | ||
29 | #endif |
File ulinux/ipv6.h added (mode: 100644) (index 0000000..dae37ea) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_IPV6_H | ||
6 | #define ULINUX_IPV6_H | ||
7 | struct k_in6_addr{ | ||
8 | union{ | ||
9 | k_u8 u6_addr8[16]; | ||
10 | k_u16 u6_addr16[8]; | ||
11 | k_u32 u6_addr32[4]; | ||
12 | } in6_u; | ||
13 | #define s6_addr in6_u.u6_addr8 | ||
14 | #define s6_addr16 in6_u.u6_addr16 | ||
15 | #define s6_addr32 in6_u.u6_addr32 | ||
16 | }; | ||
17 | #endif |
File ulinux/mmap.h added (mode: 100644) (index 0000000..5ff1403) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_MMAP_H | ||
6 | #define ULINUX_MMAP_H | ||
7 | #include <ulinux/arch/mmap.h> | ||
8 | |||
9 | #define K_PROT_READ 0x1//page can be read | ||
10 | #define K_PROT_WRITE 0x2//page can be written | ||
11 | #define K_PROT_EXEC 0x4//page can be executed | ||
12 | #define K_PROT_SEM 0x8//page may be used for atomic ops | ||
13 | #define K_PROT_NONE 0x0//page can not be accessed | ||
14 | #define K_PROT_GROWSDOWN 0x01000000//mprotect flag: extend change to start of growsdown vma | ||
15 | #define K_PROT_GROWSUP 0x02000000//mprotect flag: extend change to end of growsup vma | ||
16 | |||
17 | #define K_MAP_SHARED 0x01//Share changes | ||
18 | #define K_MAP_PRIVATE 0x02//Changes are private | ||
19 | #define K_MAP_TYPE 0x0f//Mask for type of mapping | ||
20 | #define K_MAP_FIXED 0x10//Interpret addr exactly | ||
21 | #define K_MAP_ANONYMOUS 0x20//don't use a file | ||
22 | |||
23 | #define K_MAP_GROWSDOWN 0x0100//stack-like segment | ||
24 | #define K_MAP_DENYWRITE 0x0800//ETXTBSY | ||
25 | #define K_MAP_EXECUTABLE 0x1000//mark it as an executable | ||
26 | #define K_MAP_LOCKED 0x2000//pages are locked | ||
27 | #define K_MAP_NORESERVE 0x4000//don't check for reservations | ||
28 | #define K_MAP_POPULATE 0x8000//populate (prefault) pagetables | ||
29 | #define K_MAP_NONBLOCK 0x10000//do not block on IO | ||
30 | #define K_MAP_STACK 0x20000//give out an address that is best suited for process/thread stacks | ||
31 | #define K_MAP_HUGETLB 0x40000//create a huge page mapping | ||
32 | |||
33 | #define K_MCL_CURRENT 1//lock all current mappings | ||
34 | #define K_MCL_FUTURE 2//lock all future mappings | ||
35 | #endif |
File ulinux/signal/handler.h added (mode: 100644) (index 0000000..33dbb62) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_SIGNAL_HANDLER_H | ||
6 | #define ULINUX_SIGNAL_HANDLER_H | ||
7 | struct k_sigaction { | ||
8 | void *k_sa_handler; | ||
9 | k_ul sa_flags; | ||
10 | void (*sa_restorer)(void); | ||
11 | k_ul sa_mask; | ||
12 | }; | ||
13 | #include <ulinux/arch/signal/handler.h> | ||
14 | #endif |
File ulinux/signal/signal.h added (mode: 100644) (index 0000000..4712736) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_SIGNAL_H | ||
6 | #define ULINUX_SIGNAL_H | ||
7 | #include <ulinux/arch/signal/signal.h> | ||
8 | //In POSIX a signal is sent either to a specific thread (Linux task) | ||
9 | //or to the process as a whole (Linux thread group). How the signal | ||
10 | //is sent determines whether it's to one thread or the whole group, | ||
11 | //which determines which signal mask(s) are involved in blocking it | ||
12 | //from being delivered until later. When the signal is delivered, | ||
13 | //either it's caught or ignored by a user handler or it has a default | ||
14 | //effect that applies to the whole thread group (POSIX process). | ||
15 | // | ||
16 | //The possible effects an unblocked signal set to SIG_DFL can have are: | ||
17 | // ignore - Nothing Happens | ||
18 | // terminate - kill the process, i.e. all threads in the group, | ||
19 | // similar to exit_group. The group leader (only) reports | ||
20 | // WIFSIGNALED status to its parent. | ||
21 | // coredump - write a core dump file describing all threads using | ||
22 | // the same mm and then kill all those threads | ||
23 | // stop - stop all the threads in the group, i.e. TASK_STOPPED state | ||
24 | // | ||
25 | //SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. | ||
26 | //Other signals when not blocked and set to SIG_DFL behaves as follows. | ||
27 | //The job control signals also have other special effects. | ||
28 | // | ||
29 | // +--------------------+------------------+ | ||
30 | // | POSIX signal | default action | | ||
31 | // +--------------------+------------------+ | ||
32 | // | SIGHUP | terminate | | ||
33 | // | SIGINT | terminate | | ||
34 | // | SIGQUIT | coredump | | ||
35 | // | SIGILL | coredump | | ||
36 | // | SIGTRAP | coredump | | ||
37 | // | SIGABRT/SIGIOT | coredump | | ||
38 | // | SIGBUS | coredump | | ||
39 | // | SIGFPE | coredump | | ||
40 | // | SIGKILL | terminate(+) | | ||
41 | // | SIGUSR1 | terminate | | ||
42 | // | SIGSEGV | coredump | | ||
43 | // | SIGUSR2 | terminate | | ||
44 | // | SIGPIPE | terminate | | ||
45 | // | SIGALRM | terminate | | ||
46 | // | SIGTERM | terminate | | ||
47 | // | SIGCHLD | ignore | | ||
48 | // | SIGCONT | ignore(*) | | ||
49 | // | SIGSTOP | stop(*)(+) | | ||
50 | // | SIGTSTP | stop(*) | | ||
51 | // | SIGTTIN | stop(*) | | ||
52 | // | SIGTTOU | stop(*) | | ||
53 | // | SIGURG | ignore | | ||
54 | // | SIGXCPU | coredump | | ||
55 | // | SIGXFSZ | coredump | | ||
56 | // | SIGVTALRM | terminate | | ||
57 | // | SIGPROF | terminate | | ||
58 | // | SIGPOLL/SIGIO | terminate | | ||
59 | // | SIGSYS/SIGUNUSED | coredump | | ||
60 | // | SIGSTKFLT | terminate | | ||
61 | // | SIGWINCH | ignore | | ||
62 | // | SIGPWR | terminate | | ||
63 | // | SIGRTMIN-SIGRTMAX | terminate | | ||
64 | // +--------------------+------------------+ | ||
65 | // | non-POSIX signal | default action | | ||
66 | // +--------------------+------------------+ | ||
67 | // | SIGEMT | coredump | | ||
68 | // +--------------------+------------------+ | ||
69 | // | ||
70 | //(+) For SIGKILL and SIGSTOP the action is "always", not just "default". | ||
71 | //(*) Special job control effects: | ||
72 | //When SIGCONT is sent, it resumes the process (all threads in the group) | ||
73 | //from TASK_STOPPED state and also clears any pending/queued stop signals | ||
74 | //(any of those marked with "stop(*)"). This happens regardless of blocking, | ||
75 | //catching, or ignoring SIGCONT. When any stop signal is sent, it clears | ||
76 | //any pending/queued SIGCONT signals; this happens regardless of blocking, | ||
77 | //catching, or ignored the stop signal, though (except for SIGSTOP) the | ||
78 | //default action of stopping the process may happen later or never. | ||
79 | |||
80 | #define K_SIG_BLOCK 0//for blocking signals | ||
81 | #define K_SIG_UNBLOCK 1//for unblocking signals | ||
82 | #define K_SIG_SETMASK 2//for setting the signal mask | ||
83 | |||
84 | #define K_SFD_NONBLOCK K_O_NONBLOCK | ||
85 | //---------------------------------------------------------------------------------------------- | ||
86 | //|user siginfo stuff | ||
87 | //---------------------------------------------------------------------------------------------- | ||
88 | struct k_signalfd_siginfo{ | ||
89 | k_u32 ssi_signo; | ||
90 | k_s32 ssi_errno; | ||
91 | k_s32 ssi_code; | ||
92 | k_u32 ssi_pid; | ||
93 | k_u32 ssi_uid; | ||
94 | k_s32 ssi_fd; | ||
95 | k_u32 ssi_tid; | ||
96 | k_u32 ssi_band; | ||
97 | k_u32 ssi_overrun; | ||
98 | k_u32 ssi_trapno; | ||
99 | k_s32 ssi_status; | ||
100 | k_s32 ssi_int; | ||
101 | k_u64 ssi_ptr; | ||
102 | k_u64 ssi_utime; | ||
103 | k_u64 ssi_stime; | ||
104 | k_u64 ssi_addr; | ||
105 | |||
106 | //Pad strcture to 128 bytes. Remember to update the | ||
107 | //pad size when you add new members. We use a fixed | ||
108 | //size structure to avoid compatibility problems with | ||
109 | //future versions, and we leave extra space for additional | ||
110 | //members. We use fixed size members because this strcture | ||
111 | //comes out of a read(2) and we really don't want to have | ||
112 | //a compat on read(2). | ||
113 | k_u8 __pad[48]; | ||
114 | }; | ||
115 | //---------------------------------------------------------------------------------------------- | ||
116 | #endif |
File ulinux/socket/msg.h added (mode: 100644) (index 0000000..e1d2ae2) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_SOCKET_MSG_H | ||
6 | #define ULINUX_SOCKET_MSG_H | ||
7 | #define K_SOL_SOCKET 1 | ||
8 | |||
9 | struct k_iovec{//will probably go somewhere else proper kernel facilities are used | ||
10 | void *iov_base;//BSD uses caddr_t (1003.1g requires void *) | ||
11 | k_sz iov_len; //Must be size_t (1003.1g) | ||
12 | }; | ||
13 | |||
14 | struct k_msghdr{ | ||
15 | void *msg_name; //socket name | ||
16 | k_i msg_namelen; //length of name | ||
17 | struct k_iovec *msg_iov; //data blocks | ||
18 | k_sz msg_iovlen; //number of blocks | ||
19 | void *msg_control; //per protocol magic (eg BSD file descriptor passing) | ||
20 | k_sz msg_controllen;//length of cmsg list | ||
21 | k_u msg_flags; | ||
22 | }; | ||
23 | |||
24 | struct k_cmsghdr{ | ||
25 | k_sz cmsg_len; //data byte count, including hdr | ||
26 | k_i cmsg_level;//originating protocol | ||
27 | k_i cmsg_type; //protocol-specific type | ||
28 | }; | ||
29 | |||
30 | #define __K_CMSG_NXTHDR(ctl,len,cmsg) __k_cmsg_nxthdr((ctl),(len),(cmsg)) | ||
31 | #define K_CMSG_NXTHDR(mhdr,cmsg) __k_cmsg_nxthdr((mhdr),(cmsg)) | ||
32 | |||
33 | #define K_CMSG_ALIGN(len) (((len)+sizeof(k_l)-1)&~(sizeof(k_l)-1)) | ||
34 | |||
35 | #define K_CMSG_DATA(cmsg) ((void*)((k_u8*)(cmsg)+K_CMSG_ALIGN(sizeof(struct k_cmsghdr)))) | ||
36 | #define K_CMSG_SPACE(len) (K_CMSG_ALIGN(sizeof(struct k_cmsghdr))+K_CMSG_ALIGN(len)) | ||
37 | #define K_CMSG_LEN(len) (K_CMSG_ALIGN(sizeof(struct k_cmsghdr))+(len)) | ||
38 | |||
39 | #define __K_CMSG_FIRSTHDR(ctl,len) ((len)>=sizeof(struct k_cmsghdr) ? \ | ||
40 | (struct k_cmsghdr *)(ctl) : \ | ||
41 | (struct k_cmsghdr *)NULL) | ||
42 | #define K_CMSG_FIRSTHDR(msg) __K_CMSG_FIRSTHDR((msg)->msg_control,(msg)->msg_controllen) | ||
43 | #define K_CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len>=sizeof(struct cmsghdr)&&\ | ||
44 | (cmsg)->cmsg_len<=(k_ul)\ | ||
45 | ((mhdr)->msg_controllen-\ | ||
46 | ((k_u8*)(cmsg)-(k_u8*)(mhdr)->msg_control))) | ||
47 | |||
48 | static inline struct k_cmsghdr * __k_cmsg_nxthdr(void *ctl,k_sz size,struct k_cmsghdr *cmsg) | ||
49 | { | ||
50 | struct k_cmsghdr *ptr; | ||
51 | |||
52 | ptr=(struct k_cmsghdr*)(((k_u8*)cmsg)+K_CMSG_ALIGN(cmsg->cmsg_len)); | ||
53 | if((k_ul)((k_u8*)(ptr+1)-(k_u8*)ctl)>size) return (struct k_cmsghdr *)0; | ||
54 | return ptr; | ||
55 | } | ||
56 | |||
57 | static inline struct k_cmsghdr *k_cmsg_nxthdr(struct k_msghdr *msg,struct k_cmsghdr *cmsg) | ||
58 | { | ||
59 | return __k_cmsg_nxthdr(msg->msg_control,msg->msg_controllen,cmsg); | ||
60 | } | ||
61 | |||
62 | #define K_MSG_CTRUNC 8 | ||
63 | #define K_SCM_RIGHTS 0x01//rw: access rights (array of int) | ||
64 | #endif |
File ulinux/socket/socket.h added (mode: 100644) (index 0000000..53c328f) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_SOCKET_SOCKET_H | ||
6 | #define ULINUX_SOCKET_SOCKET_H | ||
7 | struct k_in_addr{k_u32 s_addr;}; | ||
8 | |||
9 | #define __SOCK_SIZE__ 16//sizeof(struct sockaddr) | ||
10 | struct k_sockaddr_in{ | ||
11 | k_us sin_family;//address family | ||
12 | k_u16 sin_port;//port number | ||
13 | struct k_in_addr sin_addr;//internet address | ||
14 | |||
15 | //pad to size of `struct sockaddr' | ||
16 | k_u8 __pad[__SOCK_SIZE__-sizeof(k_s)-sizeof(k_us)-sizeof(struct k_in_addr)]; | ||
17 | }; | ||
18 | |||
19 | #define K_AF_UNSPEC 0 | ||
20 | #define K_AF_UNIX 1//Unix domain sockets | ||
21 | #define K_AF_LOCAL 1//POSIX name for AF_UNIX | ||
22 | #define K_AF_INET 2//Internet IP Protocol | ||
23 | #define K_AF_AX25 3//Amateur Radio AX.25 | ||
24 | #define K_AF_IPX 4//Novell IPX | ||
25 | #define K_AF_APPLETALK 5//AppleTalk DDP | ||
26 | #define K_AF_NETROM 6//Amateur Radio NET/ROM | ||
27 | #define K_AF_BRIDGE 7//Multiprotocol bridge | ||
28 | #define K_AF_ATMPVC 8//ATM PVCs | ||
29 | #define K_AF_X25 9//Reserved for X.25 project | ||
30 | #define K_AF_INET6 10//IP version 6 | ||
31 | #define K_AF_ROSE 11//Amateur Radio X.25 PLP | ||
32 | #define K_AF_DECnet 12//Reserved for DECnet project | ||
33 | #define K_AF_NETBEUI 13//Reserved for 802.2LLC project | ||
34 | #define K_AF_SECURITY 14//Security callback pseudo AF | ||
35 | #define K_AF_KEY 15//PF_KEY key management API | ||
36 | #define K_AF_NETLINK 16 | ||
37 | #define K_AF_ROUTE K_AF_NETLINK//Alias to emulate 4.4BSD | ||
38 | #define K_AF_PACKET 17//Packet family | ||
39 | #define K_AF_ASH 18//Ash | ||
40 | #define K_AF_ECONET 19//Acorn Econet | ||
41 | #define K_AF_ATMSVC 20//ATM SVCs | ||
42 | #define K_AF_RDS 21//RDS sockets | ||
43 | #define K_AF_SNA 22//Linux SNA Project (nutters!) | ||
44 | #define K_AF_IRDA 23//IRDA sockets | ||
45 | #define K_AF_PPPOX 24//PPPoX sockets | ||
46 | #define K_AF_WANPIPE 25//Wanpipe API Sockets | ||
47 | #define K_AF_LLC 26//Linux LLC | ||
48 | #define K_AF_CAN 29//Controller Area Network | ||
49 | #define K_AF_TIPC 30//TIPC sockets | ||
50 | #define K_AF_BLUETOOTH 31//Bluetooth sockets | ||
51 | #define K_AF_IUCV 32//IUCV sockets | ||
52 | #define K_AF_RXRPC 33//RxRPC sockets | ||
53 | #define K_AF_ISDN 34//mISDN sockets | ||
54 | #define K_AF_PHONET 35//Phonet sockets | ||
55 | #define K_AF_IEEE802154 36//IEEE802154 sockets | ||
56 | #define K_AF_CAIF 37//CAIF sockets | ||
57 | #define K_AF_MAX 38//For now.. | ||
58 | |||
59 | //protocol families, same as address families | ||
60 | #define K_PF_UNSPEC K_AF_UNSPEC | ||
61 | #define K_PF_UNIX K_AF_UNIX | ||
62 | #define K_PF_LOCAL K_AF_LOCAL | ||
63 | #define K_PF_INET K_AF_INET | ||
64 | #define K_PF_AX25 K_AF_AX25 | ||
65 | #define K_PF_IPX K_AF_IPX | ||
66 | #define K_PF_APPLETALK K_AF_APPLETALK | ||
67 | #define K_PF_NETROM K_AF_NETROM | ||
68 | #define K_PF_BRIDGE K_AF_BRIDGE | ||
69 | #define K_PF_ATMPVC K_AF_ATMPVC | ||
70 | #define K_PF_X25 K_AF_X25 | ||
71 | #define K_PF_INET6 K_AF_INET6 | ||
72 | #define K_PF_ROSE K_AF_ROSE | ||
73 | #define K_PF_DECnet K_AF_DECnet | ||
74 | #define K_PF_NETBEUI K_AF_NETBEUI | ||
75 | #define K_PF_SECURITY K_AF_SECURITY | ||
76 | #define K_PF_KEY K_AF_KEY | ||
77 | #define K_PF_NETLINK K_AF_NETLINK | ||
78 | #define K_PF_ROUTE K_AF_ROUTE | ||
79 | #define K_PF_PACKET K_AF_PACKET | ||
80 | #define K_PF_ASH K_AF_ASH | ||
81 | #define K_PF_ECONET K_AF_ECONET | ||
82 | #define K_PF_ATMSVC K_AF_ATMSVC | ||
83 | #define K_PF_RDS K_AF_RDS | ||
84 | #define K_PF_SNA K_AF_SNA | ||
85 | #define K_PF_IRDA K_AF_IRDA | ||
86 | #define K_PF_PPPOX K_AF_PPPOX | ||
87 | #define K_PF_WANPIPE K_AF_WANPIPE | ||
88 | #define K_PF_LLC K_AF_LLC | ||
89 | #define K_PF_CAN K_AF_CAN | ||
90 | #define K_PF_TIPC K_AF_TIPC | ||
91 | #define K_PF_BLUETOOTH K_AF_BLUETOOTH | ||
92 | #define K_PF_IUCV K_AF_IUCV | ||
93 | #define K_PF_RXRPC K_AF_RXRPC | ||
94 | #define K_PF_ISDN K_AF_ISDN | ||
95 | #define K_PF_PHONET K_AF_PHONET | ||
96 | #define K_PF_IEEE802154 K_AF_IEEE802154 | ||
97 | #define K_PF_CAIF K_AF_CAIF | ||
98 | #define K_PF_MAX K_AF_MAX | ||
99 | |||
100 | enum k_sock_type{ | ||
101 | K_SOCK_STREAM= 1, | ||
102 | K_SOCK_DGRAM= 2, | ||
103 | K_SOCK_RAW= 3, | ||
104 | K_SOCK_RDM= 4, | ||
105 | K_SOCK_SEQPACKET=5, | ||
106 | K_SOCK_DCCP= 6, | ||
107 | K_SOCK_PACKET= 10, | ||
108 | }; | ||
109 | |||
110 | #define K_SOCK_NONBLOCK K_O_NONBLOCK | ||
111 | |||
112 | //XXX:should go in a IPv4 section | ||
113 | #define K_INADDR_ANY ((k_ul)0x00000000) | ||
114 | |||
115 | //---------------------------------------------------------------------------------------------- | ||
116 | //options | ||
117 | //---------------------------------------------------------------------------------------------- | ||
118 | #define K_SOL_SOCKET 1//for setsockopt(2) | ||
119 | |||
120 | #define K_SO_DEBUG 1 | ||
121 | #define K_SO_REUSEADDR 2 | ||
122 | #define K_SO_TYPE 3 | ||
123 | #define K_SO_ERROR 4 | ||
124 | #define K_SO_DONTROUTE 5 | ||
125 | #define K_SO_BROADCAST 6 | ||
126 | #define K_SO_SNDBUF 7 | ||
127 | #define K_SO_RCVBUF 8 | ||
128 | #define K_SO_SNDBUFFORCE 32 | ||
129 | #define K_SO_RCVBUFFORCE 33 | ||
130 | #define K_SO_KEEPALIVE 9 | ||
131 | #define K_SO_OOBINLINE 10 | ||
132 | #define K_SO_NO_CHECK 11 | ||
133 | #define K_SO_PRIORITY 12 | ||
134 | #define K_SO_LINGER 13 | ||
135 | #define K_SO_BSDCOMPAT 14 | ||
136 | //to add :#define SO_REUSEPORT 15 | ||
137 | #define K_SO_PASSCRED 16 | ||
138 | #define K_SO_PEERCRED 17 | ||
139 | #define K_SO_RCVLOWAT 18 | ||
140 | #define K_SO_SNDLOWAT 19 | ||
141 | #define K_SO_RCVTIMEO 20 | ||
142 | #define K_SO_SNDTIMEO 21 | ||
143 | |||
144 | //security levels - as per NRL IPv6 - don't actually do anything | ||
145 | #define K_SO_SECURITY_AUTHENTICATION 22 | ||
146 | #define K_SO_SECURITY_ENCRYPTION_TRANSPORT 23 | ||
147 | #define K_SO_SECURITY_ENCRYPTION_NETWORK 24 | ||
148 | |||
149 | #define K_SO_BINDTODEVICE 25 | ||
150 | |||
151 | //socket filtering | ||
152 | #define K_SO_ATTACH_FILTER 26 | ||
153 | #define K_SO_DETACH_FILTER 27 | ||
154 | |||
155 | #define K_SO_PEERNAME 28 | ||
156 | #define K_SO_TIMESTAMP 29 | ||
157 | #define K_SCM_TIMESTAMP K_SO_TIMESTAMP | ||
158 | |||
159 | #define K_SO_ACCEPTCONN 30 | ||
160 | |||
161 | #define K_SO_PEERSEC 31 | ||
162 | #define K_SO_PASSSEC 34 | ||
163 | #define K_SO_TIMESTAMPNS 35 | ||
164 | #define K_SCM_TIMESTAMPNS K_SO_TIMESTAMPNS | ||
165 | |||
166 | #define K_SO_MARK 36 | ||
167 | |||
168 | #define K_SO_TIMESTAMPING 37 | ||
169 | #define K_SCM_TIMESTAMPING K_SO_TIMESTAMPING | ||
170 | |||
171 | #define K_SO_PROTOCOL 38 | ||
172 | #define K_SO_DOMAIN 39 | ||
173 | |||
174 | #define K_SO_RXQ_OVFL 40 | ||
175 | //---------------------------------------------------------------------------------------------- | ||
176 | #endif |
File ulinux/stat.h added (mode: 100644) (index 0000000..12f0850) | |||
1 | //****************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //* <digital.ragnarok AT gmail dot com> | ||
5 | //****************************************************************************** | ||
6 | #ifndef ULINUX_STAT_H | ||
7 | #define ULINUX_STAT_H | ||
8 | #include <ulinux/arch/stat.h> | ||
9 | |||
10 | #define K_S_IFSOCK 0140000 | ||
11 | #define K_S_IFLNK 0120000 | ||
12 | #define K_S_IFREG 0100000 | ||
13 | #define K_S_IFBLK 0060000 | ||
14 | #define K_S_IFDIR 0040000 | ||
15 | #define K_S_IFCHR 0020000 | ||
16 | #define K_S_IFIFO 0010000 | ||
17 | #define K_S_ISUID 0004000 | ||
18 | #define K_S_ISGID 0002000 | ||
19 | #define K_S_ISVTX 0001000 | ||
20 | |||
21 | #define K_S_IRWXU 00700 | ||
22 | #define K_S_IRUSR 00400 | ||
23 | #define K_S_IWUSR 00200 | ||
24 | #define K_S_IXUSR 00100 | ||
25 | |||
26 | #define k_S_IRWXG 00070 | ||
27 | #define k_S_IRGRP 00040 | ||
28 | #define k_S_IWGRP 00020 | ||
29 | #define k_S_IXGRP 00010 | ||
30 | |||
31 | #define K_S_IRWXO 00007 | ||
32 | #define K_S_IROTH 00004 | ||
33 | #define K_S_IWOTH 00002 | ||
34 | #define K_S_IXOTH 00001 | ||
35 | |||
36 | #define K_S_IRWXUGO (K_S_IRWXU|K_S_IRWXG|K_S_IRWXO) | ||
37 | #define K_S_IALLUGO (K_S_ISUID|K_S_ISGID|K_S_ISVTX|K_S_IRWXUGO) | ||
38 | #define K_S_IRUGO (K_S_IRUSR|K_S_IRGRP|K_S_IROTH) | ||
39 | #define K_S_IWUGO (K_S_IWUSR|K_S_IWGRP|K_S_IWOTH) | ||
40 | #define K_S_IXUGO (K_S_IXUSR|K_S_IXGRP|K_S_IXOTH) | ||
41 | #endif |
File ulinux/utils/ascii/ascii.h added (mode: 100644) (index 0000000..54577ee) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_ASCII_H | ||
6 | #define ULINUX_UTILS_ASCII_ASCII_H | ||
7 | #include <ulinux/arch/utils/ascii.h> | ||
8 | //XXX:may use linux ctype ascii table | ||
9 | static inline k_ut u_a_is_digit(k_u8 c) | ||
10 | { | ||
11 | if('0'<=c&&c<='9') return 1; | ||
12 | return 0; | ||
13 | } | ||
14 | |||
15 | static inline k_ut u_a_is_alnum(k_u8 c) | ||
16 | { | ||
17 | if('0'<=c&&c<='9') return 1; | ||
18 | if('A'<=c&&c<='Z') return 1; | ||
19 | if('a'<=c&&c<='z') return 1; | ||
20 | return 0; | ||
21 | } | ||
22 | |||
23 | static inline k_ut u_a_is_lower(k_u8 c) | ||
24 | { | ||
25 | if('a'<=c&&c<='z') return 1; | ||
26 | return 0; | ||
27 | } | ||
28 | |||
29 | static inline k_u8 u_a_2upper(k_u8 c) | ||
30 | { | ||
31 | if(u_a_is_lower(c)) c-='a'-'A'; | ||
32 | return c; | ||
33 | } | ||
34 | #endif |
File ulinux/utils/ascii/block/conv/decimal/decimal.c added (mode: 100644) (index 0000000..42f56fb) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #include <ulinux/compiler_types.h> | ||
6 | #include <ulinux/arch/types.h> | ||
7 | |||
8 | #include <ulinux/utils/ascii/ascii.h> | ||
9 | //strict unsigned decimal ascii block to byte | ||
10 | //C version | ||
11 | //caller must provide a valid memory block | ||
12 | //inplace conversion: ok | ||
13 | //return compiler boolean:success or failed | ||
14 | k_ut u_a_strict_dec2u8_blk(k_u8 *dest,k_u8 *start,k_u8 *end) | ||
15 | {//do *not* trust content | ||
16 | if((k_ul)(end-start)>=sizeof("255")-1) return 0; | ||
17 | k_u16 buf=0; | ||
18 | while(start<=end){ | ||
19 | if(!u_a_is_digit(*start)) return 0; | ||
20 | buf=buf*10+(*start-'0'); | ||
21 | ++start; | ||
22 | } | ||
23 | if(buf&0xff00) return 0;//overflow, max is 999 way below what can do a u16 | ||
24 | *dest=(k_u8)buf; | ||
25 | return 1; | ||
26 | } | ||
27 | |||
28 | //strict unisgned decimal ascii block to u16 | ||
29 | //C version | ||
30 | //caller must provide a valid memory block | ||
31 | //inplace conversion: ok | ||
32 | //return compiler boolean:success or failed | ||
33 | k_ut u_a_strict_dec2u16_blk(k_u16 *dest,k_u8 *start,k_u8 *end) | ||
34 | {//do *not* trust content | ||
35 | if((k_ul)(end-start)>=sizeof("65535")-1) return 0; | ||
36 | k_u32 buf=0; | ||
37 | while(start<=end){ | ||
38 | if(!u_a_is_digit(*start)) return 0; | ||
39 | buf=buf*10+(*start-'0'); | ||
40 | ++start; | ||
41 | } | ||
42 | if(buf&0xffff0000) return 0;//overflow, max is 99999 way below what can do a u32 | ||
43 | *dest=buf; | ||
44 | return 1; | ||
45 | } | ||
46 | |||
47 | //strict unisgned decimal ascii block to u64 | ||
48 | //C version | ||
49 | //caller must provide a valid memory block | ||
50 | //inplace conversion: ok | ||
51 | //return compiler boolean:success or failed | ||
52 | k_ut u_a_strict_dec2u64_blk(k_u64 *dest,k_u8 *start,k_u8 *end) | ||
53 | {//do *not* trust | ||
54 | if((k_ul)(end-start)>=sizeof("18446744073709551615")-1) return 0; | ||
55 | k_u64 buf=0; | ||
56 | while(start<=end){ | ||
57 | if(!u_a_is_digit(*start)) return 0; | ||
58 | k_u64 old_buf=buf; | ||
59 | buf=buf*10+(*start-'0'); | ||
60 | if(buf<old_buf) return 0;//overflow check | ||
61 | ++start; | ||
62 | } | ||
63 | *dest=buf; | ||
64 | return 1; | ||
65 | } |
File ulinux/utils/ascii/block/conv/decimal/decimal.h added (mode: 100644) (index 0000000..1ee3713) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_BLOCK_CONV_DECIMAL_DECIMAL_H | ||
6 | #define ULINUX_UTILS_ASCII_BLOCK_CONV_DECIMAL_DECIMAL_H | ||
7 | extern k_ut u_a_strict_dec2u8_blk(k_u8 *dest,k_u8 *start,k_u8 *end); | ||
8 | extern k_ut u_a_strict_dec2u16_blk(k_u16 *dest,k_u8 *start,k_u8 *end); | ||
9 | extern k_ut u_a_strict_dec2u64_blk(k_u64 *dest,k_u8 *start,k_u8 *end); | ||
10 | #endif |
File ulinux/utils/ascii/block/conv/net/net.c added (mode: 100644) (index 0000000..ff1c3f6) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #include <ulinux/compiler_types.h> | ||
6 | #include <ulinux/arch/types.h> | ||
7 | |||
8 | #include <ulinux/utils/ascii/block/conv/decimal/decimal.h> | ||
9 | #include <ulinux/utils/endian.h> | ||
10 | |||
11 | //strict ipv4 (xxx.xxx.xxx.xxx) decimal ascii block to 32 bits big endian (network endian) ipv4 | ||
12 | //C version | ||
13 | //caller must provide a valid memory block | ||
14 | //inplace conversion: ok because minimal address is 7 bytes to 4 bytes | ||
15 | //return compiler boolean:success or failed | ||
16 | k_ut u_a_strict_2ipv4_blk(k_u32 *dest,k_u8 *start,k_u8 *end) | ||
17 | {//do *not* trust content | ||
18 | k_u32 ipv4=0; | ||
19 | k_u8 *n_start=start; | ||
20 | k_u8 *n_end=start; | ||
21 | k_u8 u; | ||
22 | |||
23 | for(k_i i=0;i<=2;++i){//only for the first 3 components | ||
24 | if(*n_end=='.') return 0; | ||
25 | |||
26 | while(n_end<=end&&(n_end-n_start)<=3&&*n_end!='.') ++n_end; | ||
27 | |||
28 | if(n_end==end||*n_end!='.') return 0; | ||
29 | |||
30 | u=0; | ||
31 | if(!u_a_strict_dec2u8_blk(&u,n_start,n_end-1)) return 0; | ||
32 | ipv4|=u<<(24-(i*8)); | ||
33 | |||
34 | n_start=n_end+1; | ||
35 | n_end=n_start; | ||
36 | } | ||
37 | |||
38 | u=0; | ||
39 | if(!u_a_strict_dec2u8_blk(&u,n_start,end)) return 0; | ||
40 | ipv4|=u; | ||
41 | *dest=u_cpu2be32(ipv4); | ||
42 | return 1; | ||
43 | } |
File ulinux/utils/ascii/block/conv/net/net.h added (mode: 100644) (index 0000000..219e1fc) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_BLOCK_CONV_NET_NET_H | ||
6 | #define ULINUX_UTILS_ASCII_BLOCK_CONV_NET_NET_H | ||
7 | extern k_ut u_a_strict_2ipv4_blk(k_u32 *dest,k_u8 *start,k_u8 *end); | ||
8 | #endif |
File ulinux/utils/ascii/string/conv/decimal/decimal.c added (mode: 100644) (index 0000000..80839d3) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | |||
6 | #include <ulinux/compiler_types.h> | ||
7 | #include <ulinux/arch/types.h> | ||
8 | #include <ulinux/utils/ascii/ascii.h> | ||
9 | #include <ulinux/utils/ascii/string/string.h> | ||
10 | |||
11 | //strict unsigned decimal ascii str to u16 | ||
12 | //C version | ||
13 | //caller must provide a valid pointer with readable data | ||
14 | //inplace conversion: ok | ||
15 | //return compiler boolean:success or failed | ||
16 | k_ut u_a_strict_dec2u16(k_u16 *dest,k_u8 *start) | ||
17 | {//do *not* trust | ||
18 | k_ul len; | ||
19 | if(!u_a_strnlen(&len,start,sizeof("65535")-1)) return 0; | ||
20 | if(!len) return 0; | ||
21 | |||
22 | k_u32 buf=0; | ||
23 | k_u8 *end=start+len-1; | ||
24 | while(start<=end){ | ||
25 | if(!u_a_is_digit(*start)) return 0; | ||
26 | buf=buf*10+(*start-'0'); | ||
27 | ++start; | ||
28 | } | ||
29 | if(buf&0xffff0000) return 0;//overflow, max is 99999 way below what can do a u32 | ||
30 | *dest=(k_u16)buf; | ||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | //strict unsigned decimal ascii str to u64 | ||
35 | //C version | ||
36 | //caller must provide a valid pointer with readable data | ||
37 | //inplace conversion: ok | ||
38 | //return compiler boolean:success or failed | ||
39 | k_ut u_a_strict_dec2u64(k_u64 *dest,k_u8 *start) | ||
40 | {//do *not* trust content | ||
41 | k_ul len; | ||
42 | if(!u_a_strnlen(&len,start,sizeof("18446744073709551615")-1)) return 0; | ||
43 | if(!len) return 0; | ||
44 | |||
45 | k_u64 buf=0; | ||
46 | k_u8 *end=start+len-1; | ||
47 | while(start<=end){ | ||
48 | if(!u_a_is_digit(*start)) return 0; | ||
49 | k_u64 old_buf=buf; | ||
50 | buf=buf*10+(*start-'0'); | ||
51 | if(buf<old_buf) return 0;//overflow check | ||
52 | ++start; | ||
53 | } | ||
54 | *dest=buf; | ||
55 | return 1; | ||
56 | } |
File ulinux/utils/ascii/string/conv/decimal/decimal.h added (mode: 100644) (index 0000000..bbf2599) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_STRING_CONV_DECIMAL_DECIMAL_H | ||
6 | #define ULINUX_UTILS_ASCII_STRING_CONV_DECIMAL_DECIMAL_H | ||
7 | extern k_ut u_a_strict_dec2u16(k_u16 *dest,k_u8 *start); | ||
8 | extern k_ut u_a_strict_dec2u64(k_u64 *dest,k_u8 *start); | ||
9 | #endif |
File ulinux/utils/ascii/string/string.c added (mode: 100644) (index 0000000..71ced68) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #include <ulinux/compiler_types.h> | ||
6 | #include <ulinux/arch/types.h> | ||
7 | |||
8 | k_ut u_a_strnlen(k_ul *sz,k_u8 *start,k_ul max) | ||
9 | { | ||
10 | k_u8 *end; | ||
11 | for(end=start;max--&&*end!=0;++end); | ||
12 | if(*end==0){ | ||
13 | *sz=end-start; | ||
14 | return 1; | ||
15 | } | ||
16 | return 0; | ||
17 | } |
File ulinux/utils/ascii/string/string.h added (mode: 100644) (index 0000000..5b908a9) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_STRING_STRING_H | ||
6 | #define ULINUX_UTILS_ASCII_STRING_STRING_H | ||
7 | //strncmp - Compare two length-limited strings | ||
8 | //@cs: One string | ||
9 | //@ct: Another string | ||
10 | //@count: The maximum number of bytes to compare | ||
11 | static inline k_i u_a_strncmp(k_u8 *cs,k_u8 *ct,k_ul count) | ||
12 | { | ||
13 | k_u8 c1,c2; | ||
14 | while(count){ | ||
15 | c1=*cs++; | ||
16 | c2=*ct++; | ||
17 | if(c1!=c2) return c1<c2?-1:1; | ||
18 | if(!c1) break; | ||
19 | count--; | ||
20 | } | ||
21 | return 0; | ||
22 | } | ||
23 | |||
24 | extern k_ut u_a_strnlen(k_ul *sz,k_u8 *start,k_ul max); | ||
25 | #endif |
File ulinux/utils/ascii/string/vsprintf.c added (mode: 100644) (index 0000000..3d1e9a8) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*ripper:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //*ripped and sort of ported to user space from Linux kernel | ||
5 | //********************************************************************************************** | ||
6 | |||
7 | //linux/lib/vsprintf.c | ||
8 | // | ||
9 | // Copyright (C) 1991, 1992 Linus Torvalds | ||
10 | // | ||
11 | // | ||
12 | //vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ | ||
13 | // | ||
14 | //Wirzenius wrote this portably, Torvalds fucked it up :-) | ||
15 | // | ||
16 | // | ||
17 | //Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> | ||
18 | //- changed to provide snprintf and vsnprintf functions | ||
19 | //So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> | ||
20 | //- scnprintf and vscnprintf | ||
21 | |||
22 | #include <stdarg.h> | ||
23 | |||
24 | #include <ulinux/compiler_types.h> | ||
25 | #include <ulinux/arch/types.h> | ||
26 | #include <ulinux/mmap.h> | ||
27 | #include <ulinux/utils/div.h> | ||
28 | #include <ulinux/utils/ascii/string/string.h> | ||
29 | #include <ulinux/ipv6.h> | ||
30 | #include <ulinux/utils/endian.h> | ||
31 | #include <ulinux/utils/ipv6.h> | ||
32 | #include <ulinux/utils/mem.h> | ||
33 | #include <ulinux/utils/ascii/ascii.h> | ||
34 | #include <ulinux/arch/sysc.h> | ||
35 | #include <ulinux/error.h> | ||
36 | |||
37 | //works only for digits and letters, but small and fast | ||
38 | #define TOLOWER(x) ((x)|0x20) | ||
39 | |||
40 | #define ZEROPAD 1 //pad with zero | ||
41 | #define SIGN 2 //unsigned/signed long | ||
42 | #define PLUS 4 //show plus | ||
43 | #define SPACE 8 //space if plus | ||
44 | #define LEFT 16//left justified | ||
45 | #define SMALL 32//use lowercase in hex (must be 32 == 0x20) | ||
46 | #define SPECIAL 64//prefix hex with "0x", octal with "0" | ||
47 | |||
48 | enum format_type{ | ||
49 | FORMAT_TYPE_NONE,//Just a string part | ||
50 | FORMAT_TYPE_WIDTH, | ||
51 | FORMAT_TYPE_PRECISION, | ||
52 | FORMAT_TYPE_CHAR, | ||
53 | FORMAT_TYPE_STR, | ||
54 | FORMAT_TYPE_PTR, | ||
55 | FORMAT_TYPE_PERCENT_CHAR, | ||
56 | FORMAT_TYPE_INVALID, | ||
57 | FORMAT_TYPE_LONG_LONG, | ||
58 | FORMAT_TYPE_ULONG, | ||
59 | FORMAT_TYPE_LONG, | ||
60 | FORMAT_TYPE_UBYTE, | ||
61 | FORMAT_TYPE_BYTE, | ||
62 | FORMAT_TYPE_USHORT, | ||
63 | FORMAT_TYPE_SHORT, | ||
64 | FORMAT_TYPE_UINT, | ||
65 | FORMAT_TYPE_INT, | ||
66 | FORMAT_TYPE_NRCHARS, | ||
67 | FORMAT_TYPE_SIZE_T, | ||
68 | FORMAT_TYPE_PTRDIFF | ||
69 | }; | ||
70 | |||
71 | struct printf_spec { | ||
72 | k_u8 type; //format_type enum | ||
73 | k_u8 flags; //flags to number() | ||
74 | k_u8 base; //number base, 8, 10 or 16 only | ||
75 | k_u8 qualifier; //number qualifier, one of 'hHlLtzZ' | ||
76 | k_s16 field_width;//width of output field, -1 means not present or ignored for some reason | ||
77 | k_s16 precision; //# of digits/chars, -1 means not present or ignored for some reason | ||
78 | }; | ||
79 | |||
80 | //same with if's removed. Always emits five digits | ||
81 | static k_u8 *_put_dec_full(k_u8 *buf,k_u q) | ||
82 | { | ||
83 | //BTW, if q is in [0,9999], 8-bit ints will be enough, | ||
84 | //but anyway, gcc produces better code with full-sized ints | ||
85 | k_u d3,d2,d1,d0; | ||
86 | d1=(q>>4)&0xf; | ||
87 | d2=(q>>8)&0xf; | ||
88 | d3=(q>>12); | ||
89 | |||
90 | //Possible ways to approx. divide by 10 | ||
91 | //gcc -O2 replaces multiply with shifts and adds | ||
92 | //(x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) | ||
93 | //(x * 0x67) >> 10: 1100111 | ||
94 | //(x * 0x34) >> 9: 110100 - same | ||
95 | //(x * 0x1a) >> 8: 11010 - same | ||
96 | //(x * 0x0d) >> 7: 1101 - same, shortest code (on i386) | ||
97 | d0=6*(d3+d2+d1)+(q&0xf); | ||
98 | q=(d0*0xcd)>>11; | ||
99 | d0=d0-10*q; | ||
100 | *buf++=d0+'0'; | ||
101 | d1=q+9*d3+5*d2+d1; | ||
102 | q=(d1*0xcd)>>11; | ||
103 | d1=d1-10*q; | ||
104 | *buf++=d1+'0'; | ||
105 | |||
106 | d2=q+2*d2; | ||
107 | q=(d2*0xd)>>7; | ||
108 | d2=d2-10*q; | ||
109 | *buf++=d2+'0'; | ||
110 | |||
111 | d3=q+4*d3; | ||
112 | q=(d3*0xcd)>>11;//- shorter code | ||
113 | //q = (d3 * 0x67) >> 10; - would also work | ||
114 | d3=d3-10*q; | ||
115 | *buf++=d3+'0'; | ||
116 | *buf++=q+'0'; | ||
117 | return buf; | ||
118 | } | ||
119 | |||
120 | //Decimal conversion is by far the most typical, and is used | ||
121 | //for /proc and /sys data. This directly impacts e.g. top performance | ||
122 | //with many processes running. We optimize it for speed | ||
123 | //using code from | ||
124 | //http://www.cs.uiowa.edu/~jones/bcd/decimal.html | ||
125 | //(with permission from the author, Douglas W. Jones). | ||
126 | |||
127 | //Formats correctly any integer in [0,99999]. | ||
128 | //Outputs from one to five digits depending on input. | ||
129 | //On i386 gcc 4.1.2 -O2: ~250 bytes of code. | ||
130 | static k_u8 *_put_dec_trunc(k_u8 *buf,k_u q) | ||
131 | { | ||
132 | k_u d3,d2,d1,d0; | ||
133 | d1=(q>>4)&0xf; | ||
134 | d2=(q>>8)&0xf; | ||
135 | d3=(q>>12); | ||
136 | |||
137 | d0=6*(d3+d2+d1)+(q&0xf); | ||
138 | q=(d0*0xcd)>>11; | ||
139 | d0=d0-10*q; | ||
140 | *buf++=d0+'0';//least significant digit | ||
141 | d1=q+9*d3+5*d2+d1; | ||
142 | if(d1!=0){ | ||
143 | q=(d1*0xcd)>>11; | ||
144 | d1=d1-10*q; | ||
145 | *buf++=d1+'0';//next digit | ||
146 | |||
147 | d2=q+2*d2; | ||
148 | if((d2!=0)||(d3!= 0)){ | ||
149 | q=(d2*0xd)>>7; | ||
150 | d2=d2-10*q; | ||
151 | *buf++=d2+'0';//next digit | ||
152 | |||
153 | d3=q+4*d3; | ||
154 | if(d3!=0){ | ||
155 | q=(d3*0xcd)>>11; | ||
156 | d3=d3-10*q; | ||
157 | *buf++=d3+'0';//next digit | ||
158 | if(q!=0) *buf++=q+'0';//most sign. digit | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | return buf; | ||
163 | } | ||
164 | |||
165 | //no inlining helps gcc to use registers better | ||
166 | static k_u8 *_put_dec(k_u8 *buf,k_ull num) | ||
167 | { | ||
168 | for(;;){ | ||
169 | k_u rem; | ||
170 | if(num<100000) return _put_dec_trunc(buf,num); | ||
171 | rem=u_do_div(num,100000); | ||
172 | buf=_put_dec_full(buf,rem); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | static k_u8 *_number(k_u8 *buf,k_u8 *end,k_ull num,struct printf_spec spec) | ||
177 | { | ||
178 | //we are called with base 8, 10 or 16, only, thus don't need "G..." | ||
179 | //locase = 0 or 0x20. ORing digits or letters with 'locase' | ||
180 | //produces same digits or (maybe lowercased) letters | ||
181 | k_u8 locase=(spec.flags&SMALL); | ||
182 | if(spec.flags&LEFT) spec.flags&=~ZEROPAD; | ||
183 | k_u8 sign=0; | ||
184 | if(spec.flags&SIGN) { | ||
185 | if((k_ll)num<0){ | ||
186 | sign='-'; | ||
187 | num=-(k_ll)num; | ||
188 | spec.field_width--; | ||
189 | }else if(spec.flags&PLUS){ | ||
190 | sign='+'; | ||
191 | spec.field_width--; | ||
192 | }else if(spec.flags&SPACE){ | ||
193 | sign=' '; | ||
194 | spec.field_width--; | ||
195 | } | ||
196 | } | ||
197 | k_ut need_pfx=((spec.flags&SPECIAL)&&spec.base!=10); | ||
198 | if(need_pfx){ | ||
199 | spec.field_width--; | ||
200 | if(spec.base==16) spec.field_width--; | ||
201 | } | ||
202 | |||
203 | //generate full string in tmp[], in reverse order | ||
204 | static k_u8 digits[16]="0123456789ABCDEF";//"GHIJKLMNOPQRSTUVWXYZ"; | ||
205 | k_u8 tmp[66];//octal max digit number for unsigned 64 bits plus sign | ||
206 | k_i i=0; | ||
207 | if(num==0) tmp[i++]='0'; | ||
208 | //Generic code, for any base: | ||
209 | //else do { | ||
210 | //tmp[i++] = (digits[do_div(num,base)] | locase); | ||
211 | //} while (num != 0); | ||
212 | else if(spec.base!=10){//8 or 16 | ||
213 | k_i mask=spec.base-1; | ||
214 | k_i shift=3; | ||
215 | |||
216 | if(spec.base==16) shift=4; | ||
217 | do{ | ||
218 | tmp[i++]=(digits[((k_u8)num)&mask]|locase); | ||
219 | num>>=shift; | ||
220 | }while(num); | ||
221 | }else i=_put_dec(tmp,num)-tmp;//base 10 | ||
222 | |||
223 | //printing 100 using %2d gives "100", not "00" | ||
224 | if(i>spec.precision) spec.precision=i; | ||
225 | //leading space padding | ||
226 | spec.field_width-=spec.precision; | ||
227 | if(!(spec.flags&(ZEROPAD+LEFT))){ | ||
228 | while(--spec.field_width>=0){ | ||
229 | if(buf<end) *buf=' '; | ||
230 | ++buf; | ||
231 | } | ||
232 | } | ||
233 | //sign | ||
234 | if(sign){ | ||
235 | if(buf<end) *buf=sign; | ||
236 | ++buf; | ||
237 | } | ||
238 | //"0x" / "0" prefix | ||
239 | if(need_pfx){ | ||
240 | if(buf<end) *buf='0'; | ||
241 | ++buf; | ||
242 | if(spec.base==16){ | ||
243 | if(buf<end) *buf=('X'|locase); | ||
244 | ++buf; | ||
245 | } | ||
246 | } | ||
247 | //zero or space padding | ||
248 | if(!(spec.flags & LEFT)){ | ||
249 | k_u8 c=(spec.flags&ZEROPAD)?'0':' '; | ||
250 | while(--spec.field_width>=0){ | ||
251 | if(buf<end) *buf=c; | ||
252 | ++buf; | ||
253 | } | ||
254 | } | ||
255 | //hmm even more zero padding? | ||
256 | while(i<=--spec.precision){ | ||
257 | if(buf<end) *buf='0'; | ||
258 | ++buf; | ||
259 | } | ||
260 | //actual digits of result | ||
261 | while(--i>=0){ | ||
262 | if(buf<end) *buf=tmp[i]; | ||
263 | ++buf; | ||
264 | } | ||
265 | //trailing space padding | ||
266 | while(--spec.field_width>=0){ | ||
267 | if(buf<end) *buf=' '; | ||
268 | ++buf; | ||
269 | } | ||
270 | return buf; | ||
271 | } | ||
272 | |||
273 | static k_u8 *_string(k_u8 *buf,k_u8 *end,k_u8 *s,struct printf_spec spec) | ||
274 | { | ||
275 | if((k_ul)s<K_PAGE_SIZE) s=(k_u8*)"(null)";//XXX:why excluding first page *here*?? | ||
276 | |||
277 | k_ul len; | ||
278 | if(!u_a_strnlen(&len,s,spec.precision)) len=spec.precision; | ||
279 | |||
280 | if(!(spec.flags&LEFT))//it's RIGHT | ||
281 | while((k_s16)len<spec.field_width--){ | ||
282 | if(buf<end) *buf=' '; | ||
283 | ++buf; | ||
284 | } | ||
285 | |||
286 | for(k_ul i=0;i<len;++i){ | ||
287 | if(buf<end) *buf=*s; | ||
288 | ++buf;++s; | ||
289 | } | ||
290 | |||
291 | while((k_s16)len<spec.field_width--){ | ||
292 | if(buf<end) *buf=' '; | ||
293 | ++buf; | ||
294 | } | ||
295 | return buf; | ||
296 | } | ||
297 | |||
298 | static k_u8 _hex_asc[]="0123456789abcdef"; | ||
299 | #define _hex_asc_lo(x) _hex_asc[((x)&0x0f)] | ||
300 | #define _hex_asc_hi(x) _hex_asc[((x)&0xf0)>>4] | ||
301 | |||
302 | static inline k_u8 *_pack_hex_byte(k_u8 *buf,k_u8 byte) | ||
303 | { | ||
304 | *buf++=_hex_asc_hi(byte); | ||
305 | *buf++=_hex_asc_lo(byte); | ||
306 | return buf; | ||
307 | } | ||
308 | |||
309 | static k_u8 *_mac_address_string(k_u8 *buf,k_u8 *end,k_u8 *addr,struct printf_spec spec, | ||
310 | k_u8 *fmt) | ||
311 | { | ||
312 | k_u8 mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; | ||
313 | k_u8 *p=mac_addr; | ||
314 | k_u8 separator; | ||
315 | |||
316 | if(fmt[1]=='F') separator='-';//FDDI canonical format | ||
317 | else separator=':'; | ||
318 | |||
319 | for(int i=0;i< 6;++i){ | ||
320 | p=_pack_hex_byte(p,addr[i]); | ||
321 | if(fmt[0]=='M'&&i!=5) *p++=separator; | ||
322 | } | ||
323 | *p=0; | ||
324 | return _string(buf,end,mac_addr,spec); | ||
325 | } | ||
326 | |||
327 | static k_u8 *_ip4_string(k_u8 *p,k_u8 *addr,k_u8 *fmt) | ||
328 | { | ||
329 | k_i index,step; | ||
330 | switch(fmt[2]){ | ||
331 | case 'h': | ||
332 | # ifdef __BIG_ENDIAN | ||
333 | index=0; | ||
334 | step=1; | ||
335 | # else | ||
336 | index=3; | ||
337 | step=-1; | ||
338 | # endif | ||
339 | break; | ||
340 | case 'l': | ||
341 | index=3; | ||
342 | step=-1; | ||
343 | break; | ||
344 | case 'n': | ||
345 | case 'b': | ||
346 | default: | ||
347 | index=0; | ||
348 | step=1; | ||
349 | break; | ||
350 | } | ||
351 | |||
352 | k_ut leading_zeros=(fmt[0]=='i'); | ||
353 | for(k_i i=0;i<4;++i){ | ||
354 | k_u8 temp[3];//hold each IP quad in reverse order | ||
355 | k_ut digits=_put_dec_trunc(temp,addr[index])-temp; | ||
356 | if(leading_zeros){ | ||
357 | if(digits<3) *p++='0'; | ||
358 | if(digits<2) *p++='0'; | ||
359 | } | ||
360 | //reverse the digits in the quad | ||
361 | while(digits--) *p++=temp[digits]; | ||
362 | if(i<3) *p++='.'; | ||
363 | index+=step; | ||
364 | } | ||
365 | *p=0; | ||
366 | return p; | ||
367 | } | ||
368 | |||
369 | static k_u8 *_ip6_compressed_string(k_u8 *p,k_u8 *addr) | ||
370 | { | ||
371 | k_ut needcolon=0; | ||
372 | struct k_in6_addr in6; | ||
373 | |||
374 | u_memcpy(&in6,addr,sizeof(in6)); | ||
375 | |||
376 | k_ut useIPv4=u_ipv6_addr_v4mapped(&in6)||u_ipv6_addr_is_isatap(&in6); | ||
377 | |||
378 | k_ut zerolength[8]; | ||
379 | u_memset(zerolength,0,sizeof(zerolength)); | ||
380 | |||
381 | k_ut range; | ||
382 | if(useIPv4) range=6; | ||
383 | else range=8; | ||
384 | |||
385 | //find position of longest 0 run | ||
386 | for(k_ut i=0;i<range;++i) | ||
387 | for(k_ut j=i;j<range;++j){ | ||
388 | if(in6.s6_addr16[j]!=0) break; | ||
389 | zerolength[i]++; | ||
390 | } | ||
391 | |||
392 | k_ut longest=1; | ||
393 | k_ut colonpos=-1; | ||
394 | for(k_ut i=0;i<range;++i){ | ||
395 | if(zerolength[i]>longest){ | ||
396 | longest=zerolength[i]; | ||
397 | colonpos=i; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | //emit address | ||
402 | for(k_ut i=0;i<range;++i){ | ||
403 | if(i==colonpos){ | ||
404 | if(needcolon||i==0) *p++=':'; | ||
405 | *p++=':'; | ||
406 | needcolon=0; | ||
407 | i+=longest-1; | ||
408 | continue; | ||
409 | } | ||
410 | if(needcolon){ | ||
411 | *p++=':'; | ||
412 | needcolon=0; | ||
413 | } | ||
414 | //hex u16 without leading 0s | ||
415 | k_u16 word=u_be162cpu(in6.s6_addr16[i]); | ||
416 | k_u8 hi=word>>8; | ||
417 | k_u8 lo=word&0xff; | ||
418 | if(hi){ | ||
419 | if(hi>0x0f) p=_pack_hex_byte(p,hi); | ||
420 | else *p++=_hex_asc_lo(hi); | ||
421 | p=_pack_hex_byte(p,lo); | ||
422 | }else if(lo>0x0f) | ||
423 | p=_pack_hex_byte(p,lo); | ||
424 | else | ||
425 | *p++=_hex_asc_lo(lo); | ||
426 | needcolon=1; | ||
427 | } | ||
428 | |||
429 | if(useIPv4){ | ||
430 | if(needcolon) *p++=':'; | ||
431 | p=_ip4_string(p,&in6.s6_addr[12],(k_u8*)"I4"); | ||
432 | } | ||
433 | *p=0; | ||
434 | return p; | ||
435 | } | ||
436 | |||
437 | static k_u8 *_ip6_string(k_u8 *p,k_u8 *addr,k_u8 *fmt) | ||
438 | { | ||
439 | for(k_i i=0;i<8;++i){ | ||
440 | p=_pack_hex_byte(p,*addr++); | ||
441 | p=_pack_hex_byte(p,*addr++); | ||
442 | if(fmt[0]=='I'&&i!=7) *p++=':'; | ||
443 | } | ||
444 | *p=0; | ||
445 | return p; | ||
446 | } | ||
447 | |||
448 | static k_u8 *_ip6_addr_string(k_u8 *buf,k_u8 *end,k_u8 *addr,struct printf_spec spec,k_u8 *fmt) | ||
449 | { | ||
450 | k_u8 ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | ||
451 | |||
452 | if(fmt[0]=='I'&&fmt[2]=='c') _ip6_compressed_string(ip6_addr,addr); | ||
453 | else _ip6_string(ip6_addr,addr,fmt); | ||
454 | return _string(buf,end,ip6_addr,spec); | ||
455 | } | ||
456 | |||
457 | static k_u8 *_ip4_addr_string(k_u8 *buf,k_u8 *end,k_u8 *addr,struct printf_spec spec,k_u8 *fmt) | ||
458 | { | ||
459 | k_u8 ip4_addr[sizeof("255.255.255.255")]; | ||
460 | _ip4_string(ip4_addr,addr,fmt); | ||
461 | return _string(buf,end,ip4_addr,spec); | ||
462 | } | ||
463 | |||
464 | static k_u8 *_uuid_string(k_u8 *buf,k_u8 *end,k_u8 *addr,struct printf_spec spec,k_u8 *fmt) | ||
465 | { | ||
466 | static k_u8 be[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
467 | static k_u8 le[16]={3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; | ||
468 | k_u8 *index=be; | ||
469 | k_ut uc=0; | ||
470 | |||
471 | switch(*(++fmt)){ | ||
472 | case 'L': | ||
473 | uc=1;//fall-through | ||
474 | case 'l': | ||
475 | index=le; | ||
476 | break; | ||
477 | case 'B': | ||
478 | uc=1; | ||
479 | break; | ||
480 | } | ||
481 | |||
482 | k_u8 uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; | ||
483 | k_u8 *p=uuid; | ||
484 | for(k_i i=0;i<16;++i){ | ||
485 | p=_pack_hex_byte(p,addr[index[i]]); | ||
486 | switch(i){ | ||
487 | case 3: | ||
488 | case 5: | ||
489 | case 7: | ||
490 | case 9: | ||
491 | *p++='-'; | ||
492 | break; | ||
493 | } | ||
494 | } | ||
495 | *p=0; | ||
496 | |||
497 | if(uc){ | ||
498 | p=uuid; | ||
499 | do{ | ||
500 | *p=u_a_2upper(*p); | ||
501 | }while(*(++p)); | ||
502 | } | ||
503 | return _string(buf,end,uuid,spec); | ||
504 | } | ||
505 | |||
506 | //Show a '%p' thing. A kernel extension is that the '%p' is followed | ||
507 | //by an extra set of alphanumeric characters that are extended format | ||
508 | //specifiers. | ||
509 | // | ||
510 | //Right now we handle: | ||
511 | // | ||
512 | //- 'M' For a 6-byte MAC address, it prints the address in the | ||
513 | // usual colon-separated hex notation | ||
514 | //- 'm' For a 6-byte MAC address, it prints the hex address without colons | ||
515 | //- 'MF' For a 6-byte MAC FDDI address, it prints the address | ||
516 | // with a dash-separated hex notation | ||
517 | //- 'I' [46] for IPv4/IPv6 addresses printed in the usual way | ||
518 | // IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) | ||
519 | // IPv6 uses colon separated network-order 16 bit hex with leading 0's | ||
520 | //- 'i' [46] for 'raw' IPv4/IPv6 addresses | ||
521 | // IPv6 omits the colons (01020304...0f) | ||
522 | // IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | ||
523 | //- '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order | ||
524 | //- 'I6c' for IPv6 addresses printed as specified by | ||
525 | // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 | ||
526 | //- 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form | ||
527 | // "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
528 | // Options for %pU are: | ||
529 | // b big endian lower case hex (default) | ||
530 | // B big endian UPPER case hex | ||
531 | // l little endian lower case hex | ||
532 | // L little endian UPPER case hex | ||
533 | // big endian output byte order is: | ||
534 | // [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | ||
535 | // little endian output byte order is: | ||
536 | // [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | ||
537 | // | ||
538 | //Note: The difference between 'S' and 'F' is that on ia64 and ppc64 | ||
539 | //function pointers are really function descriptors, which contain a | ||
540 | //pointer to the real address. | ||
541 | static k_u8 *_pointer(k_u8 *fmt,k_u8 *buf,k_u8 *end,void *ptr,struct printf_spec spec) | ||
542 | { | ||
543 | if(!ptr) return _string(buf,end,(k_u8*)"(null)",spec); | ||
544 | |||
545 | switch(*fmt){ | ||
546 | case 'M'://colon separated: 00:01:02:03:04:05 | ||
547 | case 'm'://contiguous: 000102030405 | ||
548 | return _mac_address_string(buf,end,ptr,spec,fmt);//[mM]F (FDDI, bit reversed) | ||
549 | case 'I'://Formatted IP supported | ||
550 | //4: 1.2.3.4 | ||
551 | //6: 0001:0203:...:0708 | ||
552 | //6c: 1::708 or 1::1.2.3.4 | ||
553 | case 'i'://contiguous: | ||
554 | //4: 001.002.003.004 | ||
555 | //6: 000102...0f | ||
556 | switch(fmt[1]){ | ||
557 | case '6': | ||
558 | return _ip6_addr_string(buf,end,ptr,spec,fmt); | ||
559 | case '4': | ||
560 | return _ip4_addr_string(buf,end,ptr,spec,fmt); | ||
561 | } | ||
562 | break; | ||
563 | case 'U': | ||
564 | return _uuid_string(buf,end,ptr,spec,fmt); | ||
565 | } | ||
566 | |||
567 | spec.flags|=SMALL; | ||
568 | if(spec.field_width==-1){ | ||
569 | spec.field_width=2*sizeof(void*); | ||
570 | spec.flags|=ZEROPAD; | ||
571 | } | ||
572 | spec.base=16; | ||
573 | return _number(buf,end,(k_ull)ptr,spec); | ||
574 | } | ||
575 | |||
576 | static inline k_u16 _a2u16(k_u8 **s)//trusted | ||
577 | { | ||
578 | k_u16 i=0; | ||
579 | while(u_a_is_digit(**s)) i=i*10+*((*s)++)-'0'; | ||
580 | return i; | ||
581 | } | ||
582 | |||
583 | //Helper function to decode printf style format. | ||
584 | //Each call decode a token from the format and return the | ||
585 | //number of characters read (or likely the delta where it wants | ||
586 | //to go on the next call). | ||
587 | //The decoded token is returned through the parameters | ||
588 | // | ||
589 | //'h', 'l', or 'L' for integer fields | ||
590 | //'z' support added 23/7/1999 S.H. | ||
591 | //'z' changed to 'Z' --davidm 1/25/99 | ||
592 | //'t' added for ptrdiff_t | ||
593 | // | ||
594 | //@fmt: the format string | ||
595 | //@type of the token returned | ||
596 | //@flags: various flags such as +, -, # tokens.. | ||
597 | //@field_width: overwritten width | ||
598 | //@base: base of the number (octal, hex, ...) | ||
599 | //@precision: precision of a number | ||
600 | //@qualifier: qualifier of a number (long, size_t, ...) | ||
601 | static k_i _format_decode(k_u8 *fmt,struct printf_spec *spec) | ||
602 | { | ||
603 | //-------------------------------------------------------------------------------------------- | ||
604 | if(spec->type==FORMAT_TYPE_WIDTH){//we finished early by reading the field width | ||
605 | if(spec->field_width<0){ | ||
606 | spec->field_width=-spec->field_width; | ||
607 | spec->flags|=LEFT; | ||
608 | } | ||
609 | spec->type=FORMAT_TYPE_NONE; | ||
610 | goto precision; | ||
611 | } | ||
612 | |||
613 | if(spec->type==FORMAT_TYPE_PRECISION){//we finished early by reading the precision | ||
614 | if(spec->precision<0) spec->precision=0; | ||
615 | spec->type=FORMAT_TYPE_NONE; | ||
616 | goto qualifier; | ||
617 | } | ||
618 | //-------------------------------------------------------------------------------------------- | ||
619 | |||
620 | k_u8 *start=fmt; | ||
621 | |||
622 | spec->type=FORMAT_TYPE_NONE;//by default | ||
623 | for(;*fmt;++fmt) if(*fmt=='%')break; | ||
624 | |||
625 | if(fmt!=start||!*fmt) return fmt-start;//return the current non-format string | ||
626 | |||
627 | spec->flags=0;//process flags | ||
628 | |||
629 | for(;;){//this also skips first '%' | ||
630 | k_ut found=1; | ||
631 | ++fmt; | ||
632 | switch(*fmt){ | ||
633 | case '-':spec->flags|=LEFT; break; | ||
634 | case '+':spec->flags|=PLUS; break; | ||
635 | case ' ':spec->flags|=SPACE; break; | ||
636 | case '#':spec->flags|=SPECIAL;break; | ||
637 | case '0':spec->flags|=ZEROPAD;break; | ||
638 | default:found=0; | ||
639 | } | ||
640 | if(!found) break; | ||
641 | } | ||
642 | |||
643 | //get field width | ||
644 | spec->field_width=-1; | ||
645 | |||
646 | if(u_a_is_digit(*fmt)) spec->field_width=(k_s16)_a2u16(&fmt); | ||
647 | else if(*fmt=='*'){ | ||
648 | spec->type=FORMAT_TYPE_WIDTH;//get it from the argument list | ||
649 | return ++fmt-start; | ||
650 | } | ||
651 | |||
652 | //get the precision | ||
653 | precision: | ||
654 | spec->precision=-1; | ||
655 | if(*fmt=='.'){ | ||
656 | ++fmt; | ||
657 | if(u_a_is_digit(*fmt)){ | ||
658 | spec->precision=(k_s16)_a2u16(&fmt); | ||
659 | if(spec->precision<0) spec->precision=0; | ||
660 | }else if(*fmt=='*'){//it's the next argument | ||
661 | spec->type=FORMAT_TYPE_PRECISION; | ||
662 | return ++fmt-start; | ||
663 | }else spec->precision=0; | ||
664 | } | ||
665 | |||
666 | qualifier: | ||
667 | //get the conversion qualifier | ||
668 | spec->qualifier=-1; | ||
669 | if(*fmt=='h'||TOLOWER(*fmt)=='l'||TOLOWER(*fmt)=='z'||*fmt=='t'){ | ||
670 | spec->qualifier=*fmt++; | ||
671 | if(spec->qualifier==*fmt){ | ||
672 | if(spec->qualifier=='l'){ | ||
673 | spec->qualifier='L'; | ||
674 | ++fmt; | ||
675 | }else if(spec->qualifier=='h'){ | ||
676 | spec->qualifier='H'; | ||
677 | ++fmt; | ||
678 | } | ||
679 | } | ||
680 | } | ||
681 | |||
682 | spec->base=10;//default base:2/8/10/16 wanted? | ||
683 | switch(*fmt){ | ||
684 | case 'c': | ||
685 | spec->type=FORMAT_TYPE_CHAR; | ||
686 | return ++fmt-start; | ||
687 | case 's': | ||
688 | spec->type=FORMAT_TYPE_STR; | ||
689 | return ++fmt-start; | ||
690 | case 'p': | ||
691 | spec->type=FORMAT_TYPE_PTR; | ||
692 | return fmt-start; | ||
693 | //skip alnum | ||
694 | case 'n': | ||
695 | spec->type=FORMAT_TYPE_NRCHARS; | ||
696 | return ++fmt-start; | ||
697 | case '%': | ||
698 | spec->type=FORMAT_TYPE_PERCENT_CHAR; | ||
699 | return ++fmt-start; | ||
700 | //integer number formats - set up the flags and "break" | ||
701 | case 'o': | ||
702 | spec->base=8; | ||
703 | break; | ||
704 | case 'x': | ||
705 | spec->flags|=SMALL; | ||
706 | case 'X': | ||
707 | spec->base=16; | ||
708 | break; | ||
709 | case 'd': | ||
710 | case 'i': | ||
711 | spec->flags|=SIGN; | ||
712 | case 'u': | ||
713 | break; | ||
714 | default: | ||
715 | spec->type=FORMAT_TYPE_INVALID; | ||
716 | return fmt-start; | ||
717 | } | ||
718 | |||
719 | if(spec->qualifier=='L') spec->type=FORMAT_TYPE_LONG_LONG; | ||
720 | else if(spec->qualifier=='l'){ | ||
721 | if(spec->flags&SIGN) spec->type=FORMAT_TYPE_LONG; | ||
722 | else spec->type=FORMAT_TYPE_ULONG; | ||
723 | }else if(TOLOWER(spec->qualifier)=='z'){ | ||
724 | spec->type=FORMAT_TYPE_SIZE_T; | ||
725 | }else if(spec->qualifier=='t'){ | ||
726 | spec->type=FORMAT_TYPE_PTRDIFF; | ||
727 | }else if(spec->qualifier=='H'){ | ||
728 | if(spec->flags&SIGN) spec->type=FORMAT_TYPE_BYTE; | ||
729 | else spec->type=FORMAT_TYPE_UBYTE; | ||
730 | }else if(spec->qualifier=='h'){ | ||
731 | if(spec->flags&SIGN) spec->type=FORMAT_TYPE_SHORT; | ||
732 | else spec->type=FORMAT_TYPE_USHORT; | ||
733 | }else{ | ||
734 | if(spec->flags&SIGN) spec->type=FORMAT_TYPE_INT; | ||
735 | else spec->type=FORMAT_TYPE_UINT; | ||
736 | } | ||
737 | return ++fmt-start; | ||
738 | } | ||
739 | |||
740 | //vsnprintf - Format a string and place it in a buffer | ||
741 | //@buf: The buffer to place the result into | ||
742 | //@sz: The size of the buffer, including the trailing null space | ||
743 | //@fmt: The format string to use | ||
744 | //@args: Arguments for the format string | ||
745 | // | ||
746 | //This function follows C99 vsnprintf, but has some extensions: | ||
747 | //%pM output a 6-byte MAC address with colons | ||
748 | //%pm output a 6-byte MAC address without colons | ||
749 | //%pI4 print an IPv4 address without leading zeros | ||
750 | //%pi4 print an IPv4 address with leading zeros | ||
751 | //%pI6 print an IPv6 address with colons | ||
752 | //%pi6 print an IPv6 address without colons | ||
753 | //%pI6c print an IPv6 address as specified by | ||
754 | // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 | ||
755 | //%pU[bBlL] print a UUID/GUID in big or little endian using lower or upper | ||
756 | // case. | ||
757 | //%n is ignored | ||
758 | // | ||
759 | //The return value is the number of characters which would | ||
760 | //be generated for the given input, excluding the trailing | ||
761 | //'\0', as per ISO C99. If you want to have the exact | ||
762 | //number of characters written into @buf as return value | ||
763 | //(not including the trailing '\0'), use vscnprintf(). If the | ||
764 | //return is greater than or equal to @size, the resulting | ||
765 | //string is truncated. | ||
766 | // | ||
767 | //Call this function if you are already dealing with a va_list. | ||
768 | //You probably want snprintf() instead. | ||
769 | //SECURITY:fmt is **trusted**,argument related to %fmt are **trusted** | ||
770 | k_ul u_a_vsnprintf(k_u8 *buf,k_ul sz,k_u8 *fmt,va_list args) | ||
771 | { | ||
772 | k_ull num; | ||
773 | struct printf_spec spec={0,0,0,0,0,0}; | ||
774 | |||
775 | k_u8 *str=buf; | ||
776 | k_u8 *end=buf+sz;//point to the byte *after* the buffer last byte | ||
777 | |||
778 | while(*fmt){ | ||
779 | k_u8 *old_fmt=fmt; | ||
780 | k_ul read=_format_decode(fmt,&spec); | ||
781 | |||
782 | fmt+=read; | ||
783 | |||
784 | switch(spec.type){ | ||
785 | case FORMAT_TYPE_NONE:{ | ||
786 | k_ul copy=read; | ||
787 | if(str<end){ | ||
788 | if((k_ptrdiff)copy>end-str) copy=end-str; | ||
789 | u_memcpy(str,old_fmt,copy); | ||
790 | } | ||
791 | str+=read; | ||
792 | break; | ||
793 | } | ||
794 | case FORMAT_TYPE_WIDTH://get field width from the argument list | ||
795 | spec.field_width=(k_s16)va_arg(args,int); | ||
796 | break; | ||
797 | case FORMAT_TYPE_PRECISION://get precision from the argument list | ||
798 | spec.precision=(k_s16)va_arg(args,int); | ||
799 | break; | ||
800 | case FORMAT_TYPE_CHAR:{ | ||
801 | k_u8 c; | ||
802 | if(!(spec.flags&LEFT)){ | ||
803 | while(--spec.field_width>0){ | ||
804 | if(str<end) *str=' '; | ||
805 | ++str; | ||
806 | } | ||
807 | } | ||
808 | c=(k_u8)va_arg(args,int); | ||
809 | if(str<end) *str=c; | ||
810 | ++str; | ||
811 | while(--spec.field_width>0){ | ||
812 | if(str<end) *str=' '; | ||
813 | ++str; | ||
814 | } | ||
815 | break; | ||
816 | } | ||
817 | case FORMAT_TYPE_STR: | ||
818 | str=_string(str,end,va_arg(args,k_u8*),spec); | ||
819 | break; | ||
820 | case FORMAT_TYPE_PTR: | ||
821 | str=_pointer(fmt+1,str,end,va_arg(args,void*),spec); | ||
822 | while(u_a_is_alnum(*fmt)) fmt++;//alnums are extension to pointer fmt (see cmts) | ||
823 | break; | ||
824 | case FORMAT_TYPE_PERCENT_CHAR: | ||
825 | if(str<end) *str='%'; | ||
826 | ++str; | ||
827 | break; | ||
828 | case FORMAT_TYPE_INVALID: | ||
829 | if(str<end) *str='%'; | ||
830 | ++str; | ||
831 | break; | ||
832 | case FORMAT_TYPE_NRCHARS:{ | ||
833 | k_u8 qualifier=spec.qualifier; | ||
834 | |||
835 | if(qualifier=='L'){ | ||
836 | k_ull *ip=va_arg(args,k_ull*); | ||
837 | *ip=(str-buf); | ||
838 | }else if(qualifier=='l'){ | ||
839 | k_ul *ip=va_arg(args,k_ul*); | ||
840 | *ip=(str-buf); | ||
841 | }else if(TOLOWER(qualifier)=='z'){ | ||
842 | k_sz *ip=va_arg(args,k_sz*); | ||
843 | *ip=(str-buf); | ||
844 | }else{ | ||
845 | k_u *ip=va_arg(args,k_u*); | ||
846 | *ip=(str-buf); | ||
847 | } | ||
848 | break; | ||
849 | } | ||
850 | |||
851 | default: | ||
852 | switch(spec.type){ | ||
853 | case FORMAT_TYPE_LONG_LONG: | ||
854 | num=va_arg(args,k_ll); | ||
855 | break; | ||
856 | case FORMAT_TYPE_ULONG: | ||
857 | num=va_arg(args,k_ul); | ||
858 | break; | ||
859 | case FORMAT_TYPE_LONG: | ||
860 | num=va_arg(args,k_l); | ||
861 | break; | ||
862 | case FORMAT_TYPE_SIZE_T: | ||
863 | num=va_arg(args,k_sz); | ||
864 | break; | ||
865 | case FORMAT_TYPE_PTRDIFF: | ||
866 | num=va_arg(args,k_ptrdiff); | ||
867 | break; | ||
868 | case FORMAT_TYPE_UBYTE: | ||
869 | num=(k_u8)va_arg(args,int); | ||
870 | break; | ||
871 | case FORMAT_TYPE_BYTE: | ||
872 | num=(k_s8)va_arg(args,int); | ||
873 | break; | ||
874 | case FORMAT_TYPE_USHORT: | ||
875 | num=(k_us)va_arg(args,int); | ||
876 | break; | ||
877 | case FORMAT_TYPE_SHORT: | ||
878 | num=(k_s)va_arg(args,int); | ||
879 | break; | ||
880 | case FORMAT_TYPE_INT: | ||
881 | num=va_arg(args,k_i); | ||
882 | break; | ||
883 | default: | ||
884 | num=va_arg(args,k_u); | ||
885 | } | ||
886 | str=_number(str,end,num,spec); | ||
887 | } | ||
888 | } | ||
889 | |||
890 | if(sz>0){//set the terminating null byte | ||
891 | if(str<end) *str=0;else end[-1]=0; | ||
892 | } | ||
893 | return str-buf;//the trailing null byte doesn't count towards the total | ||
894 | } | ||
895 | |||
896 | //snprintf - Format a string and place it in a buffer | ||
897 | //@buf: The buffer to place the result into | ||
898 | //@size: The size of the buffer, including the trailing null space | ||
899 | //@fmt: The format string to use | ||
900 | //@...: Arguments for the format string | ||
901 | // | ||
902 | //The return value is the number of characters which would be | ||
903 | //generated for the given input, excluding the trailing null, | ||
904 | //as per ISO C99. If the return is greater than or equal to | ||
905 | //@size, the resulting string is truncated. | ||
906 | // | ||
907 | //See the vsnprintf() documentation for format string extensions over C99. | ||
908 | k_ul u_a_snprintf(k_u8 *buf,k_ul sz,k_u8 *fmt,...) | ||
909 | { | ||
910 | va_list args; | ||
911 | va_start(args,fmt); | ||
912 | k_i i=u_a_vsnprintf(buf,sz,fmt,args); | ||
913 | va_end(args); | ||
914 | return i; | ||
915 | } | ||
916 | |||
917 | //dprintf, print formatted output to file. | ||
918 | k_i u_a_dprintf(k_i f,k_u8 *buf,k_ul sz,k_u8 *fmt,...) | ||
919 | { | ||
920 | va_list args; | ||
921 | va_start(args,fmt); | ||
922 | k_ul len=u_a_vsnprintf(buf,sz,fmt,args); | ||
923 | va_end(args); | ||
924 | |||
925 | k_l r=sysc(write,3,f,buf,len); | ||
926 | if(K_ISERR(r)) return -2; | ||
927 | return 0; | ||
928 | } |
File ulinux/utils/ascii/string/vsprintf.h added (mode: 100644) (index 0000000..3972a71) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ASCII_STRING_VSPRINTF_H | ||
6 | #define ULINUX_UTILS_ASCII_STRING_VSPRINTF_H | ||
7 | extern k_ul u_a_vsnprintf(k_u8 *buf,k_ul sz,k_u8 *fmt,va_list args); | ||
8 | extern k_ul u_a_snprintf(k_u8 *buf,k_ul sz,k_u8 *fmt,...); | ||
9 | extern k_i u_a_dprintf(k_i f,k_u8 *buf,k_ul sz,k_u8 *fmt,...); | ||
10 | #endif |
File ulinux/utils/atomic.h added (mode: 100644) (index 0000000..56ea507) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_ATOMIC | ||
6 | #define ULINUX_ATOMIC | ||
7 | //---------------------------------------------------------------------------------------------- | ||
8 | //|atomic stuff (will probably move in arch.h, that's for x86-64) | ||
9 | //---------------------------------------------------------------------------------------------- | ||
10 | typedef struct{ | ||
11 | k_u8 val; | ||
12 | } k_atomic_u8_t; | ||
13 | |||
14 | static inline k_u8 k_atomic_u8_read(const k_atomic_u8_t *v) | ||
15 | { | ||
16 | return (*(volatile k_u8*)&(v)->val); | ||
17 | } | ||
18 | static inline void k_atomic_u8_set(k_atomic_u8_t *v,k_u8 u) | ||
19 | { | ||
20 | v->val=u; | ||
21 | } | ||
22 | //---------------------------------------------------------------------------------------------- | ||
23 | #endif |
File ulinux/utils/div.h added (mode: 100644) (index 0000000..31119c4) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_DIV_H | ||
6 | #define ULINUX_UTILS_DIV_H | ||
7 | #include <ulinux/arch/utils/div.h> | ||
8 | #endif |
File ulinux/utils/endian.h added (mode: 100644) (index 0000000..a1f3620) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_ENDIAN_H | ||
6 | #define ULINUX_UTILS_ENDIAN_H | ||
7 | #include <ulinux/arch/utils/endian.h> | ||
8 | #endif |
File ulinux/utils/ipv6.h added (mode: 100644) (index 0000000..59f903b) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_UTILS_IPV6 | ||
6 | #define ULINUX_UTILS_IPV6 | ||
7 | #include <ulinux/arch/utils/ipv6.h> | ||
8 | static inline k_ut u_ipv6_addr_any(struct k_in6_addr *a) | ||
9 | { | ||
10 | return ((a->s6_addr32[0]|a->s6_addr32[1]| | ||
11 | a->s6_addr32[2]|a->s6_addr32[3])==0); | ||
12 | } | ||
13 | #endif |
File ulinux/utils/mem.h added (mode: 100644) (index 0000000..74dca8d) | |||
1 | #ifndef ULINUX_UTILS_MEM_H | ||
2 | #define ULINUX_UTILS_MEM_H | ||
3 | #include <ulinux/arch/utils/mem.h> | ||
4 | #endif |
File ulinux/vim/after/syntax/c.vim added (mode: 100644) (index 0000000..b8750ed) | |||
1 | " Vim syntax file | ||
2 | " Language: C | ||
3 | " Maintainer: Sylvain BERTRAND <digital.ragnarok@gmail.com> | ||
4 | " Last Change: 2011 Nov 26 | ||
5 | |||
6 | "compiler types | ||
7 | syn keyword cType k_ut k_us k_u k_ul k_ull | ||
8 | syn keyword cType k_t k_s k_i k_l k_ll | ||
9 | |||
10 | "arch types | ||
11 | syn keyword cType k_u8 k_u16 k_u32 k_u64 | ||
12 | syn keyword cType k_s8 k_s16 k_s32 k_s64 | ||
13 | |||
14 | syn keyword cType k_sz k_ptrdiff |
File ulinux/wait.h added (mode: 100644) (index 0000000..ea0e20a) | |||
1 | //********************************************************************************************** | ||
2 | //*this code is protected by the GNU affero GPLv3 | ||
3 | //*author:Sylvain BERTRAND (sylvain.bertrand AT gmail dot com) | ||
4 | //********************************************************************************************** | ||
5 | #ifndef ULINUX_WAIT_H | ||
6 | #define ULINUX_WAIT_H | ||
7 | #define K_WNOHANG 0x00000001 | ||
8 | #define K_WUNTRACED 0x00000002 | ||
9 | #define K_WSTOPPED K_WUNTRACED | ||
10 | #define K_WEXITED 0x00000004 | ||
11 | #define K_WCONTINUED 0x00000008 | ||
12 | #define K_WNOWAIT 0x01000000//Don't reap, just poll status. | ||
13 | |||
14 | #define K_WNOTHREAD 0x20000000//Don't wait on children of other threads in this group | ||
15 | #define K_WALL 0x40000000//Wait on all children, regardless of type | ||
16 | #define K_WCLONE 0x80000000//Wait only on non-SIGCHLD children | ||
17 | #endif |