diff --git a/kernel/common/Makefile b/kernel/common/Makefile index af3636ec..0581abfb 100644 --- a/kernel/common/Makefile +++ b/kernel/common/Makefile @@ -3,7 +3,7 @@ CFLAGS += -I . kernel_common_subdirs:=libELF math -all: glib.o printk.o cpu.o bitree.o +all: glib.o printk.o cpu.o bitree.o kfifo.o @list='$(kernel_common_subdirs)'; for subdir in $$list; do \ echo "make all in $$subdir";\ cd $$subdir;\ @@ -21,4 +21,7 @@ cpu.o: cpu.c gcc $(CFLAGS) -c cpu.c -o cpu.o bitree.o: bitree.c - gcc $(CFLAGS) -c bitree.c -o bitree.o \ No newline at end of file + gcc $(CFLAGS) -c bitree.c -o bitree.o + +kfifo.o: kfifo.c + gcc $(CFLAGS) -c kfifo.c -o kfifo.o diff --git a/kernel/common/kfifo.c b/kernel/common/kfifo.c new file mode 100644 index 00000000..18d08470 --- /dev/null +++ b/kernel/common/kfifo.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include + +/** + * @brief 通过动态方式初始化kfifo缓冲队列 + * + * @param fifo 队列结构体 + * @param size 缓冲区大小 + * @param reserved 暂时保留,请置为0 + * @return int 错误码:成功->0 + */ +int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved) +{ + memset(fifo, 0, sizeof(struct kfifo_t)); + fifo->buffer = kmalloc(size, 0); + if (fifo->buffer == NULL) + goto failed; + + fifo->total_size = size; + return 0; +failed:; + return -ENOMEM; +} + +/** + * @brief 使用指定的缓冲区来初始化kfifo缓冲队列 + * + * @param fifo 队列结构体 + * @param buffer 缓冲区 + * @param size 缓冲区大小 + */ +void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size) +{ + memset(fifo, 0, sizeof(struct kfifo_t)); + + fifo->buffer = buffer; + fifo->total_size = size; +} + +/** + * @brief 向kfifo缓冲区推入数据 + * + * @param fifo 队列结构体 + * @param from 来源数据地址 + * @param size 数据大小(字节数) + * @return uint32_t 推入的数据大小 + */ +uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size) +{ + // 判断空间是否够 + if (unlikely(fifo->size + size > fifo->total_size)) + return 0; + if (unlikely(from == NULL)) + return 0; + + // 分两种情况,一种是要发生回环,另一种不发生回环 + if (fifo->in_offset + size > fifo->total_size) // 发生回环 + { + uint32_t tmp = fifo->total_size - fifo->in_offset; + memcpy(fifo->buffer + fifo->in_offset, from, tmp); + memcpy(fifo->buffer, from + tmp, size - tmp); + fifo->in_offset = size - tmp; + } + else // 不发生回环 + { + memcpy(fifo->buffer + fifo->in_offset, from, size); + fifo->in_offset += size; + } + + fifo->size += size; + + return size; +} + +/** + * @brief 从kfifo缓冲区取出数据,并从队列中删除数据 + * + * @param fifo 队列结构体 + * @param to 拷贝目标地址 + * @param size 数据大小(字节数) + * @return uint32_t 取出的数据大小 + */ +uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size) +{ + if (unlikely(to == NULL)) // 判断目标地址是否为空 + return 0; + if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据 + return 0; + + // 判断是否会发生回环 + if (fifo->out_offset + size > fifo->total_size) // 发生回环 + { + uint32_t tmp = fifo->total_size - fifo->out_offset; + memcpy(to, fifo->buffer + fifo->out_offset, tmp); + memcpy(to + tmp, fifo->buffer, size - tmp); + fifo->out_offset = size - tmp; + } + else // 未发生回环 + { + memcpy(to, fifo->buffer + fifo->out_offset, size); + fifo->out_offset += size; + } + + fifo->size -= size; + + return size; +} + +/** + * @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据 + * + * @param fifo 队列结构体 + * @param to 拷贝目标地址 + * @param size 数据大小(字节数) + * @return uint32_t 取出的数据大小 + */ +uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size) +{ + if (unlikely(to == NULL)) // 判断目标地址是否为空 + return 0; + if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据 + return 0; + + // 判断是否会发生回环 + if (fifo->out_offset + size > fifo->total_size) // 发生回环 + { + uint32_t tmp = fifo->total_size - fifo->out_offset; + memcpy(to, fifo->buffer + fifo->out_offset, tmp); + memcpy(to + tmp, fifo->buffer, size - tmp); + } + else // 未发生回环 + { + memcpy(to, fifo->buffer + fifo->out_offset, size); + } + + return size; +} + +/** + * @brief 释放通过kfifo_alloc创建的fifo缓冲区 + * + * @param fifo fifo队列结构体 + */ +void kfifo_free_alloc(struct kfifo_t *fifo) +{ + kfree(fifo->buffer); + memset(fifo, 0, sizeof(struct kfifo_t)); +} \ No newline at end of file diff --git a/kernel/common/kfifo.h b/kernel/common/kfifo.h new file mode 100644 index 00000000..a03dc475 --- /dev/null +++ b/kernel/common/kfifo.h @@ -0,0 +1,118 @@ +#pragma once + +#include + +struct kfifo_t +{ + uint32_t total_size; // 缓冲区总空间 + uint32_t size; // 元素所占的字节数 + uint32_t in_offset; // 入口偏移 + uint32_t out_offset; // 出口偏移 + void *buffer; // 缓冲区 +} __attribute__((aligned(sizeof(long)))); + +/** + * @brief 忽略kfifo队列中的所有内容,并把输入和输出偏移量都归零 + * + */ +#define kfifo_reset(fifo) (void)({ \ + (fifo)->size = 0; \ + (fifo)->in_offset = 0; \ + (fifo)->out_offset = 0; \ +}) + +/** + * @brief 忽略kfifo队列中的所有内容,并使得输出偏移量为输入偏移量 + * + */ +#define kfifo_reset_out(fifo) (void)({ \ + (fifo)->size = 0; \ + (fifo)->out_offset = (fifo)->in_offset; \ +}) + +/** + * @brief 获取kfifo缓冲区的最大大小 + * + * @param fifo 队列结构体 + * @return uint32_t 缓冲区最大大小 + */ +#define kfifo_total_size(fifo) ((fifo)->total_size) +/** + * @brief 获取kfifo缓冲区当前已有元素大小 + * + * @param fifo 队列结构体 + * @return uint32_t 缓冲区当前已有元素大小 + */ +#define kfifo_size(fifo) ((fifo)->size) + +/** + * @brief 判断kfifo缓冲区当前是否为空 + * + * @param fifo 队列结构体 + * @return uint32_t 0->非空, 1->空 + */ +#define kfifo_empty(fifo) (((fifo)->size == 0) ? 1 : 0) + +/** + * @brief 判断kfifo缓冲区当前是否为满 + * + * @param fifo 队列结构体 + * @return uint32_t 0->不满, 1->满 + */ +#define kfifo_full(fifo) (((fifo)->size == (fifo)->total_size) ? 1 : 0) + +/** + * @brief 通过动态方式初始化kfifo缓冲队列 + * + * @param fifo 队列结构体 + * @param size 缓冲区大小 + * @param reserved 暂时保留,请置为0 + * @return int 错误码:成功->0 + */ +int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved); + +/** + * @brief 释放通过kfifo_alloc创建的fifo缓冲区 + * + * @param fifo fifo队列结构体 + */ +void kfifo_free_alloc(struct kfifo_t* fifo); + +/** + * @brief 使用指定的缓冲区来初始化kfifo缓冲队列 + * + * @param fifo 队列结构体 + * @param buffer 缓冲区 + * @param size 缓冲区大小 + */ +void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size); + +/** + * @brief 向kfifo缓冲区推入数据 + * + * @param fifo 队列结构体 + * @param from 来源数据地址 + * @param size 数据大小(字节数) + * @return uint32_t 推入的数据大小 + */ +uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size); + +/** + * @brief 从kfifo缓冲区取出数据,并从队列中删除数据 + * + * @param fifo 队列结构体 + * @param to 拷贝目标地址 + * @param size 数据大小(字节数) + * @return uint32_t 取出的数据大小 + */ +uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size); + +/** + * @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据 + * + * @param fifo 队列结构体 + * @param to 拷贝目标地址 + * @param size 数据大小(字节数) + * @return uint32_t 取出的数据大小 + */ +uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size); diff --git a/kernel/driver/usb/xhci/xhci.c b/kernel/driver/usb/xhci/xhci.c index 1e13767c..7b845728 100644 --- a/kernel/driver/usb/xhci/xhci.c +++ b/kernel/driver/usb/xhci/xhci.c @@ -347,7 +347,7 @@ static uint32_t xhci_hc_get_protocol_offset(int id, uint32_t list_off, const int *offset = (uint32_t)(dw2 & 0xff) - 1; // 使其转换为zero based if (count != NULL) *count = (uint32_t)((dw2 & 0xff00) >> 8); - if (protocol_flag != NULL) + if (protocol_flag != NULL && version == 2) *protocol_flag = (uint16_t)((dw2 >> 16) & 0x0fff); return next_list_off; diff --git a/kernel/ktest/Makefile b/kernel/ktest/Makefile index dce56893..ef3b8a20 100644 --- a/kernel/ktest/Makefile +++ b/kernel/ktest/Makefile @@ -2,7 +2,10 @@ CFLAGS += -I . -all: bitree.o +all: bitree.o kfifo.o bitree.o: test-bitree.c - gcc $(CFLAGS) -c test-bitree.c -o test-bitree.o \ No newline at end of file + gcc $(CFLAGS) -c test-bitree.c -o test-bitree.o + +kfifo.o: test-kfifo.c + gcc $(CFLAGS) -c test-kfifo.c -o test-kfifo.o diff --git a/kernel/ktest/ktest.h b/kernel/ktest/ktest.h index a2be05fb..86c717df 100644 --- a/kernel/ktest/ktest.h +++ b/kernel/ktest/ktest.h @@ -1,4 +1,5 @@ #pragma once #include -uint64_t ktest_test_bitree(uint64_t arg); \ No newline at end of file +uint64_t ktest_test_bitree(uint64_t arg); +uint64_t ktest_test_kfifo(uint64_t arg); \ No newline at end of file diff --git a/kernel/ktest/test-bitree.c b/kernel/ktest/test-bitree.c index 84d83a74..ef92bc09 100644 --- a/kernel/ktest/test-bitree.c +++ b/kernel/ktest/test-bitree.c @@ -127,6 +127,6 @@ uint64_t ktest_test_bitree(uint64_t arg) kTEST("Testing case %d", i); kt_bitree_func_table[i](0, 0); } - kdebug("bitree Test done."); + kTEST("bitree Test done."); return 0; } \ No newline at end of file diff --git a/kernel/ktest/test-kfifo.c b/kernel/ktest/test-kfifo.c new file mode 100644 index 00000000..02834c60 --- /dev/null +++ b/kernel/ktest/test-kfifo.c @@ -0,0 +1,165 @@ +#include "ktest.h" +#include "ktest_utils.h" +#include +#include +#include + +static long ktest_kfifo_case0_1(uint64_t arg0, uint64_t arg1) +{ + const int fifo_size = 256; + // 创建kfifo(由kfifo申请内存) + struct kfifo_t fifo; + if (arg0 == 0) + assert(kfifo_alloc(&fifo, fifo_size, 0) == 0); + else + { + void *buf = kmalloc(fifo_size, 0); + kfifo_init(&fifo, buf, fifo_size); + } + + assert(fifo.buffer != NULL); + assert(fifo.total_size == fifo_size); + assert(kfifo_total_size(&fifo) == fifo_size); + assert(fifo.size == 0); + assert(kfifo_size(&fifo) == 0); + assert(fifo.in_offset == 0); + assert(fifo.out_offset == 0); + assert(kfifo_empty(&fifo) == 1); + assert(kfifo_full(&fifo) == 0); + + // 循环增加10个uint64_t + for (int i = 1; i <= 10; ++i) + { + uint64_t tmp = i; + assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + } + assert(fifo.in_offset == 10 * sizeof(uint64_t)); + assert(fifo.out_offset == 0); + assert(fifo.size == 10 * sizeof(uint64_t)); + assert(fifo.total_size == fifo_size); + + // 循环删除这10个uint64_t + for (int i = 1; i <= 10; ++i) + { + uint64_t tmp = 0; + assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + assert(tmp == i); + assert(fifo.size == (10 - i) * sizeof(uint64_t)); + assert(fifo.in_offset == 10 * sizeof(uint64_t)); + assert(fifo.out_offset == i * sizeof(uint64_t)); + } + + assert(fifo.in_offset == 10 * sizeof(uint64_t)); + assert(fifo.out_offset == 10 * sizeof(uint64_t)); + assert(fifo.in_offset == fifo.out_offset); + assert(kfifo_empty(&fifo) == 1); + + // reset + kfifo_reset(&fifo); + assert(fifo.in_offset == 0); + assert(fifo.out_offset == 0); + assert(fifo.size == 0); + + // 测试插入31个元素 + for (int i = 1; i <= 31; ++i) + { + uint64_t tmp = i; + assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + } + + assert(fifo.size == 31 * sizeof(uint64_t)); + assert(fifo.in_offset == 31 * sizeof(uint64_t)); + assert(fifo.out_offset == 0); + + // 然后再尝试插入一个大小为2*sizeof(uint64_t)的元素 + { + __int128_t tmp = 100; + assert(kfifo_in(&fifo, &tmp, sizeof(__int128_t)) == 0); + assert(fifo.size == 31 * sizeof(uint64_t)); + assert(fifo.in_offset == 31 * sizeof(uint64_t)); + assert(fifo.out_offset == 0); + } + // 插入一个uint64, 队列满 + { + uint64_t tmp = 32; + assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + assert(kfifo_full(&fifo)); + assert(kfifo_empty(&fifo) == 0); + assert(fifo.size == fifo.total_size); + assert(fifo.in_offset == fifo_size); + assert(fifo.out_offset == 0); + } + + // 取出之前的20个元素 + for (int i = 1; i <= 20; ++i) + { + uint64_t tmp = 0; + assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + } + assert(fifo.size == (fifo.total_size - 20 * sizeof(uint64_t))); + assert(fifo.in_offset == fifo_size); + assert(fifo.out_offset == 20 * sizeof(uint64_t)); + + // 插入10个元素,剩余10个空位 + { + uint64_t tmp = 99; + + assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + assert(fifo.in_offset == 1 * sizeof(uint64_t)); + + for (int i = 1; i <= 9; ++i) + { + assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t)); + } + assert(fifo.in_offset == 10 * sizeof(uint64_t)); + assert(fifo.size == 22 * sizeof(uint64_t)); + } + + { + // 取出20个 + char tmp[20 * sizeof(uint64_t)]; + assert(kfifo_out(&fifo, &tmp, 20 * sizeof(uint64_t)) == 20 * sizeof(uint64_t)); + assert(fifo.out_offset == 8 * sizeof(uint64_t)); + assert(fifo.size == 2 * (sizeof(uint64_t))); + } + + { + // 插入25个 + char tmp[25 * sizeof(uint64_t)]; + assert(kfifo_in(&fifo, &tmp, 25 * sizeof(uint64_t)) == 25 * sizeof(uint64_t)); + assert(fifo.out_offset == 8 * sizeof(uint64_t)); + assert(fifo.size == 27 * sizeof(uint64_t)); + assert(fifo.in_offset == 3 * sizeof(uint64_t)); + } + + // 测试reset out + uint32_t prev_in_offset = fifo.in_offset; + kfifo_reset_out(&fifo); + assert(fifo.size == 0); + assert(fifo.total_size == fifo_size); + assert(fifo.in_offset == prev_in_offset); + assert(fifo.out_offset == prev_in_offset); + + // 测试释放 + if (arg0 == 0) + { + kfifo_free_alloc(&fifo); + assert(fifo.buffer == NULL); + } +} + +static ktest_case_table kt_kfifo_func_table[] = { + ktest_kfifo_case0_1, +}; + +uint64_t ktest_test_kfifo(uint64_t arg) +{ + kTEST("Testing kfifo..."); + for (int i = 0; i < sizeof(kt_kfifo_func_table) / sizeof(ktest_case_table); ++i) + { + kTEST("Testing case %d", i); + kt_kfifo_func_table[i](i, 0); + } + kTEST("kfifo Test done."); + return 0; +} diff --git a/kernel/process/process.c b/kernel/process/process.c index 36bcadd9..ccd2a73f 100644 --- a/kernel/process/process.c +++ b/kernel/process/process.c @@ -415,8 +415,10 @@ ul initial_kernel_thread(ul arg) fat32_init(); usb_init(); - pid_t test_thread_pid = kernel_thread(ktest_test_bitree, 1, 0); - kdebug("test_thread_pid=%d", test_thread_pid); + // 对一些组件进行单元测试 + kernel_thread(ktest_test_bitree, 0, 0); + kernel_thread(ktest_test_kfifo, 0, 0); + // 准备切换到用户态 struct pt_regs *regs; @@ -644,7 +646,6 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned if (process_copy_flags(clone_flags, tsk)) goto copy_flags_failed; - kdebug("before copy mm"); // 拷贝内存空间分布结构体 if (process_copy_mm(clone_flags, tsk)) goto copy_mm_failed;