使用cargo管理一些C文件的编译,并且移动部分汇编到arch目录 (#447)

* 使用cargo管理main.c的编译

* 使用build-scripts编译架构相关的c代码

* 删除elf.h
This commit is contained in:
LoGin
2023-11-17 21:25:15 +08:00
committed by GitHub
parent e4600f7f7d
commit 46e234aef6
36 changed files with 556 additions and 1683 deletions

View File

@ -1,84 +0,0 @@
#pragma once
#include <DragonOS/stdint.h>
#include <common/glib.h>
#define BLK_TYPE_AHCI 0
#define DISK_NAME_LEN 32 // 磁盘名称的最大长度
struct blk_gendisk;
struct block_device_operation
{
long (*open)();
long (*close)();
long (*ioctl)(long cmd, long arg);
/**
* @brief 块设备驱动程序的传输函数
*
* @param gd 磁盘设备结构体
* @param cmd 控制命令
* @param base_addr 48位LBA地址
* @param count total sectors to read
* @param buf 缓冲区线性地址
* @return long
*/
long (*transfer)(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf);
};
/**
* @brief 块设备请求队列内的packet
*
*/
struct block_device_request_packet
{
uchar cmd;
uint64_t LBA_start;
uint32_t count;
uint64_t buffer_vaddr;
uint8_t device_type; // 0: ahci
void (*end_handler)(ul num, ul arg);
};
/**
* @brief 块设备的请求队列
*
*/
struct block_device_request_queue
{
struct block_device_request_packet *in_service; // 正在请求的结点
ul request_count;
};
/**
* @brief 块设备结构体(对应磁盘的一个分区)
*
*/
struct block_device
{
sector_t bd_start_sector; // 该分区的起始扇区
uint64_t bd_start_LBA; // 起始LBA号
sector_t bd_sectors_num; // 该分区的扇区数
struct vfs_superblock_t *bd_superblock; // 执行超级块的指针
struct blk_gendisk *bd_disk; // 当前分区所属的磁盘
uint16_t bd_partno; // 在磁盘上的分区号
};
// 定义blk_gendisk中的标志位
#define BLK_GF_AHCI (1 << 0)
/**
* @brief 磁盘设备结构体
*
*/
struct blk_gendisk
{
char disk_name[DISK_NAME_LEN]; // 磁盘驱动器名称
uint16_t part_cnt; // 磁盘分区计数
uint16_t flags;
struct block_device *partition; // 磁盘分区数组
const struct block_device_operation *fops; // 磁盘操作
void *private_data;
};

View File

@ -1,10 +0,0 @@
#pragma once
#include "blk_types.h"
/**
* @brief 将磁盘注册到块设备框架中
*
* @param gendisk 磁盘结构体
* @return int 错误码
*/
int blk_register_gendisk(struct blk_gendisk * gendisk);

View File

@ -1,360 +0,0 @@
#pragma once
#include <common/glib.h>
// --> begin ==============EHDR=====================
// Reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-43405.html#scrolltoc
// ====== ELF32 Header中的数据类型定义 ====
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef uint32_t Elf32_SWord;
typedef uint32_t Elf32_Word;
// ====== ELF64 Header中的数据类型定义 ====
typedef uint64_t Elf64_Addr;
typedef uint16_t Elf64_Half;
typedef uint64_t Elf64_Off;
typedef uint32_t Elf64_Sword;
typedef uint32_t Elf64_Word;
typedef uint64_t Elf64_Xword;
typedef uint64_t Elf64_Sxword;
// ====== ELF Identification Index ======
// Purpose: File identification
#define EI_MAG0 0
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
// Purpose: File class
#define EI_CLASS 4
// Purpose: Data encoding
#define EI_DATA 5
// Purpose: File version
#define EI_VERSION 6 // e_ident[EI_VERSION]指定了ELF header的版本号 当前这个值必须是EV_CURRENT
// Purpose: Operating system/ABI identification
#define EI_OSABI 7 // e_ident[EI_OSABI]指定了操作系统以及对象所对应的ABI
// Purpose: ABI version
#define EI_ABIVERSION 8 // e_ident[EI_ABIVERSION] 指定了对象所对应的ABI版本.
// Purpose: Start of padding bytes
#define EI_PAD 9 // 这个值标志了e_ident中未使用字节的的起始下标
// Purpose: Size of e_ident[]
#define EI_NIDENT 16
// EI_MAG0 - EI_MAG3 这是一个4byte的 magic number
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
// EI_CLASS e_ident[EI_CLASS]指明了文件的类型或capacity
#define ELFCLASSNONE 0 // Invalid class
#define ELFCLASS32 1 // 32bit objects
#define ELFCLASS64 2 // 64bit objects
// EI_DATA e_ident[EI_DATA]指明了与处理器相关的数据的编码方式
#define ELFDATANONE 0
#define ELFDATA2LSB 1 // 小端对齐
#define ELFDATA2MSB 2 // 大端对齐
// ELF e_type的类型定义
#define ET_NONE 0 // No file type
#define ET_REL 1 // Relocatable file
#define ET_EXEC 2 // Executable file
#define ET_DYN 3 // Shared object file
#define ET_CORE 4 // Core file
#define ET_LOPROC 0xff00 // Processor-specific
#define ET_HIPROC 0xffff // Processor-specific
// e_machine的类型定义
#define EM_NONE 0 // No machine
#define EM_SPARC 2 // SPARC
#define EM_386 3 // Intel 80386
#define EM_SPARC32PLUS 18 // Sun SPARC 32+
#define EM_SPARCV9 43 // SPARC V9
#define EM_AMD64 62 // AMD 64
// e_version的类型定义
#define EV_NONE 0 // Invalid Version
// EV_CURRENT: Value>=1 means current version
// e_flags 定义
// e_flags for SPARC
#define EF_SPARC_EXT_MASK 0xffff00 // Vendor Extension mask
#define EF_SPARC_32PLUS 0x000100 // Generic V8+ features
#define EF_SPARC_SUN_US1 0x000200 // Sun UltraSPARC 1 Extensions
#define EF_SPARC_HAL_R1 0x000400 // HAL R1 Extensions
#define EF_SPARC_SUN_US3 0x000800 // Sun UltraSPARC 3 Extensions
#define EF_SPARCV9_MM 0x3 // Mask for Memory Model
#define EF_SPARCV9_TSO 0x0 // Total Store Ordering
#define EF_SPARCV9_PSO 0x1 // Partial Store Ordering
#define EF_SPARCV9_RMO 0x2 // Relaxed Memory Ordering
#define PN_XNUM 0xffff
typedef struct
{
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
typedef struct
{
unsigned char e_ident[EI_NIDENT]; // 标志字节,这些字节与机器架构类型无关。目的是为了告诉我们如何解析这个文件的内容
Elf64_Half e_type; // 文件类型标志符
Elf64_Half e_machine; // 该文件依赖的处理器架构类型
Elf64_Word e_version; // 对象文件的版本
Elf64_Addr e_entry; // 进程的虚拟地址入点使用字节偏移量表示。如果没有entry point则该值为0
Elf64_Off e_phoff; // The program header table's file offset in bytes. 若没有则为0
Elf64_Off e_shoff; // The section header table's file offset in bytes. 若没有则为0
Elf64_Word e_flags; // 与处理器相关联的flags。格式为 EF_machine_flag 如果是x86架构那么该值为0
Elf64_Half e_ehsize; // ELF Header的大小单位字节
Elf64_Half e_phentsize; // 程序的program header table中的一个entry的大小所有的entry大小相同
Elf64_Half e_phnum; // program header table的entry数量
// e_phentsize*e_phnum=program header table的大小
// 如果没有program header table该值为0
// 如果entry num>=PN_XNUM(0xffff), 那么该值为0xffff且真实的pht的entry数量存储在section header的sh_info中index=0
// 其他情况下第一个section header entry的sh_info的值为0
Elf64_Half e_shentsize; // 每个section header的大小字节
// 每个section header是section header table的一个entry
Elf64_Half e_shnum; // section header table的entry数量
// e_shentsize*e_shnum=section header table的大小
// 如果没有section header table那么该值为0
// 如果section的数量>=SHN_LORESERVE(0xff00)那么该值为0且真实的section数量存储在
// section header at index 0的sh_size变量中否则第一个sh_size为0
Elf64_Half e_shstrndx; // 与section name string表相关联的section header table的entry的索引下标
// 如果没有name string table,那么该值等于SHN_UNDEF
// 如果对应的index>=SHN_LORESERVE(0xff00) 那么该变量值为SHN_XINDEX(0xffff)
// 且真正的section name string table的index被存放在section header的index=0处的sh_link变量中
// 否则初始section header entry的sh_link变量为0
} Elf64_Ehdr;
// --> end ==============EHDR=====================
// --> begin ==============SHDR=====================
// reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-94076.html#scrolltoc
// ===== ELF Special Section Indexes =====
#define SHN_UNDEF 0 // An undefined, missing, irrelevant, or otherwise meaningless section reference.
#define SHN_LORESERVE 0xff00 // The lower boundary of the range of reserved indexes.
// The system reserves indexes between SHN_LORESERVE and SHN_HIRESERVE, inclusive.
#define SHN_LOPROC 0xff00 // SHN_LOPROC - SHN_HIPROC 这个范围以内的数据为处理器特定的语义所保留
#define SHN_BEFORE 0xff00 // SHN_BEFORE, SHN_AFTER 与SHF_LINK_ORDER及SHF_ORDERED section flags一起提供初始和终止section的
#define SHN_AFTER 0xff01
#define SHN_AMD64_LCOMMON 0xff02 // x64 specific common block label. This label is similar to SHN_COMMON, but provides for identifying a large common block.
#define SHN_HIPROC 0xff1f
#define SHN_LOOS 0xff20 // SHN_LOOS - SHN_HIOS 这个范围你的数为操作系统特定的语义所保留
#define SHN_LOSUNW 0xff3f // SHN_LOSUNW - SHN_HISUNW Values in this inclusive range are reserved for Sun-specific semantics.
#define SHN_SUNW_IGNORE 0xff3f // This section index provides a temporary symbol definition within relocatable objects. Reserved for internal use by dtrace(1M).
#define SHN_HISUNW 0xff3f
#define SHN_HIOS 0xff3f
#define SHN_ABS 0xfff1 // 对应的引用的绝对值。 举个例子symbols defined relative to section number SHN_ABS have absolute values and are not affected by relocation.
#define SHN_COMMON 0xfff2 // Symbols defined relative to this section are common symbols
#define SHN_XINDEX 0xffff
#define SHN_HIRESERVE 0xffff // The upper boundary of the range of reserved indexes.
/*
Note -
Although index 0 is reserved as the undefined value,
the section header table contains an entry for index 0.
That is, if the e_shnum member of the ELF header indicates
a file has 6 entries in the section header table, the sections
have the indexes 0 through 5. The contents of the initial entry
are specified later in this section.
*/
typedef struct
{
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
typedef struct
{
Elf64_Word sh_name; // 段名
Elf64_Word sh_type; // 段的类型(按照内容和语义来分类)
Elf64_Xword sh_flags;
Elf64_Addr sh_addr; // 该section在进程的内存空间中的基地址。如果该段不需要出现在内存中该值为0
Elf64_Off sh_offset; // The byte offset from the beginning of the file to the first byte in the section
// 对于一个 SHT_NOBITS section这个变量指的是概念上的偏移量。因为这种段并不是真正存在于文件中
Elf64_Xword sh_size; // The section's size in bytes(如果是SHT_NOBITS类型的sectionsection不会在文件中真正占用sh_size的空间)
Elf64_Word sh_link; // A section header table index link, whose interpretation depends on the section type.
Elf64_Word sh_info; // 依赖于section type来解析的额外的信息。如果sh_flags有SHF_INFO_LINK属性那么这个变量代表一个section header table index.
Elf64_Xword sh_addralign; // 地址按照多少bytes对齐。只允许使用2的n次幂的值。如果值为0或1则意味着地址没有对齐要求。
Elf64_Xword sh_entsize; // 如果某个段拥有指定size的entry则在这里指定否则为0
} Elf64_Shdr;
// ELF Section Types, sh_type
#define SHT_NULL 0
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2 // Identifies a symbol table
#define SHT_STRTAB 3 // Identifies a string table.
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11 // Identifies a symbol table
#define SHT_INIT_ARRAY 14
#define SHT_FINI_ARRAY 15
#define SHT_PREINIT_ARRAY 16
#define SHT_GROUP 17
#define SHT_SYMTAB_SHNDX 18
#define SHT_LOOS 0x60000000
#define SHT_LOSUNW 0x6fffffef
#define SHT_SUNW_capchain 0x6fffffef
#define SHT_SUNW_capinfo 0x6ffffff0
#define SHT_SUNW_symsort 0x6ffffff1
#define SHT_SUNW_tlssort 0x6ffffff2
#define SHT_SUNW_LDYNSYM 0x6ffffff3 // Identifies a symbol table
#define SHT_SUNW_dof 0x6ffffff4
#define SHT_SUNW_cap 0x6ffffff5
#define SHT_SUNW_SIGNATURE 0x6ffffff6
#define SHT_SUNW_ANNOTATE 0x6ffffff7
#define SHT_SUNW_DEBUGSTR 0x6ffffff8
#define SHT_SUNW_DEBUG 0x6ffffff9
#define SHT_SUNW_move 0x6ffffffa
#define SHT_SUNW_COMDAT 0x6ffffffb
#define SHT_SUNW_syminfo 0x6ffffffc
#define SHT_SUNW_verdef 0x6ffffffd
#define SHT_SUNW_verneed 0x6ffffffe
#define SHT_SUNW_versym 0x6fffffff
#define SHT_HISUNW 0x6fffffff
#define SHT_HIOS 0x6fffffff
#define SHT_LOPROC 0x70000000
#define SHT_SPARC_GOTDATA 0x70000000
#define SHT_AMD64_UNWIND 0x70000001
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0xffffffff
// ELF Section Attribute Flags
#define SHF_WRITE 0x1 // Identifies a section that should be writable during process execution
#define SHF_ALLOC 0x2 // Identifies a section that occupies memory during process execution
#define SHF_EXECINSTR 0x4 // contains executable machine instructions
#define SHF_MERGE 0x10
#define SHF_STRINGS 0x20
#define SHF_INFO_LINK 0x40 // This section headers sh_info field holds a section header table index
#define SHF_LINK_ORDER 0x80 // This section adds special ordering requirements to the link-editor
#define SHF_OS_NONCONFORMING 0x100
#define SHF_GROUP 0x200
#define SHF_TLS 0x400
#define SHF_MASKOS 0x0ff00000
#define SHF_AMD64_LARGE 0x10000000 // identifies a section that can hold more than 2 Gbyte
#define SHF_ORDERED 0x40000000
#define SHF_EXCLUDE 0x80000000
#define SHF_MASKPROC 0xf0000000
// --> end ==============SHDR=====================
// --> begin ========== symbol table section ======
typedef struct
{
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
typedef struct
{
Elf64_Word st_name;
unsigned char st_info;
unsigned char st_other;
Elf64_Half st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
// --> end ========== symbol table section ======
// --> begin ========== program header =========
// Ref: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-83432.html#scrolltoc
typedef struct
{
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;
typedef struct
{
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
} Elf64_Phdr;
// ELF segment types
#define PT_NULL 0
#define PT_LOAD 1 // Specifies a loadable segment
#define PT_DYNAMIC 2 // Specifies dynamic linking information.
#define PT_INTERP 3 // Specifies the location and size of a null-terminated path name to invoke as an interpreter
#define PT_NOTE 4 // Specifies the location and size of auxiliary information
#define PT_SHLIB 5
#define PT_PHDR 6 // Specifies the location and size of the program header table
#define PT_TLS 7 // Specifies a thread-local storage template
/*
PT_LOOS - PT_HIOS
Values in this inclusive range are reserved for OS-specific semantics.
*/
#define PT_LOOS 0x60000000
#define PT_SUNW_UNWIND 0x6464e550
#define PT_SUNW_EH_FRAME 0x6474e550
#define PT_LOSUNW 0x6ffffffa
#define PT_SUNWBSS 0x6ffffffa
#define PT_SUNWSTACK 0x6ffffffb
#define PT_SUNWDTRACE 0x6ffffffc
#define PT_SUNWCAP 0x6ffffffd
#define PT_HISUNW 0x6fffffff
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
// ELF Segment Flags
#define PF_X 0x1 // Execute
#define PF_W 0x2 // Write
#define PF_R 0x4 // Read
#define PF_MASKPROC 0xf0000000 // Unspecified
// --> end ========== program header =========

View File

@ -11,7 +11,6 @@
#include <common/stddef.h>
#include <arch/arch.h>
#include <common/compiler.h>
#include <common/list.h>
#include <asm/asm.h>
@ -29,13 +28,7 @@
(type *)((unsigned long)p - (unsigned long)&(((type *)0)->member)); \
})
// 定义类型的缩写
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ul;
typedef unsigned long long int ull;
typedef long long int ll;
#define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
// 最大最小值
@ -63,272 +56,6 @@ static __always_inline ul ALIGN(const ul addr, const ul _align)
return (ul)((addr + _align - 1) & (~(_align - 1)));
}
void *memset(void *dst, unsigned char C, ul size)
{
int d0, d1;
unsigned long tmp = C * 0x0101010101010101UL;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"stosq \n\t"
"testb $4, %b3 \n\t"
"je 1f \n\t"
"stosl \n\t"
"1:\ttestb $2, %b3 \n\t"
"je 2f\n\t"
"stosw \n\t"
"2:\ttestb $1, %b3 \n\t"
"je 3f \n\t"
"stosb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1)
: "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
: "memory");
return dst;
}
void *memset_c(void *dst, uint8_t c, size_t count)
{
uint8_t *xs = (uint8_t *)dst;
while (count--)
*xs++ = c;
return dst;
}
/**
* @brief 内存拷贝函数
*
* @param dst 目标数组
* @param src 源数组
* @param Num 字节数
* @return void*
*/
static void *memcpy(void *dst, const void *src, long Num)
{
int d0 = 0, d1 = 0, d2 = 0;
__asm__ __volatile__("cld \n\t"
"rep \n\t"
"movsq \n\t"
"testb $4,%b4 \n\t"
"je 1f \n\t"
"movsl \n\t"
"1:\ttestb $2,%b4 \n\t"
"je 2f \n\t"
"movsw \n\t"
"2:\ttestb $1,%b4 \n\t"
"je 3f \n\t"
"movsb \n\t"
"3: \n\t"
: "=&c"(d0), "=&D"(d1), "=&S"(d2)
: "0"(Num / 8), "q"(Num), "1"(dst), "2"(src)
: "memory");
return dst;
}
// 从io口读入8个bit
unsigned char io_in8(unsigned short port)
{
unsigned char ret = 0;
__asm__ __volatile__("inb %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 从io口读入32个bit
unsigned int io_in32(unsigned short port)
{
unsigned int ret = 0;
__asm__ __volatile__("inl %%dx, %0 \n\t"
"mfence \n\t"
: "=a"(ret)
: "d"(port)
: "memory");
return ret;
}
// 输出8个bit到输出端口
void io_out8(unsigned short port, unsigned char value)
{
__asm__ __volatile__("outb %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
// 输出32个bit到输出端口
void io_out32(unsigned short port, unsigned int value)
{
__asm__ __volatile__("outl %0, %%dx \n\t"
"mfence \n\t"
:
: "a"(value), "d"(port)
: "memory");
}
/**
* @brief 从端口读入n个word到buffer
*
*/
#define io_insw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;insw;mfence;" ::"d"(port), "D"(buffer), "c"(nr) \
: "memory")
/**
* @brief 从输出buffer中的n个word到端口
*
*/
#define io_outsw(port, buffer, nr) \
__asm__ __volatile__("cld;rep;outsw;mfence;" ::"d"(port), "S"(buffer), "c"(nr) \
: "memory")
/**
* @brief 验证地址空间是否为用户地址空间
*
* @param addr_start 地址起始值
* @param length 地址长度
* @return true
* @return false
*/
bool verify_area(uint64_t addr_start, uint64_t length)
{
if ((addr_start + length) <= 0x00007fffffffffffUL) // 用户程序可用的的地址空间应<= 0x00007fffffffffffUL
return true;
else
return false;
}
/**
* @brief 从用户空间搬运数据到内核空间
*
* @param dst 目的地址
* @param src 源地址
* @param size 搬运的大小
* @return uint64_t
*/
static inline uint64_t copy_from_user(void *dst, void *src, uint64_t size)
{
uint64_t tmp0, tmp1;
if (!verify_area((uint64_t)src, size))
return 0;
/**
* @brief 先每次搬运8 bytes剩余就直接一个个byte搬运
*
*/
asm volatile("rep \n\t"
"movsq \n\t"
"movq %3, %0 \n\t"
"rep \n\t"
"movsb \n\t"
: "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
: "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
: "memory");
return size;
}
/**
* @brief 从内核空间搬运数据到用户空间
*
* @param dst 目的地址
* @param src 源地址
* @param size 搬运的大小
* @return uint64_t
*/
static inline uint64_t copy_to_user(void *dst, void *src, uint64_t size)
{
if (verify_area((uint64_t)src, size))
return 0;
/**
* @brief 先每次搬运8 bytes剩余就直接一个个byte搬运
*
*/
// todo:编译有bug
// asm volatile("rep \n\t"
// "movsq \n\t"
// "movq %3, %0 \n\t"
// "rep \n\t"
// "movsb \n\t"
// : "=&c"(size), "=&D"(tmp0), "=&S"(tmp1)
// : "r"(size & 7), "0"(size >> 3), "1"(dst), "2"(src)
// : "memory");
memcpy(dst, src, size);
return size;
}
/**
* @brief 这个函数让蜂鸣器发声,目前仅用于真机调试。未来将移除,请勿依赖此函数。
*
* @param times 发声循环多少遍
*/
void __experimental_beep(uint64_t times);
/**
* @brief 往指定地址写入8字节
* 防止由于编译器优化导致不支持的内存访问类型尤其是在mmio的时候
*
* @param vaddr 虚拟地址
* @param value 要写入的值
*/
static __always_inline void __write8b(uint64_t vaddr, uint64_t value)
{
asm volatile("movq %%rdx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 往指定地址写入4字节
* 防止由于编译器优化导致不支持的内存访问类型尤其是在mmio的时候
*
* @param vaddr 虚拟地址
* @param value 要写入的值
*/
static __always_inline void __write4b(uint64_t vaddr, uint32_t value)
{
asm volatile("movl %%edx, 0(%%rax)" ::"a"(vaddr), "d"(value)
: "memory");
}
/**
* @brief 从指定地址读取8字节
* 防止由于编译器优化导致不支持的内存访问类型尤其是在mmio的时候
*
* @param vaddr 虚拟地址
* @return uint64_t 读取到的值
*/
static __always_inline uint64_t __read8b(uint64_t vaddr)
{
uint64_t retval;
asm volatile("movq 0(%%rax), %0"
: "=r"(retval)
: "a"(vaddr)
: "memory");
return retval;
}
/**
* @brief 从指定地址读取4字节
* 防止由于编译器优化导致不支持的内存访问类型尤其是在mmio的时候
*
* @param vaddr 虚拟地址
* @return uint64_t 读取到的值
*/
static __always_inline uint32_t __read4b(uint64_t vaddr)
{
uint32_t retval;
asm volatile("movl 0(%%rax), %0"
: "=d"(retval)
: "a"(vaddr)
: "memory");
return retval;
}
/**
* @brief 将数据从src搬运到dst并能正确处理地址重叠的问题

View File

@ -1,354 +0,0 @@
#pragma once
#include <common/stddef.h>
#include <asm/asm.h>
#include <common/compiler.h>
//链表数据结构
struct List
{
struct List *prev, *next;
};
//初始化循环链表
static inline void list_init(struct List *list)
{
list->next = list;
io_mfence();
list->prev = list;
}
/**
* @brief
* @param entry 给定的节点
* @param node 待插入的节点
**/
static inline void list_add(struct List *entry, struct List *node)
{
node->next = entry->next;
barrier();
node->prev = entry;
barrier();
node->next->prev = node;
barrier();
entry->next = node;
}
/**
* @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
* @param entry 列表的入口
* @param node 待添加的节点
*/
static inline void list_append(struct List *entry, struct List *node)
{
struct List *tail = entry->prev;
list_add(tail, node);
}
/**
* @brief 从列表中删除节点
* @param entry 待删除的节点
*/
static inline void list_del(struct List *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
/**
* @brief 删除链表的结点,并将这个结点重新初始化
*
*/
#define list_del_init(entry) \
list_del(entry); \
list_init(entry);
/**
* @brief 将新的链表结点替换掉旧的链表结点并使得旧的结点的前后指针均为NULL
*
* @param old 要被替换的结点
* @param new 新的要换上去的结点
*/
static inline void list_replace(struct List *old, struct List *new)
{
if (old->prev != NULL)
old->prev->next = new;
new->prev = old->prev;
if (old->next != NULL)
old->next->prev = new;
new->next = old->next;
old->prev = NULL;
old->next = NULL;
}
static inline bool list_empty(struct List *entry)
{
/**
* @brief 判断循环链表是否为空
* @param entry 入口
*/
if (entry == entry->next && entry->prev == entry)
return true;
else
return false;
}
/**
* @brief 获取链表的上一个元素
*
* @param entry
* @return 链表的上一个元素
*/
static inline struct List *list_prev(struct List *entry)
{
if (entry->prev != NULL)
return entry->prev;
else
return NULL;
}
/**
* @brief 获取链表的下一个元素
*
* @param entry
* @return 链表的下一个元素
*/
static inline struct List *list_next(struct List *entry)
{
if (entry->next != NULL)
return entry->next;
else
return NULL;
}
/**
* @brief 获取当前entry的链表结构体
*
* @param ptr 指向List结构体的指针
* @param type 包裹着List结构体的外层结构体的类型
* @param member List结构体在上述的“包裹list结构体的结构体”中的变量名
*/
#define list_entry(ptr, type, member) container_of(ptr, type, member)
/**
* @brief 获取链表中的第一个元素
* 请注意,该宏要求链表非空,否则会出错
*
* @param ptr 指向链表头的指针
* @param type 包裹着List结构体的外层结构体的类型
* @param member List结构体在上述的“包裹list结构体的结构体”中的变量名
*/
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
/**
* @brief 获取链表中的第一个元素
* 若链表为空则返回NULL
*
* @param ptr 指向链表头的指针
* @param type 包裹着List结构体的外层结构体的类型
* @param member List结构体在上述的“包裹list结构体的结构体”中的变量名
*/
#define list_first_entry_or_null(ptr, type, member) (!list_empty(ptr) ? list_entry((ptr)->next, type, member) : NULL)
/**
* @brief 获取链表中的最后一个元素
* 请注意,该宏要求链表非空,否则会出错
*
* @param ptr 指向链表头的指针
* @param type 包裹着List结构体的外层结构体的类型
* @param member List结构体在上述的“包裹list结构体的结构体”中的变量名
*/
#define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
/**
* @brief 获取链表中的最后一个元素
* 若链表为空则返回NULL
*
* @param ptr 指向链表头的指针
* @param type 包裹着List结构体的外层结构体的类型
* @param member List结构体在上述的“包裹list结构体的结构体”中的变量名
*/
#define list_last_entry_or_full(ptr, type, member) (!list_empty(ptr) ? list_entry((ptr)->prev, type, member) : NULL)
/**
* @brief 获取链表中的下一个元素
*
* @param pos 指向当前的外层结构体的指针
* @param member 链表结构体在外层结构体内的变量名
*/
#define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* @brief 获取链表中的上一个元素
*
* @param pos 指向当前的外层结构体的指针
* @param member 链表结构体在外层结构体内的变量名
*/
#define list_prev_entry(pos, member) list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* @brief 遍历整个链表(从前往后)
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param head the head for your list.
*/
#define list_for_each(ptr, head) \
for ((ptr) = (head)->next; (ptr) != (head); (ptr) = (ptr)->next)
/**
* @brief 遍历整个链表(从后往前)
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param head the head for your list.
*/
#define list_for_each_prev(ptr, head) \
for ((ptr) = (head)->prev; (ptr) != (head); (ptr) = (ptr)->prev)
/**
* @brief 遍历整个链表(从前往后)(支持删除当前链表结点)
* 该宏通过暂存中间变量防止在迭代链表的过程中由于删除了当前ptr所指向的链表结点从而造成错误
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param n 用于存储临时值的List类型的指针
* @param head the head for your list.
*/
#define list_for_each_safe(ptr, n, head) \
for ((ptr) = (head)->next, (n) = (ptr)->next; (ptr) != (head); (ptr) = n, n = (ptr)->next)
/**
* @brief 遍历整个链表(从前往后)(支持删除当前链表结点)
* 该宏通过暂存中间变量防止在迭代链表的过程中由于删除了当前ptr所指向的链表结点从而造成错误
*
* @param ptr the &struct list_head to use as a loop cursor.
* @param n 用于存储临时值的List类型的指针
* @param head the head for your list.
*/
#define list_for_each_prev_safe(ptr, n, head) \
for ((ptr) = (head)->prev, (n) = (ptr)->prev; (ptr) != (head); (ptr) = n, n = (ptr)->prev)
/**
* @brief 从头开始迭代给定类型的链表
*
* @param pos 指向特定类型的结构体的指针
* @param head 链表头
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief 从头开始迭代给定类型的链表(支持删除当前链表结点)
*
* @param pos 指向特定类型的结构体的指针
* @param n 用于存储临时值的和pos相同类型的指针
* @param head 链表头
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* @brief 逆序迭代给定类型的链表
*
* @param pos 指向特定类型的结构体的指针
* @param head 链表头
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* @brief 为list_for_each_entry_continue()准备一个'pos'结构体
*
* @param pos 指向特定类型的结构体的,用作迭代起点的指针
* @param head 指向要开始迭代的struct List结构体的指针
* @param member struct List在pos的结构体中的成员变量名
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? pos : list_entry(head, typeof(*pos), member))
/**
* @brief 从指定的位置的[下一个元素开始],继续迭代给定的链表
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief 从指定的位置的[下一个元素开始],继续迭代给定的链表。(支持删除当前链表结点)
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param n 用于存储临时值的和pos相同类型的指针
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* @brief 从指定的位置的[上一个元素开始],【逆序】迭代给定的链表
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* @brief 从指定的位置的[上一个元素开始],【逆序】迭代给定的链表。(支持删除当前链表结点)
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_continue_reverse(pos, n, head, member) \
for (pos = list_prev_entry(pos, member), n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* @brief 从指定的位置开始,继续迭代给定的链表
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_from(pos, head, member) \
for (; \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* @brief 从指定的位置开始,继续迭代给定的链表.(支持删除当前链表结点)
*
* @param pos 指向特定类型的结构体的指针。该指针用作迭代的指针。
* @param n 用于存储临时值的和pos相同类型的指针
* @param head 指向链表头的struct List的指针
* @param member struct List在pos指向的结构体中的成员变量名
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))

View File

@ -13,20 +13,6 @@
#include <syscall/syscall.h>
#include <syscall/syscall_num.h>
/**
* @brief fork当前进程
*
* @return pid_t
*/
pid_t fork(void);
/**
* @brief vfork当前进程
*
* @return pid_t
*/
pid_t vfork(void);
/**
* @brief 交换n字节
* @param src 源地址