Compare commits
1 Commits
feature/mo
...
feature/st
| Author | SHA1 | Date | |
|---|---|---|---|
| f037adcebd |
@@ -1,6 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.15)
|
cmake_minimum_required(VERSION 3.15)
|
||||||
project(new_kernel C ASM)
|
project(new_kernel C ASM)
|
||||||
set(CMAKE_C_STANDARD 99)
|
|
||||||
|
|
||||||
# Define compiler run time
|
# Define compiler run time
|
||||||
SET(COMPILER_RT ${CMAKE_CURRENT_LIST_DIR}/compiler/target/bin)
|
SET(COMPILER_RT ${CMAKE_CURRENT_LIST_DIR}/compiler/target/bin)
|
||||||
@@ -8,54 +7,30 @@ SET(CMAKE_C_COMPILER ${COMPILER_RT}/i686-elf-gcc)
|
|||||||
SET(CMAKE_ASM_COMPILER ${COMPILER_RT}/i686-elf-gcc)
|
SET(CMAKE_ASM_COMPILER ${COMPILER_RT}/i686-elf-gcc)
|
||||||
|
|
||||||
# Optionally enable cmake debugging
|
# Optionally enable cmake debugging
|
||||||
#SET(CMAKE_VERBOSE_MAKEFILE ON)
|
SET(CMAKE_VERBOSE_MAKEFILE ON)
|
||||||
|
|
||||||
# Set compile flags
|
# Set compile flags
|
||||||
SET(CMAKE_C_FLAGS "-g -ffreestanding -Wall -Wextra -fno-exceptions -fstack-protector -fno-pie -m32")
|
SET(CMAKE_C_FLAGS "-g -ffreestanding -Wall -Wextra -fno-exceptions -fstack-protector -fno-pie -m32")
|
||||||
SET(CMAKE_ASM_FLAGS "${CFLAGS} -m32 -x assembler-with-cpp")
|
SET(CMAKE_ASM_FLAGS "${CFLAGS} -m32 -x assembler-with-cpp")
|
||||||
SET(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -nostdlib -lgcc")
|
SET(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -nostdlib -lgcc")
|
||||||
|
|
||||||
# Include directory
|
include_directories(AFTER include lai/include)
|
||||||
include_directories(AFTER include)
|
|
||||||
|
|
||||||
################
|
set(CMAKE_C_STANDARD 99)
|
||||||
# Features #
|
|
||||||
################
|
|
||||||
|
|
||||||
set(ENABLE_ACPI ON CACHE BOOL "Enable ACPI Suppport")
|
FILE(GLOB lai lai/core/*.c lai/helpers/*.c lai/driver/*.c)
|
||||||
|
|
||||||
####################
|
|
||||||
# ACPI Feature #
|
|
||||||
####################
|
|
||||||
if (ENABLE_ACPI)
|
|
||||||
include_directories(AFTER lai/include)
|
|
||||||
FILE(GLOB lai lai/core/*.c lai/helpers/*.c lai/driver/*.c)
|
|
||||||
ELSE()
|
|
||||||
set(lai)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
# Other sourcees
|
|
||||||
FILE(GLOB_RECURSE kernel_src kernel/**.c)
|
FILE(GLOB_RECURSE kernel_src kernel/**.c)
|
||||||
FILE(GLOB_RECURSE kernel_asm kernel/**.S)
|
FILE(GLOB_RECURSE kernel_asm kernel/**.S)
|
||||||
FILE(GLOB_RECURSE boot_asm boot/boot.S)
|
FILE(GLOB_RECURSE boot_asm boot/boot.S)
|
||||||
|
|
||||||
add_compile_definitions(__kernel__)
|
add_compile_definitions(__kernel__)
|
||||||
|
|
||||||
#######################
|
|
||||||
# Kernel Features #
|
|
||||||
#######################
|
|
||||||
|
|
||||||
# ACPI
|
|
||||||
if (ENABLE_ACPI)
|
|
||||||
add_compile_definitions(ENABLE_ACPI)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
# Run IDE in DMA mode if available (NYI)
|
# Run IDE in DMA mode if available (NYI)
|
||||||
#add_compile_definitions(IDE_ENABLE_INTERRUPT)
|
#add_compile_definitions(IDE_ENABLE_INTERRUPT)
|
||||||
# Kernel Command Line features
|
# Run the kernel shell as the main task
|
||||||
add_compile_definitions(ENABLE_K_SHELL) # Run the kernel shell as the main task
|
add_compile_definitions(ENABLE_SELF_TEST) # Support for pretty printing pci class/subclass/interface
|
||||||
add_compile_definitions(ENABLE_SELF_TEST) # Compile in self test kernel CLI command
|
|
||||||
add_compile_definitions(ENABLE_PCIPP) # Support for pretty printing pci class/subclass/interface
|
add_compile_definitions(ENABLE_PCIPP) # Support for pretty printing pci class/subclass/interface
|
||||||
|
add_compile_definitions(K_SHELL)
|
||||||
|
|
||||||
# find libgcc.a
|
# find libgcc.a
|
||||||
add_library(libgcc STATIC IMPORTED)
|
add_library(libgcc STATIC IMPORTED)
|
||||||
|
|||||||
21
include/endian.h
Normal file
21
include/endian.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Created by rick on 14-5-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef NEW_KERNEL_ENDIAN_H
|
||||||
|
#define NEW_KERNEL_ENDIAN_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
#define leu16_to_native(x) x
|
||||||
|
#define leu32_to_native(x) x
|
||||||
|
#else
|
||||||
|
#define leu16_to_native(x) ((x) >> 8 | (x) << 8)
|
||||||
|
#define leu32_to_native(x) (((x)>>24)&0xff) | \
|
||||||
|
(((x)<<8)&0xff0000) | \
|
||||||
|
(((x)>>8)&0xff00) | \
|
||||||
|
(((x)<<24)&0xff000000)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif //NEW_KERNEL_ENDIAN_H
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
#define PCI_CLASS_BRIDGE 0x06
|
#define PCI_CLASS_BRIDGE 0x06
|
||||||
|
|
||||||
// class MASS STORAGE 0x01
|
// class MASS STORAGE 0x01
|
||||||
#define PCI_SUB_CLASS_IDE 0x01
|
#define PCI_SUB_CLASS_MASS_SCSI 0x00
|
||||||
|
#define PCI_SUB_CLASS_MASS_IDE 0x01
|
||||||
|
|
||||||
// class BRIDGE 0x06
|
// class BRIDGE 0x06
|
||||||
#define PCI_SUB_CLASS_PCI_PCI_BRIDGE_4 0x04
|
#define PCI_SUB_CLASS_PCI_PCI_BRIDGE_4 0x04
|
||||||
|
|||||||
18
include/myke/drivers/pci/virtio.h
Normal file
18
include/myke/drivers/pci/virtio.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by rick on 14-5-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef NEW_KERNEL_VIRTIO_H
|
||||||
|
#define NEW_KERNEL_VIRTIO_H
|
||||||
|
|
||||||
|
#define PCI_VENDOR_VIRTIO 0x1af4
|
||||||
|
|
||||||
|
#define PCI_DEVICE_VIRTIO_NETWORK 0x1000
|
||||||
|
#define PCI_DEVICE_VIRTIO_BLOCK 0x1001
|
||||||
|
#define PCI_DEVICE_VIRTIO_BALLOON 0x1002
|
||||||
|
#define PCI_DEVICE_VIRTIO_CONSOLE 0x1003
|
||||||
|
#define PCI_DEVICE_VIRTIO_SCSI 0x1004
|
||||||
|
#define PCI_DEVICE_VIRTIO_ENTROPY 0x1005
|
||||||
|
#define PCI_DEVICE_VIRTIO_9P 0x1009
|
||||||
|
|
||||||
|
#endif //NEW_KERNEL_VIRTIO_H
|
||||||
103
include/virtio_queue.h
Normal file
103
include/virtio_queue.h
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#ifndef VIRTQUEUE_H
|
||||||
|
#define VIRTQUEUE_H
|
||||||
|
/*
|
||||||
|
* Virtual I/O Device (VIRTIO) Version 1.2
|
||||||
|
* Committee Specification Draft 01
|
||||||
|
* 09 May 2022
|
||||||
|
* Copyright (c) OASIS Open 2022. All Rights Reserved.
|
||||||
|
* Source: http://docs.oasis-open.org/virtio/virtio/v1.2/csd01/listings/
|
||||||
|
* Latest stage of narrative specification: http://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html
|
||||||
|
* TC IPR Statement: https://github.com/oasis-tcs/virtio-admin/blob/master/IPR.md
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* An interface for efficient virtio implementation.
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* This marks a buffer as continuing via the next field. */
|
||||||
|
#define VIRTQ_DESC_F_NEXT 1
|
||||||
|
/* This marks a buffer as write-only (otherwise read-only). */
|
||||||
|
#define VIRTQ_DESC_F_WRITE 2
|
||||||
|
/* This means the buffer contains a list of buffer descriptors. */
|
||||||
|
#define VIRTQ_DESC_F_INDIRECT 4
|
||||||
|
|
||||||
|
/* The device uses this in used->flags to advise the driver: don't kick me
|
||||||
|
* when you add a buffer. It's unreliable, so it's simply an
|
||||||
|
* optimization. */
|
||||||
|
#define VIRTQ_USED_F_NO_NOTIFY 1
|
||||||
|
/* The driver uses this in avail->flags to advise the device: don't
|
||||||
|
* interrupt me when you consume a buffer. It's unreliable, so it's
|
||||||
|
* simply an optimization. */
|
||||||
|
#define VIRTQ_AVAIL_F_NO_INTERRUPT 1
|
||||||
|
|
||||||
|
/* Support for indirect descriptors */
|
||||||
|
#define VIRTIO_F_INDIRECT_DESC 28
|
||||||
|
|
||||||
|
/* Support for avail_event and used_event fields */
|
||||||
|
#define VIRTIO_F_EVENT_IDX 29
|
||||||
|
|
||||||
|
/* Arbitrary descriptor layouts. */
|
||||||
|
#define VIRTIO_F_ANY_LAYOUT 27
|
||||||
|
|
||||||
|
/* Virtqueue descriptors: 16 bytes.
|
||||||
|
* These can chain together via "next". */
|
||||||
|
struct virtq_desc {
|
||||||
|
/* Address (guest-physical). */
|
||||||
|
le64 addr;
|
||||||
|
/* Length. */
|
||||||
|
le32 len;
|
||||||
|
/* The flags as indicated above. */
|
||||||
|
le16 flags;
|
||||||
|
/* We chain unused descriptors via this, too */
|
||||||
|
le16 next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct virtq_avail {
|
||||||
|
le16 flags;
|
||||||
|
le16 idx;
|
||||||
|
le16 ring[];
|
||||||
|
/* Only if VIRTIO_F_EVENT_IDX: le16 used_event; */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* le32 is used here for ids for padding reasons. */
|
||||||
|
struct virtq_used_elem {
|
||||||
|
/* Index of start of used descriptor chain. */
|
||||||
|
le32 id;
|
||||||
|
/* Total length of the descriptor chain which was written to. */
|
||||||
|
le32 len;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct virtq_used {
|
||||||
|
le16 flags;
|
||||||
|
le16 idx;
|
||||||
|
struct virtq_used_elem ring[];
|
||||||
|
/* Only if VIRTIO_F_EVENT_IDX: le16 avail_event; */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct virtq {
|
||||||
|
unsigned int num;
|
||||||
|
|
||||||
|
struct virtq_desc *desc;
|
||||||
|
struct virtq_avail *avail;
|
||||||
|
struct virtq_used *used;
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline int virtq_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old_idx)
|
||||||
|
{
|
||||||
|
return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get location of event indices (only with VIRTIO_F_EVENT_IDX) */
|
||||||
|
static inline le16 *virtq_used_event(struct virtq *vq)
|
||||||
|
{
|
||||||
|
/* For backwards compat, used event index is at *end* of avail ring. */
|
||||||
|
return &vq->avail->ring[vq->num];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline le16 *virtq_avail_event(struct virtq *vq)
|
||||||
|
{
|
||||||
|
/* For backwards compat, avail event index is at *end* of used ring. */
|
||||||
|
return (le16 *)&vq->used->ring[vq->num];
|
||||||
|
}
|
||||||
|
#endif /* VIRTQUEUE_H */
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
//
|
//
|
||||||
// Created by rick on 23-02-21.
|
// Created by rick on 23-02-21.
|
||||||
//
|
//
|
||||||
#ifdef ENABLE_K_SHELL
|
|
||||||
#include <attributes.h>
|
#include <attributes.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -293,6 +292,7 @@ void att_noreturn main_loop(void *data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef K_SHELL
|
||||||
|
|
||||||
void main_loop_start() {
|
void main_loop_start() {
|
||||||
task_spawn(main_loop, NULL, "main");
|
task_spawn(main_loop, NULL, "main");
|
||||||
|
|||||||
@@ -421,7 +421,7 @@ void ide_register_block_devices() {
|
|||||||
|
|
||||||
uint8_t att_used ide_pci_validate(const pci_device *device) {
|
uint8_t att_used ide_pci_validate(const pci_device *device) {
|
||||||
if (device->class != PCI_CLASS_MASS_STORAGE
|
if (device->class != PCI_CLASS_MASS_STORAGE
|
||||||
|| device->subclass != PCI_SUB_CLASS_IDE
|
|| device->subclass != PCI_SUB_CLASS_MASS_IDE
|
||||||
|| (device->programInterface != 0x8A && device->programInterface != 0x80)) {
|
|| (device->programInterface != 0x8A && device->programInterface != 0x80)) {
|
||||||
return PCI_VALIDATE_FAIL;
|
return PCI_VALIDATE_FAIL;
|
||||||
}
|
}
|
||||||
@@ -686,7 +686,7 @@ PCI_DRIVER(900) = {
|
|||||||
.validatable = true,
|
.validatable = true,
|
||||||
.initialisable = true,
|
.initialisable = true,
|
||||||
.match.class = PCI_CLASS_MASS_STORAGE,
|
.match.class = PCI_CLASS_MASS_STORAGE,
|
||||||
.match.subclass = PCI_SUB_CLASS_IDE,
|
.match.subclass = PCI_SUB_CLASS_MASS_IDE,
|
||||||
.mask.class = true,
|
.mask.class = true,
|
||||||
.mask.subclass = true,
|
.mask.subclass = true,
|
||||||
.validate = ide_pci_validate,
|
.validate = ide_pci_validate,
|
||||||
|
|||||||
46
kernel/drivers/pci/virtio/virtio-lock.c
Normal file
46
kernel/drivers/pci/virtio/virtio-lock.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
//
|
||||||
|
// Created by rick on 14-5-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <endian.h>
|
||||||
|
|
||||||
|
#include <myke/drivers/pci/pci.h>
|
||||||
|
#include <myke/drivers/pci/virtio.h>
|
||||||
|
|
||||||
|
uint8_t att_used virtio_pci_block_validate(const pci_device *pci_device) {
|
||||||
|
uint32_t capacity_l = pci_config_read_double_word(pci_device->bus, pci_device->slot, pci_device->func, 0x14);
|
||||||
|
uint32_t capacity_h = pci_config_read_double_word(pci_device->bus, pci_device->slot, pci_device->func, 0x18);
|
||||||
|
uint32_t segment_size_max = pci_config_read_double_word(pci_device->bus, pci_device->slot, pci_device->func, 0x1c);
|
||||||
|
uint32_t segment_count_max = pci_config_read_double_word(pci_device->bus, pci_device->slot, pci_device->func, 0x20);
|
||||||
|
uint16_t cylinder_count = pci_config_read_word(pci_device->bus, pci_device->slot, pci_device->func, 0x24);
|
||||||
|
uint8_t head_count = pci_config_read_byte(pci_device->bus, pci_device->slot, pci_device->func, 0x26);
|
||||||
|
uint8_t sector_count = pci_config_read_byte(pci_device->bus, pci_device->slot, pci_device->func, 0x27);
|
||||||
|
uint32_t block_length = pci_config_read_double_word(pci_device->bus, pci_device->slot, pci_device->func, 0x28);
|
||||||
|
|
||||||
|
capacity_l = leu32_to_native(capacity_l);
|
||||||
|
capacity_h = leu32_to_native(capacity_h);
|
||||||
|
uint64_t capacity = ((uint64_t) capacity_h) >> 32 | capacity_l;
|
||||||
|
|
||||||
|
return PCI_VALIDATE_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t att_used virtio_pci_block_initialize(pci_device *pci_device) {
|
||||||
|
return PCI_INIT_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
PCI_DRIVER(900) = {
|
||||||
|
.name = "pci-virtio-block",
|
||||||
|
.description = "Para-virtualized Block Device",
|
||||||
|
.validatable = true,
|
||||||
|
.initialisable = true,
|
||||||
|
.match.class = PCI_CLASS_MASS_STORAGE,
|
||||||
|
.match.subclass = PCI_SUB_CLASS_MASS_SCSI,
|
||||||
|
.match.vendor = PCI_VENDOR_VIRTIO,
|
||||||
|
.match.device = PCI_DEVICE_VIRTIO_BLOCK,
|
||||||
|
.mask.class = true,
|
||||||
|
.mask.subclass = true,
|
||||||
|
.mask.vendor = true,
|
||||||
|
.mask.device = true,
|
||||||
|
.validate = virtio_pci_block_validate,
|
||||||
|
.initialize = virtio_pci_block_initialize,
|
||||||
|
};
|
||||||
@@ -37,6 +37,10 @@ while [[ "$#" -gt 0 ]]; do
|
|||||||
ARGS="${ARGS} -drive id=disk1,file=$2,if=none -device ahci,id=ahci -device ide-drive,drive=disk1,bus=ahci.0"
|
ARGS="${ARGS} -drive id=disk1,file=$2,if=none -device ahci,id=ahci -device ide-drive,drive=disk1,bus=ahci.0"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
|
--virtio)
|
||||||
|
ARGS="$ARGS -drive id=disk1,file=$2,if=virtio"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
-m | --max-cpu)
|
-m | --max-cpu)
|
||||||
ARGS="${ARGS} -cpu max"
|
ARGS="${ARGS} -cpu max"
|
||||||
shift
|
shift
|
||||||
|
|||||||
Reference in New Issue
Block a user