🆕 内核核心api文档

This commit is contained in:
fslongjin 2022-07-12 20:39:33 +08:00
parent a5ce84c26e
commit 39a09ffd72
4 changed files with 237 additions and 10 deletions

View File

@ -18,6 +18,7 @@
:caption: 内核层 :caption: 内核层
kernel/boot/index kernel/boot/index
kernel/core_api/index
kernel/process_management/index kernel/process_management/index
kernel/filesystem/index kernel/filesystem/index

View File

@ -0,0 +1,10 @@
核心API文档
====================================
这里是DragonOS的核心api文档。
.. toctree::
:maxdepth: 1
:caption: 内核实用函数库
kernel_api

View File

@ -0,0 +1,223 @@
# DragonOS内核核心API
## 循环链表管理函数
  循环链表是内核的重要的数据结构之一。包含在`kernel/common/glib.h`中。
### `void list_init(struct List *list)`
#### 描述
  初始化一个List结构体使其prev和next指针指向自身
#### 参数
**list**
  要被初始化的List结构体
### `void list_add(struct List *entry, struct List *node)`
#### 描述
  将node插入到entry的后方
#### 参数
**entry**
  已存在于循环链表中的一个结点
**node**
  待插入的结点
### `void list_append(struct List *entry, struct List *node)`
#### 描述
  将node插入到entry的前方
#### 参数
**entry**
  已存在于循环链表中的一个结点
**node**
  待插入的结点
### `void list_del(struct List *entry)`
#### 描述
  从链表中删除结点entry
#### 参数
**entry**
  待删除的结点
### `bool list_empty(struct List *entry)`
#### 描述
  判断链表是否为空
#### 参数
**entry**
  链表中的一个结点
### `struct List *list_prev(struct List *entry)`
#### 描述
获取entry的前一个结点
#### 参数
**entry**
  链表中的一个结点
### `struct List *list_next(struct List *entry)`
#### 描述
  获取entry的后一个结点
#### 参数
**entry**
  链表中的一个结点
---
## 基础C函数库
  内核编程与应用层编程不同你将无法使用LibC中的函数来进行编程。为此内核实现了一些常用的C语言函数并尽量使其与标准C库中的函数行为相近。值得注意的是这些函数的行为可能与标准C库函数不同请在使用时仔细阅读以下文档这将会为你带来帮助。
### 字符串操作
#### `int strlen(const char *s)`
##### 描述
测量并返回字符串长度。
##### 参数
**src**
源字符串
#### `long strnlen(const char *src, unsigned long maxlen)`
##### 描述
  测量并返回字符串长度。当字符串长度大于maxlen时返回maxlen
##### 参数
**src**
  源字符串
**maxlen**
  最大长度
#### `long strnlen_user(const char *src, unsigned long maxlen)`
##### 描述
测量并返回字符串长度。当字符串长度大于maxlen时返回maxlen。
  该函数会进行地址空间校验要求src字符串必须来自用户空间。当源字符串来自内核空间时将返回0.
##### 参数
**src**
  源字符串,地址位于用户空间
**maxlen**
  最大长度
#### `char *strncpy(char *dst, const char *src, long count)`
##### 描述
拷贝长度为count个字节的字符串返回dst字符串
##### 参数
**src**
  源字符串
**dst**
  目标字符串
**count**
  要拷贝的源字符串的长度
#### `long strncpy_from_user(char *dst, const char *src, unsigned long size)`
##### 描述
  从用户空间拷贝长度为count个字节的字符串到内核空间返回拷贝的字符串的大小
  该函数会对字符串的地址空间进行校验,防止出现地址空间越界的问题。
##### 参数
**src**
  源字符串
**dst**
  目标字符串
**size**
  要拷贝的源字符串的长度
#### `int strcmp(char *FirstPart, char *SecondPart)`
##### 描述
  比较两个字符串的大小。
***返回值***
| 情况 | 返回值 |
| ----------------------- | --- |
| FirstPart == SecondPart | 0 |
| FirstPart > SecondPart | 1 |
| FirstPart < SecondPart | -1 |
##### 参数
**FirstPart**
  第一个字符串
**SecondPart**
&emsp;&emsp;第二个字符串

View File

@ -283,13 +283,6 @@ int strcmp(char *FirstPart, char *SecondPart)
return __res; return __res;
} }
// void *memset_c(void *dst, unsigned char c, ul n)
// {
// unsigned char *s = (unsigned char *)dst;
// for (int i = 0; i < n; ++i)
// s[i] = c;
// return dst;
// }
// 从io口读入8个bit // 从io口读入8个bit
unsigned char io_in8(unsigned short port) unsigned char io_in8(unsigned short port)
@ -541,7 +534,7 @@ static inline uint64_t copy_to_user(void *dst, void *src, uint64_t size)
*/ */
long strnlen_user(const char *src, unsigned long maxlen); long strnlen_user(const char *src, unsigned long maxlen);
char *strncpy(char *Dest, const char *Src, long Count) char *strncpy(char *dst, const char *src, long count)
{ {
__asm__ __volatile__("cld \n\t" __asm__ __volatile__("cld \n\t"
"1: \n\t" "1: \n\t"
@ -555,9 +548,9 @@ char *strncpy(char *Dest, const char *Src, long Count)
"stosb \n\t" "stosb \n\t"
"2: \n\t" "2: \n\t"
: :
: "S"(Src), "D"(Dest), "c"(Count) : "S"(src), "D"(dst), "c"(count)
: "ax", "memory"); : "ax", "memory");
return Dest; return dst;
} }
long strncpy_from_user(char *dst, const char *src, unsigned long size) long strncpy_from_user(char *dst, const char *src, unsigned long size)