kv2: initialize memory manager, early pci devices
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
e5b55a6ba5
commit
a252a3f8e9
58 changed files with 1270 additions and 505 deletions
|
|
@ -329,13 +329,16 @@ typedef volatile struct {
|
|||
|
||||
} PACKED hbafis_t;
|
||||
|
||||
struct sata_device {
|
||||
struct pci_device *controller;
|
||||
uint8_t port;
|
||||
struct ahci_host {
|
||||
struct pci_device *device;
|
||||
hbamem_t *abar;
|
||||
};
|
||||
|
||||
struct sata_device {
|
||||
struct ahci_host *host;
|
||||
uint8_t port;
|
||||
size_t sector_size;
|
||||
size_t sector_count;
|
||||
hbamem_t *abar;
|
||||
};
|
||||
|
||||
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write);
|
||||
|
|
|
|||
24
include/drivers/block.h
Normal file
24
include/drivers/block.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _BLOCK_H_
|
||||
#define _BLOCK_H_
|
||||
|
||||
#include <kernel/device.h>
|
||||
|
||||
struct block_device;
|
||||
struct block_ops {
|
||||
ssize_t (*read)(struct block_device *dev, char *buf, size_t offset, size_t n);
|
||||
ssize_t (*write)(struct block_device *dev, char *buf, size_t offset, size_t n);
|
||||
};
|
||||
|
||||
struct block_device {
|
||||
char *name;
|
||||
dev_t dev;
|
||||
size_t size;
|
||||
size_t block_size;
|
||||
struct block_ops ops;
|
||||
};
|
||||
|
||||
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n);
|
||||
ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n);
|
||||
|
||||
#endif /* _BLOCK_H_ */
|
||||
|
|
@ -4,16 +4,39 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
|
||||
struct pci_host {
|
||||
vaddr_t base;
|
||||
u16 segment_group;
|
||||
u8 start_bus;
|
||||
u8 end_bus;
|
||||
};
|
||||
|
||||
struct pci_device {
|
||||
u64 class;
|
||||
u64 vendor;
|
||||
u8 bus;
|
||||
u8 slot;
|
||||
u8 func;
|
||||
struct pci_host *host;
|
||||
|
||||
u16 class_major;
|
||||
u16 class_minor;
|
||||
u16 vendor;
|
||||
u16 device_id;
|
||||
|
||||
u8 bus;
|
||||
u8 slot;
|
||||
u8 func;
|
||||
|
||||
paddr_t bar0;
|
||||
paddr_t bar1;
|
||||
paddr_t bar2;
|
||||
paddr_t bar3;
|
||||
} PACKED;
|
||||
paddr_t bar4;
|
||||
paddr_t bar5;
|
||||
};
|
||||
|
||||
void pci_register_host(struct pci_host *host);
|
||||
void pci_init();
|
||||
u32 pci_config_read(struct pci_host *host, u32 bus, u32 slot, u32 func, u32 offset);
|
||||
void pci_config_write(struct pci_host *host, u32 bus, u32 slot, u32 func, u32 offset, u32 val);
|
||||
size_t pci_find_device(u16 minor, u16 major, list_t *out);
|
||||
|
||||
#endif /* _PCI_H_ */
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
|
||||
#include <kernel/device.h>
|
||||
|
||||
#define DEV_CLASS_TTY 0x01
|
||||
|
||||
struct tty_serial_ops {
|
||||
void (*serial_putchar)(struct tty *tty, char c);
|
||||
char (*serial_getchar)(struct tty *tty);
|
||||
|
|
@ -13,15 +11,13 @@ struct tty_serial_ops {
|
|||
|
||||
struct tty {
|
||||
dev_t dev;
|
||||
dev_t serial_device;
|
||||
void *serial_device_private;
|
||||
|
||||
void *priv;
|
||||
struct tty_serial_ops *serial_ops;
|
||||
};
|
||||
|
||||
void tty_layer_init();
|
||||
|
||||
struct tty *tty_create(const char *name, dev_t serial_device);
|
||||
struct tty *tty_create(const char *name, void *priv);
|
||||
void tty_putchar(struct tty *tty, char c);
|
||||
char tty_getchar(struct tty *tty);
|
||||
void tty_write(struct tty *tty, const char *s);
|
||||
|
|
|
|||
80
include/fs/vfs.h
Normal file
80
include/fs/vfs.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _VFS_H_
|
||||
#define _VFS_H_
|
||||
|
||||
#include <kernel/spinlock.h>
|
||||
#include <lib/rwlock.h>
|
||||
#include <lib/list.h>
|
||||
|
||||
#define DIRENT_UNKNOWN_TYPE 0x0
|
||||
#define DIRENT_REGULAR_TYPE 0x1
|
||||
#define DIRENT_DIRECTORY_TYPE 0x2
|
||||
#define DIRENT_CHRDEV_TYPE 0x3
|
||||
#define DIRENT_BLKDEV_TYPE 0x4
|
||||
#define DIRENT_FIFO_TYPE 0x5
|
||||
#define DIRENT_SOCKET_TYPE 0x6
|
||||
#define DIRENT_SYMLINK_TYPE 0x7
|
||||
|
||||
#define DIRENT_MOUNT_POINT 0x8
|
||||
|
||||
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
|
||||
|
||||
typedef struct _inode inode_t;
|
||||
typedef struct _dirent dirent_t;
|
||||
typedef struct _fs fs_t;
|
||||
|
||||
struct fs_ops {
|
||||
int (*alloc_inode)(fs_t *fs, inode_t *out);
|
||||
int (*free_inode)(fs_t *fs, inode_t *inode);
|
||||
};
|
||||
|
||||
struct fs_superblock {
|
||||
u64 type;
|
||||
size_t block_size;
|
||||
size_t n_blocks;
|
||||
};
|
||||
|
||||
struct inode_ops {
|
||||
/* for all files */
|
||||
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||
/* for directory files */
|
||||
int (*mkdir)(inode_t *inode, const char *name, dirent_t *out);
|
||||
int (*creat)(inode_t *inode, const char *name, dirent_t *out);
|
||||
int (*link)(inode_t *inode, const char *name, dirent_t *out);
|
||||
int (*unlink)(inode_t *inode, const char *name);
|
||||
int (*lookup)(inode_t *parent, const char *name, dirent_t *out);
|
||||
};
|
||||
|
||||
struct _fs {
|
||||
struct fs_superblock superblock;
|
||||
struct _inode *root_inode;
|
||||
u64 fs_type;
|
||||
spinlock_t lock;
|
||||
struct fs_ops fs_ops;
|
||||
};
|
||||
|
||||
struct _dirent {
|
||||
inode_t *inode;
|
||||
size_t rec_len;
|
||||
size_t name_len;
|
||||
size_t flags;
|
||||
char *name;
|
||||
};
|
||||
|
||||
struct _inode {
|
||||
u64 ino_num;
|
||||
list_t dentries;
|
||||
void *data;
|
||||
u64 refcount;
|
||||
rwlock_t rwlock;
|
||||
struct fs *fs;
|
||||
struct _inode *parent_inode;
|
||||
struct inode_ops i_ops;
|
||||
};
|
||||
|
||||
void vfs_init();
|
||||
inode_t *inode_create();
|
||||
void inode_free(inode_t *inode);
|
||||
|
||||
#endif /* _VFS_H_ */
|
||||
|
|
@ -4,8 +4,9 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
void *buddy_alloc(size_t size);
|
||||
void buddy_free(void *ptr);
|
||||
ssize_t buddy_order(size_t len);
|
||||
void *buddy_alloc(size_t order);
|
||||
void buddy_free(void *ptr, size_t order);
|
||||
void buddy_init();
|
||||
|
||||
#endif /* _BUDDY_H_ */
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define DEV_CLASS_TTY 1
|
||||
|
||||
typedef u64 dev_t;
|
||||
typedef u64 dev_class_t;
|
||||
|
|
|
|||
|
|
@ -31,5 +31,7 @@ size_t mem_get_zones(struct mem_zone **ret);
|
|||
void mem_init();
|
||||
|
||||
extern vaddr_t hhdm_start;
|
||||
extern vaddr_t hhdm_end;
|
||||
|
||||
|
||||
#endif /* _MEM_H_ */
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include <kernel/types.h>
|
||||
|
||||
#include <lib/rbtree.h>
|
||||
|
||||
#define PAGE_FLAGS_EMPTY(flags) (!((flags) & PAGE_PRESENT))
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -19,7 +21,20 @@ typedef struct {
|
|||
u64 pte;
|
||||
} pte_t; /* page table entries */
|
||||
|
||||
void page_init();
|
||||
struct page {
|
||||
uint8_t order;
|
||||
uint32_t flags;
|
||||
struct page *next;
|
||||
};
|
||||
|
||||
vaddr_t page_lowest_free(vaddr_t after, size_t len);
|
||||
void *map_mmio_dev(paddr_t base, size_t len);
|
||||
void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree);
|
||||
pgd_t *pgd_get();
|
||||
void pgd_load();
|
||||
void tlb_flush();
|
||||
void page_init();
|
||||
|
||||
extern pgd_t *kpgd;
|
||||
|
||||
#endif /* _PAGE_H_ */
|
||||
|
|
|
|||
|
|
@ -5,21 +5,30 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
#define ALLOC_KERN 0x1
|
||||
#define ALLOC_USER 0x2
|
||||
|
||||
typedef struct _mem_cache_t {
|
||||
char *name;
|
||||
size_t item_size;
|
||||
size_t cache_size;
|
||||
size_t offset;
|
||||
size_t alignment; /* must be a power of 2 */
|
||||
size_t num_members;
|
||||
size_t num_free;
|
||||
vaddr_t next_free;
|
||||
spinlock_t lock;
|
||||
size_t alignment; /* must be a power of 2 */
|
||||
size_t num_members;
|
||||
size_t num_free;
|
||||
vaddr_t next_free;
|
||||
spinlock_t lock;
|
||||
struct _mem_cache_t *next;
|
||||
} mem_cache_t;
|
||||
|
||||
mem_cache_t *slab_create(size_t item_size, size_t cache_size, size_t alignment);
|
||||
mem_cache_t *slab_create(const char *name, size_t item_size, size_t cache_size, size_t alignment);
|
||||
void *slab_alloc(mem_cache_t *cache);
|
||||
void slab_free(mem_cache_t *cache, void *ptr);
|
||||
|
||||
#endif /* _SLAB_H_ */
|
||||
void *kalloc(size_t size, u64 flags);
|
||||
void *kzalloc(size_t size, u64 flags);
|
||||
void kfree(void *ptr);
|
||||
|
||||
void kalloc_init();
|
||||
|
||||
#endif /* _SLAB_H_ */
|
||||
|
|
|
|||
|
|
@ -27,4 +27,7 @@ typedef int16_t i16;
|
|||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
typedef u64 off_t;
|
||||
typedef u64 user_t;
|
||||
|
||||
#endif /* _TYPES_H_ */
|
||||
|
|
|
|||
26
include/lib/list.h
Normal file
26
include/lib/list.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _LIST_H_
|
||||
#define _LIST_H_
|
||||
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
typedef struct _list_item {
|
||||
void *ptr;
|
||||
struct _list_head *list;
|
||||
struct _list_item *next;
|
||||
struct _list_item *prev;
|
||||
} list_item_t;
|
||||
|
||||
typedef struct _list_head {
|
||||
struct _list_item *head;
|
||||
struct _list_item *tail;
|
||||
spinlock_t lock;
|
||||
} list_t;
|
||||
|
||||
|
||||
void list_create(list_t *list);
|
||||
list_item_t *list_insert(list_t *list, void *item);
|
||||
void list_remove(list_item_t *item);
|
||||
void list_init();
|
||||
|
||||
#endif /* _LIST_H_ */
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _RBTREE_H_
|
||||
#define _RBTREE_H_
|
||||
#ifndef _rbtree_tH_
|
||||
#define _rbtree_tH_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/spinlock.h>
|
||||
|
|
@ -8,33 +8,33 @@
|
|||
#define RB_RED 0
|
||||
#define RB_BLACK 1
|
||||
|
||||
typedef struct _rbnode {
|
||||
struct _rbnode *parent;
|
||||
struct _rbnode *left;
|
||||
struct _rbnode *right;
|
||||
typedef struct _rbnode_t {
|
||||
struct _rbnode_t *parent;
|
||||
struct _rbnode_t *left;
|
||||
struct _rbnode_t *right;
|
||||
int color;
|
||||
|
||||
u64 key;
|
||||
u64 value;
|
||||
} rbnode;
|
||||
} rbnode_t;
|
||||
|
||||
/* we use this rbtree structure because the root node will potentially be
|
||||
/* we use this rbtree_tstructure because the root node will potentially be
|
||||
* changed when we insert or delete a node
|
||||
*/
|
||||
typedef struct _rbtree {
|
||||
rbnode *root;
|
||||
typedef struct _rbtree_t{
|
||||
rbnode_t *root;
|
||||
spinlock_t lock;
|
||||
size_t num_nodes;
|
||||
} rbtree;
|
||||
} rbtree_t;
|
||||
|
||||
rbnode *rbt_insert(rbtree *tree, u64 key);
|
||||
void rbt_delete(rbtree *tree, rbnode *del);
|
||||
void rbt_destroy(rbtree *tree);
|
||||
rbnode *rbt_search(rbtree *tree, u64 key);
|
||||
rbnode *rbt_successor(rbnode *node);
|
||||
u64 rbt_next_key(rbtree *tree);
|
||||
rbnode *rbt_minimum(rbnode *node);
|
||||
rbnode_t *rbt_insert(rbtree_t*tree, u64 key);
|
||||
void rbt_delete(rbtree_t*tree, rbnode_t *del);
|
||||
void rbt_destroy(rbtree_t*tree);
|
||||
rbnode_t *rbt_search(rbtree_t*tree, u64 key);
|
||||
rbnode_t *rbt_successor(rbnode_t *node);
|
||||
u64 rbt_next_key(rbtree_t*tree);
|
||||
rbnode_t *rbt_minimum(rbnode_t *node);
|
||||
|
||||
void rbt_slab_init();
|
||||
void rbt_init();
|
||||
|
||||
#endif /* _RBTREE_H_ */
|
||||
#endif /* _rbtree_tH_ */
|
||||
|
|
|
|||
19
include/lib/rwlock.h
Normal file
19
include/lib/rwlock.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _RWLOCK_H_
|
||||
#define _RWLOCK_H_
|
||||
|
||||
#include <lib/sem.h>
|
||||
|
||||
typedef struct _rwlock {
|
||||
sem_t sem;
|
||||
spinlock_t reader_lock;
|
||||
u64 reader_count;
|
||||
} rwlock_t;
|
||||
|
||||
void read_lock(rwlock_t *lock);
|
||||
void read_unlock(rwlock_t *lock);
|
||||
void write_lock(rwlock_t *lock);
|
||||
void write_unlock(rwlock_t *lock);
|
||||
void rwlock_init(rwlock_t *lock);
|
||||
|
||||
#endif /* _RWLOCK_H_ */
|
||||
17
include/lib/sem.h
Normal file
17
include/lib/sem.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _SEM_H_
|
||||
#define _SEM_H_
|
||||
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
typedef struct _sem_t {
|
||||
u64 sem;
|
||||
spinlock_t lock;
|
||||
} sem_t;
|
||||
|
||||
bool sem_trywait(sem_t *sem);
|
||||
void sem_wait(sem_t *sem);
|
||||
void sem_post(sem_t *sem);
|
||||
void sem_init(sem_t *sem, size_t n_res);
|
||||
|
||||
#endif /* _SEM_H_ */
|
||||
Loading…
Reference in a new issue