[libk]: libk 模块在工程上、架构设计做出重大改变,脱离magnitude(kernel)管理
This commit is contained in:
11
arch/aarch64/asm_src/memory.S
Normal file
11
arch/aarch64/asm_src/memory.S
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
/**
|
||||
* 内存复制
|
||||
* void *memcpy(void *dest, const void *src, size_t n);
|
||||
* x0: dest
|
||||
* x1: src
|
||||
* x2: len
|
||||
**/
|
||||
memcpy:
|
||||
ret
|
||||
|
||||
0
arch/aarch64/asm_src/string.S
Normal file
0
arch/aarch64/asm_src/string.S
Normal file
19
arch/aarch64/bits/alltypes.h
Normal file
19
arch/aarch64/bits/alltypes.h
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
#ifndef BITS_ALLTYPES_H
|
||||
#define BITS_ALLTYPES_H
|
||||
|
||||
typedef unsigned long size_t;
|
||||
typedef unsigned long uintptr_t;
|
||||
typedef unsigned long uint64_t;
|
||||
typedef signed long ssize_t;
|
||||
typedef signed long intptr_t;
|
||||
typedef signed long int64_t;
|
||||
|
||||
typedef unsigned int uint32_t;
|
||||
typedef signed int int32_t;
|
||||
|
||||
typedef unsigned short uint16_t;
|
||||
typedef signed short int16_t;
|
||||
|
||||
#endif
|
||||
|
||||
14
arch/aarch64/crt_arch.h
Normal file
14
arch/aarch64/crt_arch.h
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#ifndef CRT_ARCH
|
||||
#define CRT_ARCH
|
||||
|
||||
#define CTR_ARCH_START_ASM(START) \
|
||||
__asm__( \
|
||||
".text \n" \
|
||||
".global " START "\n" \
|
||||
".type " START ",%function\n" \
|
||||
START ":\n" \
|
||||
" b " START "_c\n" \
|
||||
);
|
||||
|
||||
#endif
|
||||
12
arch/aarch64/libkasm.h
Normal file
12
arch/aarch64/libkasm.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by dongl on 25-8-5.
|
||||
//
|
||||
|
||||
#ifndef LIBKASM_H
|
||||
#define LIBKASM_H
|
||||
|
||||
#define __asm_memcpy
|
||||
|
||||
void crt0(void);
|
||||
|
||||
#endif //LIBKASM_H
|
||||
12
arch/crt.h
Normal file
12
arch/crt.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by dongl on 25-8-12.
|
||||
//
|
||||
|
||||
#ifndef CRT_H
|
||||
#define CRT_H
|
||||
|
||||
#include <crt_arch.h>
|
||||
|
||||
void _start_c( long *p );
|
||||
|
||||
#endif //CRT_H
|
||||
10
arch/libk.h
Normal file
10
arch/libk.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 25-8-5.
|
||||
//
|
||||
|
||||
#ifndef LIBK_H
|
||||
#define LIBK_H
|
||||
|
||||
#include <libkasm.h>
|
||||
|
||||
#endif //LIBK_H
|
||||
3
crt/aarch64/libk_fini.S
Normal file
3
crt/aarch64/libk_fini.S
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
libk_fini:
|
||||
ret
|
||||
3
crt/aarch64/libk_init.S
Normal file
3
crt/aarch64/libk_init.S
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
libk_init:
|
||||
ret
|
||||
35
crt/crt0.c
Normal file
35
crt/crt0.c
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by dongl on 25-8-11.
|
||||
//
|
||||
#include <libk.h>
|
||||
#include <features.h>
|
||||
|
||||
#define START "_start"
|
||||
|
||||
#include <crt.h>
|
||||
|
||||
weak void libk_init();
|
||||
weak void libk_fini();
|
||||
int main( int argc, char **argv );
|
||||
int rt_main( int argc, char **argv );
|
||||
|
||||
int libk_start_main(
|
||||
int (*)(),
|
||||
int,
|
||||
char **,
|
||||
void (*)(),
|
||||
void(*)(),
|
||||
void(*)()
|
||||
);
|
||||
|
||||
void _start_c( long *p )
|
||||
{
|
||||
// main 入口参数
|
||||
const int argc = (int) p[0];
|
||||
char **argv = (void *)(p+1);
|
||||
(void) argv;
|
||||
|
||||
p[0] >> (sizeof(long) - 1)
|
||||
? libk_start_main(main, argc, argv, libk_init, libk_fini, 0)
|
||||
: libk_start_main(rt_main, argc, argv, libk_init, libk_fini, 0);
|
||||
}
|
||||
25
include/features.h
Normal file
25
include/features.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _FEATURES_H
|
||||
#define _FEATURES_H
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#define __restrict restrict
|
||||
#elif !defined(__GNUC__)
|
||||
#define __restrict
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
|
||||
#define __inline inline
|
||||
#elif !defined(__GNUC__)
|
||||
#define __inline
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 201112L
|
||||
#elif defined(__GNUC__)
|
||||
#define _Noreturn __attribute__((__noreturn__))
|
||||
#else
|
||||
#define _Noreturn
|
||||
#endif
|
||||
|
||||
#define __REDIR(x,y) __typeof__(x) x __asm__(#y)
|
||||
|
||||
#endif
|
||||
45
include/memory.h
Normal file
45
include/memory.h
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by dongl on 25-7-30.
|
||||
//
|
||||
|
||||
#ifndef LIBK_MEMORY_H
|
||||
#define LIBK_MEMORY_H
|
||||
|
||||
#include "stddef.h" // for size_t
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// 复制 n 字节从 src 到 dest(不处理重叠)
|
||||
void *memcpy(void *dest, const void *src, size_t n);
|
||||
|
||||
// 填充 s 指向的内存块,共 n 字节为 c 值
|
||||
void *memset(void *s, const int c, size_t n);
|
||||
|
||||
// 比较两个内存块 s1 和 s2 的前 n 字节
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
// 复制 n 字节从 src 到 dest,安全处理重叠区域
|
||||
void *memmove(void *dest, const void *src, size_t n);
|
||||
|
||||
// 清零 n 字节内存块
|
||||
void bzero(void *s, size_t n);
|
||||
|
||||
// 在内存块中查找第一次出现字符 c
|
||||
void *memchr(const void *s, int c, size_t n);
|
||||
|
||||
// 交换两个内存区域内容(长度为 n 字节)
|
||||
void memswap(void *a, void *b, size_t n);
|
||||
|
||||
// 高速 memcpy 实现(使用 uint32_t)——可选
|
||||
void *memcpy32(void *dest, const void *src, size_t n);
|
||||
|
||||
// 高速 memcmp 实现(使用 uint16_t)——可选
|
||||
int memcmp16(const void *s1, const void *s2, size_t n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // LIBK_MEMORY_H
|
||||
14
include/stddef.h
Normal file
14
include/stddef.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _STDDEF_H
|
||||
#define _STDDEF_H
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#define NULL nullptr
|
||||
#elif defined(__cplusplus)
|
||||
#define NULL 0L
|
||||
#else
|
||||
#define NULL ((void*)0)
|
||||
#endif
|
||||
|
||||
#include <bits/alltypes.h>
|
||||
|
||||
#endif
|
||||
51
include/string.h
Normal file
51
include/string.h
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by dongl on 25-7-30.
|
||||
//
|
||||
|
||||
#ifndef LIBK_STRING_H
|
||||
#define LIBK_STRING_H
|
||||
|
||||
#include "stddef.h" // for size_t
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// 计算字符串长度(不包含结尾的 '\0')
|
||||
size_t strlen(const char *str);
|
||||
|
||||
// 复制字符串 src 到 dest,返回 dest
|
||||
char *strcpy(char *dest, const char *src);
|
||||
|
||||
// 最多复制 n 个字符到 dest(不足补零),返回 dest
|
||||
char *strncpy(char *dest, const char *src, size_t n);
|
||||
|
||||
// 比较两个字符串,相等返回 0
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
|
||||
// 比较两个字符串,最多比较 n 个字符
|
||||
int strncmp(const char *s1, const char *s2, size_t n);
|
||||
|
||||
// 查找字符 c 在字符串中第一次出现的位置
|
||||
char *strchr(const char *s, int c);
|
||||
|
||||
// 查找字符 c 在字符串中最后一次出现的位置
|
||||
char *strrchr(const char *s, int c);
|
||||
|
||||
// 拼接 src 到 dest 的末尾,返回 dest
|
||||
char *strcat(char *dest, const char *src);
|
||||
|
||||
// 最多拼接 n 个字符
|
||||
char *strncat(char *dest, const char *src, size_t n);
|
||||
|
||||
// 查找子串 needle 在 haystack 中第一次出现的位置
|
||||
char *strstr(const char *haystack, const char *needle);
|
||||
|
||||
// 可选:复制字符串(需要分配内存)
|
||||
char *strdup(const char *s); // 如果你实现了 malloc()
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // LIBK_STRING_H
|
||||
17
src/include/features.h
Normal file
17
src/include/features.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by dongl on 25-8-11.
|
||||
//
|
||||
|
||||
#ifndef FEATURES_H
|
||||
#define FEATURES_H
|
||||
|
||||
#include "../../include/features.h"
|
||||
|
||||
#define weak __attribute__((__weak__))
|
||||
#define hidden __attribute__((__visibility__("hidden")))
|
||||
#define weak_alias(old, new) \
|
||||
extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
|
||||
|
||||
#define General_API weak
|
||||
|
||||
#endif
|
||||
10
src/include/memory.h
Normal file
10
src/include/memory.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 25-8-11.
|
||||
//
|
||||
|
||||
#ifndef MEMORY_H
|
||||
#define MEMORY_H
|
||||
|
||||
#include "../../include/string.h"
|
||||
|
||||
#endif //MEMORY_H
|
||||
19
src/include/stdbool.h
Normal file
19
src/include/stdbool.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by dongl on 25-8-12.
|
||||
//
|
||||
|
||||
#ifndef STDBOOL_H
|
||||
#define STDBOOL_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define bool _Bool
|
||||
|
||||
#endif
|
||||
|
||||
#define __bool_true_false_are_defined 1
|
||||
|
||||
|
||||
#endif //STDBOOL_H
|
||||
10
src/include/stddef.h
Normal file
10
src/include/stddef.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 25-8-11.
|
||||
//
|
||||
|
||||
#ifndef STDDEF_H
|
||||
#define STDDEF_H
|
||||
|
||||
#include "../../include/stddef.h"
|
||||
|
||||
#endif //STDDEF_H
|
||||
11
src/include/string.h
Normal file
11
src/include/string.h
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by dongl on 25-8-11.
|
||||
//
|
||||
|
||||
#ifndef STRING_H
|
||||
#define STRING_H
|
||||
|
||||
#include "../../include/string.h"
|
||||
|
||||
|
||||
#endif
|
||||
21
src/libk_start_main.c
Normal file
21
src/libk_start_main.c
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by dongl on 25-8-12.
|
||||
//
|
||||
|
||||
int exit(int code);
|
||||
|
||||
int libk_start_main(
|
||||
int (*main)(int,char **),
|
||||
int argc,
|
||||
char **argv,
|
||||
void (*init_dummy)(),
|
||||
void (*fini_dummy)(),
|
||||
void (*ldso_dummy)()
|
||||
) {
|
||||
(void)(ldso_dummy);
|
||||
|
||||
init_dummy();
|
||||
exit(main(argc, argv));
|
||||
fini_dummy();
|
||||
return 0;
|
||||
}
|
||||
61
src/memory.c
Normal file
61
src/memory.c
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by dongl on 25-7-31.
|
||||
//
|
||||
|
||||
#include <libk.h>
|
||||
#include <memory.h>
|
||||
#include <features.h>
|
||||
|
||||
#ifndef __asm_memcpy
|
||||
// 复制 n 字节从 src 到 dest(不处理重叠)
|
||||
General_API void* memcpy(void *dest, const void *src, size_t n) {
|
||||
char *d = (char*)dest;
|
||||
const char *s = (const char*)src;
|
||||
|
||||
while (n--) {
|
||||
*d++ = *s++; // 正确:对 char* 指针递增
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __asm_memset
|
||||
// 填充 s 指向的内存块,共 n 字节为 c 值
|
||||
General_API void *memset(void *s, const int c, size_t n) {
|
||||
char *d = (char*)s;
|
||||
|
||||
while (n--) {
|
||||
*d++ = (char) c;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __asm_memcmp
|
||||
// 比较两个内存块 s1 和 s2 的前 n 字节
|
||||
General_API int memcmp(const void *s1, const void *s2, size_t n) {
|
||||
const unsigned char *p1 = s1;
|
||||
const unsigned char *p2 = s2;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (p1[i] != p2[i]) {
|
||||
return p1[i] - p2[i]; // 返回有符号差值
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef __asm_memmove
|
||||
// 复制 n 字节从 src 到 dest,安全处理重叠区域
|
||||
General_API void *memmove(void *dest, const void *src, size_t n) {
|
||||
(void ) dest;
|
||||
(void ) src;
|
||||
(void ) n;
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
17
src/string.c
Normal file
17
src/string.c
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by dongl on 25-7-31.
|
||||
//
|
||||
|
||||
#include <libk.h>
|
||||
#include <string.h>
|
||||
#include <features.h>
|
||||
|
||||
#ifndef __asm_strlen
|
||||
General_API size_t strlen(const char *str)
|
||||
{
|
||||
size_t len = 0;
|
||||
while (str[len] != '\0')
|
||||
++len;
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user