Feat: lab 4
This commit is contained in:
55
kernel/include/dtb.h
Normal file
55
kernel/include/dtb.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <vector.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t totalsize;
|
||||
uint32_t off_dt_struct;
|
||||
uint32_t off_dt_strings;
|
||||
uint32_t off_mem_rsvmap;
|
||||
uint32_t version;
|
||||
uint32_t last_comp_version;
|
||||
uint32_t boot_cpuid_phys;
|
||||
uint32_t size_dt_strings;
|
||||
uint32_t size_dt_struct;
|
||||
}__attribute__((packed)) fdt_header_t;
|
||||
|
||||
typedef struct {
|
||||
uint64_t address;
|
||||
uint64_t size;
|
||||
}__attribute__((packed)) fdt_reserve_entry_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t len;
|
||||
uint32_t nameoff;
|
||||
}__attribute__((packed)) fdt_prop_header_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t len;
|
||||
const char *name;
|
||||
void *value;
|
||||
} fdt_prop_t;
|
||||
|
||||
#define FDT_BEGIN_NODE 0x00000001
|
||||
#define FDT_END_NODE 0x00000002
|
||||
#define FDT_PROP 0x00000003
|
||||
#define FDT_NOP 0x00000004
|
||||
#define FDT_END 0x00000009
|
||||
|
||||
#define FDT_PATH_BUFFER_LEN 0x10
|
||||
|
||||
typedef void (*fdt_callback_func_t)(const vector_t *);
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const fdt_callback_func_t func;
|
||||
} fdt_callback_t;
|
||||
|
||||
void fdt_traverse(const vector_t *struct_cbs);
|
||||
|
||||
extern void *dtb_start;
|
||||
extern void *dtb_end;
|
||||
extern void *dtb_memory_start;
|
||||
extern void *dtb_memory_end;
|
||||
extern vector_t *dtb_reserved_entries;
|
||||
55
kernel/include/initrd.h
Normal file
55
kernel/include/initrd.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <dtb.h>
|
||||
#include <vector.h>
|
||||
|
||||
typedef struct {
|
||||
char c_magic[6];
|
||||
char c_ino[8];
|
||||
char c_mode[8];
|
||||
char c_uid[8];
|
||||
char c_gid[8];
|
||||
char c_nlink[8];
|
||||
char c_mtime[8];
|
||||
char c_filesize[8];
|
||||
char c_devmajor[8];
|
||||
char c_devminor[8];
|
||||
char c_rdevmajor[8];
|
||||
char c_rdevminor[8];
|
||||
char c_namesize[8];
|
||||
char c_check[8];
|
||||
}__attribute__((packed)) cpio_newc_header_t;
|
||||
|
||||
typedef struct file_node {
|
||||
struct file_node *l, *r;
|
||||
int rand;
|
||||
int node_size;
|
||||
|
||||
int ino;
|
||||
int mode;
|
||||
int uid;
|
||||
int gid;
|
||||
int nlink;
|
||||
int mtime;
|
||||
int filesize;
|
||||
int devmajor;
|
||||
int devminor;
|
||||
int rdevmajor;
|
||||
int rdevminor;
|
||||
int namesize;
|
||||
|
||||
char *filename;
|
||||
uint8_t *filecontent;
|
||||
} file_node_t;
|
||||
|
||||
typedef void (*initrd_callback_func_t)(file_node_t *);
|
||||
|
||||
file_node_t *initrd_init(void);
|
||||
void initrd_fdt_callback(const vector_t *props);
|
||||
file_node_t *initrd_get(file_node_t *root, const char *filename);
|
||||
void initrd_traverse(file_node_t *tr, initrd_callback_func_t func);
|
||||
|
||||
extern fdt_callback_t initrd_dtb_cb;
|
||||
extern void *initrd_start;
|
||||
extern void *initrd_end;
|
||||
27
kernel/include/kmalloc.h
Normal file
27
kernel/include/kmalloc.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define KMALLOC_MAX_ALLOCATOR_SIZE 256
|
||||
|
||||
typedef struct kmalloc_header {
|
||||
size_t unit;
|
||||
size_t left;
|
||||
void *begin;
|
||||
struct kmalloc_header *page_prev, *page_next;
|
||||
}__attribute__((packed)) kmalloc_header_t;
|
||||
|
||||
typedef struct {
|
||||
size_t left;
|
||||
kmalloc_header_t *page_begin;
|
||||
} kmalloc_allocator_t;
|
||||
|
||||
void init_mman_kmalloc();
|
||||
void *simple_alloc(size_t size);
|
||||
void *mman_alloc(size_t size);
|
||||
void mman_free(void *ptr);
|
||||
|
||||
extern void *(*kmalloc)(size_t size);
|
||||
extern void (*kfree)(void *ptr);
|
||||
|
||||
extern kmalloc_allocator_t mman_kmalloc_pool[KMALLOC_MAX_ALLOCATOR_SIZE + 1];
|
||||
51
kernel/include/mbox.h
Normal file
51
kernel/include/mbox.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2018 bzt (bztsrc@github)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/* a properly aligned buffer */
|
||||
extern volatile unsigned int mbox[36];
|
||||
|
||||
#define MBOX_REQUEST 0
|
||||
|
||||
/* channels */
|
||||
#define MBOX_CH_POWER 0
|
||||
#define MBOX_CH_FB 1
|
||||
#define MBOX_CH_VUART 2
|
||||
#define MBOX_CH_VCHIQ 3
|
||||
#define MBOX_CH_LEDS 4
|
||||
#define MBOX_CH_BTNS 5
|
||||
#define MBOX_CH_TOUCH 6
|
||||
#define MBOX_CH_COUNT 7
|
||||
#define MBOX_CH_PROP 8
|
||||
|
||||
/* tags */
|
||||
#define MBOX_TAG_LAST 0
|
||||
#define MBOX_TAG_BOARD_REVISION 0x00010002
|
||||
#define MBOX_TAG_GETSERIAL 0x00010004
|
||||
#define MBOX_TAG_ARM_MEMORY 0x00010005
|
||||
|
||||
int mbox_call(unsigned char ch);
|
||||
unsigned int get_board_revision(void);
|
||||
unsigned int get_memory_base_addr(void);
|
||||
unsigned int get_memory_size(void);
|
||||
37
kernel/include/mman.h
Normal file
37
kernel/include/mman.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <dtb.h>
|
||||
|
||||
#define MMAN_NO_PAGE 0xffffffffffffffff
|
||||
#define PAGE_SIZE (1 << 12)
|
||||
|
||||
// PAGE_FREE => available
|
||||
// PAGE_ALLOCATED => this range is allocated explicitly
|
||||
// PAGE_DIVIDED => this range is divided into smaller range
|
||||
// PAGE_RESERVED => this range is reserved explicitly
|
||||
typedef enum :uint8_t {
|
||||
PAGE_FREE = 0,
|
||||
PAGE_ALLOCATED = 1,
|
||||
PAGE_DIVIDED = 2,
|
||||
PAGE_RESERVED = 3,
|
||||
} page_state_t;
|
||||
|
||||
typedef struct {
|
||||
page_state_t state;
|
||||
uint64_t maxsz;
|
||||
} page_header_t;
|
||||
|
||||
void mman_init();
|
||||
|
||||
void *allocate_page(size_t page_cnt);
|
||||
void free_page(void *ptr);
|
||||
void reserve_page(void *begin, void *end);
|
||||
|
||||
void mman_fdt_memory_cb_func(const vector_t *props);
|
||||
|
||||
extern fdt_callback_t mman_dtb_memory_cb;
|
||||
extern void *mman_memory_start;
|
||||
extern void *mman_memory_end;
|
||||
extern size_t mman_page_cnt;
|
||||
extern page_header_t *mman_frame_array;
|
||||
29
kernel/include/queue.h
Normal file
29
kernel/include/queue.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct queue_node {
|
||||
struct queue_node *prev, *next;
|
||||
uint64_t value;
|
||||
} queue_node_t;
|
||||
|
||||
typedef struct queue {
|
||||
size_t size;
|
||||
|
||||
queue_node_t *begin, *end;
|
||||
} queue_t;
|
||||
|
||||
queue_t *make_queue();
|
||||
|
||||
uint64_t queue_back(const queue_t *queue);
|
||||
uint64_t queue_front(const queue_t *queue);
|
||||
|
||||
void queue_push_back(queue_t *queue, uint64_t val);
|
||||
void queue_push_front(queue_t *queue, uint64_t val);
|
||||
|
||||
#define QUEUE_BACK(type, queue) ((type *)queue_back(queue))
|
||||
#define QUEUE_FRONT(type, queue) ((type *)queue_front(queue))
|
||||
#define QUEUE_PUSH_BACK(queue, val) \
|
||||
queue_push_back(queue, (uint64_t)val)
|
||||
#define QUEUE_PUSH_FRONT(queue, val) \
|
||||
queue_push_front(queue, (uint64_t)val)
|
||||
8
kernel/include/random.h
Normal file
8
kernel/include/random.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
extern const int _random_a;
|
||||
extern const int _random_c;
|
||||
extern const int _random_m;
|
||||
extern int seed;
|
||||
|
||||
int random(void);
|
||||
15
kernel/include/shell.h
Normal file
15
kernel/include/shell.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <initrd.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void help(void);
|
||||
void hello(void);
|
||||
void hwinfo(void);
|
||||
void memalloc(size_t size);
|
||||
void ls(file_node_t *root);
|
||||
void cat(file_node_t *root, const char *filename);
|
||||
void reboot(void);
|
||||
|
||||
int // is continue
|
||||
shell(file_node_t *);
|
||||
38
kernel/include/utils.h
Normal file
38
kernel/include/utils.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <uart.h>
|
||||
|
||||
uint32_t msb32(uint32_t);
|
||||
uint64_t msb64(uint64_t);
|
||||
|
||||
uint32_t hton32(const uint32_t);
|
||||
uint32_t ntoh32(const uint32_t);
|
||||
|
||||
uint64_t hton64(const uint64_t);
|
||||
uint64_t ntoh64(const uint64_t);
|
||||
|
||||
int isdigit(int);
|
||||
int isxdigit(int);
|
||||
int isupper(int);
|
||||
int isspace(int);
|
||||
|
||||
int32_t atoi32(const char *);
|
||||
uint32_t atoh32(const char *);
|
||||
|
||||
void exit(int);
|
||||
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#define MAX(x, y) (((x) < (y)) ? (y) : (x))
|
||||
|
||||
#define ALIGN(ptr, cnt) (void *)((( \
|
||||
((uint64_t)(ptr) - 1) >> (cnt)) + 1) << (cnt))
|
||||
|
||||
#define ALIGN4(ptr) ALIGN(ptr, 2)
|
||||
#define ALIGN8(ptr) ALIGN(ptr, 3)
|
||||
#define ALIGN4K(ptr) ALIGN(ptr, 12)
|
||||
|
||||
#define BUMP(orig_type, bump_type, ptr) ( \
|
||||
(ptr = (orig_type*)((bump_type*)ptr + 1)), \
|
||||
((bump_type*)ptr - 1) \
|
||||
)
|
||||
18
kernel/include/vector.h
Normal file
18
kernel/include/vector.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
size_t cap;
|
||||
|
||||
uint64_t *data;
|
||||
} vector_t;
|
||||
|
||||
vector_t *make_vector(size_t size);
|
||||
|
||||
uint64_t vec_at(const vector_t *vec, size_t idx);
|
||||
void vec_push(vector_t *vec, uint64_t val);
|
||||
|
||||
#define VEC_AT(type, vec, idx) ((type *)vec_at((vec), (idx)))
|
||||
#define VEC_PUSH(vec, val) vec_push(vec, (uint64_t)val)
|
||||
Reference in New Issue
Block a user