aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--arch/riscv/conf/init-link.S3
-rw-r--r--arch/riscv/conf/kernel-link.S3
-rw-r--r--common/bytes.c27
-rw-r--r--include/apos/builtin.h8
-rw-r--r--include/apos/bytes.h56
-rw-r--r--include/apos/string.h53
-rw-r--r--include/apos/types.h37
-rw-r--r--include/libfdt_env.h1
-rwxr-xr-xscripts/gen-padding9
10 files changed, 139 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index 187c269..1cce5e4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
DO != echo > deps.mk
-DEBUGFLAGS != [ $(DEBUG) ] && echo "-g3 -DDEBUG" || echo "-O2"
+# this could be done better
+DEBUGFLAGS != [ $(DEBUG) ] && echo "-Og -g3 -DDEBUG" || echo "-flto -O3"
CFLAGS = -fno-pie -ffreestanding -nostdlib -std=c11 -Wall -Wextra
DEPFLAGS = -MT $@ -MMD -MP -MF $@.d
@@ -45,6 +46,7 @@ include deps.mk
apos.bin: kernel.elf init.elf
$(CROSS_COMPILE)$(OBJCOPY) $(OBJCOPY_FLAGS) kernel.elf kernel.bin
$(CROSS_COMPILE)$(OBJCOPY) $(OBJCOPY_FLAGS) init.elf init.bin
+ ./scripts/gen-padding 4096 init.bin
cat init.bin kernel.bin > $@
kernel.elf: $(KERNEL_OBJECTS) $(KERNEL_LD)
diff --git a/arch/riscv/conf/init-link.S b/arch/riscv/conf/init-link.S
index f1ceadc..2bb904b 100644
--- a/arch/riscv/conf/init-link.S
+++ b/arch/riscv/conf/init-link.S
@@ -7,7 +7,6 @@ SECTIONS {
. = ABSOLUTE(PM_KERN);
.text ALIGN(4K) : AT(0) {
*(.init.start)
- *(.text);
- . = ALIGN(4K);
+ *(.text*);
}
}
diff --git a/arch/riscv/conf/kernel-link.S b/arch/riscv/conf/kernel-link.S
index ebcc877..4592086 100644
--- a/arch/riscv/conf/kernel-link.S
+++ b/arch/riscv/conf/kernel-link.S
@@ -7,7 +7,6 @@ SECTIONS {
. = ABSOLUTE(VM_KERN);
.text ALIGN(4K) : AT(0) {
*(.kernel.start);
- *(.text);
- . = ALIGN(4K);
+ *(.text*);
}
}
diff --git a/common/bytes.c b/common/bytes.c
index 1196989..af65568 100644
--- a/common/bytes.c
+++ b/common/bytes.c
@@ -1,31 +1,14 @@
#include <apos/types.h>
#include <apos/attrs.h>
+#include <apos/bytes.h>
+#include <apos/builtin.h>
-/* Functions for swapping endianness.
- *
- * Typically you wouldn't call these directly, instead using macros such as
- * le16_to_cpu, cpu_to_be32, etc. that take the system endianness into account.
- *
- * The macros cpu_to_be64 etc. use GCC's and Clang's __builtin_bswap16/32/64, but in
- * the case that the arch compiler doesn't provide the __builtin functions they're replaced
- * with __bswaphi2/si2/di2 respectively, and it is up to the developer to
- * provide these. The naming scheme uses GCC's internal naming scheme, where
- *
- * HI ~= 16 bits
- * SI ~= 32 bits
- * DI ~= 64 bits
- *
- * Here we assume this to be true in all cases, and it probably is, but in the
- * case that some architecture is different (SI ~= 64 bits or something) we define
- * these functions as __weak, allowing any arch to redefine them.
- */
-
-__weak uint16_t __bswaphi2(uint16_t u)
+__weak uint16_t __bswap16(uint16_t u)
{
return (u & 0xff00) >> 8 | (u & 0x00ff) << 8;
}
-__weak uint32_t __bswapsi2(uint32_t u)
+__weak uint32_t __bswap32(uint32_t u)
{
return (u & 0xff000000) >> 24 |
(u & 0x00ff0000) >> 8 |
@@ -33,7 +16,7 @@ __weak uint32_t __bswapsi2(uint32_t u)
(u & 0x000000ff) << 24;
}
-__weak uint64_t __bswapdi2(uint64_t u)
+__weak uint64_t __bswap64(uint64_t u)
{
return (u & 0xff00000000000000ULL) >> 56 |
(u & 0x00ff000000000000ULL) >> 40 |
diff --git a/include/apos/builtin.h b/include/apos/builtin.h
new file mode 100644
index 0000000..52a3fa2
--- /dev/null
+++ b/include/apos/builtin.h
@@ -0,0 +1,8 @@
+#ifndef APOS_BUILTIN_H
+#define APOS_BUILTIN_H
+
+#ifndef __has_builtin
+#define __has_builtin(x) (0)
+#endif
+
+#endif /* APOS_BUILTIN_H */
diff --git a/include/apos/bytes.h b/include/apos/bytes.h
new file mode 100644
index 0000000..da0ab67
--- /dev/null
+++ b/include/apos/bytes.h
@@ -0,0 +1,56 @@
+#ifndef APOS_BYTES_H
+#define APOS_BYTES_H
+
+#include <apos/types.h>
+
+uint16_t __bswap16(uint16_t u);
+uint32_t __bswap32(uint32_t u);
+uint64_t __bswap64(uint64_t u);
+
+#if __has_builtin(__builtin_bswap16)
+#define __bswap16(x) __builtin_bswap16(x)
+#endif
+
+#if __has_builtin(__builtin_bswap32)
+#define __bswap32(x) __builtin_bswap32(x)
+#endif
+
+#if __has_builtin(__builtin_bswap64)
+#define __bswap64(x) __builtin_bswap64(x)
+#endif
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define be16_to_cpu(x) __bswap16(x)
+#define be32_to_cpu(x) __bswap32(x)
+#define be64_to_cpu(x) __bswap64(x)
+
+#define cpu_to_be16(x) __bswap16(x)
+#define cpu_to_be32(x) __bswap32(x)
+#define cpu_to_be64(x) __bswap64(x)
+
+#define le16_to_cpu(x) (x)
+#define le32_to_cpu(x) (x)
+#define le64_to_cpu(x) (x)
+
+#define cpu_to_le16(x) (x)
+#define cpu_to_le32(x) (x)
+#define cpu_to_le64(x) (x)
+#else
+#define be16_to_cpu(x) (x)
+#define be32_to_cpu(x) (x)
+#define be64_to_cpu(x) (x)
+
+#define cpu_to_be16(x) (x)
+#define cpu_to_be32(x) (x)
+#define cpu_to_be64(x) (x)
+
+#define le16_to_cpu(x) __bswap16(x)
+#define le32_to_cpu(x) __bswap32(x)
+#define le64_to_cpu(x) __bswap64(x)
+
+#define cpu_to_le16(x) __bswap16(x)
+#define cpu_to_le32(x) __bswap32(x)
+#define cpu_to_le64(x) __bswap64(x)
+#endif
+
+#endif /* APOS_BYTES_H */
diff --git a/include/apos/string.h b/include/apos/string.h
index 787f630..67cdae8 100644
--- a/include/apos/string.h
+++ b/include/apos/string.h
@@ -2,6 +2,7 @@
#define APOS_STRING_H
#include <apos/types.h>
+#include <apos/builtin.h>
/* follow C library functions, drop location and error functions */
@@ -51,28 +52,80 @@ int memcmp(const void *ptr1, const void *ptr2, size_t num);
* an issue.
*/
+#if __has_builtin(__builtin_memcpy)
#define memcpy(dst, src, num) __builtin_memcpy(dst, src, num)
+#endif
+
+#if __has_builtin(__builtin_memmove)
#define memmove(dst, src, num) __builtin_memmove(dst, src, num)
+#endif
+
+#if __has_builtin(__builtin_strcpy)
#define strcpy(dst, src) __builtin_strcpy(dst, src)
+#endif
+
+#if __has_builtin(__builtin_strncpy)
#define strncpy(dst, src, num) __builtin_strncpy(dst, src, num)
+#endif
+#if __has_builtin(__builtin_strcat)
#define strcat(dst, src) __builtin_strcat(dst, src)
+#endif
+
+#if __has_builtin(__builtin_strncat)
#define strncat(dst, src, num) __builtin_strncat(dst, src, num)
+#endif
+#if __has_builtin(__builtin_memcmp)
#define memcmp(p1, p2, num) __builtin_memcmp(p1, p2, num)
+#endif
+
+#if __has_builtin(__builtin_strcmp)
#define strcmp(s1, s2) __builtin_strcmp(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strncmp)
#define strncmp(s1, s2, num) __builtin_strncmp(s1, s2, num)
+#endif
+#if __has_builtin(__builtin_strncmp)
#define memchr(ptr, val, num) __builtin_memchr(ptr, val, num)
+#endif
+
+#if __has_builtin(__builtin_strchr)
#define strchr(str, chr) __builtin_strchr(str, chr)
+#endif
+
+#if __has_builtin(__builtin_strcspn)
#define strcspn(s1, s2) __builtin_strcspn(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strpbrk)
#define strpbrk(s1, s2) __builtin_strpbrk(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strchr)
#define strrchr(s1, s2) __builtin_strrchr(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strspn)
#define strspn(s1, s2) __builtin_strspn(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strstr)
#define strstr(s1, s2) __builtin_strstr(s1, s2)
+#endif
+
+#if __has_builtin(__builtin_strtok)
#define strtok(s1, s2) __builtin_strtok(s1, s2)
+#endif
+#if __has_builtin(__builtin_memset)
#define memset(p, v, n) __builtin_memset(p, v, n)
+#endif
+
+#if __has_builtin(__builtin_strlen)
#define strlen(s) __builtin_strlen(s)
+#endif
#endif /* APOS_STRING_H */
diff --git a/include/apos/types.h b/include/apos/types.h
index f63857b..791c394 100644
--- a/include/apos/types.h
+++ b/include/apos/types.h
@@ -1,6 +1,8 @@
#ifndef APOS_TYPES_H
#define APOS_TYPES_H
+#include <apos/builtin.h>
+
typedef _Bool bool;
#define true 1
#define false 0
@@ -10,7 +12,6 @@ typedef __WCHAR_TYPE__ wchar_t;
typedef __WINT_TYPE__ wint_t;
typedef __INTMAX_TYPE__ intmax_t;
typedef __UINTMAX_TYPE__ uintmax_t;
-typedef __SIG_ATOMIC_TYPE__ sig_atomic_t;
typedef __INT8_TYPE__ int8_t;
typedef __INT16_TYPE__ int16_t;
typedef __INT32_TYPE__ int32_t;
@@ -174,40 +175,6 @@ typedef intmax_t ssize_t;
#define UINTPTR_WIDTH __INTPTR_WIDTH__
#define UINTMAX_WIDTH __INTMAX_WIDTH__
-#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-#define be16_to_cpu(x) __builtin_bswap16(x)
-#define be32_to_cpu(x) __builtin_bswap32(x)
-#define be64_to_cpu(x) __builtin_bswap64(x)
-
-#define cpu_to_be16(x) __builtin_bswap16(x)
-#define cpu_to_be32(x) __builtin_bswap32(x)
-#define cpu_to_be64(x) __builtin_bswap64(x)
-
-#define le16_to_cpu(x) (x)
-#define le32_to_cpu(x) (x)
-#define le64_to_cpu(x) (x)
-
-#define cpu_to_le16(x) (x)
-#define cpu_to_le32(x) (x)
-#define cpu_to_le64(x) (x)
-#else
-#define be16_to_cpu(x) (x)
-#define be32_to_cpu(x) (x)
-#define be64_to_cpu(x) (x)
-
-#define cpu_to_be16(x) (x)
-#define cpu_to_be32(x) (x)
-#define cpu_to_be64(x) (x)
-
-#define le16_to_cpu(x) __builtin_bswap16(x)
-#define le32_to_cpu(x) __builtin_bswap32(x)
-#define le64_to_cpu(x) __builtin_bswap64(x)
-
-#define cpu_to_le16(x) __builtin_bswap16(x)
-#define cpu_to_le32(x) __builtin_bswap32(x)
-#define cpu_to_le64(x) __builtin_bswap64(x)
-#endif
-
#define NULL 0
#endif /* APOS_TYPES_H */
diff --git a/include/libfdt_env.h b/include/libfdt_env.h
index 166b558..96da42e 100644
--- a/include/libfdt_env.h
+++ b/include/libfdt_env.h
@@ -4,6 +4,7 @@
#include <apos/types.h>
#include <apos/string.h>
+#include <apos/bytes.h>
typedef int16_t fdt16_t;
typedef int32_t fdt32_t;
diff --git a/scripts/gen-padding b/scripts/gen-padding
new file mode 100755
index 0000000..22c12f9
--- /dev/null
+++ b/scripts/gen-padding
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+align="$1"
+filename="$2"
+
+filesize=$(stat -c "%s" "${filename}")
+padsize=$(echo "${filesize}" | awk "{print ${align} - (\$1 % ${align})}")
+
+dd if=/dev/zero ibs=1 count="${padsize}" >> "${filename}"