Compare commits
190 Commits
647d6a273d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c8112f1a7 | |||
| 49662aa994 | |||
| bcf1c6d110 | |||
| b62687f5e7 | |||
| 7b4b5a3440 | |||
| 896bb05073 | |||
| fc10de7ccb | |||
| e8a490336c | |||
| e741ab0fa1 | |||
| 7d7b1f0319 | |||
| 97d26b5d4e | |||
| e78fcf63b6 | |||
| bfad22211f | |||
| 6e2542d896 | |||
| 659bc50aec | |||
| f6c5e3e640 | |||
| 8957408890 | |||
| db8544065e | |||
| eee0b7c3ff | |||
| ea4e016b1e | |||
| 9fd015a84e | |||
| 090ab99903 | |||
| 3e16e69602 | |||
| 58c58bcccf | |||
| f173b7bccc | |||
| fde0eb0964 | |||
| 05ee0a7f49 | |||
| a863b2ca13 | |||
| 15f6158cea | |||
| c7eb2dbdf3 | |||
| 380b172968 | |||
| ae0e79aa01 | |||
| 01f0871a83 | |||
| ac1fafedcc | |||
| 7a696c4d63 | |||
| 89d3ee7b92 | |||
| 879783052d | |||
| b480f783ec | |||
| 2957206718 | |||
| a41a3eeb66 | |||
| 6aa2329267 | |||
| 5256fd2e0a | |||
| 652e170da9 | |||
| a2691bd70d | |||
| b2169cbce6 | |||
| c9b3d50523 | |||
| e717b95df0 | |||
| 0d8d731d91 | |||
| 122c325a5b | |||
| d5bc88584d | |||
| 63b2284324 | |||
| 917321eb5f | |||
| 13bf1ba647 | |||
| 0a8b03d349 | |||
| 4473ee4d49 | |||
| ded722fb4c | |||
| facc17c5a1 | |||
| 26abd4e18e | |||
| 50fd526e6f | |||
| c0e091b647 | |||
| 372da006d0 | |||
| 7219eac65f | |||
| 0dc74beb0f | |||
| 7bdd9d19d0 | |||
| 991f7024d5 | |||
| 6d1696b77e | |||
| b19b7f064a | |||
| f449ca8161 | |||
| 5540bc4147 | |||
| d8f136a76f | |||
| 8be90c49bc | |||
| 59c59c3316 | |||
| c5cfa82ffb | |||
| c0d38862f2 | |||
| 74e94eedae | |||
| d241975ce1 | |||
| 20663b533b | |||
| 6f002f0c07 | |||
| f121e80bfd | |||
| 4e28a35af9 | |||
| ddb7641b05 | |||
| a6ad9b9b2e | |||
| c0bd07174d | |||
| 8f4e15b78e | |||
| 17e6e53948 | |||
| 223835c37d | |||
| aedd53b3e0 | |||
| 6339044e4c | |||
| 924576cf0d | |||
| ac333c9677 | |||
| 6e47fa084b | |||
| 32c10f7db2 | |||
| 9919cbf66e | |||
| fc1a6cacdc | |||
| 040bdea08e | |||
| 579cd07109 | |||
| 7bf7de44d2 | |||
| 58aff97c28 | |||
| 2bb9b9b4b3 | |||
| 59ec05c898 | |||
| ac0ef53e87 | |||
| 5aea95129e | |||
| 92d10f612e | |||
| f3f40a465b | |||
| d3130884b6 | |||
| 5a516a6130 | |||
| 689494db63 | |||
| 1287c8e335 | |||
| 763a742f6d | |||
| 4ad586552d | |||
| 8bc93069be | |||
| 160821869d | |||
| aa1b1e1af9 | |||
| e19414a64e | |||
| 98fd86477a | |||
| 16a442f473 | |||
| becfa228fe | |||
| c0ce647c74 | |||
| 05250083b9 | |||
| bb170ba613 | |||
| c79e412215 | |||
| 585e021c7f | |||
| 43d03eb4a9 | |||
| 2a6792b44a | |||
| 954898b14d | |||
| d266d2ca88 | |||
| c72f2f9b31 | |||
| 62e2f4896a | |||
| 9ccdebcd96 | |||
| fe533c2e62 | |||
| ee5c225954 | |||
| 8fccb74542 | |||
| bc8ecbd6fa | |||
| 60484b286f | |||
| 12158d9208 | |||
| 5c94c30e1b | |||
| 81d8ab3d79 | |||
| 6a58c716bd | |||
| c295af9bd5 | |||
| d5ee98708b | |||
| f6679eb4b5 | |||
| ed3924722e | |||
| 4ef03eac97 | |||
| 7ce68b0488 | |||
| 77796dd317 | |||
| f1a72cc9da | |||
| 718379ce3b | |||
| 14aa28b043 | |||
| 6045726e41 | |||
| 45e55d8907 | |||
| e9d6431728 | |||
| aa8be130f9 | |||
| a19e6938eb | |||
| 32206069bc | |||
| 46a1903f8d | |||
| a3ca49ff1f | |||
| 94f92d982c | |||
| 4f5f84bb5b | |||
| 5cb5ae2dfe | |||
| 7704e3c5c0 | |||
| 376e4b4c45 | |||
| 24c37e7381 | |||
| fb9c67ab15 | |||
| d52ad29afa | |||
| 1dc26d3c06 | |||
| a05fcdde8c | |||
| deb2f52a35 | |||
| 8224659c48 | |||
| 1922d78661 | |||
| 6d1ecc2388 | |||
| 5cf658c175 | |||
| ef2738bfb7 | |||
| d7865b2929 | |||
| 9c79971bdc | |||
| ff75c15ba3 | |||
| 9e6fa0a1ba | |||
| 6e95519acc | |||
| d081655913 | |||
| 9c3eb8d270 | |||
| 68479bf07e | |||
| d528314ae3 | |||
| 40dd29b876 | |||
| dc1d7e3fae | |||
| 8f8ba2751c | |||
| 928d3e3fe7 | |||
| 4ef7b8e71c | |||
| 1fcd72e578 | |||
| 5e1b5c329b | |||
| b2f795b1e1 | |||
| 16967cd9c0 |
@@ -98,7 +98,8 @@ namespace BAN
|
|||||||
|
|
||||||
struct EntryHash
|
struct EntryHash
|
||||||
{
|
{
|
||||||
constexpr bool operator()(const Key& a)
|
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
||||||
|
constexpr bool operator()(const U& a)
|
||||||
{
|
{
|
||||||
return HASH()(a);
|
return HASH()(a);
|
||||||
}
|
}
|
||||||
@@ -110,7 +111,8 @@ namespace BAN
|
|||||||
|
|
||||||
struct EntryComp
|
struct EntryComp
|
||||||
{
|
{
|
||||||
constexpr bool operator()(const Entry& a, const Key& b)
|
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
||||||
|
constexpr bool operator()(const Entry& a, const U& b)
|
||||||
{
|
{
|
||||||
return COMP()(a.key, b);
|
return COMP()(a.key, b);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,11 +67,20 @@ namespace BAN::sort
|
|||||||
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
||||||
void quick_sort(It begin, It end, Comp comp = {})
|
void quick_sort(It begin, It end, Comp comp = {})
|
||||||
{
|
{
|
||||||
if (distance(begin, end) <= 1)
|
while (distance(begin, end) > 1)
|
||||||
return;
|
{
|
||||||
const auto [lt, gt] = detail::partition(begin, end, comp);
|
const auto [lt, gt] = detail::partition(begin, end, comp);
|
||||||
quick_sort(begin, lt, comp);
|
if (distance(begin, lt) < distance(gt, end))
|
||||||
quick_sort(gt, end, comp);
|
{
|
||||||
|
quick_sort(begin, lt);
|
||||||
|
begin = gt;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
quick_sort(gt, end);
|
||||||
|
end = lt;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
||||||
@@ -102,13 +111,28 @@ namespace BAN::sort
|
|||||||
template<typename It, typename Comp>
|
template<typename It, typename Comp>
|
||||||
void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp)
|
void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp)
|
||||||
{
|
{
|
||||||
if (distance(begin, end) <= 16)
|
for (size_t i = 0; i < max_depth; i++)
|
||||||
return insertion_sort(begin, end, comp);
|
{
|
||||||
if (max_depth == 0)
|
const size_t len = distance(begin, end);
|
||||||
return heap_sort(begin, end, comp);
|
if (len <= 1)
|
||||||
const auto [lt, gt] = detail::partition(begin, end, comp);
|
return;
|
||||||
intro_sort_impl(begin, lt, max_depth - 1, comp);
|
if (len <= 16)
|
||||||
intro_sort_impl(gt, end, max_depth - 1, comp);
|
return insertion_sort(begin, end, comp);
|
||||||
|
|
||||||
|
const auto [lt, gt] = detail::partition(begin, end, comp);
|
||||||
|
if (distance(begin, lt) < distance(gt, end))
|
||||||
|
{
|
||||||
|
intro_sort_impl(begin, lt, max_depth - 1, comp);
|
||||||
|
begin = gt;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
intro_sort_impl(gt, end, max_depth - 1, comp);
|
||||||
|
end = lt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
heap_sort(begin, end, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,3 +100,22 @@ namespace BAN
|
|||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <BAN/Formatter.h>
|
||||||
|
|
||||||
|
namespace BAN::Formatter
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename F, typename T>
|
||||||
|
void print_argument(F putc, const Span<T>& span, const ValueFormat& format)
|
||||||
|
{
|
||||||
|
putc('[');
|
||||||
|
for (typename Span<T>::size_type i = 0; i < span.size(); i++)
|
||||||
|
{
|
||||||
|
if (i != 0) putc(',');
|
||||||
|
print_argument(putc, span[i], format);
|
||||||
|
}
|
||||||
|
putc(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ To change the bootloader you can set environment variable BANAN\_BOOTLOADER; sup
|
|||||||
|
|
||||||
To run with UEFI set environment variable BANAN\_UEFI\_BOOT=1. You will also have to set OVMF\_PATH to the correct OVMF (default */usr/share/ovmf/x64/OVMF.fd*).
|
To run with UEFI set environment variable BANAN\_UEFI\_BOOT=1. You will also have to set OVMF\_PATH to the correct OVMF (default */usr/share/ovmf/x64/OVMF.fd*).
|
||||||
|
|
||||||
To build an image with no physical root filesystem, but an initrd set environment variable BANAN\_INITRD=1. This can be used when testing on hardware with unsupported USB controller.
|
To build an image with no physical root filesystem, but an initrd set environment variable BANAN\_INITRD=1. This can be used when testing on hardware with unsupported USB controller. If BANAN\_INITRD is set to a value larger than 1, initrd will be gzip compressed.
|
||||||
|
|
||||||
If you have corrupted your disk image or want to create new one, you can either manually delete *build/banan-os.img* and build system will automatically create you a new one or you can run the following command.
|
If you have corrupted your disk image or want to create new one, you can either manually delete *build/banan-os.img* and build system will automatically create you a new one or you can run the following command.
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
Binary file not shown.
@@ -11,6 +11,7 @@ set(KERNEL_SOURCES
|
|||||||
kernel/Audio/Controller.cpp
|
kernel/Audio/Controller.cpp
|
||||||
kernel/Audio/HDAudio/AudioFunctionGroup.cpp
|
kernel/Audio/HDAudio/AudioFunctionGroup.cpp
|
||||||
kernel/Audio/HDAudio/Controller.cpp
|
kernel/Audio/HDAudio/Controller.cpp
|
||||||
|
kernel/Banos.cpp
|
||||||
kernel/BootInfo.cpp
|
kernel/BootInfo.cpp
|
||||||
kernel/CPUID.cpp
|
kernel/CPUID.cpp
|
||||||
kernel/Credentials.cpp
|
kernel/Credentials.cpp
|
||||||
@@ -121,6 +122,7 @@ set(KERNEL_SOURCES
|
|||||||
kernel/USB/USBManager.cpp
|
kernel/USB/USBManager.cpp
|
||||||
kernel/USB/XHCI/Controller.cpp
|
kernel/USB/XHCI/Controller.cpp
|
||||||
kernel/USB/XHCI/Device.cpp
|
kernel/USB/XHCI/Device.cpp
|
||||||
|
kernel/UserCopy.cpp
|
||||||
icxxabi.cpp
|
icxxabi.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ namespace Kernel
|
|||||||
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
|
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
|
||||||
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
|
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
|
||||||
|
|
||||||
static bool s_is_initialized = false;
|
|
||||||
|
|
||||||
static PageTable* s_kernel = nullptr;
|
static PageTable* s_kernel = nullptr;
|
||||||
static bool s_has_nxe = false;
|
static bool s_has_nxe = false;
|
||||||
static bool s_has_pge = false;
|
static bool s_has_pge = false;
|
||||||
@@ -260,7 +258,11 @@ namespace Kernel
|
|||||||
|
|
||||||
ASSERT(index < 512);
|
ASSERT(index < 512);
|
||||||
ASSERT(s_fast_page_pt);
|
ASSERT(s_fast_page_pt);
|
||||||
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
|
||||||
|
if (index < reserved_fast_pages)
|
||||||
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
|
else
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Disabled);
|
||||||
|
|
||||||
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
|
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
|
||||||
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
|
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
|
||||||
@@ -274,7 +276,11 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
ASSERT(index < 512);
|
ASSERT(index < 512);
|
||||||
ASSERT(s_fast_page_pt);
|
ASSERT(s_fast_page_pt);
|
||||||
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
|
||||||
|
if (index < reserved_fast_pages)
|
||||||
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
|
else
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Disabled);
|
||||||
|
|
||||||
ASSERT((s_fast_page_pt[index] & Flags::Present));
|
ASSERT((s_fast_page_pt[index] & Flags::Present));
|
||||||
s_fast_page_pt[index] = 0;
|
s_fast_page_pt[index] = 0;
|
||||||
@@ -341,32 +347,10 @@ namespace Kernel
|
|||||||
const bool is_userspace = (vaddr < KERNEL_OFFSET);
|
const bool is_userspace = (vaddr < KERNEL_OFFSET);
|
||||||
if (is_userspace && this != &PageTable::current())
|
if (is_userspace && this != &PageTable::current())
|
||||||
;
|
;
|
||||||
else if (pages <= 32 || !s_is_initialized)
|
else if (pages >= full_tlb_flush_threshold)
|
||||||
{
|
invalidate_full_address_space(!is_userspace);
|
||||||
for (size_t i = 0; i < pages; i++)
|
else for (size_t i = 0; i < pages; i++)
|
||||||
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
|
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
|
||||||
}
|
|
||||||
else if (is_userspace || !s_has_pge)
|
|
||||||
{
|
|
||||||
asm volatile("movl %0, %%cr3" :: "r"(static_cast<uint32_t>(m_highest_paging_struct)));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
asm volatile(
|
|
||||||
"movl %%cr4, %%eax;"
|
|
||||||
|
|
||||||
"andl $~0x80, %%eax;"
|
|
||||||
"movl %%eax, %%cr4;"
|
|
||||||
|
|
||||||
"movl %0, %%cr3;"
|
|
||||||
|
|
||||||
"orl $0x80, %%eax;"
|
|
||||||
"movl %%eax, %%cr4;"
|
|
||||||
:
|
|
||||||
: "r"(static_cast<uint32_t>(m_highest_paging_struct))
|
|
||||||
: "eax"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (send_smp_message)
|
if (send_smp_message)
|
||||||
{
|
{
|
||||||
@@ -381,6 +365,34 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PageTable::invalidate_full_address_space(bool global)
|
||||||
|
{
|
||||||
|
if (!global || !s_has_pge)
|
||||||
|
{
|
||||||
|
asm volatile(
|
||||||
|
"movl %%cr3, %%eax;"
|
||||||
|
"movl %%eax, %%cr3;"
|
||||||
|
::: "eax"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asm volatile(
|
||||||
|
"movl %%cr4, %%eax;"
|
||||||
|
|
||||||
|
"andl $~0x80, %%eax;"
|
||||||
|
"movl %%eax, %%cr4;"
|
||||||
|
|
||||||
|
"movl %%cr3, %%ecx;"
|
||||||
|
"movl %%ecx, %%cr3;"
|
||||||
|
|
||||||
|
"orl $0x80, %%eax;"
|
||||||
|
"movl %%eax, %%cr4;"
|
||||||
|
::: "eax", "ecx"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PageTable::unmap_page(vaddr_t vaddr, bool invalidate)
|
void PageTable::unmap_page(vaddr_t vaddr, bool invalidate)
|
||||||
{
|
{
|
||||||
ASSERT(vaddr);
|
ASSERT(vaddr);
|
||||||
@@ -597,30 +609,24 @@ namespace Kernel
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::reserve_page(vaddr_t vaddr, bool only_free, bool send_smp_message)
|
void PageTable::reserve_page(vaddr_t vaddr)
|
||||||
{
|
{
|
||||||
SpinLockGuard _(m_lock);
|
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
if (only_free && !is_page_free(vaddr))
|
SpinLockGuard _(m_lock);
|
||||||
return false;
|
ASSERT(is_page_free(vaddr));
|
||||||
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, send_smp_message);
|
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, false);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::reserve_range(vaddr_t vaddr, size_t bytes, bool only_free)
|
void PageTable::reserve_range(vaddr_t vaddr, size_t bytes)
|
||||||
{
|
{
|
||||||
if (size_t rem = bytes % PAGE_SIZE)
|
if (size_t rem = bytes % PAGE_SIZE)
|
||||||
bytes += PAGE_SIZE - rem;
|
bytes += PAGE_SIZE - rem;
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
if (only_free && !is_range_free(vaddr, bytes))
|
ASSERT(is_range_free(vaddr, bytes));
|
||||||
return false;
|
|
||||||
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
|
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
|
||||||
reserve_page(vaddr + offset, true, false);
|
reserve_page(vaddr + offset);
|
||||||
invalidate_range(vaddr, bytes / PAGE_SIZE, true);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
||||||
@@ -675,7 +681,7 @@ namespace Kernel
|
|||||||
vaddr |= (vaddr_t)pdpte << 30;
|
vaddr |= (vaddr_t)pdpte << 30;
|
||||||
vaddr |= (vaddr_t)pde << 21;
|
vaddr |= (vaddr_t)pde << 21;
|
||||||
vaddr |= (vaddr_t)pte << 12;
|
vaddr |= (vaddr_t)pte << 12;
|
||||||
ASSERT(reserve_page(vaddr));
|
reserve_page(vaddr);
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
unmap_fast_page(2);
|
unmap_fast_page(2);
|
||||||
@@ -693,7 +699,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
if (is_page_free(vaddr))
|
if (is_page_free(vaddr))
|
||||||
{
|
{
|
||||||
ASSERT(reserve_page(vaddr));
|
reserve_page(vaddr);
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -726,7 +732,7 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
if (valid)
|
if (valid)
|
||||||
{
|
{
|
||||||
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
|
reserve_range(vaddr, page_count * PAGE_SIZE);
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ signal_trampoline:
|
|||||||
addl $24, %esp
|
addl $24, %esp
|
||||||
|
|
||||||
// restore sigmask
|
// restore sigmask
|
||||||
movl $83, %eax // SYS_SIGPROCMASK
|
movl $78, %eax // SYS_SIGPROCMASK
|
||||||
movl $3, %ebx // SIG_SETMASK
|
movl $3, %ebx // SIG_SETMASK
|
||||||
leal 72(%esp), %ecx // set
|
leal 72(%esp), %ecx // set
|
||||||
xorl %edx, %edx // oset
|
xorl %edx, %edx // oset
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ asm_yield_trampoline:
|
|||||||
pushl %ebp
|
pushl %ebp
|
||||||
|
|
||||||
pushl %esp
|
pushl %esp
|
||||||
call scheduler_on_yield
|
call scheduler_on_yield_trampoline
|
||||||
addl $4, %esp
|
addl $4, %esp
|
||||||
|
|
||||||
popl %ebp
|
popl %ebp
|
||||||
|
|||||||
@@ -27,14 +27,10 @@
|
|||||||
|
|
||||||
isr_stub:
|
isr_stub:
|
||||||
intr_header 12
|
intr_header 12
|
||||||
movl %cr0, %eax; pushl %eax
|
|
||||||
movl %cr2, %eax; pushl %eax
|
|
||||||
movl %cr3, %eax; pushl %eax
|
|
||||||
movl %cr4, %eax; pushl %eax
|
|
||||||
|
|
||||||
movl 48(%esp), %edi // isr number
|
movl 32(%esp), %edi // isr number
|
||||||
movl 52(%esp), %esi // error code
|
movl 36(%esp), %esi // error code
|
||||||
leal 56(%esp), %edx // interrupt stack ptr
|
leal 40(%esp), %edx // interrupt stack ptr
|
||||||
movl %esp, %ecx // register ptr
|
movl %esp, %ecx // register ptr
|
||||||
|
|
||||||
# stack frame for stack trace
|
# stack frame for stack trace
|
||||||
@@ -52,7 +48,7 @@ isr_stub:
|
|||||||
call cpp_isr_handler
|
call cpp_isr_handler
|
||||||
|
|
||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
addl $24, %esp
|
addl $8, %esp
|
||||||
|
|
||||||
intr_footer 12
|
intr_footer 12
|
||||||
addl $8, %esp
|
addl $8, %esp
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ namespace Kernel
|
|||||||
SpinLock PageTable::s_fast_page_lock;
|
SpinLock PageTable::s_fast_page_lock;
|
||||||
|
|
||||||
static constexpr vaddr_t s_hhdm_offset = 0xFFFF800000000000;
|
static constexpr vaddr_t s_hhdm_offset = 0xFFFF800000000000;
|
||||||
static bool s_is_initialized = false;
|
|
||||||
|
|
||||||
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
|
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
|
||||||
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
|
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
|
||||||
@@ -383,7 +382,11 @@ namespace Kernel
|
|||||||
|
|
||||||
ASSERT(index < 512);
|
ASSERT(index < 512);
|
||||||
ASSERT(s_fast_page_pt);
|
ASSERT(s_fast_page_pt);
|
||||||
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
|
||||||
|
if (index < reserved_fast_pages)
|
||||||
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
|
else
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Disabled);
|
||||||
|
|
||||||
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
|
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
|
||||||
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
|
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
|
||||||
@@ -397,7 +400,11 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
ASSERT(index < 512);
|
ASSERT(index < 512);
|
||||||
ASSERT(s_fast_page_pt);
|
ASSERT(s_fast_page_pt);
|
||||||
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
|
||||||
|
if (index < reserved_fast_pages)
|
||||||
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
|
else
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Disabled);
|
||||||
|
|
||||||
ASSERT((s_fast_page_pt[index] & Flags::Present));
|
ASSERT((s_fast_page_pt[index] & Flags::Present));
|
||||||
s_fast_page_pt[index] = 0;
|
s_fast_page_pt[index] = 0;
|
||||||
@@ -470,32 +477,10 @@ namespace Kernel
|
|||||||
const bool is_userspace = (vaddr < KERNEL_OFFSET);
|
const bool is_userspace = (vaddr < KERNEL_OFFSET);
|
||||||
if (is_userspace && this != &PageTable::current())
|
if (is_userspace && this != &PageTable::current())
|
||||||
;
|
;
|
||||||
else if (pages <= 32 || !s_is_initialized)
|
else if (pages >= full_tlb_flush_threshold)
|
||||||
{
|
invalidate_full_address_space(!is_userspace);
|
||||||
for (size_t i = 0; i < pages; i++)
|
else for (size_t i = 0; i < pages; i++)
|
||||||
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
|
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
|
||||||
}
|
|
||||||
else if (is_userspace || !s_has_pge)
|
|
||||||
{
|
|
||||||
asm volatile("movq %0, %%cr3" :: "r"(m_highest_paging_struct));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
asm volatile(
|
|
||||||
"movq %%cr4, %%rax;"
|
|
||||||
|
|
||||||
"andq $~0x80, %%rax;"
|
|
||||||
"movq %%rax, %%cr4;"
|
|
||||||
|
|
||||||
"movq %0, %%cr3;"
|
|
||||||
|
|
||||||
"orq $0x80, %%rax;"
|
|
||||||
"movq %%rax, %%cr4;"
|
|
||||||
:
|
|
||||||
: "r"(m_highest_paging_struct)
|
|
||||||
: "rax"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (send_smp_message)
|
if (send_smp_message)
|
||||||
{
|
{
|
||||||
@@ -510,6 +495,34 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PageTable::invalidate_full_address_space(bool global)
|
||||||
|
{
|
||||||
|
if (!global || !s_has_pge)
|
||||||
|
{
|
||||||
|
asm volatile(
|
||||||
|
"movq %%cr3, %%rax;"
|
||||||
|
"movq %%rax, %%cr3;"
|
||||||
|
::: "rax"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asm volatile(
|
||||||
|
"movq %%cr4, %%rax;"
|
||||||
|
|
||||||
|
"andq $~0x80, %%rax;"
|
||||||
|
"movq %%rax, %%cr4;"
|
||||||
|
|
||||||
|
"movq %%cr3, %%rcx;"
|
||||||
|
"movq %%rcx, %%cr3;"
|
||||||
|
|
||||||
|
"orq $0x80, %%rax;"
|
||||||
|
"movq %%rax, %%cr4;"
|
||||||
|
::: "rax", "rcx"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PageTable::unmap_page(vaddr_t vaddr, bool invalidate)
|
void PageTable::unmap_page(vaddr_t vaddr, bool invalidate)
|
||||||
{
|
{
|
||||||
ASSERT(vaddr);
|
ASSERT(vaddr);
|
||||||
@@ -551,12 +564,70 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
|
||||||
const size_t page_count = range_page_count(vaddr, size);
|
ASSERT(is_canonical(vaddr));
|
||||||
|
ASSERT(is_canonical(vaddr + size - 1));
|
||||||
|
|
||||||
|
const vaddr_t uc_vaddr_start = uncanonicalize(vaddr);
|
||||||
|
const vaddr_t uc_vaddr_end = uncanonicalize(vaddr + size - 1);
|
||||||
|
|
||||||
|
uint16_t pml4e = (uc_vaddr_start >> 39) & 0x1FF;
|
||||||
|
uint16_t pdpte = (uc_vaddr_start >> 30) & 0x1FF;
|
||||||
|
uint16_t pde = (uc_vaddr_start >> 21) & 0x1FF;
|
||||||
|
uint16_t pte = (uc_vaddr_start >> 12) & 0x1FF;
|
||||||
|
|
||||||
|
const uint16_t e_pml4e = (uc_vaddr_end >> 39) & 0x1FF;
|
||||||
|
const uint16_t e_pdpte = (uc_vaddr_end >> 30) & 0x1FF;
|
||||||
|
const uint16_t e_pde = (uc_vaddr_end >> 21) & 0x1FF;
|
||||||
|
const uint16_t e_pte = (uc_vaddr_end >> 12) & 0x1FF;
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
for (vaddr_t page = 0; page < page_count; page++)
|
|
||||||
unmap_page(vaddr + page * PAGE_SIZE, false);
|
uint64_t* pml4 = P2V(m_highest_paging_struct);
|
||||||
invalidate_range(vaddr, page_count, true);
|
for (; pml4e <= e_pml4e; pml4e++)
|
||||||
|
{
|
||||||
|
#define UNALLOCATE_TABLE_IF_EMPTY(outer, inner) \
|
||||||
|
if (old_##inner##e == 0 && inner##e == 512) { \
|
||||||
|
unallocate_page(outer[outer##e] & s_page_addr_mask); \
|
||||||
|
outer[outer##e] = 0; \
|
||||||
|
}
|
||||||
|
if (!(pml4[pml4e] & Flags::Present))
|
||||||
|
continue;
|
||||||
|
const uint16_t old_pdpte = pdpte;
|
||||||
|
uint64_t* pdpt = P2V(pml4[pml4e] & s_page_addr_mask);
|
||||||
|
for (; pdpte < 512; pdpte++)
|
||||||
|
{
|
||||||
|
if (pml4e == e_pml4e && pdpte > e_pdpte)
|
||||||
|
break;
|
||||||
|
if (!(pdpt[pdpte] & Flags::Present))
|
||||||
|
continue;
|
||||||
|
const uint16_t old_pde = pde;
|
||||||
|
uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
|
||||||
|
for (; pde < 512; pde++)
|
||||||
|
{
|
||||||
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde > e_pde)
|
||||||
|
break;
|
||||||
|
if (!(pd[pde] & Flags::Present))
|
||||||
|
continue;
|
||||||
|
const uint16_t old_pte = pte;
|
||||||
|
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
|
||||||
|
for (; pte < 512; pte++)
|
||||||
|
{
|
||||||
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde == e_pde && pte > e_pte)
|
||||||
|
break;
|
||||||
|
pt[pte] = 0;
|
||||||
|
}
|
||||||
|
UNALLOCATE_TABLE_IF_EMPTY(pd, pt);
|
||||||
|
pte = 0;
|
||||||
|
}
|
||||||
|
UNALLOCATE_TABLE_IF_EMPTY(pdpt, pd);
|
||||||
|
pde = 0;
|
||||||
|
}
|
||||||
|
UNALLOCATE_TABLE_IF_EMPTY(pml4, pdpt);
|
||||||
|
pdpte = 0;
|
||||||
|
#undef UNALLOCATE_TABLE_IF_EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidate_range(vaddr, range_page_count(vaddr, size), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags, MemoryType memory_type, bool invalidate)
|
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags, MemoryType memory_type, bool invalidate)
|
||||||
@@ -746,29 +817,71 @@ namespace Kernel
|
|||||||
return get_page_data(addr) & s_page_addr_mask;
|
return get_page_data(addr) & s_page_addr_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::reserve_page(vaddr_t vaddr, bool only_free, bool invalidate)
|
void PageTable::reserve_page(vaddr_t vaddr)
|
||||||
{
|
{
|
||||||
SpinLockGuard _(m_lock);
|
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
if (only_free && !is_page_free(vaddr))
|
SpinLockGuard _(m_lock);
|
||||||
return false;
|
ASSERT(is_page_free(vaddr));
|
||||||
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, invalidate);
|
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, false);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::reserve_range(vaddr_t vaddr, size_t bytes, bool only_free)
|
void PageTable::reserve_range(vaddr_t vaddr, size_t bytes)
|
||||||
{
|
{
|
||||||
if (size_t rem = bytes % PAGE_SIZE)
|
if (size_t rem = bytes % PAGE_SIZE)
|
||||||
bytes += PAGE_SIZE - rem;
|
bytes += PAGE_SIZE - rem;
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
|
||||||
|
ASSERT(is_canonical(vaddr));
|
||||||
|
ASSERT(is_canonical(vaddr + bytes - 1));
|
||||||
|
|
||||||
|
const vaddr_t uc_vaddr_start = uncanonicalize(vaddr);
|
||||||
|
uint16_t pml4e = (uc_vaddr_start >> 39) & 0x1FF;
|
||||||
|
uint16_t pdpte = (uc_vaddr_start >> 30) & 0x1FF;
|
||||||
|
uint16_t pde = (uc_vaddr_start >> 21) & 0x1FF;
|
||||||
|
uint16_t pte = (uc_vaddr_start >> 12) & 0x1FF;
|
||||||
|
|
||||||
|
size_t pages_to_reserve = bytes / PAGE_SIZE;
|
||||||
|
ASSERT(pages_to_reserve);
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
if (only_free && !is_range_free(vaddr, bytes))
|
|
||||||
return false;
|
uint64_t* pml4 = P2V(m_highest_paging_struct);
|
||||||
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
|
for (;; pml4e++)
|
||||||
reserve_page(vaddr + offset, true, false);
|
{
|
||||||
invalidate_range(vaddr, bytes / PAGE_SIZE, true);
|
#define CHECK_IF_PRESENT(expr) \
|
||||||
return true;
|
if (!((expr) & Flags::Present)) { \
|
||||||
|
const paddr_t paddr = allocate_zeroed_page_aligned_page(); \
|
||||||
|
ASSERT(paddr); \
|
||||||
|
(expr) = paddr | Flags::Present; \
|
||||||
|
}
|
||||||
|
CHECK_IF_PRESENT(pml4[pml4e]);
|
||||||
|
uint64_t* pdpt = P2V(pml4[pml4e] & s_page_addr_mask);
|
||||||
|
for (; pdpte < 512; pdpte++)
|
||||||
|
{
|
||||||
|
CHECK_IF_PRESENT(pdpt[pdpte]);
|
||||||
|
uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
|
||||||
|
for (; pde < 512; pde++)
|
||||||
|
{
|
||||||
|
CHECK_IF_PRESENT(pd[pde]);
|
||||||
|
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
|
||||||
|
for (; pte < 512; pte++)
|
||||||
|
{
|
||||||
|
ASSERT(!(pt[pte] & Flags::Used));
|
||||||
|
pt[pte] = Flags::Reserved;
|
||||||
|
|
||||||
|
pages_to_reserve--;
|
||||||
|
if (pages_to_reserve == 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pte = 0;
|
||||||
|
}
|
||||||
|
pde = 0;
|
||||||
|
}
|
||||||
|
pdpte = 0;
|
||||||
|
#undef CHECK_IF_PRESENT
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
||||||
@@ -782,6 +895,7 @@ namespace Kernel
|
|||||||
|
|
||||||
ASSERT(is_canonical(first_address));
|
ASSERT(is_canonical(first_address));
|
||||||
ASSERT(is_canonical(last_address - 1));
|
ASSERT(is_canonical(last_address - 1));
|
||||||
|
|
||||||
const vaddr_t uc_vaddr_start = uncanonicalize(first_address);
|
const vaddr_t uc_vaddr_start = uncanonicalize(first_address);
|
||||||
const vaddr_t uc_vaddr_end = uncanonicalize(last_address - 1);
|
const vaddr_t uc_vaddr_end = uncanonicalize(last_address - 1);
|
||||||
|
|
||||||
@@ -831,7 +945,7 @@ namespace Kernel
|
|||||||
vaddr |= static_cast<uint64_t>(pde) << 21;
|
vaddr |= static_cast<uint64_t>(pde) << 21;
|
||||||
vaddr |= static_cast<uint64_t>(pte) << 12;
|
vaddr |= static_cast<uint64_t>(pte) << 12;
|
||||||
vaddr = canonicalize(vaddr);
|
vaddr = canonicalize(vaddr);
|
||||||
ASSERT(reserve_page(vaddr));
|
reserve_page(vaddr);
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
pte = 0;
|
pte = 0;
|
||||||
@@ -845,7 +959,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
if (vaddr_t vaddr = canonicalize(uc_vaddr); is_page_free(vaddr))
|
if (vaddr_t vaddr = canonicalize(uc_vaddr); is_page_free(vaddr))
|
||||||
{
|
{
|
||||||
ASSERT(reserve_page(vaddr));
|
reserve_page(vaddr);
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -855,44 +969,90 @@ namespace Kernel
|
|||||||
|
|
||||||
vaddr_t PageTable::reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address)
|
vaddr_t PageTable::reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address)
|
||||||
{
|
{
|
||||||
if (first_address >= KERNEL_OFFSET && first_address < (vaddr_t)g_kernel_start)
|
if (first_address >= KERNEL_OFFSET && first_address < reinterpret_cast<vaddr_t>(g_kernel_start))
|
||||||
first_address = (vaddr_t)g_kernel_start;
|
first_address = reinterpret_cast<vaddr_t>(g_kernel_start);
|
||||||
if (size_t rem = first_address % PAGE_SIZE)
|
if (const auto rem = first_address % PAGE_SIZE)
|
||||||
first_address += PAGE_SIZE - rem;
|
first_address += PAGE_SIZE - rem;
|
||||||
if (size_t rem = last_address % PAGE_SIZE)
|
if (const auto rem = last_address % PAGE_SIZE)
|
||||||
last_address -= rem;
|
last_address -= rem;
|
||||||
|
|
||||||
ASSERT(is_canonical(first_address));
|
ASSERT(is_canonical(first_address));
|
||||||
ASSERT(is_canonical(last_address - 1));
|
ASSERT(is_canonical(last_address - 1));
|
||||||
|
|
||||||
|
const vaddr_t uc_vaddr_start = uncanonicalize(first_address);
|
||||||
|
const vaddr_t uc_vaddr_end = uncanonicalize(last_address - 1);
|
||||||
|
|
||||||
|
uint16_t pml4e = (uc_vaddr_start >> 39) & 0x1FF;
|
||||||
|
uint16_t pdpte = (uc_vaddr_start >> 30) & 0x1FF;
|
||||||
|
uint16_t pde = (uc_vaddr_start >> 21) & 0x1FF;
|
||||||
|
uint16_t pte = (uc_vaddr_start >> 12) & 0x1FF;
|
||||||
|
|
||||||
|
const uint16_t e_pml4e = (uc_vaddr_end >> 39) & 0x1FF;
|
||||||
|
const uint16_t e_pdpte = (uc_vaddr_end >> 30) & 0x1FF;
|
||||||
|
const uint16_t e_pde = (uc_vaddr_end >> 21) & 0x1FF;
|
||||||
|
const uint16_t e_pte = (uc_vaddr_end >> 12) & 0x1FF;
|
||||||
|
|
||||||
|
vaddr_t vaddr = first_address;
|
||||||
|
size_t free_count = 0;
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
for (vaddr_t vaddr = first_address; vaddr < last_address;)
|
const uint64_t* pml4 = P2V(m_highest_paging_struct);
|
||||||
|
for (; pml4e <= e_pml4e; pml4e++)
|
||||||
{
|
{
|
||||||
bool valid { true };
|
#define CHECK_IF_PRESENT(expr, advance) \
|
||||||
for (size_t page = 0; page < page_count; page++)
|
if (!((expr) & Flags::Present)) { \
|
||||||
{
|
if ((free_count += advance) >= page_count) \
|
||||||
if (!is_canonical(vaddr + page * PAGE_SIZE))
|
goto found_free_region; \
|
||||||
{
|
continue; \
|
||||||
vaddr = canonicalize(uncanonicalize(vaddr + page * PAGE_SIZE));
|
|
||||||
valid = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!is_page_free(vaddr + page * PAGE_SIZE))
|
|
||||||
{
|
|
||||||
vaddr += (page + 1) * PAGE_SIZE;
|
|
||||||
valid = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (valid)
|
CHECK_IF_PRESENT(pml4[pml4e], 512 * 512 * 512);
|
||||||
|
const uint64_t* pdpt = P2V(pml4[pml4e] & s_page_addr_mask);
|
||||||
|
for (; pdpte < 512; pdpte++)
|
||||||
{
|
{
|
||||||
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
|
if (pml4e == e_pml4e && pdpte > e_pdpte)
|
||||||
return vaddr;
|
break;
|
||||||
|
CHECK_IF_PRESENT(pdpt[pdpte], 512 * 512);
|
||||||
|
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
|
||||||
|
for (; pde < 512; pde++)
|
||||||
|
{
|
||||||
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde > e_pde)
|
||||||
|
break;
|
||||||
|
CHECK_IF_PRESENT(pd[pde], 512);
|
||||||
|
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
|
||||||
|
for (; pte < 512; pte++)
|
||||||
|
{
|
||||||
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde == e_pde && pte > e_pte)
|
||||||
|
break;
|
||||||
|
if (!(pt[pte] & Flags::Used))
|
||||||
|
{
|
||||||
|
if (++free_count >= page_count)
|
||||||
|
goto found_free_region;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vaddr = 0;
|
||||||
|
vaddr |= static_cast<uint64_t>(pml4e) << 39;
|
||||||
|
vaddr |= static_cast<uint64_t>(pdpte) << 30;
|
||||||
|
vaddr |= static_cast<uint64_t>(pde) << 21;
|
||||||
|
vaddr |= static_cast<uint64_t>(pte) << 12;
|
||||||
|
vaddr = canonicalize(vaddr + PAGE_SIZE);
|
||||||
|
free_count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pte = 0;
|
||||||
|
}
|
||||||
|
pde = 0;
|
||||||
}
|
}
|
||||||
|
pdpte = 0;
|
||||||
|
#undef CHECK_IF_PRESENT
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
found_free_region:
|
||||||
|
reserve_range(vaddr, page_count * PAGE_SIZE);
|
||||||
|
return vaddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::is_page_free(vaddr_t page) const
|
bool PageTable::is_page_free(vaddr_t page) const
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ signal_trampoline:
|
|||||||
addq $40, %rsp
|
addq $40, %rsp
|
||||||
|
|
||||||
// restore sigmask
|
// restore sigmask
|
||||||
movq $83, %rdi // SYS_SIGPROCMASK
|
movq $78, %rdi // SYS_SIGPROCMASK
|
||||||
movq $3, %rsi // SIG_SETMASK
|
movq $3, %rsi // SIG_SETMASK
|
||||||
leaq 192(%rsp), %rdx // set
|
leaq 192(%rsp), %rdx // set
|
||||||
xorq %r10, %r10 // oset
|
xorq %r10, %r10 // oset
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ asm_yield_trampoline:
|
|||||||
pushq %r15
|
pushq %r15
|
||||||
|
|
||||||
movq %rsp, %rdi
|
movq %rsp, %rdi
|
||||||
call scheduler_on_yield
|
call scheduler_on_yield_trampoline
|
||||||
|
|
||||||
popq %r15
|
popq %r15
|
||||||
popq %r14
|
popq %r14
|
||||||
|
|||||||
@@ -44,17 +44,12 @@
|
|||||||
|
|
||||||
isr_stub:
|
isr_stub:
|
||||||
intr_header 24
|
intr_header 24
|
||||||
movq %cr0, %rax; pushq %rax
|
|
||||||
movq %cr2, %rax; pushq %rax
|
|
||||||
movq %cr3, %rax; pushq %rax
|
|
||||||
movq %cr4, %rax; pushq %rax
|
|
||||||
|
|
||||||
movq 152(%rsp), %rdi // isr number
|
movq 120(%rsp), %rdi // isr number
|
||||||
movq 160(%rsp), %rsi // error code
|
movq 128(%rsp), %rsi // error code
|
||||||
leaq 168(%rsp), %rdx // interrupt stack ptr
|
leaq 136(%rsp), %rdx // interrupt stack ptr
|
||||||
movq %rsp, %rcx // register ptr
|
movq %rsp, %rcx // register ptr
|
||||||
call cpp_isr_handler
|
call cpp_isr_handler
|
||||||
addq $32, %rsp
|
|
||||||
|
|
||||||
intr_footer 24
|
intr_footer 24
|
||||||
addq $16, %rsp
|
addq $16, %rsp
|
||||||
|
|||||||
@@ -41,7 +41,18 @@ SECTIONS
|
|||||||
{
|
{
|
||||||
g_kernel_writable_start = .;
|
g_kernel_writable_start = .;
|
||||||
*(.data)
|
*(.data)
|
||||||
|
|
||||||
|
. = ALIGN(8);
|
||||||
|
g_drv_builtin_begin = .;
|
||||||
|
KEEP(*(.banos-driver))
|
||||||
|
g_drv_builtin_end = .;
|
||||||
|
. = ALIGN(8);
|
||||||
|
g_banos_export = .;
|
||||||
|
KEEP(*(.banos-export))
|
||||||
|
g_banos_export_end = .;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_kernel_bss_start = .;
|
g_kernel_bss_start = .;
|
||||||
|
|||||||
27
kernel/include/banos/driver.h
Normal file
27
kernel/include/banos/driver.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
// Copyright (c) 2026 Dcraftbg
|
||||||
|
// 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.
|
||||||
|
#include "version.h"
|
||||||
|
#include "revision.h"
|
||||||
|
|
||||||
|
#define BANOS_DRIVER_REVISION_CURRENT 0
|
||||||
|
|
||||||
|
typedef struct Banos_Driver Banos_Driver;
|
||||||
|
struct Banos_Driver {
|
||||||
|
unsigned long driver_size;
|
||||||
|
banos_version_t minimal_banos_version;
|
||||||
|
const char* name;
|
||||||
|
const char* license;
|
||||||
|
banos_version_t version;
|
||||||
|
|
||||||
|
// NOTE: checkout BANOS_DRIVER_INSTANCE_SIZE.
|
||||||
|
// You may use this instance data for anything you wish to store.
|
||||||
|
// If you need more than that just allocate it on the heap or
|
||||||
|
// globally if you add the proper verification of having your driver run only
|
||||||
|
// within a single instance
|
||||||
|
int (*init)(Banos_Driver* drv);
|
||||||
|
int (*uninit)(Banos_Driver* drv);
|
||||||
|
};
|
||||||
|
#define BANOS_DRIVER_API static __attribute__((section(".banos-driver"), used, aligned(8)))
|
||||||
15
kernel/include/banos/export.h
Normal file
15
kernel/include/banos/export.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
// Copyright (c) 2026 Dcraftbg
|
||||||
|
// 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.
|
||||||
|
typedef struct Banos_Symbol {
|
||||||
|
const char* name;
|
||||||
|
void* arg;
|
||||||
|
} Banos_Symbol;
|
||||||
|
#define BANOS_EXPORT_SYMBOL(symname, str, ptr) \
|
||||||
|
static __attribute__((section(".banos-export"), used, aligned(8))) Banos_Symbol __symbol_##symname = {\
|
||||||
|
.name = str, \
|
||||||
|
.arg = ptr \
|
||||||
|
};
|
||||||
|
#define BANOS_EXPORT(name) BANOS_EXPORT_SYMBOL(name, #name, (void*)&name)
|
||||||
13
kernel/include/banos/print.h
Normal file
13
kernel/include/banos/print.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
// Copyright (c) 2026 Dcraftbg
|
||||||
|
// 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.
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void banos_dprintln(const char* str);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
2
kernel/include/banos/revision.h
Normal file
2
kernel/include/banos/revision.h
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#pragma once
|
||||||
|
typedef unsigned long banos_revision_t;
|
||||||
28
kernel/include/banos/version.h
Normal file
28
kernel/include/banos/version.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
// Copyright (c) 2026 Dcraftbg
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
// [ 8 bit major ] [ 8 minor ] [ 16 patch]
|
||||||
|
typedef unsigned int banos_version_t;
|
||||||
|
#define BANOS_VERSION_MAJOR_SHIFT 24
|
||||||
|
#define BANOS_VERSION_MINOR_SHIFT 16
|
||||||
|
#define BANOS_VERSION_PATCH_SHIFT 0
|
||||||
|
|
||||||
|
#define BANOS_VERSION_MAJOR_MASK 0xFF
|
||||||
|
#define BANOS_VERSION_MINOR_MASK 0xFF
|
||||||
|
#define BANOS_VERSION_PATCH_MASK 0xFFFF
|
||||||
|
|
||||||
|
#define BANOS_VERSION_MAKE(major, minor, patch) \
|
||||||
|
(banos_version_t)( \
|
||||||
|
(((major) & BANOS_VERSION_MAJOR_MASK) << BANOS_VERSION_MAJOR_SHIFT) | \
|
||||||
|
(((minor) & BANOS_VERSION_MINOR_MASK) << BANOS_VERSION_MINOR_SHIFT) | \
|
||||||
|
(((patch) & BANOS_VERSION_PATCH_MASK) << BANOS_VERSION_PATCH_SHIFT) \
|
||||||
|
)
|
||||||
|
|
||||||
|
#define BANOS_VERSION_CURRENT BANOS_VERSION_MAKE(0, 0, 1)
|
||||||
|
|
||||||
|
#define BANOS_VERSION_GET_MAJOR(v) (((v) >> BANOS_VERSION_MAJOR_SHIFT) & BANOS_VERSION_MAJOR_MASK)
|
||||||
|
#define BANOS_VERSION_GET_MINOR(v) (((v) >> BANOS_VERSION_MINOR_SHIFT) & BANOS_VERSION_MINOR_MASK)
|
||||||
|
#define BANOS_VERSION_GET_PATCH(v) (((v) >> BANOS_VERSION_PATCH_SHIFT) & BANOS_VERSION_PATCH_MASK)
|
||||||
@@ -8,6 +8,7 @@ namespace Kernel::API
|
|||||||
enum SharedPageFeature : uint32_t
|
enum SharedPageFeature : uint32_t
|
||||||
{
|
{
|
||||||
SPF_GETTIME = 1 << 0,
|
SPF_GETTIME = 1 << 0,
|
||||||
|
SPF_RDTSCP = 1 << 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SharedPage
|
struct SharedPage
|
||||||
@@ -18,9 +19,8 @@ namespace Kernel::API
|
|||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
uint8_t shift;
|
uint64_t realtime_s;
|
||||||
uint64_t mult;
|
uint32_t realtime_ns;
|
||||||
uint64_t realtime_seconds;
|
|
||||||
} gettime_shared;
|
} gettime_shared;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
@@ -28,6 +28,8 @@ namespace Kernel::API
|
|||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
uint32_t seq;
|
uint32_t seq;
|
||||||
|
uint32_t mult;
|
||||||
|
int8_t shift;
|
||||||
uint64_t last_ns;
|
uint64_t last_ns;
|
||||||
uint64_t last_tsc;
|
uint64_t last_tsc;
|
||||||
} gettime_local;
|
} gettime_local;
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<uint8_t> reserve_gsi(uint32_t gsi);
|
BAN::ErrorOr<uint8_t> reserve_gsi(uint32_t gsi);
|
||||||
|
|
||||||
void initialize_timer();
|
void initialize_timer();
|
||||||
|
void set_timer_dealine(uint64_t ns);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t read_from_local_apic(ptrdiff_t);
|
uint32_t read_from_local_apic(ptrdiff_t);
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int cmd, void* arg) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ThreadBlocker m_sample_data_blocker;
|
ThreadBlocker m_sample_data_blocker;
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> enable_output_path(uint8_t index);
|
BAN::ErrorOr<void> enable_output_path(uint8_t index);
|
||||||
BAN::ErrorOr<void> disable_output_path(uint8_t index);
|
BAN::ErrorOr<void> disable_output_path(uint8_t index);
|
||||||
|
|
||||||
void reset_stream();
|
BAN::ErrorOr<void> reset_stream();
|
||||||
|
|
||||||
BAN::ErrorOr<void> recurse_output_paths(const HDAudio::AFGWidget& widget, BAN::Vector<const HDAudio::AFGWidget*>& path);
|
BAN::ErrorOr<void> recurse_output_paths(const HDAudio::AFGWidget& widget, BAN::Vector<const HDAudio::AFGWidget*>& path);
|
||||||
|
|
||||||
|
|||||||
10
kernel/include/kernel/Banos.h
Normal file
10
kernel/include/kernel/Banos.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <BAN/Vector.h>
|
||||||
|
#include <BAN/StringView.h>
|
||||||
|
typedef struct Banos_Symbol Banos_Symbol;
|
||||||
|
namespace Banos {
|
||||||
|
void* resolve_symbol(const char* name);
|
||||||
|
void import_symbols(Banos_Symbol* symbols, size_t count);
|
||||||
|
void initialize_initial_drivers(void);
|
||||||
|
BAN::ErrorOr<size_t> load_driver_from_image(const char* u_image);
|
||||||
|
}
|
||||||
@@ -82,5 +82,8 @@ namespace CPUID
|
|||||||
bool has_pat();
|
bool has_pat();
|
||||||
bool has_1gib_pages();
|
bool has_1gib_pages();
|
||||||
bool has_invariant_tsc();
|
bool has_invariant_tsc();
|
||||||
|
bool has_rdtscp();
|
||||||
|
uint64_t get_tsc_frequency();
|
||||||
|
bool has_kvm_pvclock();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,8 @@
|
|||||||
|
|
||||||
#define DEBUG_VTTY 1
|
#define DEBUG_VTTY 1
|
||||||
|
|
||||||
|
#define DEBUG_DEVFS 0
|
||||||
|
|
||||||
#define DEBUG_PCI 0
|
#define DEBUG_PCI 0
|
||||||
#define DEBUG_SCHEDULER 0
|
#define DEBUG_SCHEDULER 0
|
||||||
#define DEBUG_PS2 1
|
#define DEBUG_PS2 1
|
||||||
|
|||||||
@@ -12,10 +12,6 @@ namespace Kernel
|
|||||||
virtual ~Device() = default;
|
virtual ~Device() = default;
|
||||||
virtual void update() {}
|
virtual void update() {}
|
||||||
|
|
||||||
virtual bool is_device() const override { return true; }
|
|
||||||
virtual bool is_partition() const { return false; }
|
|
||||||
virtual bool is_storage_device() const { return false; }
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::UniqPtr<MemoryRegion>> mmap_region(PageTable&, off_t offset, size_t len, AddressRange, MemoryRegion::Type, PageTable::flags_t, int status_flags)
|
virtual BAN::ErrorOr<BAN::UniqPtr<MemoryRegion>> mmap_region(PageTable&, off_t offset, size_t len, AddressRange, MemoryRegion::Type, PageTable::flags_t, int status_flags)
|
||||||
{
|
{
|
||||||
(void)offset; (void)len; (void)status_flags;
|
(void)offset; (void)len; (void)status_flags;
|
||||||
@@ -27,7 +23,9 @@ namespace Kernel
|
|||||||
protected:
|
protected:
|
||||||
Device(mode_t, uid_t, gid_t);
|
Device(mode_t, uid_t, gid_t);
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() final override { return BAN::Error::from_errno(EINVAL); }
|
private:
|
||||||
|
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
|
||||||
|
BAN::ErrorOr<void> sync_data() final override { return {}; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class BlockDevice : public Device, public BAN::Weakable<BlockDevice>
|
class BlockDevice : public Device, public BAN::Weakable<BlockDevice>
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int cmd, void* arg) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ namespace Kernel::ELF
|
|||||||
size_t size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool open_execfd;
|
|
||||||
vaddr_t entry_point;
|
vaddr_t entry_point;
|
||||||
|
BAN::Optional<vaddr_t> interp_base;
|
||||||
BAN::Optional<TLS> master_tls;
|
BAN::Optional<TLS> master_tls;
|
||||||
BAN::Vector<BAN::UniqPtr<MemoryRegion>> regions;
|
BAN::Vector<BAN::UniqPtr<MemoryRegion>> regions;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,24 +21,7 @@ namespace Kernel
|
|||||||
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
|
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Epoll() {
|
Epoll();
|
||||||
m_ino = 0;
|
|
||||||
m_mode = Mode::IRUSR | Mode::IWUSR;
|
|
||||||
m_nlink = 0;
|
|
||||||
m_uid = 0;
|
|
||||||
m_gid = 0;
|
|
||||||
m_size = 0;
|
|
||||||
m_atime = {};
|
|
||||||
m_mtime = {};
|
|
||||||
m_ctime = {};
|
|
||||||
m_blksize = PAGE_SIZE;
|
|
||||||
m_blocks = 0;
|
|
||||||
m_dev = 0;
|
|
||||||
m_rdev = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
bool is_epoll() const override { return true; }
|
|
||||||
|
|
||||||
const FileSystem* filesystem() const override { return nullptr; }
|
const FileSystem* filesystem() const override { return nullptr; }
|
||||||
|
|
||||||
@@ -47,7 +30,8 @@ namespace Kernel
|
|||||||
bool has_error_impl() const override { return false; }
|
bool has_error_impl() const override { return false; }
|
||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> fsync_impl() override { return {}; }
|
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
||||||
|
BAN::ErrorOr<void> sync_data() override { return {}; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct ListenEventList
|
struct ListenEventList
|
||||||
|
|||||||
@@ -11,38 +11,22 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uint64_t initval, bool semaphore);
|
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uint64_t initval, bool semaphore);
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventFD(uint64_t initval, bool is_semaphore);
|
||||||
|
|
||||||
const FileSystem* filesystem() const override { return nullptr; }
|
const FileSystem* filesystem() const override { return nullptr; }
|
||||||
|
|
||||||
protected:
|
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
||||||
|
BAN::ErrorOr<void> sync_data() override { return {}; }
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
BAN::ErrorOr<void> fsync_impl() final override { return {}; }
|
|
||||||
|
|
||||||
bool can_read_impl() const override { return m_value > 0; }
|
bool can_read_impl() const override { return m_value > 0; }
|
||||||
bool can_write_impl() const override { return m_value < UINT64_MAX - 1; }
|
bool can_write_impl() const override { return m_value < UINT64_MAX - 1; }
|
||||||
bool has_error_impl() const override { return false; }
|
bool has_error_impl() const override { return false; }
|
||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
|
||||||
EventFD(uint64_t initval, bool is_semaphore)
|
|
||||||
: m_is_semaphore(is_semaphore)
|
|
||||||
, m_value(initval)
|
|
||||||
{
|
|
||||||
m_ino = 0;
|
|
||||||
m_mode = { Mode::IFCHR | Mode::IRUSR | Mode::IWUSR };
|
|
||||||
m_nlink = 0;
|
|
||||||
m_uid = 0;
|
|
||||||
m_gid = 0;
|
|
||||||
m_size = 0;
|
|
||||||
m_atime = {};
|
|
||||||
m_mtime = {};
|
|
||||||
m_ctime = {};
|
|
||||||
m_blksize = 8;
|
|
||||||
m_blocks = 0;
|
|
||||||
m_dev = 0;
|
|
||||||
m_rdev = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const bool m_is_semaphore;
|
const bool m_is_semaphore;
|
||||||
BAN::Atomic<uint64_t> m_value;
|
BAN::Atomic<uint64_t> m_value;
|
||||||
|
|||||||
@@ -99,7 +99,8 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
|
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
|
||||||
BAN::ErrorOr<void> release_block(uint32_t block);
|
BAN::ErrorOr<void> release_block(uint32_t block);
|
||||||
|
|
||||||
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>>& inode_cache() { return m_inode_cache; }
|
BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> open_inode(ino_t);
|
||||||
|
void remove_from_cache(ino_t);
|
||||||
|
|
||||||
const Ext2::Superblock& superblock() const { return m_superblock; }
|
const Ext2::Superblock& superblock() const { return m_superblock; }
|
||||||
|
|
||||||
@@ -155,6 +156,7 @@ namespace Kernel
|
|||||||
BAN::RefPtr<Inode> m_root_inode;
|
BAN::RefPtr<Inode> m_root_inode;
|
||||||
BAN::Vector<uint32_t> m_superblock_backups;
|
BAN::Vector<uint32_t> m_superblock_backups;
|
||||||
|
|
||||||
|
Mutex m_inode_cache_lock;
|
||||||
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>> m_inode_cache;
|
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>> m_inode_cache;
|
||||||
|
|
||||||
BlockBufferManager m_buffer_manager;
|
BlockBufferManager m_buffer_manager;
|
||||||
|
|||||||
@@ -16,25 +16,12 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
~Ext2Inode();
|
~Ext2Inode();
|
||||||
|
|
||||||
#if 0
|
|
||||||
virtual ino_t ino() const override { return m_ino; };
|
|
||||||
virtual Mode mode() const override { return { m_inode.mode }; }
|
|
||||||
virtual nlink_t nlink() const override { return m_inode.links_count; }
|
|
||||||
virtual uid_t uid() const override { return m_inode.uid; }
|
|
||||||
virtual gid_t gid() const override { return m_inode.gid; }
|
|
||||||
virtual off_t size() const override { return m_inode.size; }
|
|
||||||
virtual timespec atime() const override { return timespec { .tv_sec = m_inode.atime, .tv_nsec = 0 }; }
|
|
||||||
virtual timespec mtime() const override { return timespec { .tv_sec = m_inode.mtime, .tv_nsec = 0 }; }
|
|
||||||
virtual timespec ctime() const override { return timespec { .tv_sec = m_inode.ctime, .tv_nsec = 0 }; }
|
|
||||||
virtual blksize_t blksize() const override;
|
|
||||||
virtual blkcnt_t blocks() const override;
|
|
||||||
virtual dev_t dev() const override { return 0; }
|
|
||||||
virtual dev_t rdev() const override { return 0; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const override;
|
virtual const FileSystem* filesystem() const override;
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
|
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
|
||||||
|
virtual BAN::ErrorOr<void> sync_data() override;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||||
@@ -49,10 +36,6 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) override;
|
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() override;
|
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
@@ -66,13 +49,13 @@ namespace Kernel
|
|||||||
// NOTE: the inode might have more blocks than what this suggests if it has been shrinked
|
// NOTE: the inode might have more blocks than what this suggests if it has been shrinked
|
||||||
uint32_t max_used_data_block_count() const { return size() / blksize(); }
|
uint32_t max_used_data_block_count() const { return size() / blksize(); }
|
||||||
|
|
||||||
BAN::ErrorOr<void> sync_no_lock();
|
BAN::ErrorOr<void> sync_inode_no_lock();
|
||||||
|
|
||||||
BAN::ErrorOr<bool> is_directory_empty_no_lock();
|
BAN::ErrorOr<bool> is_directory_empty_no_lock();
|
||||||
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_no_lock(BAN::StringView);
|
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_no_lock(BAN::StringView);
|
||||||
|
|
||||||
/* needs write end of the lock when allocate is true*/
|
/* needs write end of the lock when allocate is true*/
|
||||||
BAN::ErrorOr<BAN::Optional<uint32_t>> block_from_indirect_block_no_lock(uint32_t& block, uint32_t index, uint32_t depth, bool allocate);
|
BAN::ErrorOr<BAN::Optional<uint32_t>> block_from_indirect_block_no_lock(uint32_t block, uint32_t index, uint32_t depth, bool allocate);
|
||||||
BAN::ErrorOr<BAN::Optional<uint32_t>> fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate);
|
BAN::ErrorOr<BAN::Optional<uint32_t>> fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate);
|
||||||
|
|
||||||
/* needs write end of the lock */
|
/* needs write end of the lock */
|
||||||
@@ -88,7 +71,13 @@ namespace Kernel
|
|||||||
private:
|
private:
|
||||||
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino);
|
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino);
|
||||||
|
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> create(Ext2FS&, uint32_t);
|
BAN::Optional<uint32_t> block_cache_find(uint32_t block, uint32_t index) const;
|
||||||
|
void block_cache_remove(uint32_t block, uint32_t index);
|
||||||
|
void block_cache_add(uint32_t block, uint32_t index, uint32_t target);
|
||||||
|
|
||||||
|
BAN::RefPtr<Inode> dir_cache_find(BAN::StringView) const;
|
||||||
|
void dir_cache_remove(BAN::StringView);
|
||||||
|
void dir_cache_add(BAN::StringView, BAN::RefPtr<Inode>);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct ScopedSync
|
struct ScopedSync
|
||||||
@@ -101,7 +90,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
// TODO: there was some memcmp smarty pants stuff here.
|
// TODO: there was some memcmp smarty pants stuff here.
|
||||||
// How do we wanna approach it?
|
// How do we wanna approach it?
|
||||||
if (auto ret = inode.sync_no_lock(); ret.is_error())
|
if (auto ret = inode.sync_inode_no_lock(); ret.is_error())
|
||||||
dwarnln("failed to sync inode: {}", ret.error());
|
dwarnln("failed to sync inode: {}", ret.error());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +113,27 @@ namespace Kernel
|
|||||||
const uint32_t m_og_faddr;
|
const uint32_t m_og_faddr;
|
||||||
const Ext2::Osd2 m_og_osd2;
|
const Ext2::Osd2 m_og_osd2;
|
||||||
|
|
||||||
|
struct BlockCacheEntry
|
||||||
|
{
|
||||||
|
mutable uint32_t freq;
|
||||||
|
uint32_t block;
|
||||||
|
uint32_t index;
|
||||||
|
uint32_t target;
|
||||||
|
};
|
||||||
|
mutable SpinLock m_block_cache_lock;
|
||||||
|
BAN::Array<BlockCacheEntry, 8> m_block_cache;
|
||||||
|
|
||||||
|
struct DirCacheEntry
|
||||||
|
{
|
||||||
|
mutable size_t freq { 0 };
|
||||||
|
BAN::RefPtr<Inode> inode;
|
||||||
|
size_t name_len { 0 };
|
||||||
|
char name[256];
|
||||||
|
};
|
||||||
|
static constexpr size_t dir_cache_size = 32;
|
||||||
|
mutable RWLock m_dir_cache_lock;
|
||||||
|
BAN::Vector<DirCacheEntry> m_dir_cache;
|
||||||
|
|
||||||
friend class Ext2FS;
|
friend class Ext2FS;
|
||||||
friend class BAN::RefPtr<Ext2Inode>;
|
friend class BAN::RefPtr<Ext2Inode>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ namespace Kernel
|
|||||||
|
|
||||||
const FAT::DirectoryEntry& entry() const { return m_entry; }
|
const FAT::DirectoryEntry& entry() const { return m_entry; }
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
|
virtual BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
||||||
|
virtual BAN::ErrorOr<void> sync_data() override { return {}; }
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||||
//virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
//virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||||
@@ -29,9 +32,6 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
//virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
//virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
//virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
//virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
//virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
|
||||||
//virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) override;
|
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() override { return {}; }
|
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
|
|||||||
@@ -62,6 +62,24 @@ namespace Kernel
|
|||||||
mode_t mode;
|
mode_t mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum InodeKind : uint8_t
|
||||||
|
{
|
||||||
|
DEVICE = 0x01,
|
||||||
|
EPOLL = 0x02,
|
||||||
|
PIPE = 0x04,
|
||||||
|
TTY = 0x08,
|
||||||
|
PARTITION = 0x10,
|
||||||
|
STORAGE = 0x20,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SyncType
|
||||||
|
{
|
||||||
|
General,
|
||||||
|
Mode,
|
||||||
|
UidGid,
|
||||||
|
Times,
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~Inode() {}
|
virtual ~Inode() {}
|
||||||
|
|
||||||
@@ -83,10 +101,12 @@ namespace Kernel
|
|||||||
dev_t dev() const { return m_dev; }
|
dev_t dev() const { return m_dev; }
|
||||||
dev_t rdev() const { return m_rdev; }
|
dev_t rdev() const { return m_rdev; }
|
||||||
|
|
||||||
virtual bool is_device() const { return false; }
|
bool is_device() const { return m_kind & InodeKind::DEVICE; }
|
||||||
virtual bool is_epoll() const { return false; }
|
bool is_epoll() const { return m_kind & InodeKind::EPOLL; }
|
||||||
virtual bool is_pipe() const { return false; }
|
bool is_pipe() const { return m_kind & InodeKind::PIPE; }
|
||||||
virtual bool is_tty() const { return false; }
|
bool is_tty() const { return m_kind & InodeKind::TTY; }
|
||||||
|
bool is_partition() const { return m_kind & InodeKind::PARTITION; }
|
||||||
|
bool is_storage_device() const { return m_kind & InodeKind::STORAGE; }
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const = 0;
|
virtual const FileSystem* filesystem() const = 0;
|
||||||
|
|
||||||
@@ -125,12 +145,12 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> fsync();
|
BAN::ErrorOr<void> fsync();
|
||||||
|
|
||||||
// Select/Non blocking API
|
// Select/Non blocking API
|
||||||
bool can_read() const;
|
bool can_read() const { return can_read_impl(); }
|
||||||
bool can_write() const;
|
bool can_write() const { return can_write_impl(); }
|
||||||
bool has_error() const;
|
bool has_error() const { return has_error_impl(); }
|
||||||
bool has_hungup() const;
|
bool has_hungup() const { return has_hungup_impl(); }
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl(int request, void* arg);
|
BAN::ErrorOr<long> ioctl(unsigned long request, void* arg);
|
||||||
|
|
||||||
BAN::ErrorOr<void> add_epoll(class Epoll*);
|
BAN::ErrorOr<void> add_epoll(class Epoll*);
|
||||||
void del_epoll(class Epoll*);
|
void del_epoll(class Epoll*);
|
||||||
@@ -139,6 +159,9 @@ namespace Kernel
|
|||||||
virtual void on_close(int status_flags) { (void)status_flags; }
|
virtual void on_close(int status_flags) { (void)status_flags; }
|
||||||
virtual void on_clone(int status_flags) { (void)status_flags; }
|
virtual void on_clone(int status_flags) { (void)status_flags; }
|
||||||
|
|
||||||
|
virtual BAN::ErrorOr<void> sync_inode(SyncType) = 0;
|
||||||
|
virtual BAN::ErrorOr<void> sync_data() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Directory API
|
// Directory API
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
@@ -169,10 +192,6 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); }
|
|
||||||
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
|
|
||||||
virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) { return BAN::Error::from_errno(ENOTSUP); }
|
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() = 0;
|
|
||||||
|
|
||||||
// Select/Non blocking API
|
// Select/Non blocking API
|
||||||
virtual bool can_read_impl() const = 0;
|
virtual bool can_read_impl() const = 0;
|
||||||
@@ -180,9 +199,14 @@ namespace Kernel
|
|||||||
virtual bool has_error_impl() const = 0;
|
virtual bool has_error_impl() const = 0;
|
||||||
virtual bool has_hungup_impl() const = 0;
|
virtual bool has_hungup_impl() const = 0;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<long> ioctl_impl(int, void*) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// TODO: this is supposed to be const I guess?
|
||||||
|
// But the thing is I would have to refactor a big chunk of the codebase
|
||||||
|
// to add it as a parameter to Inode() soooooo yeah no, not doing that rn.
|
||||||
|
uint8_t m_kind = 0;
|
||||||
|
|
||||||
BAN::Atomic<ino_t> m_ino;
|
BAN::Atomic<ino_t> m_ino;
|
||||||
BAN::Atomic<mode_t> m_mode;
|
BAN::Atomic<mode_t> m_mode;
|
||||||
BAN::Atomic<nlink_t> m_nlink;
|
BAN::Atomic<nlink_t> m_nlink;
|
||||||
@@ -197,12 +221,14 @@ namespace Kernel
|
|||||||
BAN::Atomic<blkcnt_t> m_blocks;
|
BAN::Atomic<blkcnt_t> m_blocks;
|
||||||
BAN::Atomic<dev_t> m_dev;
|
BAN::Atomic<dev_t> m_dev;
|
||||||
BAN::Atomic<dev_t> m_rdev;
|
BAN::Atomic<dev_t> m_rdev;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpinLock m_shared_region_lock;
|
SpinLock m_shared_region_lock;
|
||||||
BAN::WeakPtr<SharedFileData> m_shared_region;
|
BAN::WeakPtr<SharedFileData> m_shared_region;
|
||||||
|
|
||||||
SpinLock m_epoll_lock;
|
SpinLock m_epoll_lock;
|
||||||
BAN::LinkedList<class Epoll*> m_epolls;
|
BAN::LinkedList<class Epoll*> m_epolls;
|
||||||
|
|
||||||
friend class Epoll;
|
friend class Epoll;
|
||||||
friend class FileBackedRegion;
|
friend class FileBackedRegion;
|
||||||
friend class OpenFileDescriptorSet;
|
friend class OpenFileDescriptorSet;
|
||||||
|
|||||||
@@ -5,35 +5,38 @@
|
|||||||
#include <kernel/Memory/ByteRingBuffer.h>
|
#include <kernel/Memory/ByteRingBuffer.h>
|
||||||
#include <kernel/ThreadBlocker.h>
|
#include <kernel/ThreadBlocker.h>
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class Pipe : public Inode
|
class Pipe final : public Inode, public BAN::Weakable<Pipe>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(const Credentials&);
|
static BAN::ErrorOr<BAN::RefPtr<Inode>> open(BAN::RefPtr<Inode>, int status_flags);
|
||||||
|
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uid_t, gid_t);
|
||||||
virtual bool is_pipe() const override { return true; }
|
~Pipe();
|
||||||
|
|
||||||
void on_close(int status_flags) override;
|
void on_close(int status_flags) override;
|
||||||
void on_clone(int status_flags) override;
|
void on_clone(int status_flags) override;
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const override { return nullptr; }
|
virtual const FileSystem* filesystem() const override { return nullptr; }
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
|
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
|
||||||
|
virtual BAN::ErrorOr<void> sync_data() override;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() final override { return {}; }
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return !m_buffer->empty(); }
|
virtual bool can_read_impl() const override { return !m_buffer->empty(); }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
virtual bool has_error_impl() const override { return m_reading_count == 0; }
|
virtual bool has_error_impl() const override { return m_reading_count == 0; }
|
||||||
virtual bool has_hungup_impl() const override { return m_writing_count == 0; }
|
virtual bool has_hungup_impl() const override { return m_writing_count == 0; }
|
||||||
|
|
||||||
virtual BAN::ErrorOr<long> ioctl_impl(int, void*) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pipe(const Credentials&);
|
Pipe(const struct stat&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_mutex;
|
Mutex m_mutex;
|
||||||
@@ -41,8 +44,10 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::UniqPtr<ByteRingBuffer> m_buffer;
|
BAN::UniqPtr<ByteRingBuffer> m_buffer;
|
||||||
|
|
||||||
BAN::Atomic<uint32_t> m_writing_count { 1 };
|
BAN::Atomic<uint32_t> m_writing_count { 0 };
|
||||||
BAN::Atomic<uint32_t> m_reading_count { 1 };
|
BAN::Atomic<uint32_t> m_reading_count { 0 };
|
||||||
|
|
||||||
|
BAN::RefPtr<Inode> m_named_inode;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace Kernel
|
|||||||
static void initialize();
|
static void initialize();
|
||||||
static ProcFileSystem& get();
|
static ProcFileSystem& get();
|
||||||
|
|
||||||
|
void post_scheduler_initialize();
|
||||||
|
|
||||||
BAN::ErrorOr<void> on_process_create(Process&);
|
BAN::ErrorOr<void> on_process_create(Process&);
|
||||||
void on_process_delete(Process&);
|
void on_process_delete(Process&);
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ namespace Kernel
|
|||||||
class ProcROInode final : public TmpInode
|
class ProcROInode final : public TmpInode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem&, void*, mode_t, uid_t, gid_t);
|
||||||
~ProcROInode() = default;
|
~ProcROInode() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -92,10 +92,11 @@ namespace Kernel
|
|||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, const TmpInodeInfo&);
|
ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem&, void*, const TmpInodeInfo&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t (*m_callback)(off_t, BAN::ByteSpan);
|
BAN::ErrorOr<size_t> (*m_callback)(off_t, BAN::ByteSpan, void*);
|
||||||
|
void* m_argument;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProcSymlinkInode final : public TmpInode
|
class ProcSymlinkInode final : public TmpInode
|
||||||
|
|||||||
@@ -40,9 +40,9 @@ namespace Kernel
|
|||||||
m_gid = info.gid;
|
m_gid = info.gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> fsync_impl() final override { return {}; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
|
||||||
|
BAN::ErrorOr<void> sync_data() final override { return {}; }
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,18 +26,14 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
~TmpInode();
|
virtual ~TmpInode();
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const override;
|
virtual const FileSystem* filesystem() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) override;
|
|
||||||
virtual BAN::ErrorOr<void> fsync_impl() override { return {}; }
|
|
||||||
|
|
||||||
void sync();
|
void write_inode_to_fs();
|
||||||
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() { return {}; };
|
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() { return {}; };
|
||||||
|
|
||||||
void free_all_blocks();
|
void free_all_blocks();
|
||||||
@@ -49,6 +45,10 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<size_t> block_index_with_allocation(size_t data_block_index);
|
BAN::ErrorOr<size_t> block_index_with_allocation(size_t data_block_index);
|
||||||
BAN::ErrorOr<size_t> block_index_from_indirect_with_allocation_no_lock(size_t& block, size_t index, uint32_t depth);
|
BAN::ErrorOr<size_t> block_index_from_indirect_with_allocation_no_lock(size_t& block, size_t index, uint32_t depth);
|
||||||
|
|
||||||
|
private:
|
||||||
|
BAN::ErrorOr<void> sync_inode(SyncType) override;
|
||||||
|
BAN::ErrorOr<void> sync_data() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TmpFileSystem& m_fs;
|
TmpFileSystem& m_fs;
|
||||||
TmpBlocks m_tmp_blocks;
|
TmpBlocks m_tmp_blocks;
|
||||||
@@ -66,7 +66,7 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<TmpFileInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<TmpFileInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
||||||
~TmpFileInode();
|
~TmpFileInode();
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
@@ -82,13 +82,36 @@ namespace Kernel
|
|||||||
friend class TmpInode;
|
friend class TmpInode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// NOTE: this is just a dummy, when opening a fifo a pipe is created
|
||||||
|
class TmpFIFOInode : public TmpInode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static BAN::ErrorOr<BAN::RefPtr<TmpFIFOInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
||||||
|
~TmpFIFOInode();
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
|
|
||||||
|
virtual bool can_read_impl() const override { return false; }
|
||||||
|
virtual bool can_write_impl() const override { return false; }
|
||||||
|
virtual bool has_error_impl() const override { return false; }
|
||||||
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
TmpFIFOInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
|
|
||||||
|
friend class TmpInode;
|
||||||
|
};
|
||||||
|
|
||||||
class TmpSocketInode : public TmpInode
|
class TmpSocketInode : public TmpInode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
||||||
~TmpSocketInode();
|
~TmpSocketInode();
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
@@ -110,7 +133,7 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
|
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
|
||||||
~TmpSymlinkInode();
|
~TmpSymlinkInode();
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
BAN::ErrorOr<BAN::String> link_target_impl() override;
|
BAN::ErrorOr<BAN::String> link_target_impl() override;
|
||||||
BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
|
BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
|
||||||
|
|
||||||
|
|||||||
@@ -92,8 +92,9 @@ namespace Kernel
|
|||||||
MountPoint* mount_from_root_inode(BAN::RefPtr<Inode>);
|
MountPoint* mount_from_root_inode(BAN::RefPtr<Inode>);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_mutex;
|
|
||||||
BAN::RefPtr<FileSystem> m_root_fs;
|
BAN::RefPtr<FileSystem> m_root_fs;
|
||||||
|
|
||||||
|
Mutex m_mount_point_lock;
|
||||||
BAN::Vector<MountPoint> m_mount_points;
|
BAN::Vector<MountPoint> m_mount_points;
|
||||||
|
|
||||||
friend class BAN::RefPtr<VirtualFileSystem>;
|
friend class BAN::RefPtr<VirtualFileSystem>;
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid);
|
static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid);
|
||||||
|
static BAN::ErrorOr<void> initialize_tty_thread();
|
||||||
|
|
||||||
void notify();
|
void notify();
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Kernel
|
|||||||
virtual uint32_t lock_depth() const = 0;
|
virtual uint32_t lock_depth() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mutex : public BaseMutex
|
class Mutex final : public BaseMutex
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(Mutex);
|
BAN_NON_COPYABLE(Mutex);
|
||||||
BAN_NON_MOVABLE(Mutex);
|
BAN_NON_MOVABLE(Mutex);
|
||||||
@@ -40,6 +40,7 @@ namespace Kernel
|
|||||||
pid_t expected = -1;
|
pid_t expected = -1;
|
||||||
while (!m_locker.compare_exchange(expected, tid))
|
while (!m_locker.compare_exchange(expected, tid))
|
||||||
{
|
{
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
|
||||||
Processor::yield();
|
Processor::yield();
|
||||||
expected = -1;
|
expected = -1;
|
||||||
}
|
}
|
||||||
@@ -84,13 +85,14 @@ namespace Kernel
|
|||||||
pid_t locker() const override { return m_locker; }
|
pid_t locker() const override { return m_locker; }
|
||||||
bool is_locked() const override { return m_locker != -1; }
|
bool is_locked() const override { return m_locker != -1; }
|
||||||
uint32_t lock_depth() const override { return m_lock_depth; }
|
uint32_t lock_depth() const override { return m_lock_depth; }
|
||||||
|
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Atomic<pid_t> m_locker { -1 };
|
BAN::Atomic<pid_t> m_locker { -1 };
|
||||||
uint32_t m_lock_depth { 0 };
|
uint32_t m_lock_depth { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
class PriorityMutex : public BaseMutex
|
class PriorityMutex final : public BaseMutex
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(PriorityMutex);
|
BAN_NON_COPYABLE(PriorityMutex);
|
||||||
BAN_NON_MOVABLE(PriorityMutex);
|
BAN_NON_MOVABLE(PriorityMutex);
|
||||||
@@ -113,6 +115,7 @@ namespace Kernel
|
|||||||
pid_t expected = -1;
|
pid_t expected = -1;
|
||||||
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
|
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
|
||||||
{
|
{
|
||||||
|
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
|
||||||
Processor::yield();
|
Processor::yield();
|
||||||
expected = -1;
|
expected = -1;
|
||||||
}
|
}
|
||||||
@@ -164,6 +167,7 @@ namespace Kernel
|
|||||||
pid_t locker() const override { return m_locker; }
|
pid_t locker() const override { return m_locker; }
|
||||||
bool is_locked() const override { return m_locker != -1; }
|
bool is_locked() const override { return m_locker != -1; }
|
||||||
uint32_t lock_depth() const override { return m_lock_depth; }
|
uint32_t lock_depth() const override { return m_lock_depth; }
|
||||||
|
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Atomic<pid_t> m_locker { -1 };
|
BAN::Atomic<pid_t> m_locker { -1 };
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <kernel/Lock/Mutex.h>
|
#include <kernel/Lock/SpinLock.h>
|
||||||
#include <kernel/Lock/LockGuard.h>
|
#include <kernel/Lock/SpinLockAsMutex.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
@@ -15,15 +15,18 @@ namespace Kernel
|
|||||||
|
|
||||||
void rd_lock()
|
void rd_lock()
|
||||||
{
|
{
|
||||||
LockGuard _(m_mutex);
|
SpinLockGuard _(m_lock);
|
||||||
while (m_writers_waiting > 0 || m_writer != -1)
|
while (m_writers_waiting > 0 || m_writer != -1)
|
||||||
m_thread_blocker.block_indefinite(&m_mutex);
|
{
|
||||||
|
SpinLockGuardAsMutex smutex(_);
|
||||||
|
m_thread_blocker.block_indefinite(&smutex);
|
||||||
|
}
|
||||||
m_readers_active++;
|
m_readers_active++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rd_unlock()
|
void rd_unlock()
|
||||||
{
|
{
|
||||||
LockGuard _(m_mutex);
|
SpinLockGuard _(m_lock);
|
||||||
if (--m_readers_active == 0)
|
if (--m_readers_active == 0)
|
||||||
m_thread_blocker.unblock();
|
m_thread_blocker.unblock();
|
||||||
}
|
}
|
||||||
@@ -36,11 +39,14 @@ namespace Kernel
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LockGuard _(m_mutex);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
m_writers_waiting++;
|
m_writers_waiting++;
|
||||||
while (m_readers_active > 0 || m_writer != -1)
|
while (m_readers_active > 0 || m_writer != -1)
|
||||||
m_thread_blocker.block_indefinite(&m_mutex);
|
{
|
||||||
|
SpinLockGuardAsMutex smutex(_);
|
||||||
|
m_thread_blocker.block_indefinite(&smutex);
|
||||||
|
}
|
||||||
m_writers_waiting--;
|
m_writers_waiting--;
|
||||||
|
|
||||||
m_writer = Thread::current_tid();
|
m_writer = Thread::current_tid();
|
||||||
@@ -51,13 +57,13 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
if (--m_writer_depth != 0)
|
if (--m_writer_depth != 0)
|
||||||
return;
|
return;
|
||||||
LockGuard _(m_mutex);
|
SpinLockGuard _(m_lock);
|
||||||
m_writer = -1;
|
m_writer = -1;
|
||||||
m_thread_blocker.unblock();
|
m_thread_blocker.unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_mutex;
|
SpinLock m_lock;
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_thread_blocker;
|
||||||
uint32_t m_readers_active { 0 };
|
uint32_t m_readers_active { 0 };
|
||||||
uint32_t m_writers_waiting { 0 };
|
uint32_t m_writers_waiting { 0 };
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace Kernel
|
|||||||
// FIXME: this should probably be ordered tree like map
|
// FIXME: this should probably be ordered tree like map
|
||||||
// for fast lookup and less memory usage
|
// for fast lookup and less memory usage
|
||||||
BAN::Vector<paddr_t> pages;
|
BAN::Vector<paddr_t> pages;
|
||||||
|
BAN::Vector<uint32_t> writers;
|
||||||
BAN::RefPtr<Inode> inode;
|
BAN::RefPtr<Inode> inode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ namespace Kernel
|
|||||||
requires BAN::is_same_v<decltype(func()), void>;
|
requires BAN::is_same_v<decltype(func()), void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
concept with_per_cpu_fast_page_callback = requires(F func, void* addr)
|
||||||
|
{
|
||||||
|
requires BAN::is_same_v<decltype(func(addr)), void>;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
concept with_fast_page_callback_error = requires(F func)
|
concept with_fast_page_callback_error = requires(F func)
|
||||||
{
|
{
|
||||||
@@ -45,6 +51,10 @@ namespace Kernel
|
|||||||
WriteThrough,
|
WriteThrough,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static constexpr bool full_tlb_flush_threshold = 32;
|
||||||
|
|
||||||
|
static constexpr size_t reserved_fast_pages = 0x10;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void initialize_fast_page();
|
static void initialize_fast_page();
|
||||||
static void initialize_and_load();
|
static void initialize_and_load();
|
||||||
@@ -72,6 +82,18 @@ namespace Kernel
|
|||||||
unmap_fast_page();
|
unmap_fast_page();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<with_per_cpu_fast_page_callback F>
|
||||||
|
static void with_per_cpu_fast_page(paddr_t paddr, F callback)
|
||||||
|
{
|
||||||
|
const auto state = Processor::get_interrupt_state();
|
||||||
|
Processor::set_interrupt_state(InterruptState::Disabled);
|
||||||
|
const size_t index = Processor::current_index() + reserved_fast_pages;
|
||||||
|
void* addr = map_fast_page(index, paddr);
|
||||||
|
callback(addr);
|
||||||
|
unmap_fast_page(index);
|
||||||
|
Processor::set_interrupt_state(state);
|
||||||
|
}
|
||||||
|
|
||||||
template<with_fast_page_callback_error F>
|
template<with_fast_page_callback_error F>
|
||||||
static BAN::ErrorOr<void> with_fast_page(paddr_t paddr, F callback)
|
static BAN::ErrorOr<void> with_fast_page(paddr_t paddr, F callback)
|
||||||
{
|
{
|
||||||
@@ -123,8 +145,8 @@ namespace Kernel
|
|||||||
bool is_page_free(vaddr_t) const;
|
bool is_page_free(vaddr_t) const;
|
||||||
bool is_range_free(vaddr_t, size_t bytes) const;
|
bool is_range_free(vaddr_t, size_t bytes) const;
|
||||||
|
|
||||||
bool reserve_page(vaddr_t, bool only_free = true, bool invalidate = true);
|
void reserve_page(vaddr_t);
|
||||||
bool reserve_range(vaddr_t, size_t bytes, bool only_free = true);
|
void reserve_range(vaddr_t, size_t bytes);
|
||||||
|
|
||||||
vaddr_t reserve_free_page(vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
vaddr_t reserve_free_page(vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
||||||
vaddr_t reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
vaddr_t reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
||||||
@@ -133,6 +155,7 @@ namespace Kernel
|
|||||||
|
|
||||||
void invalidate_page(vaddr_t addr, bool send_smp_message) { invalidate_range(addr, 1, send_smp_message); }
|
void invalidate_page(vaddr_t addr, bool send_smp_message) { invalidate_range(addr, 1, send_smp_message); }
|
||||||
void invalidate_range(vaddr_t addr, size_t pages, bool send_smp_message);
|
void invalidate_range(vaddr_t addr, size_t pages, bool send_smp_message);
|
||||||
|
void invalidate_full_address_space(bool global);
|
||||||
|
|
||||||
InterruptState lock() const { return m_lock.lock(); }
|
InterruptState lock() const { return m_lock.lock(); }
|
||||||
void unlock(InterruptState state) const { m_lock.unlock(state); }
|
void unlock(InterruptState state) const { m_lock.unlock(state); }
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ namespace Kernel
|
|||||||
REG_TDLEN = 0x3808,
|
REG_TDLEN = 0x3808,
|
||||||
REG_TDH = 0x3810,
|
REG_TDH = 0x3810,
|
||||||
REG_TDT = 0x3818,
|
REG_TDT = 0x3818,
|
||||||
|
REG_RXCSUM = 0x5000,
|
||||||
REG_MTA = 0x5200,
|
REG_MTA = 0x5200,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -167,6 +168,15 @@ namespace Kernel
|
|||||||
RCTL_BSIZE_16384 = (0b01 << 16) | RCTL_BSEX,
|
RCTL_BSIZE_16384 = (0b01 << 16) | RCTL_BSEX,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum E1000_RXCSUM : uint32_t
|
||||||
|
{
|
||||||
|
RXSUM_IPOFLD = 1 << 8,
|
||||||
|
RXSUM_TUOFLD = 1 << 9,
|
||||||
|
RXSUM_CRCOFLD = 1 << 11,
|
||||||
|
RXSUM_IPPCE = 1 << 12,
|
||||||
|
RXSUM_PCSD = 1 << 13,
|
||||||
|
};
|
||||||
|
|
||||||
enum E1000_CMD : uint8_t
|
enum E1000_CMD : uint8_t
|
||||||
{
|
{
|
||||||
CMD_EOP = 1 << 0,
|
CMD_EOP = 1 << 0,
|
||||||
@@ -178,6 +188,26 @@ namespace Kernel
|
|||||||
CMD_IDE = 1 << 7,
|
CMD_IDE = 1 << 7,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum E1000_RX_STS : uint8_t
|
||||||
|
{
|
||||||
|
RX_STS_IPCS = 1 << 6,
|
||||||
|
RX_STS_TCPCS = 1 << 5,
|
||||||
|
RX_STS_UDPCS = 1 << 5,
|
||||||
|
RX_STS_EOP = 1 << 1,
|
||||||
|
RX_STS_DD = 1 << 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum E1000_RX_ERR : uint8_t
|
||||||
|
{
|
||||||
|
RX_ERR_RXE = 1 << 7,
|
||||||
|
RX_ERR_IPE = 1 << 6,
|
||||||
|
RX_ERR_TCPE = 1 << 5,
|
||||||
|
RX_ERR_CXE = 1 << 4,
|
||||||
|
RX_ERR_SEQ = 1 << 2,
|
||||||
|
RX_ERR_SE = 1 << 1,
|
||||||
|
RX_ERR_CE = 1 << 0,
|
||||||
|
};
|
||||||
|
|
||||||
struct e1000_rx_desc
|
struct e1000_rx_desc
|
||||||
{
|
{
|
||||||
uint64_t addr;
|
uint64_t addr;
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ namespace Kernel
|
|||||||
uint32_t read32(uint16_t reg);
|
uint32_t read32(uint16_t reg);
|
||||||
void write32(uint16_t reg, uint32_t value);
|
void write32(uint16_t reg, uint32_t value);
|
||||||
|
|
||||||
BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan> payload) override;
|
BAN::ErrorOr<void> send_raw_bytes(BAN::Span<const BAN::ConstByteSpan> buffers) override;
|
||||||
|
|
||||||
bool can_read_impl() const override { return false; }
|
bool can_read_impl() const override { return false; }
|
||||||
bool can_write_impl() const override { return false; }
|
bool can_write_impl() const override { return false; }
|
||||||
@@ -74,8 +74,8 @@ namespace Kernel
|
|||||||
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
||||||
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
||||||
|
|
||||||
BAN::Atomic<uint32_t> m_tx_head1 { 0 };
|
BAN::Atomic<uint32_t> m_tx_head { 0 };
|
||||||
BAN::Atomic<uint32_t> m_tx_head2 { 0 };
|
BAN::Atomic<uint32_t> m_tx_commit { 0 };
|
||||||
|
|
||||||
SpinLock m_rx_lock;
|
SpinLock m_rx_lock;
|
||||||
ThreadBlocker m_rx_blocker;
|
ThreadBlocker m_rx_blocker;
|
||||||
|
|||||||
@@ -41,14 +41,14 @@ namespace Kernel
|
|||||||
|
|
||||||
ARPTable& arp_table() { return *m_arp_table; }
|
ARPTable& arp_table() { return *m_arp_table; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> handle_ipv4_packet(NetworkInterface&, BAN::ConstByteSpan);
|
BAN::ErrorOr<void> handle_ipv4_packet(NetworkInterface&, BAN::ConstByteSpan, uint32_t validated_cksums);
|
||||||
|
|
||||||
virtual void unbind_socket(uint16_t port) override;
|
virtual void unbind_socket(uint16_t port) override;
|
||||||
virtual BAN::ErrorOr<void> bind_socket_with_target(BAN::RefPtr<NetworkSocket>, const sockaddr* target_address, socklen_t target_address_len) override;
|
virtual BAN::ErrorOr<void> bind_socket_with_target(BAN::RefPtr<NetworkSocket>, const sockaddr* target_address, socklen_t target_address_len) override;
|
||||||
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) override;
|
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) override;
|
||||||
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) override;
|
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) override;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;
|
virtual BAN::ErrorOr<void> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;
|
||||||
|
|
||||||
virtual Socket::Domain domain() const override { return Socket::Domain::INET ;}
|
virtual Socket::Domain domain() const override { return Socket::Domain::INET ;}
|
||||||
virtual size_t header_size() const override { return sizeof(IPv4Header); }
|
virtual size_t header_size() const override { return sizeof(IPv4Header); }
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Kernel
|
|||||||
{}
|
{}
|
||||||
~LoopbackInterface();
|
~LoopbackInterface();
|
||||||
|
|
||||||
BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan> payload) override;
|
BAN::ErrorOr<void> send_raw_bytes(BAN::Span<const BAN::ConstByteSpan> buffers) override;
|
||||||
|
|
||||||
bool can_read_impl() const override { return false; }
|
bool can_read_impl() const override { return false; }
|
||||||
bool can_write_impl() const override { return false; }
|
bool can_write_impl() const override { return false; }
|
||||||
@@ -46,7 +46,7 @@ namespace Kernel
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_buffer_lock;
|
SpinLock m_buffer_lock;
|
||||||
BAN::UniqPtr<VirtualRange> m_buffer;
|
BAN::UniqPtr<VirtualRange> m_buffer;
|
||||||
|
|
||||||
uint32_t m_buffer_tail { 0 };
|
uint32_t m_buffer_tail { 0 };
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ namespace Kernel
|
|||||||
ARP = 0x0806,
|
ARP = 0x0806,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum NetworkChecksum : uint32_t
|
||||||
|
{
|
||||||
|
CKSUM_IPV4 = 1 << 0,
|
||||||
|
CKSUM_TCP = 1 << 1,
|
||||||
|
CKSUM_UDP = 1 << 2,
|
||||||
|
};
|
||||||
|
|
||||||
class NetworkInterface : public CharacterDevice
|
class NetworkInterface : public CharacterDevice
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(NetworkInterface);
|
BAN_NON_COPYABLE(NetworkInterface);
|
||||||
@@ -59,11 +66,33 @@ namespace Kernel
|
|||||||
|
|
||||||
virtual BAN::StringView name() const override { return m_name; }
|
virtual BAN::StringView name() const override { return m_name; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::ConstByteSpan payload)
|
BAN::ErrorOr<void> send_with_ethernet_header(BAN::MACAddress dst_mac, EtherType ether_type, BAN::ConstByteSpan buffer)
|
||||||
{
|
{
|
||||||
return send_bytes(destination, protocol, { &payload, 1 });
|
BAN::ConstByteSpan buffer_array[1] { buffer };
|
||||||
|
return send_with_ethernet_header(dst_mac, ether_type, buffer_array);
|
||||||
}
|
}
|
||||||
virtual BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan> payload) = 0;
|
|
||||||
|
template<size_t SIZE>
|
||||||
|
BAN::ErrorOr<void> send_with_ethernet_header(BAN::MACAddress dst_mac, EtherType ether_type, const BAN::ConstByteSpan (&buffer_array)[SIZE])
|
||||||
|
{
|
||||||
|
const auto ethernet_header = EthernetHeader {
|
||||||
|
.dst_mac = dst_mac,
|
||||||
|
.src_mac = get_mac_address(),
|
||||||
|
.ether_type = ether_type,
|
||||||
|
};
|
||||||
|
|
||||||
|
BAN::ConstByteSpan new_buffer_array[SIZE + 1];
|
||||||
|
new_buffer_array[0] = BAN::ConstByteSpan::from(ethernet_header);
|
||||||
|
for (size_t i = 0; i < SIZE; i++)
|
||||||
|
new_buffer_array[i + 1] = buffer_array[i];
|
||||||
|
|
||||||
|
return send_raw_bytes({ new_buffer_array, SIZE + 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BAN::ErrorOr<void> send_raw_bytes(BAN::Span<const BAN::ConstByteSpan> buffers) = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Type m_type;
|
const Type m_type;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) = 0;
|
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) = 0;
|
||||||
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) = 0;
|
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) = 0;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0;
|
virtual BAN::ErrorOr<void> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0;
|
||||||
|
|
||||||
virtual Socket::Domain domain() const = 0;
|
virtual Socket::Domain domain() const = 0;
|
||||||
virtual size_t header_size() const = 0;
|
virtual size_t header_size() const = 0;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<BAN::RefPtr<Socket>> create_socket(Socket::Domain, Socket::Type, mode_t, uid_t, gid_t);
|
BAN::ErrorOr<BAN::RefPtr<Socket>> create_socket(Socket::Domain, Socket::Type, mode_t, uid_t, gid_t);
|
||||||
BAN::ErrorOr<void> connect_sockets(Socket::Domain, BAN::RefPtr<Socket>, BAN::RefPtr<Socket>);
|
BAN::ErrorOr<void> connect_sockets(Socket::Domain, BAN::RefPtr<Socket>, BAN::RefPtr<Socket>);
|
||||||
|
|
||||||
void on_receive(NetworkInterface&, BAN::ConstByteSpan);
|
void on_receive(NetworkInterface&, BAN::ConstByteSpan, uint32_t validated_cksums);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NetworkManager() {}
|
NetworkManager() {}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace Kernel
|
|||||||
virtual void get_protocol_header(BAN::ByteSpan header, BAN::ConstByteSpan payload, uint16_t dst_port, PseudoHeader) = 0;
|
virtual void get_protocol_header(BAN::ByteSpan header, BAN::ConstByteSpan payload, uint16_t dst_port, PseudoHeader) = 0;
|
||||||
virtual NetworkProtocol protocol() const = 0;
|
virtual NetworkProtocol protocol() const = 0;
|
||||||
|
|
||||||
virtual void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len) = 0;
|
virtual void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len, uint32_t validated_cksums) = 0;
|
||||||
|
|
||||||
bool is_bound() const { return m_address_len >= static_cast<socklen_t>(sizeof(sa_family_t)) && m_address.ss_family != AF_UNSPEC; }
|
bool is_bound() const { return m_address_len >= static_cast<socklen_t>(sizeof(sa_family_t)) && m_address.ss_family != AF_UNSPEC; }
|
||||||
in_port_t bound_port() const
|
in_port_t bound_port() const
|
||||||
@@ -54,7 +54,7 @@ namespace Kernel
|
|||||||
protected:
|
protected:
|
||||||
NetworkSocket(NetworkLayer&, const Socket::Info&);
|
NetworkSocket(NetworkLayer&, const Socket::Info&);
|
||||||
|
|
||||||
virtual BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override;
|
virtual BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
||||||
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) override;
|
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) override;
|
||||||
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) override = 0;
|
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) override = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -7,25 +7,25 @@ namespace Kernel
|
|||||||
|
|
||||||
enum RTL8169_IO_REGS : uint16_t
|
enum RTL8169_IO_REGS : uint16_t
|
||||||
{
|
{
|
||||||
RTL8169_IO_IDR0 = 0x00,
|
RTL8169_IO_IDR0 = 0x00,
|
||||||
RTL8169_IO_IDR1 = 0x01,
|
RTL8169_IO_IDR1 = 0x01,
|
||||||
RTL8169_IO_IDR2 = 0x02,
|
RTL8169_IO_IDR2 = 0x02,
|
||||||
RTL8169_IO_IDR3 = 0x03,
|
RTL8169_IO_IDR3 = 0x03,
|
||||||
RTL8169_IO_IDR4 = 0x04,
|
RTL8169_IO_IDR4 = 0x04,
|
||||||
RTL8169_IO_IDR5 = 0x05,
|
RTL8169_IO_IDR5 = 0x05,
|
||||||
|
|
||||||
RTL8169_IO_TNPDS = 0x20,
|
RTL8169_IO_TNPDS = 0x20,
|
||||||
RTL8169_IO_CR = 0x37,
|
RTL8169_IO_CR = 0x37,
|
||||||
RTL8169_IO_TPPoll = 0x38,
|
RTL8169_IO_TPPoll = 0x38,
|
||||||
RTL8169_IO_IMR = 0x3C,
|
RTL8169_IO_IMR = 0x3C,
|
||||||
RTL8169_IO_ISR = 0x3E,
|
RTL8169_IO_ISR = 0x3E,
|
||||||
RTL8169_IO_TCR = 0x40,
|
RTL8169_IO_TCR = 0x40,
|
||||||
RTL8169_IO_RCR = 0x44,
|
RTL8169_IO_RCR = 0x44,
|
||||||
RTL8169_IO_9346CR = 0x50,
|
RTL8169_IO_PHYSts = 0x6C,
|
||||||
RTL8169_IO_PHYSts = 0x6C,
|
RTL8169_IO_RMS = 0xDA,
|
||||||
RTL8169_IO_RMS = 0xDA,
|
RTL8169_IO_CPlusCR = 0xE0,
|
||||||
RTL8169_IO_RDSAR = 0xE4,
|
RTL8169_IO_RDSAR = 0xE4,
|
||||||
RTL8169_IO_MTPS = 0xEC,
|
RTL8169_IO_MTPS = 0xEC,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum RTL8169_CR : uint8_t
|
enum RTL8169_CR : uint8_t
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace Kernel
|
|||||||
virtual bool link_up() override { return m_link_up; }
|
virtual bool link_up() override { return m_link_up; }
|
||||||
virtual int link_speed() override;
|
virtual int link_speed() override;
|
||||||
|
|
||||||
virtual size_t payload_mtu() const override { return 7436 - sizeof(EthernetHeader); }
|
virtual size_t payload_mtu() const override { return 0x1FF8 - sizeof(EthernetHeader); }
|
||||||
|
|
||||||
virtual void handle_irq() override;
|
virtual void handle_irq() override;
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<void> initialize();
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan>) override;
|
virtual BAN::ErrorOr<void> send_raw_bytes(BAN::Span<const BAN::ConstByteSpan> buffers) override;
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return false; }
|
virtual bool can_read_impl() const override { return false; }
|
||||||
virtual bool can_write_impl() const override { return false; }
|
virtual bool can_write_impl() const override { return false; }
|
||||||
@@ -64,14 +64,17 @@ namespace Kernel
|
|||||||
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
||||||
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
||||||
|
|
||||||
SpinLock m_lock;
|
BAN::Atomic<bool> m_rx_thread_should_die { false };
|
||||||
|
BAN::Atomic<bool> m_rx_thread_is_dead { true };
|
||||||
|
|
||||||
bool m_thread_should_die { false };
|
uint32_t m_rx_head { 0 };
|
||||||
BAN::Atomic<bool> m_thread_is_dead { true };
|
SpinLock m_rx_lock;
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_rx_blocker;
|
||||||
|
|
||||||
uint32_t m_rx_current { 0 };
|
BAN::Atomic<uint32_t> m_tx_head { 0 };
|
||||||
size_t m_tx_current { 0 };
|
BAN::Atomic<uint32_t> m_tx_commit { 0 };
|
||||||
|
SpinLock m_tx_lock;
|
||||||
|
ThreadBlocker m_tx_blocker;
|
||||||
|
|
||||||
BAN::MACAddress m_mac_address {};
|
BAN::MACAddress m_mac_address {};
|
||||||
BAN::Atomic<bool> m_link_up { false };
|
BAN::Atomic<bool> m_link_up { false };
|
||||||
|
|||||||
@@ -66,9 +66,9 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
||||||
BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int, void*) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) override;
|
||||||
|
|
||||||
void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len) override;
|
void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len, uint32_t validated_cksums) override;
|
||||||
|
|
||||||
bool can_read_impl() const override;
|
bool can_read_impl() const override;
|
||||||
bool can_write_impl() const override;
|
bool can_write_impl() const override;
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ namespace Kernel
|
|||||||
void get_protocol_header(BAN::ByteSpan header, BAN::ConstByteSpan payload, uint16_t dst_port, PseudoHeader) override;
|
void get_protocol_header(BAN::ByteSpan header, BAN::ConstByteSpan payload, uint16_t dst_port, PseudoHeader) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len) override;
|
void receive_packet(BAN::ConstByteSpan, const sockaddr* sender, socklen_t sender_len, uint32_t validated_cksums) override;
|
||||||
|
|
||||||
BAN::ErrorOr<void> connect_impl(const sockaddr*, socklen_t) override;
|
BAN::ErrorOr<void> connect_impl(const sockaddr*, socklen_t) override;
|
||||||
BAN::ErrorOr<void> bind_impl(const sockaddr* address, socklen_t address_len) override;
|
BAN::ErrorOr<void> bind_impl(const sockaddr* address, socklen_t address_len) override;
|
||||||
@@ -41,7 +41,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
||||||
BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int, void*) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) override;
|
||||||
|
|
||||||
bool can_read_impl() const override { return !m_packets.empty(); }
|
bool can_read_impl() const override { return !m_packets.empty(); }
|
||||||
bool can_write_impl() const override { return true; }
|
bool can_write_impl() const override { return true; }
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<void> bind_impl(const sockaddr*, socklen_t) override;
|
virtual BAN::ErrorOr<void> bind_impl(const sockaddr*, socklen_t) override;
|
||||||
virtual BAN::ErrorOr<size_t> recvmsg_impl(msghdr& message, int flags) override;
|
virtual BAN::ErrorOr<size_t> recvmsg_impl(msghdr& message, int flags) override;
|
||||||
virtual BAN::ErrorOr<size_t> sendmsg_impl(const msghdr& message, int flags) override;
|
virtual BAN::ErrorOr<size_t> sendmsg_impl(const msghdr& message, int flags) override;
|
||||||
|
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) override;
|
||||||
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) override;
|
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) override;
|
||||||
virtual BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
virtual BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) override;
|
||||||
virtual BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
virtual BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) override;
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<size_t> recvmsg(int socket, msghdr& message, int flags);
|
BAN::ErrorOr<size_t> recvmsg(int socket, msghdr& message, int flags);
|
||||||
BAN::ErrorOr<size_t> sendmsg(int socket, const msghdr& message, int flags);
|
BAN::ErrorOr<size_t> sendmsg(int socket, const msghdr& message, int flags);
|
||||||
|
|
||||||
|
int get_max_open_fd() const;
|
||||||
|
|
||||||
BAN::ErrorOr<VirtualFileSystem::File> file_of(int) const;
|
BAN::ErrorOr<VirtualFileSystem::File> file_of(int) const;
|
||||||
BAN::ErrorOr<BAN::String> path_of(int) const;
|
BAN::ErrorOr<BAN::String> path_of(int) const;
|
||||||
BAN::ErrorOr<BAN::RefPtr<Inode>> inode_of(int);
|
BAN::ErrorOr<BAN::RefPtr<Inode>> inode_of(int);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <BAN/Vector.h>
|
#include <BAN/Vector.h>
|
||||||
#include <kernel/ACPI/AML/Node.h>
|
#include <kernel/ACPI/AML/Node.h>
|
||||||
#include <kernel/Memory/Types.h>
|
#include <kernel/Memory/Types.h>
|
||||||
#include <kernel/Storage/StorageController.h>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
@@ -179,7 +178,7 @@ namespace Kernel::PCI
|
|||||||
private:
|
private:
|
||||||
struct PCIeInfo
|
struct PCIeInfo
|
||||||
{
|
{
|
||||||
paddr_t bus_paddr[256];
|
paddr_t bus_paddr[256] {};
|
||||||
};
|
};
|
||||||
|
|
||||||
PCIManager() = default;
|
PCIManager() = default;
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ namespace Kernel
|
|||||||
~Process();
|
~Process();
|
||||||
void cleanup_function(Thread*);
|
void cleanup_function(Thread*);
|
||||||
|
|
||||||
void register_to_scheduler();
|
BAN::ErrorOr<vaddr_t> setup_initial_process_stack(MemoryBackedRegion&, BAN::Span<BAN::String> argv, BAN::Span<BAN::String> envp, BAN::Span<LibELF::AuxiliaryVector> auxv);
|
||||||
|
|
||||||
void exit(int status, int signal);
|
void exit(int status, int signal);
|
||||||
|
|
||||||
void add_thread(Thread*);
|
|
||||||
// returns true if thread was the last one
|
// returns true if thread was the last one
|
||||||
bool on_thread_exit(Thread&);
|
bool on_thread_exit(Thread&);
|
||||||
|
|
||||||
@@ -68,7 +68,6 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<long> sys_exec(const char* path, const char* const* argv, const char* const* envp);
|
BAN::ErrorOr<long> sys_exec(const char* path, const char* const* argv, const char* const* envp);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_wait(pid_t pid, int* stat_loc, int options);
|
BAN::ErrorOr<long> sys_wait(pid_t pid, int* stat_loc, int options);
|
||||||
BAN::ErrorOr<long> sys_sleep(int seconds);
|
|
||||||
BAN::ErrorOr<long> sys_nanosleep(const timespec* rqtp, timespec* rmtp);
|
BAN::ErrorOr<long> sys_nanosleep(const timespec* rqtp, timespec* rmtp);
|
||||||
BAN::ErrorOr<long> sys_setitimer(int which, const itimerval* value, itimerval* ovalue);
|
BAN::ErrorOr<long> sys_setitimer(int which, const itimerval* value, itimerval* ovalue);
|
||||||
|
|
||||||
@@ -100,13 +99,14 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<long> open_inode(VirtualFileSystem::File&&, int flags);
|
BAN::ErrorOr<long> open_inode(VirtualFileSystem::File&&, int flags);
|
||||||
|
|
||||||
BAN::ErrorOr<void> create_file_or_dir(int fd, const char* path, mode_t mode) const;
|
BAN::ErrorOr<void> create_file(int fd, const char* path, mode_t) const;
|
||||||
BAN::ErrorOr<long> sys_openat(int, const char* path, int, mode_t);
|
BAN::ErrorOr<long> sys_openat(int fd, const char* path, int flags, mode_t);
|
||||||
BAN::ErrorOr<long> sys_close(int fd);
|
BAN::ErrorOr<long> sys_close(int fd);
|
||||||
BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count);
|
BAN::ErrorOr<long> sys_read(int fd, void* buffer, size_t count);
|
||||||
BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count);
|
BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count);
|
||||||
BAN::ErrorOr<long> sys_access(const char* path, int amode);
|
BAN::ErrorOr<long> sys_access(const char* path, int amode);
|
||||||
BAN::ErrorOr<long> sys_create_dir(const char*, mode_t);
|
BAN::ErrorOr<long> sys_mkdirat(int fd, const char* path, mode_t);
|
||||||
|
BAN::ErrorOr<long> sys_mkfifoat(int fd, const char* path, mode_t);
|
||||||
BAN::ErrorOr<long> sys_hardlinkat(int fd1, const char* path1, int fd2, const char* path2, int flag);
|
BAN::ErrorOr<long> sys_hardlinkat(int fd1, const char* path1, int fd2, const char* path2, int flag);
|
||||||
BAN::ErrorOr<long> sys_renameat(int oldfd, const char* old, int newfd, const char* _new);
|
BAN::ErrorOr<long> sys_renameat(int oldfd, const char* old, int newfd, const char* _new);
|
||||||
BAN::ErrorOr<long> sys_unlinkat(int fd, const char* path, int flag);
|
BAN::ErrorOr<long> sys_unlinkat(int fd, const char* path, int flag);
|
||||||
@@ -136,7 +136,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<long> sys_recvmsg(int socket, msghdr* message, int flags);
|
BAN::ErrorOr<long> sys_recvmsg(int socket, msghdr* message, int flags);
|
||||||
BAN::ErrorOr<long> sys_sendmsg(int socket, const msghdr* message, int flags);
|
BAN::ErrorOr<long> sys_sendmsg(int socket, const msghdr* message, int flags);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_ioctl(int fildes, int request, void* arg);
|
BAN::ErrorOr<long> sys_ioctl(int fildes, unsigned long request, void* arg);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_pselect(sys_pselect_t* arguments);
|
BAN::ErrorOr<long> sys_pselect(sys_pselect_t* arguments);
|
||||||
BAN::ErrorOr<long> sys_ppoll(pollfd* fds, nfds_t nfds, const timespec* tmp_p, const sigset_t* sigmask);
|
BAN::ErrorOr<long> sys_ppoll(pollfd* fds, nfds_t nfds, const timespec* tmp_p, const sigset_t* sigmask);
|
||||||
@@ -209,27 +209,31 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<long> sys_set_gsbase(void*);
|
BAN::ErrorOr<long> sys_set_gsbase(void*);
|
||||||
BAN::ErrorOr<long> sys_get_gsbase();
|
BAN::ErrorOr<long> sys_get_gsbase();
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_pthread_create(const pthread_attr_t* attr, void (*entry)(void*), void* arg);
|
BAN::ErrorOr<long> sys_thread_create(void (*entry)(void*), void* arg, void* stack_base, size_t stack_size);
|
||||||
BAN::ErrorOr<long> sys_pthread_exit(void* value);
|
BAN::ErrorOr<long> sys_thread_exit(void* value);
|
||||||
BAN::ErrorOr<long> sys_pthread_join(pthread_t thread, void** value);
|
BAN::ErrorOr<long> sys_thread_join(pid_t tid, void** value);
|
||||||
BAN::ErrorOr<long> sys_pthread_self();
|
BAN::ErrorOr<long> sys_thread_getid();
|
||||||
BAN::ErrorOr<long> sys_pthread_kill(pthread_t thread, int signal);
|
BAN::ErrorOr<long> sys_thread_kill(pid_t tid, int signal);
|
||||||
|
BAN::ErrorOr<long> sys_thread_detach(pid_t tid);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
|
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_load_keymap(const char* path);
|
BAN::ErrorOr<long> sys_load_keymap(const char* path);
|
||||||
|
|
||||||
|
BAN::ErrorOr<long> sys_banos_install(const char* object);
|
||||||
|
|
||||||
BAN::RefPtr<TTY> controlling_terminal() { return m_controlling_terminal; }
|
BAN::RefPtr<TTY> controlling_terminal() { return m_controlling_terminal; }
|
||||||
|
|
||||||
static Process& current() { return Thread::current().process(); }
|
static Process& current() { return Thread::current().process(); }
|
||||||
|
|
||||||
vaddr_t shared_page_vaddr() const { return m_shared_page_vaddr; }
|
vaddr_t shared_page_vaddr() const { return m_shared_page_vaddr; }
|
||||||
|
|
||||||
PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); }
|
PageTable& page_table() { return *m_page_table; }
|
||||||
|
|
||||||
size_t proc_meminfo(off_t offset, BAN::ByteSpan) const;
|
size_t proc_meminfo(off_t offset, BAN::ByteSpan) const;
|
||||||
size_t proc_cmdline(off_t offset, BAN::ByteSpan) const;
|
size_t proc_cmdline(off_t offset, BAN::ByteSpan) const;
|
||||||
size_t proc_environ(off_t offset, BAN::ByteSpan) const;
|
size_t proc_environ(off_t offset, BAN::ByteSpan) const;
|
||||||
|
size_t proc_cputime(off_t offset, BAN::ByteSpan) const;
|
||||||
BAN::ErrorOr<BAN::String> proc_cwd() const;
|
BAN::ErrorOr<BAN::String> proc_cwd() const;
|
||||||
BAN::ErrorOr<BAN::String> proc_exe() const;
|
BAN::ErrorOr<BAN::String> proc_exe() const;
|
||||||
|
|
||||||
@@ -276,13 +280,12 @@ namespace Kernel
|
|||||||
// You must hold reader end of m_mapped_region_lock when calling this.
|
// You must hold reader end of m_mapped_region_lock when calling this.
|
||||||
size_t find_mapped_region(vaddr_t) const;
|
size_t find_mapped_region(vaddr_t) const;
|
||||||
|
|
||||||
|
BAN::ErrorOr<AddressRange> find_free_address_range(size_t size);
|
||||||
|
|
||||||
BAN::ErrorOr<VirtualFileSystem::File> find_file(int fd, const char* path, int flags) const;
|
BAN::ErrorOr<VirtualFileSystem::File> find_file(int fd, const char* path, int flags) const;
|
||||||
BAN::ErrorOr<FileParent> find_parent_file(int fd, const char* path, int flags) const;
|
BAN::ErrorOr<FileParent> find_parent_file(int fd, const char* path, int flags) const;
|
||||||
BAN::ErrorOr<VirtualFileSystem::File> find_relative_parent(int fd, const char* path) const;
|
BAN::ErrorOr<VirtualFileSystem::File> find_relative_parent(int fd, const char* path) const;
|
||||||
|
|
||||||
BAN::ErrorOr<void> read_from_user(const void* user_addr, void* out, size_t size);
|
|
||||||
BAN::ErrorOr<void> read_string_from_user(const char* user_addr, char* out, size_t max_size);
|
|
||||||
BAN::ErrorOr<void> write_to_user(void* user_addr, const void* in, size_t size);
|
|
||||||
BAN::ErrorOr<MemoryRegion*> validate_and_pin_pointer_access(const void*, size_t, bool needs_write);
|
BAN::ErrorOr<MemoryRegion*> validate_and_pin_pointer_access(const void*, size_t, bool needs_write);
|
||||||
|
|
||||||
uint64_t signal_pending_mask() const
|
uint64_t signal_pending_mask() const
|
||||||
@@ -361,13 +364,13 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::Vector<Thread*> m_threads;
|
BAN::Vector<Thread*> m_threads;
|
||||||
|
|
||||||
struct pthread_info_t
|
struct exited_thread_info_t
|
||||||
{
|
{
|
||||||
pthread_t thread;
|
pid_t tid;
|
||||||
void* value;
|
void* value;
|
||||||
};
|
};
|
||||||
BAN::Vector<pthread_info_t> m_exited_pthreads;
|
BAN::Vector<exited_thread_info_t> m_exited_threads;
|
||||||
ThreadBlocker m_pthread_exit_blocker;
|
ThreadBlocker m_thread_exit_blocker;
|
||||||
|
|
||||||
uint64_t m_alarm_interval_ns { 0 };
|
uint64_t m_alarm_interval_ns { 0 };
|
||||||
uint64_t m_alarm_wake_time_ns { 0 };
|
uint64_t m_alarm_wake_time_ns { 0 };
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ namespace Kernel
|
|||||||
BAN_NON_MOVABLE(Processor);
|
BAN_NON_MOVABLE(Processor);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
struct TLBEntry
|
||||||
|
{
|
||||||
|
vaddr_t vaddr;
|
||||||
|
size_t page_count;
|
||||||
|
class PageTable* page_table;
|
||||||
|
};
|
||||||
|
|
||||||
struct SMPMessage
|
struct SMPMessage
|
||||||
{
|
{
|
||||||
enum class Type
|
enum class Type
|
||||||
@@ -43,18 +50,19 @@ namespace Kernel
|
|||||||
Type type;
|
Type type;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
struct
|
TLBEntry flush_tlb;
|
||||||
{
|
|
||||||
uintptr_t vaddr;
|
|
||||||
size_t page_count;
|
|
||||||
void* page_table;
|
|
||||||
} flush_tlb;
|
|
||||||
SchedulerQueue::Node* new_thread;
|
SchedulerQueue::Node* new_thread;
|
||||||
SchedulerQueue::Node* unblock_thread;
|
SchedulerQueue::Node* unblock_thread;
|
||||||
bool dummy;
|
bool dummy;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct LoadStats
|
||||||
|
{
|
||||||
|
uint64_t ns_idle;
|
||||||
|
uint64_t ns_total;
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Processor& create(ProcessorID id);
|
static Processor& create(ProcessorID id);
|
||||||
static Processor& initialize();
|
static Processor& initialize();
|
||||||
@@ -70,9 +78,6 @@ namespace Kernel
|
|||||||
static void set_smp_enabled() { s_is_smp_enabled = true; }
|
static void set_smp_enabled() { s_is_smp_enabled = true; }
|
||||||
static void wait_until_processors_ready();
|
static void wait_until_processors_ready();
|
||||||
|
|
||||||
static void toggle_should_print_cpu_load() { s_should_print_cpu_load = !s_should_print_cpu_load; }
|
|
||||||
static bool get_should_print_cpu_load() { return s_should_print_cpu_load; }
|
|
||||||
|
|
||||||
static ProcessorID bsp_id() { return s_bsp_id; }
|
static ProcessorID bsp_id() { return s_bsp_id; }
|
||||||
static bool current_is_bsp() { return current_id() == bsp_id(); }
|
static bool current_is_bsp() { return current_id() == bsp_id(); }
|
||||||
|
|
||||||
@@ -114,10 +119,12 @@ namespace Kernel
|
|||||||
static void* get_current_page_table() { return read_gs_sized<void*>(offsetof(Processor, m_current_page_table)); }
|
static void* get_current_page_table() { return read_gs_sized<void*>(offsetof(Processor, m_current_page_table)); }
|
||||||
static void set_current_page_table(void* page_table) { write_gs_sized<void*>(offsetof(Processor, m_current_page_table), page_table); }
|
static void set_current_page_table(void* page_table) { write_gs_sized<void*>(offsetof(Processor, m_current_page_table), page_table); }
|
||||||
|
|
||||||
|
static LoadStats get_load_stats(size_t index);
|
||||||
|
|
||||||
static void yield();
|
static void yield();
|
||||||
static Scheduler& scheduler() { return *read_gs_sized<Scheduler*>(offsetof(Processor, m_scheduler)); }
|
static Scheduler& scheduler() { return *read_gs_sized<Scheduler*>(offsetof(Processor, m_scheduler)); }
|
||||||
|
|
||||||
static void initialize_tsc(uint8_t shift, uint64_t mult, uint64_t realtime_seconds);
|
static void initialize_tsc(uint64_t realtime_seconds);
|
||||||
static void update_tsc();
|
static void update_tsc();
|
||||||
static uint64_t ns_since_boot_tsc();
|
static uint64_t ns_since_boot_tsc();
|
||||||
|
|
||||||
@@ -130,7 +137,7 @@ namespace Kernel
|
|||||||
static void handle_ipi();
|
static void handle_ipi();
|
||||||
|
|
||||||
static void handle_smp_messages();
|
static void handle_smp_messages();
|
||||||
static void send_smp_message(ProcessorID, const SMPMessage&, bool send_ipi = true);
|
static bool send_smp_message(ProcessorID, const SMPMessage&, bool send_ipi = true);
|
||||||
static void broadcast_smp_message(const SMPMessage&);
|
static void broadcast_smp_message(const SMPMessage&);
|
||||||
|
|
||||||
static void load_segments();
|
static void load_segments();
|
||||||
@@ -178,11 +185,13 @@ namespace Kernel
|
|||||||
asm volatile("mov %[value], %%gs:%a[offset]" :: [value]"r"(value), [offset]"ir"(offset) : "memory");
|
asm volatile("mov %[value], %%gs:%a[offset]" :: [value]"r"(value), [offset]"ir"(offset) : "memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lock_tlb_lock();
|
||||||
|
void unlock_tlb_lock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static ProcessorID s_bsp_id;
|
static ProcessorID s_bsp_id;
|
||||||
static BAN::Atomic<uint8_t> s_processor_count;
|
static BAN::Atomic<uint8_t> s_processor_count;
|
||||||
static BAN::Atomic<bool> s_is_smp_enabled;
|
static BAN::Atomic<bool> s_is_smp_enabled;
|
||||||
static BAN::Atomic<bool> s_should_print_cpu_load;
|
|
||||||
static paddr_t s_shared_page_paddr;
|
static paddr_t s_shared_page_paddr;
|
||||||
static vaddr_t s_shared_page_vaddr;
|
static vaddr_t s_shared_page_vaddr;
|
||||||
|
|
||||||
@@ -202,15 +211,19 @@ namespace Kernel
|
|||||||
|
|
||||||
Scheduler* m_scheduler { nullptr };
|
Scheduler* m_scheduler { nullptr };
|
||||||
|
|
||||||
uint64_t m_start_ns { 0 };
|
BAN::Atomic<bool> m_load_stat_lock;
|
||||||
uint64_t m_idle_ns { 0 };
|
uint64_t m_load_start_ns { 0 };
|
||||||
uint64_t m_last_update_ns { 0 };
|
LoadStats m_load_stats {};
|
||||||
uint64_t m_next_update_ns { 0 };
|
|
||||||
|
|
||||||
BAN::Atomic<SMPMessage*> m_smp_pending { nullptr };
|
BAN::Atomic<SMPMessage*> m_smp_pending { nullptr };
|
||||||
BAN::Atomic<SMPMessage*> m_smp_free { nullptr };
|
BAN::Atomic<SMPMessage*> m_smp_free { nullptr };
|
||||||
SMPMessage* m_smp_message_storage { nullptr };
|
SMPMessage* m_smp_message_storage { nullptr };
|
||||||
|
|
||||||
|
BAN::Atomic<bool> m_tlb_lock { false };
|
||||||
|
size_t m_tlb_entry_count { 0 };
|
||||||
|
BAN::Array<TLBEntry, 32> m_tlb_entries;
|
||||||
|
bool m_tlb_global { false };
|
||||||
|
|
||||||
void* m_current_page_table { nullptr };
|
void* m_current_page_table { nullptr };
|
||||||
|
|
||||||
friend class BAN::Array<Processor, 0xFF>;
|
friend class BAN::Array<Processor, 0xFF>;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ namespace Kernel
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
void add_thread_to_back(Node*);
|
void add_thread_to_back(Node*);
|
||||||
void add_thread_with_wake_time(Node*);
|
bool add_thread_with_wake_time(Node*); // return true if node was inserted as the first element
|
||||||
template<typename F>
|
template<typename F>
|
||||||
Node* remove_with_condition(F callback);
|
Node* remove_with_condition(F callback);
|
||||||
void remove_node(Node*);
|
void remove_node(Node*);
|
||||||
@@ -60,7 +60,8 @@ namespace Kernel
|
|||||||
void reschedule(YieldRegisters*);
|
void reschedule(YieldRegisters*);
|
||||||
void reschedule_if_idle();
|
void reschedule_if_idle();
|
||||||
|
|
||||||
void timer_interrupt();
|
void on_timer_interrupt();
|
||||||
|
void on_yield(YieldRegisters*);
|
||||||
|
|
||||||
static BAN::ErrorOr<void> bind_thread_to_processor(Thread*, ProcessorID);
|
static BAN::ErrorOr<void> bind_thread_to_processor(Thread*, ProcessorID);
|
||||||
// if thread is already bound, this will never fail
|
// if thread is already bound, this will never fail
|
||||||
@@ -82,6 +83,7 @@ namespace Kernel
|
|||||||
void update_most_loaded_node_queue(SchedulerQueue::Node*, SchedulerQueue* target_queue);
|
void update_most_loaded_node_queue(SchedulerQueue::Node*, SchedulerQueue* target_queue);
|
||||||
void remove_node_from_most_loaded(SchedulerQueue::Node*);
|
void remove_node_from_most_loaded(SchedulerQueue::Node*);
|
||||||
|
|
||||||
|
void update_wake_up_deadline();
|
||||||
void wake_up_sleeping_threads();
|
void wake_up_sleeping_threads();
|
||||||
|
|
||||||
void do_load_balancing();
|
void do_load_balancing();
|
||||||
|
|||||||
@@ -10,12 +10,14 @@
|
|||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class AHCIController final : public StorageController, public Interruptable
|
class AHCIController final : public BAN::RefCounted<AHCIController>, public Interruptable
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(AHCIController);
|
BAN_NON_COPYABLE(AHCIController);
|
||||||
BAN_NON_MOVABLE(AHCIController);
|
BAN_NON_MOVABLE(AHCIController);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static BAN::ErrorOr<BAN::RefPtr<AHCIController>> create(PCI::Device&);
|
||||||
|
|
||||||
~AHCIController();
|
~AHCIController();
|
||||||
|
|
||||||
virtual void handle_irq() override;
|
virtual void handle_irq() override;
|
||||||
@@ -27,6 +29,7 @@ namespace Kernel
|
|||||||
: m_pci_device(pci_device)
|
: m_pci_device(pci_device)
|
||||||
{ }
|
{ }
|
||||||
BAN::ErrorOr<void> initialize();
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
BAN::Optional<AHCIPortType> check_port_type(volatile HBAPortMemorySpace&);
|
BAN::Optional<AHCIPortType> check_port_type(volatile HBAPortMemorySpace&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -56,8 +56,6 @@ namespace Kernel
|
|||||||
|
|
||||||
// Non-owning pointers
|
// Non-owning pointers
|
||||||
BAN::Vector<ATADevice*> m_devices;
|
BAN::Vector<ATADevice*> m_devices;
|
||||||
|
|
||||||
friend class ATAController;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,22 @@
|
|||||||
|
|
||||||
#include <BAN/UniqPtr.h>
|
#include <BAN/UniqPtr.h>
|
||||||
#include <kernel/PCI.h>
|
#include <kernel/PCI.h>
|
||||||
#include <kernel/Storage/StorageController.h>
|
|
||||||
#include <kernel/Storage/ATA/ATABus.h>
|
#include <kernel/Storage/ATA/ATABus.h>
|
||||||
#include <kernel/Storage/ATA/ATADevice.h>
|
#include <kernel/Storage/ATA/ATADevice.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class ATAController : public StorageController
|
class ATAController : public BAN::RefCounted<ATAController>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<StorageController>> create(PCI::Device&);
|
static BAN::ErrorOr<BAN::RefPtr<ATAController>> create(PCI::Device&);
|
||||||
virtual BAN::ErrorOr<void> initialize() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ATAController(PCI::Device& pci_device)
|
ATAController(PCI::Device& pci_device)
|
||||||
: m_pci_device(pci_device)
|
: m_pci_device(pci_device)
|
||||||
{ }
|
{ }
|
||||||
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PCI::Device& m_pci_device;
|
PCI::Device& m_pci_device;
|
||||||
|
|||||||
@@ -10,13 +10,13 @@
|
|||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class NVMeController final : public StorageController, public CharacterDevice
|
class NVMeController final : public CharacterDevice
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(NVMeController);
|
BAN_NON_COPYABLE(NVMeController);
|
||||||
BAN_NON_MOVABLE(NVMeController);
|
BAN_NON_MOVABLE(NVMeController);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<StorageController>> create(PCI::Device&);
|
static BAN::ErrorOr<BAN::RefPtr<NVMeController>> create(PCI::Device&);
|
||||||
|
|
||||||
NVMeQueue& io_queue() { return *m_io_queue; }
|
NVMeQueue& io_queue() { return *m_io_queue; }
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
NVMeController(PCI::Device& pci_device);
|
NVMeController(PCI::Device& pci_device);
|
||||||
virtual BAN::ErrorOr<void> initialize() override;
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
BAN::ErrorOr<void> identify_controller();
|
BAN::ErrorOr<void> identify_controller();
|
||||||
BAN::ErrorOr<void> identify_namespaces();
|
BAN::ErrorOr<void> identify_namespaces();
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ namespace Kernel
|
|||||||
char m_label[36 * 4 + 1];
|
char m_label[36 * 4 + 1];
|
||||||
const BAN::String m_name;
|
const BAN::String m_name;
|
||||||
|
|
||||||
public:
|
|
||||||
virtual bool is_partition() const override { return true; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <BAN/RefPtr.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
|
||||||
{
|
|
||||||
|
|
||||||
class StorageController : public BAN::RefCounted<StorageController>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~StorageController() {}
|
|
||||||
virtual BAN::ErrorOr<void> initialize() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -14,7 +14,10 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
StorageDevice()
|
StorageDevice()
|
||||||
: BlockDevice(0660, 0, 0)
|
: BlockDevice(0660, 0, 0)
|
||||||
{ }
|
{
|
||||||
|
m_kind |= InodeKind::STORAGE;
|
||||||
|
}
|
||||||
|
|
||||||
virtual ~StorageDevice();
|
virtual ~StorageDevice();
|
||||||
|
|
||||||
BAN::ErrorOr<void> initialize_partitions(BAN::StringView name_prefix);
|
BAN::ErrorOr<void> initialize_partitions(BAN::StringView name_prefix);
|
||||||
@@ -35,7 +38,6 @@ namespace Kernel
|
|||||||
|
|
||||||
size_t drop_disk_cache();
|
size_t drop_disk_cache();
|
||||||
BAN::ErrorOr<void> sync_disk_cache();
|
BAN::ErrorOr<void> sync_disk_cache();
|
||||||
virtual bool is_storage_device() const override { return true; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan) = 0;
|
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan) = 0;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ namespace Kernel
|
|||||||
|
|
||||||
void on_close(int) override;
|
void on_close(int) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int, void*) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PseudoTerminalMaster(BAN::UniqPtr<VirtualRange>, mode_t, uid_t, gid_t);
|
PseudoTerminalMaster(BAN::UniqPtr<VirtualRange>, mode_t, uid_t, gid_t);
|
||||||
|
|||||||
@@ -49,19 +49,8 @@ namespace Kernel
|
|||||||
void on_key_event(LibInput::KeyEvent);
|
void on_key_event(LibInput::KeyEvent);
|
||||||
void handle_input_byte(uint8_t);
|
void handle_input_byte(uint8_t);
|
||||||
|
|
||||||
virtual bool is_tty() const override { return true; }
|
|
||||||
|
|
||||||
virtual void clear() = 0;
|
virtual void clear() = 0;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) override;
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<long> ioctl_impl(int, void*) override;
|
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return m_output.flush; }
|
|
||||||
virtual bool has_error_impl() const override { return false; }
|
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
|
||||||
|
|
||||||
virtual bool master_has_closed() const { return false; }
|
virtual bool master_has_closed() const { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -70,10 +59,16 @@ namespace Kernel
|
|||||||
virtual bool putchar_impl(uint8_t ch) = 0;
|
virtual bool putchar_impl(uint8_t ch) = 0;
|
||||||
virtual void after_write() {}
|
virtual void after_write() {}
|
||||||
|
|
||||||
|
void update_winsize(unsigned short cols, unsigned short rows);
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) final override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) final override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) final override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) final override;
|
||||||
|
|
||||||
void update_winsize(unsigned short cols, unsigned short rows);
|
virtual bool can_read_impl() const override { return m_output.flush; }
|
||||||
|
virtual bool has_error_impl() const override { return false; }
|
||||||
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
virtual BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool putchar(uint8_t ch);
|
bool putchar(uint8_t ch);
|
||||||
@@ -107,7 +102,7 @@ namespace Kernel
|
|||||||
protected:
|
protected:
|
||||||
Mutex m_mutex;
|
Mutex m_mutex;
|
||||||
|
|
||||||
RecursiveSpinLock m_write_lock;
|
Mutex m_write_lock;
|
||||||
ThreadBlocker m_write_blocker;
|
ThreadBlocker m_write_blocker;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -41,22 +41,20 @@ namespace Kernel
|
|||||||
// TODO: userspace stack size is hard limited, maybe make this dynamic?
|
// TODO: userspace stack size is hard limited, maybe make this dynamic?
|
||||||
#if ARCH(x86_64)
|
#if ARCH(x86_64)
|
||||||
static constexpr size_t userspace_stack_size { 32 << 20 };
|
static constexpr size_t userspace_stack_size { 32 << 20 };
|
||||||
|
static constexpr vaddr_t userspace_stack_base { 0x0000700000000000 };
|
||||||
#elif ARCH(i686)
|
#elif ARCH(i686)
|
||||||
static constexpr size_t userspace_stack_size { 4 << 20 };
|
static constexpr size_t userspace_stack_size { 4 << 20 };
|
||||||
|
static constexpr vaddr_t userspace_stack_base { 0xB0000000 };
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*);
|
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*);
|
||||||
static BAN::ErrorOr<Thread*> create_userspace(Process*, PageTable&);
|
static BAN::ErrorOr<Thread*> create_userspace(Process*, PageTable&, vaddr_t userspace_stack_vaddr, size_t userspace_stack_size, vaddr_t entry_point, vaddr_t stack_pointer);
|
||||||
~Thread();
|
~Thread();
|
||||||
|
|
||||||
BAN::ErrorOr<Thread*> pthread_create(entry_t, void*);
|
|
||||||
|
|
||||||
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
|
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
|
||||||
void setup_process_cleanup();
|
void setup_process_cleanup();
|
||||||
|
|
||||||
BAN::ErrorOr<void> initialize_userspace(vaddr_t entry, BAN::Span<BAN::String> argv, BAN::Span<BAN::String> envp, BAN::Span<LibELF::AuxiliaryVector> auxv);
|
|
||||||
|
|
||||||
// Returns true, if thread is going to trigger signal
|
// Returns true, if thread is going to trigger signal
|
||||||
bool is_interrupted_by_signal(bool skip_stop_and_cont = false) const;
|
bool is_interrupted_by_signal(bool skip_stop_and_cont = false) const;
|
||||||
|
|
||||||
@@ -77,15 +75,21 @@ namespace Kernel
|
|||||||
|
|
||||||
// blocks current thread and returns either on unblock, eintr, spuriously or after timeout
|
// blocks current thread and returns either on unblock, eintr, spuriously or after timeout
|
||||||
// if mutex is not nullptr, it will be atomically freed before blocking and automatically locked on wake
|
// if mutex is not nullptr, it will be atomically freed before blocking and automatically locked on wake
|
||||||
BAN::ErrorOr<void> sleep_or_eintr_ns(uint64_t ns);
|
BAN::ErrorOr<void> sleep_or_eintr_for_ns(uint64_t timeout_ns);
|
||||||
|
BAN::ErrorOr<void> sleep_or_eintr_until_ns(uint64_t waketime_ns);
|
||||||
BAN::ErrorOr<void> block_or_eintr_indefinite(ThreadBlocker& thread_blocker, BaseMutex* mutex);
|
BAN::ErrorOr<void> block_or_eintr_indefinite(ThreadBlocker& thread_blocker, BaseMutex* mutex);
|
||||||
BAN::ErrorOr<void> block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout, BaseMutex* mutex);
|
BAN::ErrorOr<void> block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout, BaseMutex* mutex);
|
||||||
BAN::ErrorOr<void> block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout, BaseMutex* mutex);
|
BAN::ErrorOr<void> block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout, BaseMutex* mutex);
|
||||||
|
|
||||||
BAN::ErrorOr<void> sleep_or_eintr_ms(uint64_t ms)
|
BAN::ErrorOr<void> sleep_or_eintr_for_ms(uint64_t timeout_ms)
|
||||||
{
|
{
|
||||||
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(ms, 1'000'000));
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000));
|
||||||
return sleep_or_eintr_ns(ms * 1'000'000);
|
return sleep_or_eintr_for_ns(timeout_ms * 1'000'000);
|
||||||
|
}
|
||||||
|
BAN::ErrorOr<void> sleep_or_eintr_until_ms(uint64_t waketime_ms)
|
||||||
|
{
|
||||||
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(waketime_ms, 1'000'000));
|
||||||
|
return sleep_or_eintr_until_ns(waketime_ms * 1'000'000);
|
||||||
}
|
}
|
||||||
BAN::ErrorOr<void> block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout, BaseMutex* mutex)
|
BAN::ErrorOr<void> block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout, BaseMutex* mutex)
|
||||||
{
|
{
|
||||||
@@ -106,8 +110,6 @@ namespace Kernel
|
|||||||
vaddr_t kernel_stack_top() const { return m_kernel_stack->vaddr() + m_kernel_stack->size(); }
|
vaddr_t kernel_stack_top() const { return m_kernel_stack->vaddr() + m_kernel_stack->size(); }
|
||||||
VirtualRange& kernel_stack() { return *m_kernel_stack; }
|
VirtualRange& kernel_stack() { return *m_kernel_stack; }
|
||||||
|
|
||||||
MemoryBackedRegion& userspace_stack() { ASSERT(is_userspace() && m_userspace_stack); return *m_userspace_stack; }
|
|
||||||
|
|
||||||
static Thread& current();
|
static Thread& current();
|
||||||
static pid_t current_tid();
|
static pid_t current_tid();
|
||||||
|
|
||||||
@@ -117,6 +119,9 @@ namespace Kernel
|
|||||||
const Process& process() const;
|
const Process& process() const;
|
||||||
bool has_process() const { return m_process; }
|
bool has_process() const { return m_process; }
|
||||||
|
|
||||||
|
void detach() { m_is_detached = true; }
|
||||||
|
bool is_detached() const { return m_is_detached; }
|
||||||
|
|
||||||
bool is_userspace() const { return m_is_userspace; }
|
bool is_userspace() const { return m_is_userspace; }
|
||||||
|
|
||||||
uint64_t cpu_time_ns() const;
|
uint64_t cpu_time_ns() const;
|
||||||
@@ -148,8 +153,6 @@ namespace Kernel
|
|||||||
private:
|
private:
|
||||||
Thread(pid_t tid, Process*);
|
Thread(pid_t tid, Process*);
|
||||||
|
|
||||||
void setup_exec(vaddr_t ip, vaddr_t sp);
|
|
||||||
|
|
||||||
static void on_exit_trampoline(Thread*);
|
static void on_exit_trampoline(Thread*);
|
||||||
void on_exit();
|
void on_exit();
|
||||||
|
|
||||||
@@ -171,13 +174,16 @@ namespace Kernel
|
|||||||
BAN::UniqPtr<PageTable> m_keep_alive_page_table;
|
BAN::UniqPtr<PageTable> m_keep_alive_page_table;
|
||||||
|
|
||||||
BAN::UniqPtr<VirtualRange> m_kernel_stack;
|
BAN::UniqPtr<VirtualRange> m_kernel_stack;
|
||||||
MemoryBackedRegion* m_userspace_stack { nullptr };
|
|
||||||
const pid_t m_tid { 0 };
|
const pid_t m_tid { 0 };
|
||||||
State m_state { State::NotStarted };
|
State m_state { State::NotStarted };
|
||||||
Process* m_process { nullptr };
|
Process* m_process { nullptr };
|
||||||
bool m_is_userspace { false };
|
bool m_is_userspace { false };
|
||||||
|
BAN::Atomic<bool> m_is_detached { false };
|
||||||
bool m_delete_process { false };
|
bool m_delete_process { false };
|
||||||
|
|
||||||
|
vaddr_t m_userspace_stack_vaddr { 0 };
|
||||||
|
size_t m_userspace_stack_size { 0 };
|
||||||
|
|
||||||
vaddr_t m_fsbase { 0 };
|
vaddr_t m_fsbase { 0 };
|
||||||
vaddr_t m_gsbase { 0 };
|
vaddr_t m_gsbase { 0 };
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Kernel
|
|||||||
class HPET final : public Timer, public Interruptable
|
class HPET final : public Timer, public Interruptable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<HPET>> create(bool force_pic);
|
static BAN::ErrorOr<BAN::UniqPtr<HPET>> create();
|
||||||
~HPET();
|
~HPET();
|
||||||
|
|
||||||
virtual uint64_t ms_since_boot() const override;
|
virtual uint64_t ms_since_boot() const override;
|
||||||
@@ -26,7 +26,7 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
HPET() = default;
|
HPET() = default;
|
||||||
BAN::ErrorOr<void> initialize(bool force_pic);
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
volatile HPETRegisters& registers();
|
volatile HPETRegisters& registers();
|
||||||
const volatile HPETRegisters& registers() const;
|
const volatile HPETRegisters& registers() const;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <BAN/UniqPtr.h>
|
#include <BAN/UniqPtr.h>
|
||||||
#include <BAN/Vector.h>
|
#include <BAN/Vector.h>
|
||||||
|
#include <kernel/Memory/DMARegion.h>
|
||||||
#include <kernel/Timer/RTC.h>
|
#include <kernel/Timer/RTC.h>
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@@ -31,7 +32,14 @@ namespace Kernel
|
|||||||
class SystemTimer : public Timer
|
class SystemTimer : public Timer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void initialize(bool force_pic);
|
struct TSCInfo
|
||||||
|
{
|
||||||
|
int8_t shift;
|
||||||
|
uint32_t mult;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void initialize();
|
||||||
static SystemTimer& get();
|
static SystemTimer& get();
|
||||||
static bool is_initialized();
|
static bool is_initialized();
|
||||||
|
|
||||||
@@ -44,29 +52,60 @@ namespace Kernel
|
|||||||
virtual bool pre_scheduler_sleep_needs_lock() const override;
|
virtual bool pre_scheduler_sleep_needs_lock() const override;
|
||||||
virtual void pre_scheduler_sleep_ns(uint64_t) override;
|
virtual void pre_scheduler_sleep_ns(uint64_t) override;
|
||||||
|
|
||||||
void sleep_ms(uint64_t ms) const { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(ms, 1'000'000)); return sleep_ns(ms * 1'000'000); }
|
void sleep_for_ns(uint64_t timeout_ns) const;
|
||||||
void sleep_ns(uint64_t ns) const;
|
void sleep_until_ns(uint64_t waketime_ns) const;
|
||||||
|
|
||||||
|
void sleep_for_ms(uint64_t timeout_ms) const
|
||||||
|
{
|
||||||
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000));
|
||||||
|
return sleep_for_ns(timeout_ms * 1'000'000);
|
||||||
|
}
|
||||||
|
void sleep_until_ms(uint64_t waketime_ms) const
|
||||||
|
{
|
||||||
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(waketime_ms, 1'000'000));
|
||||||
|
return sleep_until_ns(waketime_ms * 1'000'000);
|
||||||
|
}
|
||||||
|
|
||||||
void dont_invoke_scheduler() { m_timer->m_should_invoke_scheduler = false; }
|
void dont_invoke_scheduler() { m_timer->m_should_invoke_scheduler = false; }
|
||||||
|
|
||||||
void update_tsc() const;
|
void update_tsc();
|
||||||
uint64_t ns_since_boot_no_tsc() const;
|
TSCInfo tsc_info() const;
|
||||||
|
|
||||||
|
uint64_t ns_since_boot_no_tsc() const { return m_timer->ns_since_boot(); }
|
||||||
|
|
||||||
timespec real_time() const;
|
timespec real_time() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SystemTimer() = default;
|
SystemTimer() = default;
|
||||||
|
|
||||||
void initialize_timers(bool force_pic);
|
void initialize_timers();
|
||||||
|
|
||||||
uint64_t get_tsc_frequency() const;
|
void initialize_invariant_tsc();
|
||||||
|
void initialize_pvclock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
enum class TSCType
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Invariant,
|
||||||
|
PVClock,
|
||||||
|
};
|
||||||
|
|
||||||
uint64_t m_boot_time { 0 };
|
uint64_t m_boot_time { 0 };
|
||||||
BAN::UniqPtr<RTC> m_rtc;
|
BAN::UniqPtr<RTC> m_rtc;
|
||||||
BAN::UniqPtr<Timer> m_timer;
|
BAN::UniqPtr<Timer> m_timer;
|
||||||
bool m_has_invariant_tsc { false };
|
|
||||||
mutable uint32_t m_timer_ticks { 0 };
|
TSCType m_tsc_type = TSCType::None;
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
int8_t shift;
|
||||||
|
uint32_t mult;
|
||||||
|
} invariant;
|
||||||
|
} m_tsc_info;
|
||||||
|
|
||||||
|
BAN::UniqPtr<DMARegion> m_tsc_page;
|
||||||
|
uint64_t m_tsc_update_ns { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
bool can_read_impl() const override { return true; }
|
bool can_read_impl() const override { return true; }
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override;
|
BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
USBJoystick(USBHIDDriver&);
|
USBJoystick(USBHIDDriver&);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ namespace Kernel
|
|||||||
const uint64_t m_block_count;
|
const uint64_t m_block_count;
|
||||||
const uint32_t m_block_size;
|
const uint32_t m_block_size;
|
||||||
|
|
||||||
const char m_name[4];
|
char m_name[4];
|
||||||
|
|
||||||
friend class BAN::RefPtr<USBSCSIDevice>;
|
friend class BAN::RefPtr<USBSCSIDevice>;
|
||||||
};
|
};
|
||||||
|
|||||||
12
kernel/include/kernel/UserCopy.h
Normal file
12
kernel/include/kernel/UserCopy.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/Errors.h>
|
||||||
|
|
||||||
|
namespace Kernel
|
||||||
|
{
|
||||||
|
|
||||||
|
BAN::ErrorOr<void> read_from_user(const void* user_addr, void* out, size_t size);
|
||||||
|
BAN::ErrorOr<void> read_string_from_user(const char* user_addr, char* out, size_t max_size);
|
||||||
|
BAN::ErrorOr<void> write_to_user(void* user_addr, const void* in, size_t size);
|
||||||
|
|
||||||
|
};
|
||||||
@@ -952,7 +952,7 @@ acpi_release_global_lock:
|
|||||||
{
|
{
|
||||||
if (IO::inw(fadt().pm1a_cnt_blk) & PM1_CNT_SCI_EN)
|
if (IO::inw(fadt().pm1a_cnt_blk) & PM1_CNT_SCI_EN)
|
||||||
break;
|
break;
|
||||||
SystemTimer::get().sleep_ms(10);
|
SystemTimer::get().sleep_for_ms(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(IO::inw(fadt().pm1a_cnt_blk) & PM1_CNT_SCI_EN))
|
if (!(IO::inw(fadt().pm1a_cnt_blk) & PM1_CNT_SCI_EN))
|
||||||
|
|||||||
@@ -2146,9 +2146,9 @@ namespace Kernel::ACPI::AML
|
|||||||
|
|
||||||
auto milliseconds = TRY(convert_node(TRY(parse_node(context)), ConvInteger, sizeof(uint64_t)));
|
auto milliseconds = TRY(convert_node(TRY(parse_node(context)), ConvInteger, sizeof(uint64_t)));
|
||||||
|
|
||||||
const uint64_t wakeup_ms = SystemTimer::get().ms_since_boot() + milliseconds.as.integer.value;
|
const uint64_t waketime_ms = SystemTimer::get().ms_since_boot() + milliseconds.as.integer.value;
|
||||||
for (uint64_t curr_ms = SystemTimer::get().ms_since_boot(); curr_ms < wakeup_ms; curr_ms = SystemTimer::get().ms_since_boot())
|
while (SystemTimer::get().ms_since_boot() < waketime_ms)
|
||||||
SystemTimer::get().sleep_ms(wakeup_ms - curr_ms);
|
SystemTimer::get().sleep_until_ms(waketime_ms);
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -2392,7 +2392,7 @@ namespace Kernel::ACPI::AML
|
|||||||
result.type = Node::Type::Integer;
|
result.type = Node::Type::Integer;
|
||||||
result.as.integer.value = BAN::numeric_limits<uint64_t>::max();
|
result.as.integer.value = BAN::numeric_limits<uint64_t>::max();
|
||||||
|
|
||||||
const uint64_t wake_time_ms = (timeout_ms >= 0xFFFF)
|
const uint64_t waketime_ms = (timeout_ms >= 0xFFFF)
|
||||||
? BAN::numeric_limits<uint64_t>::max()
|
? BAN::numeric_limits<uint64_t>::max()
|
||||||
: SystemTimer::get().ms_since_boot() + timeout_ms;
|
: SystemTimer::get().ms_since_boot() + timeout_ms;
|
||||||
|
|
||||||
@@ -2401,9 +2401,9 @@ namespace Kernel::ACPI::AML
|
|||||||
{
|
{
|
||||||
if (sync_object->node.as.mutex->mutex.try_lock())
|
if (sync_object->node.as.mutex->mutex.try_lock())
|
||||||
break;
|
break;
|
||||||
if (SystemTimer::get().ms_since_boot() >= wake_time_ms)
|
if (SystemTimer::get().ms_since_boot() >= waketime_ms)
|
||||||
return result;
|
return result;
|
||||||
SystemTimer::get().sleep_ms(1);
|
Processor::yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sync_object->node.as.mutex->global_lock)
|
if (sync_object->node.as.mutex->global_lock)
|
||||||
|
|||||||
@@ -437,8 +437,27 @@ namespace Kernel
|
|||||||
|
|
||||||
dprintln("CPU {}: lapic timer frequency: {} Hz", Kernel::Processor::current_id(), m_lapic_timer_frequency_hz);
|
dprintln("CPU {}: lapic timer frequency: {} Hz", Kernel::Processor::current_id(), m_lapic_timer_frequency_hz);
|
||||||
|
|
||||||
write_to_local_apic(LAPIC_TIMER_LVT, TimerModePeriodic | IRQ_TIMER);
|
write_to_local_apic(LAPIC_TIMER_LVT, TimerModeOneShot | IRQ_TIMER);
|
||||||
write_to_local_apic(LAPIC_TIMER_INITIAL_REG, m_lapic_timer_frequency_hz / 2 / 100);
|
write_to_local_apic(LAPIC_TIMER_INITIAL_REG, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void APIC::set_timer_dealine(uint64_t target_ns)
|
||||||
|
{
|
||||||
|
// TODO: don't hardcode divide by 2
|
||||||
|
const uint64_t effective_freq = m_lapic_timer_frequency_hz / 2;
|
||||||
|
|
||||||
|
const uint64_t delta_ns = [&]() -> uint64_t {
|
||||||
|
const uint64_t current_ns = SystemTimer::get().ns_since_boot();
|
||||||
|
if (current_ns >= target_ns)
|
||||||
|
return 1;
|
||||||
|
const uint64_t delta_ns = target_ns - current_ns;
|
||||||
|
if (BAN::Math::will_multiplication_overflow(delta_ns, effective_freq))
|
||||||
|
return BAN::numeric_limits<uint64_t>::max() / effective_freq;
|
||||||
|
return delta_ns;
|
||||||
|
}();
|
||||||
|
|
||||||
|
const uint64_t ticks = BAN::Math::div_round_up<uint64_t>(delta_ns * effective_freq, 1'000'000'000);
|
||||||
|
write_to_local_apic(LAPIC_TIMER_INITIAL_REG, BAN::Math::min<uint64_t>(ticks, BAN::numeric_limits<uint32_t>::max()));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t APIC::read_from_local_apic(ptrdiff_t offset)
|
uint32_t APIC::read_from_local_apic(ptrdiff_t offset)
|
||||||
|
|||||||
@@ -77,9 +77,9 @@ namespace Kernel
|
|||||||
return to_copy;
|
return to_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<long> AudioController::ioctl_impl(int cmd, void* arg)
|
BAN::ErrorOr<long> AudioController::ioctl_impl(unsigned long request, void* arg)
|
||||||
{
|
{
|
||||||
switch (cmd)
|
switch (request)
|
||||||
{
|
{
|
||||||
case SND_GET_CHANNELS:
|
case SND_GET_CHANNELS:
|
||||||
*static_cast<uint32_t*>(arg) = get_channels();
|
*static_cast<uint32_t*>(arg) = get_channels();
|
||||||
@@ -92,7 +92,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
SpinLockGuard _(m_spinlock);
|
SpinLockGuard _(m_spinlock);
|
||||||
*static_cast<uint32_t*>(arg) = m_sample_data->size();
|
*static_cast<uint32_t*>(arg) = m_sample_data->size();
|
||||||
if (cmd == SND_RESET_BUFFER)
|
if (request == SND_RESET_BUFFER)
|
||||||
m_sample_data->pop(m_sample_data->size());
|
m_sample_data->pop(m_sample_data->size());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ namespace Kernel
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CharacterDevice::ioctl_impl(cmd, arg);
|
return CharacterDevice::ioctl_impl(request, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <kernel/Audio/HDAudio/AudioFunctionGroup.h>
|
#include <kernel/Audio/HDAudio/AudioFunctionGroup.h>
|
||||||
#include <kernel/Audio/HDAudio/Registers.h>
|
#include <kernel/Audio/HDAudio/Registers.h>
|
||||||
#include <kernel/FS/DevFS/FileSystem.h>
|
#include <kernel/FS/DevFS/FileSystem.h>
|
||||||
|
#include <kernel/Timer/Timer.h>
|
||||||
|
|
||||||
#include <BAN/Sort.h>
|
#include <BAN/Sort.h>
|
||||||
|
|
||||||
@@ -202,12 +203,12 @@ namespace Kernel
|
|||||||
ASSERT(m_stream_index == 0xFF);
|
ASSERT(m_stream_index == 0xFF);
|
||||||
m_stream_index = TRY(m_controller->allocate_stream(HDAudio::StreamType::Output, this));
|
m_stream_index = TRY(m_controller->allocate_stream(HDAudio::StreamType::Output, this));
|
||||||
|
|
||||||
reset_stream();
|
TRY(reset_stream());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void HDAudioFunctionGroup::reset_stream()
|
BAN::ErrorOr<void> HDAudioFunctionGroup::reset_stream()
|
||||||
{
|
{
|
||||||
using Regs = HDAudio::Regs;
|
using Regs = HDAudio::Regs;
|
||||||
|
|
||||||
@@ -219,13 +220,23 @@ namespace Kernel
|
|||||||
// stop stream
|
// stop stream
|
||||||
bar.write8(base + Regs::SDCTL, bar.read8(base + Regs::SDCTL) & 0xFD);
|
bar.write8(base + Regs::SDCTL, bar.read8(base + Regs::SDCTL) & 0xFD);
|
||||||
|
|
||||||
|
const auto timeout_ms = SystemTimer::get().ms_since_boot() + 100;
|
||||||
|
|
||||||
// reset stream
|
// reset stream
|
||||||
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE) | 1);
|
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE) | 1);
|
||||||
while (!(bar.read8(base + Regs::SDCTL) & 1))
|
while (!(bar.read8(base + Regs::SDCTL) & 1))
|
||||||
|
{
|
||||||
|
if (SystemTimer::get().ms_since_boot() > timeout_ms)
|
||||||
|
return BAN::Error::from_errno(ETIMEDOUT);
|
||||||
Processor::pause();
|
Processor::pause();
|
||||||
|
}
|
||||||
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE));
|
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE));
|
||||||
while ((bar.read8(base + Regs::SDCTL) & 1))
|
while ((bar.read8(base + Regs::SDCTL) & 1))
|
||||||
|
{
|
||||||
|
if (SystemTimer::get().ms_since_boot() > timeout_ms)
|
||||||
|
return BAN::Error::from_errno(ETIMEDOUT);
|
||||||
Processor::pause();
|
Processor::pause();
|
||||||
|
}
|
||||||
|
|
||||||
// set bdl address, total size and lvi
|
// set bdl address, total size and lvi
|
||||||
const paddr_t bdl_paddr = m_bdl_region->paddr() + bdl_offset();
|
const paddr_t bdl_paddr = m_bdl_region->paddr() + bdl_offset();
|
||||||
@@ -244,6 +255,8 @@ namespace Kernel
|
|||||||
m_bdl_head = 0;
|
m_bdl_head = 0;
|
||||||
m_bdl_tail = 0;
|
m_bdl_tail = 0;
|
||||||
m_stream_running = false;
|
m_stream_running = false;
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> HDAudioFunctionGroup::initialize_output()
|
BAN::ErrorOr<void> HDAudioFunctionGroup::initialize_output()
|
||||||
@@ -435,7 +448,7 @@ namespace Kernel
|
|||||||
continue;
|
continue;
|
||||||
const auto& amp = path[i]->output_amplifier.value();
|
const auto& amp = path[i]->output_amplifier.value();
|
||||||
|
|
||||||
const int32_t step_mdB = amp.step_size * 250;
|
const int32_t step_mdB = (amp.step_size + 1) * 250;
|
||||||
m_volume_info.step_mdB = step_mdB;
|
m_volume_info.step_mdB = step_mdB;
|
||||||
m_volume_info.min_mdB = -amp.offset * step_mdB;
|
m_volume_info.min_mdB = -amp.offset * step_mdB;
|
||||||
m_volume_info.max_mdB = (amp.num_steps - amp.offset) * step_mdB;
|
m_volume_info.max_mdB = (amp.num_steps - amp.offset) * step_mdB;
|
||||||
@@ -621,7 +634,13 @@ namespace Kernel
|
|||||||
|
|
||||||
m_bdl_tail = (m_bdl_tail + 1) % m_bdl_entry_count;
|
m_bdl_tail = (m_bdl_tail + 1) % m_bdl_entry_count;
|
||||||
if (m_bdl_tail == m_bdl_head)
|
if (m_bdl_tail == m_bdl_head)
|
||||||
reset_stream();
|
{
|
||||||
|
if (auto ret = reset_stream(); ret.is_error())
|
||||||
|
{
|
||||||
|
dwarnln("failed to reset HDA stream: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
queue_bdl_data();
|
queue_bdl_data();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ namespace Kernel
|
|||||||
// 4.3 The software must wait at least 521 us (25 frames) after reading CRST as a 1
|
// 4.3 The software must wait at least 521 us (25 frames) after reading CRST as a 1
|
||||||
// before assuming that codecs have all made status change requests and have been
|
// before assuming that codecs have all made status change requests and have been
|
||||||
// registered by the controller
|
// registered by the controller
|
||||||
SystemTimer::get().sleep_ms(1);
|
SystemTimer::get().sleep_for_ns(521'000);
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
215
kernel/kernel/Banos.cpp
Normal file
215
kernel/kernel/Banos.cpp
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
#include <kernel/Debug.h>
|
||||||
|
#include <kernel/Banos.h>
|
||||||
|
#include <BAN/Assert.h>
|
||||||
|
#include <banos/driver.h>
|
||||||
|
#include <banos/print.h>
|
||||||
|
#include <banos/export.h>
|
||||||
|
#include <kernel/FS/VirtualFileSystem.h>
|
||||||
|
#include <kernel/Memory/PageTable.h>
|
||||||
|
#include <kernel/ELF.h>
|
||||||
|
#include <LibELF/Types.h>
|
||||||
|
#include <LibELF/Values.h>
|
||||||
|
#include <kernel/Process.h>
|
||||||
|
#include <BAN/HashMap.h>
|
||||||
|
#include <kernel/Lock/SpinLock.h>
|
||||||
|
#include <kernel/UserCopy.h>
|
||||||
|
|
||||||
|
using namespace LibELF;
|
||||||
|
using namespace Kernel;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
void banos_dprintln(const char* str) {
|
||||||
|
dprintln("{}", str);
|
||||||
|
}
|
||||||
|
void* banos_lookup_symbol(const char* str) {
|
||||||
|
return Banos::resolve_symbol(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BANOS_EXPORT(banos_dprintln);
|
||||||
|
BANOS_EXPORT(banos_lookup_symbol);
|
||||||
|
|
||||||
|
BAN::HashMap<BAN::StringView, void*> g_banos_symbols;
|
||||||
|
void* Banos::resolve_symbol(const char* name) {
|
||||||
|
auto it = g_banos_symbols.find(name);
|
||||||
|
return it == g_banos_symbols.end() ? NULL : it->value;
|
||||||
|
}
|
||||||
|
void Banos::import_symbols(Banos_Symbol* symbols, size_t count) {
|
||||||
|
for(size_t i = 0; i < count; ++i) {
|
||||||
|
auto sym = symbols + i;
|
||||||
|
MUST(g_banos_symbols.insert(sym->name, sym->arg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: driver unloading with a reference counter
|
||||||
|
struct Driver_Instance {
|
||||||
|
Banos_Driver* drv;
|
||||||
|
};
|
||||||
|
static BAN::Vector<Driver_Instance> s_driver_instaces;
|
||||||
|
static SpinLock s_driver_instaces_lock;
|
||||||
|
|
||||||
|
extern Banos_Symbol g_banos_export[],
|
||||||
|
g_banos_export_end[];
|
||||||
|
static void load_drv(Banos_Driver* drv) {
|
||||||
|
ASSERT(drv->driver_size >= sizeof(Banos_Driver));
|
||||||
|
dprintln("Loading driver:");
|
||||||
|
dprintln(" name: {}", drv->name);
|
||||||
|
if(drv->license) dprintln(" license: {}", drv->license);
|
||||||
|
dprintln(" version: {}.{}.{}", BANOS_VERSION_GET_MAJOR(drv->version), BANOS_VERSION_GET_MINOR(drv->version), BANOS_VERSION_GET_PATCH(drv->version));
|
||||||
|
int e = drv->init(drv);
|
||||||
|
if(e < 0) dprintln(" Failed to init {} => {}", drv->name, -e);
|
||||||
|
}
|
||||||
|
BAN::ErrorOr<size_t> Banos::load_driver_from_image(const char* u_image) {
|
||||||
|
if(!Process::current().credentials().is_superuser()) return BAN::Error::from_errno(EPERM);
|
||||||
|
// TODO: permission verification. Only root should be allowed to do this
|
||||||
|
LibELF::ElfNativeFileHeader header;
|
||||||
|
|
||||||
|
const unsigned char elf_class =
|
||||||
|
#if ARCH(i686)
|
||||||
|
ELFCLASS32;
|
||||||
|
#elif ARCH(x86_64)
|
||||||
|
ELFCLASS64;
|
||||||
|
#else
|
||||||
|
# error update elf class
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// TODO: is banan-os really ever gonna be running on MSB machines?
|
||||||
|
const unsigned char elf_data = ELFDATA2LSB;
|
||||||
|
// TODO: do we need to verify e_machine? I mean we do not really care.
|
||||||
|
// But I'm leaving this todo:
|
||||||
|
// Look up EM_X86_64 and EM_360|EM_860|EM_960
|
||||||
|
TRY(read_from_user(u_image, &header, sizeof header));
|
||||||
|
if( header.e_ident[EI_MAG0] != ELFMAG0 ||
|
||||||
|
header.e_ident[EI_MAG1] != ELFMAG1 ||
|
||||||
|
header.e_ident[EI_MAG2] != ELFMAG2 ||
|
||||||
|
header.e_ident[EI_MAG3] != ELFMAG3 ||
|
||||||
|
header.e_ident[EI_CLASS] != elf_class ||
|
||||||
|
header.e_ident[EI_DATA] != elf_data ||
|
||||||
|
header.e_ident[EI_VERSION] != EV_CURRENT ||
|
||||||
|
header.e_type != ET_REL ||
|
||||||
|
header.e_version != EV_CURRENT ||
|
||||||
|
header.e_ehsize != sizeof(header) ||
|
||||||
|
header.e_shentsize != sizeof(ElfNativeSectionHeader))
|
||||||
|
return BAN::Error::from_errno(EINVAL);
|
||||||
|
|
||||||
|
|
||||||
|
BAN::Vector<LibELF::ElfNativeSectionHeader> secs(header.e_shnum);
|
||||||
|
TRY(read_from_user(u_image + header.e_shoff, secs.data(), secs.size() * sizeof(*secs.data())));
|
||||||
|
auto shstr = secs[header.e_shstrndx];
|
||||||
|
|
||||||
|
size_t total_size = 0;
|
||||||
|
|
||||||
|
LibELF::ElfNativeSectionHeader *strtab = nullptr,
|
||||||
|
*symtab = nullptr,
|
||||||
|
*driver_section = nullptr;
|
||||||
|
|
||||||
|
for(auto& sec : secs) {
|
||||||
|
if(sec.sh_flags & LibELF::SHF_ALLOC) {
|
||||||
|
sec.sh_addr = total_size;
|
||||||
|
total_size += sec.sh_size;
|
||||||
|
}
|
||||||
|
if(sec.sh_name == 0) continue;
|
||||||
|
char name[256];
|
||||||
|
TRY(read_string_from_user(u_image + shstr.sh_offset + sec.sh_name, name, sizeof name));
|
||||||
|
BAN::StringView name_sv(name);
|
||||||
|
|
||||||
|
if(sec.sh_type == LibELF::SHT_SYMTAB) {
|
||||||
|
symtab = &sec;
|
||||||
|
}
|
||||||
|
// TODO: verify sh_type for both of these?
|
||||||
|
if(name_sv == ".strtab") {
|
||||||
|
strtab = &sec;
|
||||||
|
} else if(name_sv == ".banos-driver") {
|
||||||
|
driver_section = &sec;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!symtab || !strtab || !driver_section)
|
||||||
|
return BAN::Error::from_errno(EINVAL);
|
||||||
|
total_size += PAGE_SIZE;
|
||||||
|
total_size &= ~(PAGE_SIZE-1);
|
||||||
|
auto driver = TRY(VirtualRange::create_to_vaddr_range(PageTable::kernel(), { KERNEL_OFFSET, UINTPTR_MAX }, total_size, PageTable::Execute | PageTable::ReadWrite | PageTable::Present, true));
|
||||||
|
|
||||||
|
for(auto& sec : secs) {
|
||||||
|
if(sec.sh_flags & LibELF::SHF_ALLOC) {
|
||||||
|
sec.sh_addr += driver->vaddr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Banos_Driver* banos_driver = reinterpret_cast<Banos_Driver*>(driver_section->sh_addr);
|
||||||
|
for(auto& sec : secs) {
|
||||||
|
if(sec.sh_name == 0) continue;
|
||||||
|
if(sec.sh_flags & LibELF::SHF_ALLOC) {
|
||||||
|
TRY(read_from_user(u_image + sec.sh_offset, reinterpret_cast<char*>(sec.sh_addr), sec.sh_size));
|
||||||
|
}
|
||||||
|
if(sec.sh_type == LibELF::SHT_RELA) {
|
||||||
|
auto& link_sec = secs[sec.sh_info];
|
||||||
|
size_t rela_count = sec.sh_size/sizeof(LibELF::ElfNativeRelocationA);
|
||||||
|
BAN::Vector<LibELF::ElfNativeRelocationA> rela_data(rela_count);
|
||||||
|
TRY(read_from_user(u_image + sec.sh_offset, rela_data.data(), rela_count * sizeof *rela_data.data()));
|
||||||
|
|
||||||
|
for(auto rela : rela_data) {
|
||||||
|
auto type = ELF64_R_TYPE(rela.r_info);
|
||||||
|
auto symbol = ELF64_R_SYM(rela.r_info);
|
||||||
|
|
||||||
|
vaddr_t value = 0;
|
||||||
|
|
||||||
|
LibELF::ElfNativeSymbol sym;
|
||||||
|
TRY(read_from_user(u_image + symtab->sh_offset + sizeof(sym) * symbol, &sym, sizeof sym));
|
||||||
|
|
||||||
|
if(sym.st_shndx) {
|
||||||
|
value = secs[sym.st_shndx].sh_addr;
|
||||||
|
} else {
|
||||||
|
char name[256];
|
||||||
|
TRY(read_string_from_user(u_image + strtab->sh_offset + sym.st_name, name, sizeof name));
|
||||||
|
|
||||||
|
value = reinterpret_cast<vaddr_t>(Banos::resolve_symbol(name));
|
||||||
|
if(!value) {
|
||||||
|
derrorln("Failed to find symbol {}", name);
|
||||||
|
return BAN::Error::from_errno(ENOENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vaddr_t at = link_sec.sh_addr + rela.r_offset;
|
||||||
|
size_t size = 0;
|
||||||
|
switch(type) {
|
||||||
|
case LibELF::R_X86_64_PLT32:
|
||||||
|
case LibELF::R_X86_64_PC32:
|
||||||
|
value -= at;
|
||||||
|
// fallthrough
|
||||||
|
case LibELF::R_X86_64_32:
|
||||||
|
case LibELF::R_X86_64_32S:
|
||||||
|
value += rela.r_addend;
|
||||||
|
size = sizeof(uint32_t);
|
||||||
|
break;
|
||||||
|
case LibELF::R_X86_64_64:
|
||||||
|
value += rela.r_addend;
|
||||||
|
size = sizeof(uint64_t);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
derrorln("TODO: Unsupported relocation type {}", type);
|
||||||
|
return BAN::Error::from_errno(ENOSYS);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(size) {
|
||||||
|
case 4: *reinterpret_cast<uint32_t*>(at) = value; break;
|
||||||
|
case 8: *reinterpret_cast<uint64_t*>(at) = value; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Driver_Instance instance;
|
||||||
|
instance.drv = banos_driver;
|
||||||
|
load_drv(instance.drv);
|
||||||
|
SpinLockGuard _(s_driver_instaces_lock);
|
||||||
|
TRY(s_driver_instaces.push_back(instance));
|
||||||
|
// TODO: import symbols and resolve redefintions :)
|
||||||
|
return s_driver_instaces.size() - 1;
|
||||||
|
}
|
||||||
|
// NOTE: should be more than plenty ;)
|
||||||
|
extern char g_drv_builtin_begin[];
|
||||||
|
extern char g_drv_builtin_end[];
|
||||||
|
void Banos::initialize_initial_drivers(void) {
|
||||||
|
import_symbols(g_banos_export, g_banos_export_end - g_banos_export);
|
||||||
|
char* head = g_drv_builtin_begin;
|
||||||
|
while(head < g_drv_builtin_end) {
|
||||||
|
Banos_Driver* drv = (Banos_Driver*)head;
|
||||||
|
load_drv(drv);
|
||||||
|
head += drv->driver_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -85,6 +85,38 @@ namespace CPUID
|
|||||||
return buffer[3] & (1 << 8);
|
return buffer[3] & (1 << 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool has_rdtscp()
|
||||||
|
{
|
||||||
|
uint32_t buffer[4] {};
|
||||||
|
get_cpuid(0x80000000, buffer);
|
||||||
|
if (buffer[0] < 0x80000001)
|
||||||
|
return false;
|
||||||
|
get_cpuid(0x80000001, buffer);
|
||||||
|
return buffer[3] & (1 << 27);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t get_tsc_frequency()
|
||||||
|
{
|
||||||
|
uint32_t buffer[4];
|
||||||
|
get_cpuid(0x00, buffer);
|
||||||
|
if (buffer[0] < 0x15)
|
||||||
|
return 0;
|
||||||
|
get_cpuid(0x15, buffer);
|
||||||
|
if (buffer[0] == 0 || buffer[1] == 0 || buffer[2] == 0)
|
||||||
|
return 0;
|
||||||
|
return static_cast<uint64_t>(buffer[2]) * buffer[1] / buffer[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool has_kvm_pvclock()
|
||||||
|
{
|
||||||
|
uint32_t buffer[4] {};
|
||||||
|
get_cpuid(0x40000000, buffer);
|
||||||
|
if (buffer[1] != 0x4B4D564B || buffer[2] != 0x564B4D56 || buffer[3] != 0x4D)
|
||||||
|
return false;
|
||||||
|
get_cpuid(0x40000001, buffer);
|
||||||
|
return buffer[0] & (1 << 3);
|
||||||
|
}
|
||||||
|
|
||||||
const char* feature_string_ecx(uint32_t feat)
|
const char* feature_string_ecx(uint32_t feat)
|
||||||
{
|
{
|
||||||
switch (feat)
|
switch (feat)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Kernel
|
|||||||
MUST(DevFileSystem::get().allocate_inode(create_inode_info(mode, uid, gid))),
|
MUST(DevFileSystem::get().allocate_inode(create_inode_info(mode, uid, gid))),
|
||||||
create_inode_info(mode, uid, gid)
|
create_inode_info(mode, uid, gid)
|
||||||
)
|
)
|
||||||
{ }
|
{
|
||||||
|
m_kind |= InodeKind::DEVICE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,9 +135,9 @@ namespace Kernel
|
|||||||
return bytes_to_copy;
|
return bytes_to_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<long> FramebufferDevice::ioctl_impl(int cmd, void* arg)
|
BAN::ErrorOr<long> FramebufferDevice::ioctl_impl(unsigned long request, void* arg)
|
||||||
{
|
{
|
||||||
switch (cmd)
|
switch (request)
|
||||||
{
|
{
|
||||||
case FB_MSYNC_RECTANGLE:
|
case FB_MSYNC_RECTANGLE:
|
||||||
{
|
{
|
||||||
@@ -152,7 +152,7 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return CharacterDevice::ioctl(cmd, arg);
|
return CharacterDevice::ioctl_impl(request, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t FramebufferDevice::get_pixel(uint32_t x, uint32_t y) const
|
uint32_t FramebufferDevice::get_pixel(uint32_t x, uint32_t y) const
|
||||||
@@ -356,51 +356,7 @@ namespace Kernel
|
|||||||
|
|
||||||
void do_msync(uint32_t first_pixel, uint32_t pixel_count)
|
void do_msync(uint32_t first_pixel, uint32_t pixel_count)
|
||||||
{
|
{
|
||||||
if (!Processor::get_should_print_cpu_load())
|
m_framebuffer->sync_pixels_linear(first_pixel, pixel_count);
|
||||||
return m_framebuffer->sync_pixels_linear(first_pixel, pixel_count);
|
|
||||||
|
|
||||||
const uint32_t fb_width = m_framebuffer->width();
|
|
||||||
|
|
||||||
// If we are here (in FramebufferMemoryRegion), our terminal driver is FramebufferTerminalDriver
|
|
||||||
ASSERT(g_terminal_driver->has_font());
|
|
||||||
const auto& font = g_terminal_driver->font();
|
|
||||||
|
|
||||||
const uint32_t x = first_pixel % fb_width;
|
|
||||||
const uint32_t y = first_pixel / fb_width;
|
|
||||||
|
|
||||||
const uint32_t load_w = 16 * font.width();
|
|
||||||
const uint32_t load_h = Processor::count() * font.height();
|
|
||||||
|
|
||||||
if (y >= load_h || x + pixel_count <= fb_width - load_w)
|
|
||||||
return m_framebuffer->sync_pixels_linear(first_pixel, pixel_count);
|
|
||||||
|
|
||||||
if (x >= fb_width - load_w && x + pixel_count <= fb_width)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (x < fb_width - load_w)
|
|
||||||
m_framebuffer->sync_pixels_linear(first_pixel, fb_width - load_w - x);
|
|
||||||
|
|
||||||
if (x + pixel_count > fb_width)
|
|
||||||
{
|
|
||||||
const uint32_t past_last_pixel = first_pixel + pixel_count;
|
|
||||||
|
|
||||||
first_pixel = (y + 1) * fb_width;
|
|
||||||
pixel_count = past_last_pixel - first_pixel;
|
|
||||||
|
|
||||||
const uint32_t cpu_load_end = load_h * fb_width;
|
|
||||||
|
|
||||||
while (pixel_count && first_pixel < cpu_load_end)
|
|
||||||
{
|
|
||||||
m_framebuffer->sync_pixels_linear(first_pixel, BAN::Math::min(pixel_count, fb_width - load_w));
|
|
||||||
|
|
||||||
const uint32_t advance = BAN::Math::min(pixel_count, fb_width);
|
|
||||||
pixel_count -= advance;
|
|
||||||
first_pixel += advance;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pixel_count)
|
|
||||||
m_framebuffer->sync_pixels_linear(first_pixel, pixel_count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
namespace Kernel::ELF
|
namespace Kernel::ELF
|
||||||
{
|
{
|
||||||
@@ -291,7 +290,9 @@ namespace Kernel::ELF
|
|||||||
TRY(memory_regions.emplace_back(BAN::move(region)));
|
TRY(memory_regions.emplace_back(BAN::move(region)));
|
||||||
}
|
}
|
||||||
|
|
||||||
result.open_execfd = !interpreter.empty();
|
if (!interpreter.empty())
|
||||||
|
result.interp_base = load_base_vaddr;
|
||||||
|
|
||||||
result.entry_point = load_base_vaddr + file_header.e_entry;
|
result.entry_point = load_base_vaddr + file_header.e_entry;
|
||||||
result.regions = BAN::move(memory_regions);
|
result.regions = BAN::move(memory_regions);
|
||||||
return BAN::move(result);
|
return BAN::move(result);
|
||||||
|
|||||||
@@ -14,6 +14,24 @@ namespace Kernel
|
|||||||
return BAN::RefPtr<Epoll>::adopt(epoll_ptr);
|
return BAN::RefPtr<Epoll>::adopt(epoll_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Epoll::Epoll()
|
||||||
|
{
|
||||||
|
m_ino = 0;
|
||||||
|
m_mode = Mode::IRUSR | Mode::IWUSR;
|
||||||
|
m_nlink = 0;
|
||||||
|
m_uid = 0;
|
||||||
|
m_gid = 0;
|
||||||
|
m_size = 0;
|
||||||
|
m_atime = {};
|
||||||
|
m_mtime = {};
|
||||||
|
m_ctime = {};
|
||||||
|
m_blksize = PAGE_SIZE;
|
||||||
|
m_blocks = 0;
|
||||||
|
m_dev = 0;
|
||||||
|
m_rdev = 0;
|
||||||
|
m_kind = InodeKind::EPOLL;
|
||||||
|
}
|
||||||
|
|
||||||
Epoll::~Epoll()
|
Epoll::~Epoll()
|
||||||
{
|
{
|
||||||
for (auto& [inode, _] : m_listening_events)
|
for (auto& [inode, _] : m_listening_events)
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ namespace Kernel
|
|||||||
[](void* _devfs)
|
[](void* _devfs)
|
||||||
{
|
{
|
||||||
auto* devfs = static_cast<DevFileSystem*>(_devfs);
|
auto* devfs = static_cast<DevFileSystem*>(_devfs);
|
||||||
|
uint64_t next_update_ms = SystemTimer::get().ms_since_boot();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@@ -57,7 +58,8 @@ namespace Kernel
|
|||||||
for (auto& device : devfs->m_devices)
|
for (auto& device : devfs->m_devices)
|
||||||
device->update();
|
device->update();
|
||||||
}
|
}
|
||||||
SystemTimer::get().sleep_ms(10);
|
SystemTimer::get().sleep_until_ms(next_update_ms);
|
||||||
|
next_update_ms += 10;
|
||||||
}
|
}
|
||||||
}, s_instance
|
}, s_instance
|
||||||
));
|
));
|
||||||
@@ -153,7 +155,7 @@ namespace Kernel
|
|||||||
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
|
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
|
||||||
MUST(m_devices.push_back(device));
|
MUST(m_devices.push_back(device));
|
||||||
|
|
||||||
dprintln("Added device /dev/{}", device->name());
|
dprintln_if(DEBUG_DEVFS, "Added device /dev/{}", device->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevFileSystem::remove_device(BAN::RefPtr<Device> device)
|
void DevFileSystem::remove_device(BAN::RefPtr<Device> device)
|
||||||
@@ -170,7 +172,7 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintln("Removed device /dev/{}", device->name());
|
dprintln_if(DEBUG_DEVFS, "Removed device /dev/{}", device->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<TmpInode> inode)
|
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<TmpInode> inode)
|
||||||
|
|||||||
@@ -14,6 +14,25 @@ namespace Kernel
|
|||||||
return BAN::RefPtr<Inode>(BAN::RefPtr<EventFD>::adopt(eventfd_ptr));
|
return BAN::RefPtr<Inode>(BAN::RefPtr<EventFD>::adopt(eventfd_ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventFD::EventFD(uint64_t initval, bool is_semaphore)
|
||||||
|
: m_is_semaphore(is_semaphore)
|
||||||
|
, m_value(initval)
|
||||||
|
{
|
||||||
|
m_ino = 0;
|
||||||
|
m_mode = Mode::IFCHR | Mode::IRUSR | Mode::IWUSR;
|
||||||
|
m_nlink = 0;
|
||||||
|
m_uid = 0;
|
||||||
|
m_gid = 0;
|
||||||
|
m_size = 0;
|
||||||
|
m_atime = {};
|
||||||
|
m_mtime = {};
|
||||||
|
m_ctime = {};
|
||||||
|
m_blksize = 8;
|
||||||
|
m_blocks = 0;
|
||||||
|
m_dev = 0;
|
||||||
|
m_rdev = 0;
|
||||||
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> EventFD::read_impl(off_t, BAN::ByteSpan buffer)
|
BAN::ErrorOr<size_t> EventFD::read_impl(off_t, BAN::ByteSpan buffer)
|
||||||
{
|
{
|
||||||
if (buffer.size() < sizeof(uint64_t))
|
if (buffer.size() < sizeof(uint64_t))
|
||||||
|
|||||||
@@ -167,10 +167,36 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<void> Ext2FS::initialize_root_inode()
|
BAN::ErrorOr<void> Ext2FS::initialize_root_inode()
|
||||||
{
|
{
|
||||||
m_root_inode = TRY(Ext2Inode::create(*this, Ext2::Enum::ROOT_INO));
|
m_root_inode = TRY(open_inode(Ext2::Enum::ROOT_INO));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> Ext2FS::open_inode(ino_t ino)
|
||||||
|
{
|
||||||
|
LockGuard _(m_inode_cache_lock);
|
||||||
|
|
||||||
|
auto it = m_inode_cache.find(ino);
|
||||||
|
if (it != m_inode_cache.end())
|
||||||
|
return it->value;
|
||||||
|
|
||||||
|
auto inode_location = TRY(locate_inode(ino));
|
||||||
|
|
||||||
|
auto block_buffer = TRY(get_block_buffer());
|
||||||
|
TRY(read_block(inode_location.block, block_buffer));
|
||||||
|
|
||||||
|
auto& inode = block_buffer.span().slice(inode_location.offset).as<Ext2::Inode>();
|
||||||
|
|
||||||
|
auto result = TRY(BAN::RefPtr<Ext2Inode>::create(*this, inode, ino));
|
||||||
|
TRY(m_inode_cache.insert(ino, result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ext2FS::remove_from_cache(ino_t ino)
|
||||||
|
{
|
||||||
|
LockGuard _(m_inode_cache_lock);
|
||||||
|
m_inode_cache.remove(ino);
|
||||||
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<uint32_t> Ext2FS::create_inode(const Ext2::Inode& ext2_inode)
|
BAN::ErrorOr<uint32_t> Ext2FS::create_inode(const Ext2::Inode& ext2_inode)
|
||||||
{
|
{
|
||||||
auto bgd_buffer = TRY(m_buffer_manager.get_buffer());
|
auto bgd_buffer = TRY(m_buffer_manager.get_buffer());
|
||||||
|
|||||||
@@ -42,24 +42,6 @@ namespace Kernel
|
|||||||
return (m_ino - 1) / m_fs.superblock().blocks_per_group;
|
return (m_ino - 1) / m_fs.superblock().blocks_per_group;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> Ext2Inode::create(Ext2FS& fs, uint32_t inode_ino)
|
|
||||||
{
|
|
||||||
auto it = fs.inode_cache().find(inode_ino);
|
|
||||||
if (it != fs.inode_cache().end())
|
|
||||||
return it->value;
|
|
||||||
|
|
||||||
auto inode_location = TRY(fs.locate_inode(inode_ino));
|
|
||||||
|
|
||||||
auto block_buffer = TRY(fs.get_block_buffer());
|
|
||||||
TRY(fs.read_block(inode_location.block, block_buffer));
|
|
||||||
|
|
||||||
auto& inode = block_buffer.span().slice(inode_location.offset).as<Ext2::Inode>();
|
|
||||||
|
|
||||||
auto result = TRY(BAN::RefPtr<Ext2Inode>::create(fs, inode, inode_ino));
|
|
||||||
TRY(fs.inode_cache().insert(inode_ino, result));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ext2Inode::~Ext2Inode()
|
Ext2Inode::~Ext2Inode()
|
||||||
{
|
{
|
||||||
if (m_nlink > 0)
|
if (m_nlink > 0)
|
||||||
@@ -73,57 +55,69 @@ namespace Kernel
|
|||||||
return &m_fs;
|
return &m_fs;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::Optional<uint32_t>> Ext2Inode::block_from_indirect_block_no_lock(uint32_t& block, uint32_t index, uint32_t depth, bool allocate)
|
BAN::ErrorOr<void> Ext2Inode::sync_inode(SyncType)
|
||||||
|
{
|
||||||
|
RWLockRDGuard _(m_lock);
|
||||||
|
TRY(sync_inode_no_lock());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<void> Ext2Inode::sync_data()
|
||||||
|
{
|
||||||
|
RWLockRDGuard _(m_lock);
|
||||||
|
for (size_t i = 0; i < max_used_data_block_count(); i++)
|
||||||
|
if (const auto fs_block = TRY(fs_block_of_data_block_index_no_lock(i, false)); fs_block.has_value())
|
||||||
|
TRY(m_fs.sync_block(fs_block.value()));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::Optional<uint32_t>> Ext2Inode::block_from_indirect_block_no_lock(uint32_t block, uint32_t index, uint32_t depth, bool allocate)
|
||||||
{
|
{
|
||||||
const uint32_t indices_per_fs_block = blksize() / sizeof(uint32_t);
|
const uint32_t indices_per_fs_block = blksize() / sizeof(uint32_t);
|
||||||
|
|
||||||
if (block == 0 && !allocate)
|
ASSERT(block != 0);
|
||||||
return BAN::Optional<uint32_t>();
|
|
||||||
|
|
||||||
if (depth == 0)
|
if (depth == 0)
|
||||||
|
return BAN::Optional<uint32_t>(block);
|
||||||
|
|
||||||
|
uint32_t local_index = index;
|
||||||
|
for (uint32_t i = 1; i < depth; i++)
|
||||||
|
local_index /= indices_per_fs_block;
|
||||||
|
local_index %= indices_per_fs_block;
|
||||||
|
|
||||||
|
uint32_t next_block = 0;
|
||||||
|
|
||||||
|
if (auto cached = block_cache_find(block, local_index); cached.has_value())
|
||||||
|
next_block = cached.value();
|
||||||
|
|
||||||
|
if (next_block == 0)
|
||||||
{
|
{
|
||||||
if (block == 0)
|
auto block_buffer = TRY(m_fs.get_block_buffer());
|
||||||
|
TRY(m_fs.read_block(block, block_buffer));
|
||||||
|
auto block_span = block_buffer.span().as_span<uint32_t>();
|
||||||
|
|
||||||
|
next_block = block_span[local_index];
|
||||||
|
|
||||||
|
if (next_block == 0)
|
||||||
{
|
{
|
||||||
block = TRY(m_fs.reserve_free_block(block_group()));
|
if (!allocate)
|
||||||
|
return BAN::Optional<uint32_t>();
|
||||||
|
|
||||||
|
auto zero_buffer = TRY(m_fs.get_block_buffer());
|
||||||
|
memset(zero_buffer.data(), 0, zero_buffer.size());
|
||||||
|
|
||||||
|
next_block = TRY(m_fs.reserve_free_block(block_group()));
|
||||||
|
TRY(m_fs.write_block(next_block, zero_buffer));
|
||||||
m_blocks++;
|
m_blocks++;
|
||||||
|
|
||||||
auto block_buffer = TRY(m_fs.get_block_buffer());
|
block_span[local_index] = next_block;
|
||||||
memset(block_buffer.data(), 0x00, block_buffer.size());
|
|
||||||
TRY(m_fs.write_block(block, block_buffer));
|
TRY(m_fs.write_block(block, block_buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
return BAN::Optional<uint32_t>(block);
|
block_cache_add(block, local_index, next_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto block_buffer = TRY(m_fs.get_block_buffer());
|
return block_from_indirect_block_no_lock(next_block, index, depth - 1, allocate);
|
||||||
|
|
||||||
bool needs_write = false;
|
|
||||||
|
|
||||||
if (block != 0)
|
|
||||||
TRY(m_fs.read_block(block, block_buffer));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block = TRY(m_fs.reserve_free_block(block_group()));
|
|
||||||
m_blocks++;
|
|
||||||
|
|
||||||
memset(block_buffer.data(), 0, block_buffer.size());
|
|
||||||
|
|
||||||
needs_write = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t divisor = 1;
|
|
||||||
for (uint32_t i = 1; i < depth; i++)
|
|
||||||
divisor *= indices_per_fs_block;
|
|
||||||
|
|
||||||
uint32_t& new_block = block_buffer.span().as_span<uint32_t>()[(index / divisor) % indices_per_fs_block];
|
|
||||||
const auto old_block = new_block;
|
|
||||||
|
|
||||||
const auto result = TRY(block_from_indirect_block_no_lock(new_block, index, depth - 1, allocate));
|
|
||||||
|
|
||||||
if (needs_write || old_block != new_block)
|
|
||||||
TRY(m_fs.write_block(block, block_buffer));
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::Optional<uint32_t>> Ext2Inode::fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate)
|
BAN::ErrorOr<BAN::Optional<uint32_t>> Ext2Inode::fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate)
|
||||||
@@ -150,16 +144,29 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
data_block_index -= 12;
|
data_block_index -= 12;
|
||||||
|
|
||||||
if (data_block_index < indices_per_block)
|
uint32_t depth_block_count = indices_per_block;
|
||||||
return block_from_indirect_block_no_lock(m_ext2_blocks.block[12], data_block_index, 1, allocate);
|
for (size_t i = 0; i < 3; i++)
|
||||||
data_block_index -= indices_per_block;
|
{
|
||||||
|
if (data_block_index < depth_block_count)
|
||||||
|
{
|
||||||
|
auto& block = m_ext2_blocks.block[12 + i];
|
||||||
|
if (block == 0)
|
||||||
|
{
|
||||||
|
if (!allocate)
|
||||||
|
return BAN::Optional<uint32_t>();
|
||||||
|
|
||||||
if (data_block_index < indices_per_block * indices_per_block)
|
auto zero_buffer = TRY(m_fs.get_block_buffer());
|
||||||
return block_from_indirect_block_no_lock(m_ext2_blocks.block[13], data_block_index, 2, allocate);
|
memset(zero_buffer.data(), 0, zero_buffer.size());
|
||||||
data_block_index -= indices_per_block * indices_per_block;
|
|
||||||
|
|
||||||
if (data_block_index < indices_per_block * indices_per_block * indices_per_block)
|
block = TRY(m_fs.reserve_free_block(block_group()));
|
||||||
return block_from_indirect_block_no_lock(m_ext2_blocks.block[14], data_block_index, 3, allocate);
|
TRY(m_fs.write_block(block, zero_buffer));
|
||||||
|
m_blocks++;
|
||||||
|
}
|
||||||
|
return block_from_indirect_block_no_lock(block, data_block_index, i + 1, allocate);
|
||||||
|
}
|
||||||
|
data_block_index -= depth_block_count;
|
||||||
|
depth_block_count *= indices_per_block;
|
||||||
|
}
|
||||||
|
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
@@ -195,7 +202,7 @@ namespace Kernel
|
|||||||
memset(m_ext2_blocks.block, 0, sizeof(m_ext2_blocks.block));
|
memset(m_ext2_blocks.block, 0, sizeof(m_ext2_blocks.block));
|
||||||
memcpy(m_ext2_blocks.block, target.data(), target.size());
|
memcpy(m_ext2_blocks.block, target.data(), target.size());
|
||||||
m_size = target.size();
|
m_size = target.size();
|
||||||
TRY(sync_no_lock());
|
TRY(sync_inode_no_lock());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,13 +221,11 @@ namespace Kernel
|
|||||||
if (static_cast<BAN::make_unsigned_t<decltype(offset)>>(offset) >= UINT32_MAX || buffer.size() >= UINT32_MAX || buffer.size() >= (size_t)(UINT32_MAX - offset))
|
if (static_cast<BAN::make_unsigned_t<decltype(offset)>>(offset) >= UINT32_MAX || buffer.size() >= UINT32_MAX || buffer.size() >= (size_t)(UINT32_MAX - offset))
|
||||||
return BAN::Error::from_errno(EOVERFLOW);
|
return BAN::Error::from_errno(EOVERFLOW);
|
||||||
|
|
||||||
RWLockRDGuard _0(m_lock);
|
RWLockRDGuard _(m_lock);
|
||||||
|
|
||||||
if (static_cast<BAN::make_unsigned_t<decltype(offset)>>(offset) >= static_cast<size_t>(m_size))
|
if (static_cast<BAN::make_unsigned_t<decltype(offset)>>(offset) >= static_cast<size_t>(m_size))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
ScopedSync _1(*this);
|
|
||||||
|
|
||||||
uint32_t count = buffer.size();
|
uint32_t count = buffer.size();
|
||||||
if (offset + buffer.size() > static_cast<size_t>(m_size))
|
if (offset + buffer.size() > static_cast<size_t>(m_size))
|
||||||
count = m_size - offset;
|
count = m_size - offset;
|
||||||
@@ -267,7 +272,7 @@ namespace Kernel
|
|||||||
if (static_cast<size_t>(m_size) < offset + buffer.size())
|
if (static_cast<size_t>(m_size) < offset + buffer.size())
|
||||||
TRY(truncate_impl(offset + buffer.size()));
|
TRY(truncate_impl(offset + buffer.size()));
|
||||||
|
|
||||||
ScopedSync _(*this);
|
ScopedSync _1(*this);
|
||||||
|
|
||||||
const uint32_t block_size = blksize();
|
const uint32_t block_size = blksize();
|
||||||
|
|
||||||
@@ -334,7 +339,7 @@ namespace Kernel
|
|||||||
const auto old_size = static_cast<size_t>(m_size);
|
const auto old_size = static_cast<size_t>(m_size);
|
||||||
|
|
||||||
m_size = new_size;
|
m_size = new_size;
|
||||||
if (auto ret = sync_no_lock(); ret.is_error())
|
if (auto ret = sync_inode_no_lock(); ret.is_error())
|
||||||
{
|
{
|
||||||
m_size = old_size;
|
m_size = old_size;
|
||||||
return ret.release_error();
|
return ret.release_error();
|
||||||
@@ -343,82 +348,6 @@ namespace Kernel
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::chmod_impl(mode_t mode)
|
|
||||||
{
|
|
||||||
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
|
|
||||||
|
|
||||||
RWLockWRGuard _(m_lock);
|
|
||||||
|
|
||||||
if (m_mode == mode)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
const auto old_mode = m_mode.load();
|
|
||||||
|
|
||||||
m_mode = (m_mode & Inode::Mode::TYPE_MASK) | mode;
|
|
||||||
if (auto ret = sync_no_lock(); ret.is_error())
|
|
||||||
{
|
|
||||||
m_mode = old_mode;
|
|
||||||
return ret.release_error();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::chown_impl(uid_t uid, gid_t gid)
|
|
||||||
{
|
|
||||||
RWLockWRGuard _(m_lock);
|
|
||||||
|
|
||||||
if (m_uid == uid && m_gid == gid)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
const auto old_uid = m_uid.load();
|
|
||||||
const auto old_gid = m_gid.load();
|
|
||||||
|
|
||||||
m_uid = uid;
|
|
||||||
m_gid = gid;
|
|
||||||
if (auto ret = sync_no_lock(); ret.is_error())
|
|
||||||
{
|
|
||||||
m_uid = old_uid;
|
|
||||||
m_gid = old_gid;
|
|
||||||
return ret.release_error();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::utimens_impl(const timespec times[2])
|
|
||||||
{
|
|
||||||
RWLockWRGuard _(m_lock);
|
|
||||||
|
|
||||||
const uint32_t old_times[2] {
|
|
||||||
static_cast<uint32_t>(m_atime.tv_sec),
|
|
||||||
static_cast<uint32_t>(m_mtime.tv_sec),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (times[0].tv_nsec != UTIME_OMIT)
|
|
||||||
m_atime.tv_sec = times[0].tv_sec;
|
|
||||||
if (times[1].tv_nsec != UTIME_OMIT)
|
|
||||||
m_mtime.tv_sec = times[1].tv_sec;
|
|
||||||
|
|
||||||
if (auto ret = sync_no_lock(); ret.is_error())
|
|
||||||
{
|
|
||||||
m_atime.tv_sec = old_times[0];
|
|
||||||
m_mtime.tv_sec = old_times[1];
|
|
||||||
return ret.release_error();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::fsync_impl()
|
|
||||||
{
|
|
||||||
RWLockRDGuard _(m_lock);
|
|
||||||
for (size_t i = 0; i < max_used_data_block_count(); i++)
|
|
||||||
if (const auto fs_block = TRY(fs_block_of_data_block_index_no_lock(i, false)); fs_block.has_value())
|
|
||||||
TRY(m_fs.sync_block(fs_block.value()));
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::cleanup_indirect_block_no_lock(uint32_t block, uint32_t depth)
|
BAN::ErrorOr<void> Ext2Inode::cleanup_indirect_block_no_lock(uint32_t block, uint32_t depth)
|
||||||
{
|
{
|
||||||
ASSERT(block);
|
ASSERT(block);
|
||||||
@@ -467,7 +396,7 @@ done:
|
|||||||
// mark blocks as deleted
|
// mark blocks as deleted
|
||||||
memset(m_ext2_blocks.block, 0x00, sizeof(m_ext2_blocks.block));
|
memset(m_ext2_blocks.block, 0x00, sizeof(m_ext2_blocks.block));
|
||||||
|
|
||||||
TRY(sync_no_lock());
|
TRY(sync_inode_no_lock());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -604,7 +533,7 @@ done:
|
|||||||
|
|
||||||
const uint32_t new_ino = TRY(m_fs.create_inode(initialize_new_inode_info(mode, uid, gid)));
|
const uint32_t new_ino = TRY(m_fs.create_inode(initialize_new_inode_info(mode, uid, gid)));
|
||||||
|
|
||||||
auto inode_or_error = Ext2Inode::create(m_fs, new_ino);
|
auto inode_or_error = m_fs.open_inode(new_ino);
|
||||||
if (inode_or_error.is_error())
|
if (inode_or_error.is_error())
|
||||||
{
|
{
|
||||||
TRY(m_fs.delete_inode(new_ino));
|
TRY(m_fs.delete_inode(new_ino));
|
||||||
@@ -630,7 +559,7 @@ done:
|
|||||||
|
|
||||||
const uint32_t new_ino = TRY(m_fs.create_inode(initialize_new_inode_info(mode, uid, gid)));
|
const uint32_t new_ino = TRY(m_fs.create_inode(initialize_new_inode_info(mode, uid, gid)));
|
||||||
|
|
||||||
auto inode_or_error = Ext2Inode::create(m_fs, new_ino);
|
auto inode_or_error = m_fs.open_inode(new_ino);
|
||||||
if (inode_or_error.is_error())
|
if (inode_or_error.is_error())
|
||||||
{
|
{
|
||||||
TRY(m_fs.delete_inode(new_ino));
|
TRY(m_fs.delete_inode(new_ino));
|
||||||
@@ -747,7 +676,7 @@ done:
|
|||||||
memcpy(new_entry.name, name.data(), name.size());
|
memcpy(new_entry.name, name.data(), name.size());
|
||||||
|
|
||||||
inode.m_nlink++;
|
inode.m_nlink++;
|
||||||
TRY(inode.sync_no_lock());
|
TRY(inode.sync_inode_no_lock());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
@@ -872,13 +801,13 @@ needs_new_block:
|
|||||||
if (entry_name == "."_sv)
|
if (entry_name == "."_sv)
|
||||||
{
|
{
|
||||||
m_nlink--;
|
m_nlink--;
|
||||||
TRY(sync_no_lock());
|
TRY(sync_inode_no_lock());
|
||||||
}
|
}
|
||||||
else if (entry_name == ".."_sv)
|
else if (entry_name == ".."_sv)
|
||||||
{
|
{
|
||||||
auto parent = TRY(Ext2Inode::create(m_fs, entry.inode));
|
auto parent = TRY(m_fs.open_inode(entry.inode));
|
||||||
parent->m_nlink--;
|
parent->m_nlink--;
|
||||||
TRY(parent->sync_no_lock());
|
TRY(parent->sync_inode_no_lock());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
@@ -922,7 +851,7 @@ needs_new_block:
|
|||||||
auto& entry = block_buffer.span().slice(offset).as<Ext2::LinkedDirectoryEntry>();
|
auto& entry = block_buffer.span().slice(offset).as<Ext2::LinkedDirectoryEntry>();
|
||||||
if (entry.inode && name == BAN::StringView(entry.name, entry.name_len))
|
if (entry.inode && name == BAN::StringView(entry.name, entry.name_len))
|
||||||
{
|
{
|
||||||
auto inode = TRY(Ext2Inode::create(m_fs, entry.inode));
|
auto inode = TRY(m_fs.open_inode(entry.inode));
|
||||||
if (cleanup_directory && inode->mode().ifdir())
|
if (cleanup_directory && inode->mode().ifdir())
|
||||||
{
|
{
|
||||||
if (!TRY(inode->is_directory_empty_no_lock()))
|
if (!TRY(inode->is_directory_empty_no_lock()))
|
||||||
@@ -935,16 +864,12 @@ needs_new_block:
|
|||||||
else
|
else
|
||||||
inode->m_nlink--;
|
inode->m_nlink--;
|
||||||
|
|
||||||
TRY(sync_no_lock());
|
TRY(sync_inode_no_lock());
|
||||||
|
|
||||||
// NOTE: If this was the last link to inode we must
|
// NOTE: If this was the last link to inode we must
|
||||||
// remove it from inode cache to trigger cleanup
|
// remove it from inode cache to trigger cleanup
|
||||||
if (inode->nlink() == 0)
|
if (inode->nlink() == 0)
|
||||||
{
|
m_fs.remove_from_cache(inode->ino());
|
||||||
auto& cache = m_fs.inode_cache();
|
|
||||||
if (cache.contains(inode->ino()))
|
|
||||||
cache.remove(inode->ino());
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: This should expand the last inode if exists
|
// FIXME: This should expand the last inode if exists
|
||||||
entry.inode = 0;
|
entry.inode = 0;
|
||||||
@@ -954,6 +879,8 @@ needs_new_block:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dir_cache_remove(name);
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -964,13 +891,9 @@ needs_new_block:
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Ext2Inode::sync_no_lock()
|
BAN::ErrorOr<void> Ext2Inode::sync_inode_no_lock()
|
||||||
{
|
{
|
||||||
auto inode_location = TRY(m_fs.locate_inode(ino()));
|
const Ext2::Inode inode {
|
||||||
auto block_buffer = TRY(m_fs.get_block_buffer());
|
|
||||||
|
|
||||||
TRY(m_fs.read_block(inode_location.block, block_buffer));
|
|
||||||
Ext2::Inode inode {
|
|
||||||
.mode = static_cast<uint16_t>(m_mode),
|
.mode = static_cast<uint16_t>(m_mode),
|
||||||
.uid = static_cast<uint16_t>(m_uid),
|
.uid = static_cast<uint16_t>(m_uid),
|
||||||
.size = static_cast<uint32_t>(m_size),
|
.size = static_cast<uint32_t>(m_size),
|
||||||
@@ -988,8 +911,15 @@ needs_new_block:
|
|||||||
.file_acl = m_og_file_acl,
|
.file_acl = m_og_file_acl,
|
||||||
.dir_acl = m_og_dir_acl,
|
.dir_acl = m_og_dir_acl,
|
||||||
.faddr = m_og_faddr,
|
.faddr = m_og_faddr,
|
||||||
.osd2 = m_og_osd2
|
.osd2 = m_og_osd2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto inode_location = TRY(m_fs.locate_inode(ino()));
|
||||||
|
auto block_buffer = TRY(m_fs.get_block_buffer());
|
||||||
|
|
||||||
|
// FIXME: race condition when syncing multiple inodes :)
|
||||||
|
|
||||||
|
TRY(m_fs.read_block(inode_location.block, block_buffer));
|
||||||
if (memcmp(block_buffer.data() + inode_location.offset, &inode, sizeof(Ext2::Inode)))
|
if (memcmp(block_buffer.data() + inode_location.offset, &inode, sizeof(Ext2::Inode)))
|
||||||
{
|
{
|
||||||
memcpy(block_buffer.data() + inode_location.offset, &inode, sizeof(Ext2::Inode));
|
memcpy(block_buffer.data() + inode_location.offset, &inode, sizeof(Ext2::Inode));
|
||||||
@@ -1009,6 +939,9 @@ needs_new_block:
|
|||||||
{
|
{
|
||||||
ASSERT(mode().ifdir());
|
ASSERT(mode().ifdir());
|
||||||
|
|
||||||
|
if (auto cached = dir_cache_find(file_name))
|
||||||
|
return cached;
|
||||||
|
|
||||||
auto block_buffer = TRY(m_fs.get_block_buffer());
|
auto block_buffer = TRY(m_fs.get_block_buffer());
|
||||||
|
|
||||||
for (uint32_t i = 0; i < max_used_data_block_count(); i++)
|
for (uint32_t i = 0; i < max_used_data_block_count(); i++)
|
||||||
@@ -1024,11 +957,129 @@ needs_new_block:
|
|||||||
auto& entry = entry_span.as<const Ext2::LinkedDirectoryEntry>();
|
auto& entry = entry_span.as<const Ext2::LinkedDirectoryEntry>();
|
||||||
BAN::StringView entry_name(entry.name, entry.name_len);
|
BAN::StringView entry_name(entry.name, entry.name_len);
|
||||||
if (entry.inode && entry_name == file_name)
|
if (entry.inode && entry_name == file_name)
|
||||||
return BAN::RefPtr<Inode>(TRY(Ext2Inode::create(m_fs, entry.inode)));
|
{
|
||||||
|
auto inode = BAN::RefPtr<Inode>(TRY(m_fs.open_inode(entry.inode)));
|
||||||
|
dir_cache_add(file_name, inode);
|
||||||
|
return inode;
|
||||||
|
}
|
||||||
entry_span = entry_span.slice(entry.rec_len);
|
entry_span = entry_span.slice(entry.rec_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return BAN::Error::from_errno(ENOENT);
|
return BAN::Error::from_errno(ENOENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BAN::Optional<uint32_t> Ext2Inode::block_cache_find(uint32_t block, uint32_t index) const
|
||||||
|
{
|
||||||
|
SpinLockGuard _(m_block_cache_lock);
|
||||||
|
for (const auto& cache : m_block_cache)
|
||||||
|
{
|
||||||
|
if (cache.block != block || cache.index != index)
|
||||||
|
continue;
|
||||||
|
cache.freq++;
|
||||||
|
return cache.target;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ext2Inode::block_cache_remove(uint32_t block, uint32_t index)
|
||||||
|
{
|
||||||
|
SpinLockGuard _(m_block_cache_lock);
|
||||||
|
for (auto& cache : m_block_cache)
|
||||||
|
{
|
||||||
|
if (cache.block != block || cache.index != index)
|
||||||
|
continue;
|
||||||
|
cache = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ext2Inode::block_cache_add(uint32_t block, uint32_t index, uint32_t target)
|
||||||
|
{
|
||||||
|
SpinLockGuard _(m_block_cache_lock);
|
||||||
|
|
||||||
|
size_t min_freq = BAN::numeric_limits<size_t>::max();
|
||||||
|
size_t min_index = 0;
|
||||||
|
for (size_t i = 0; i < m_block_cache.size(); i++)
|
||||||
|
{
|
||||||
|
const auto& cache = m_block_cache[i];
|
||||||
|
if (cache.block == block && cache.index == index)
|
||||||
|
{
|
||||||
|
ASSERT(cache.target == target);
|
||||||
|
cache.freq++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (cache.freq < min_freq)
|
||||||
|
{
|
||||||
|
min_freq = cache.freq;
|
||||||
|
min_index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_block_cache[min_index] = {
|
||||||
|
.freq = 1,
|
||||||
|
.block = block,
|
||||||
|
.index = index,
|
||||||
|
.target = target,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::RefPtr<Inode> Ext2Inode::dir_cache_find(BAN::StringView name) const
|
||||||
|
{
|
||||||
|
RWLockRDGuard _(m_dir_cache_lock);
|
||||||
|
for (const auto& cache : m_dir_cache)
|
||||||
|
{
|
||||||
|
if (!cache.inode || name != BAN::StringView(cache.name, cache.name_len))
|
||||||
|
continue;
|
||||||
|
BAN::atomic_add_fetch(cache.freq, 1);
|
||||||
|
return cache.inode;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ext2Inode::dir_cache_remove(BAN::StringView name)
|
||||||
|
{
|
||||||
|
RWLockWRGuard _(m_dir_cache_lock);
|
||||||
|
for (auto& cache : m_dir_cache)
|
||||||
|
{
|
||||||
|
if (!cache.inode || name != BAN::StringView(cache.name, cache.name_len))
|
||||||
|
continue;
|
||||||
|
cache.freq = 0;
|
||||||
|
cache.inode = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ext2Inode::dir_cache_add(BAN::StringView name, BAN::RefPtr<Inode> inode)
|
||||||
|
{
|
||||||
|
RWLockWRGuard _(m_dir_cache_lock);
|
||||||
|
|
||||||
|
if (m_dir_cache.empty() && m_dir_cache.resize(dir_cache_size).is_error())
|
||||||
|
return;
|
||||||
|
|
||||||
|
size_t min_freq = BAN::numeric_limits<size_t>::max();
|
||||||
|
size_t min_index = 0;
|
||||||
|
for (size_t i = 0; i < m_dir_cache.size(); i++)
|
||||||
|
{
|
||||||
|
const auto& cache = m_dir_cache[i];
|
||||||
|
if (cache.inode && name == BAN::StringView(cache.name, cache.name_len))
|
||||||
|
{
|
||||||
|
ASSERT(cache.inode == inode);
|
||||||
|
cache.freq++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (cache.freq < min_freq)
|
||||||
|
{
|
||||||
|
min_freq = cache.freq;
|
||||||
|
min_index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& cache = m_dir_cache[min_index];
|
||||||
|
cache.freq = 1;
|
||||||
|
cache.inode = inode;
|
||||||
|
cache.name_len = name.size();
|
||||||
|
memcpy(cache.name, name.data(), name.size());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <kernel/Memory/FileBackedRegion.h>
|
#include <kernel/Memory/FileBackedRegion.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <sys/statvfs.h>
|
#include <sys/statvfs.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
@@ -246,55 +247,93 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
|
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
|
||||||
{
|
{
|
||||||
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
|
|
||||||
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
||||||
return BAN::Error::from_errno(EROFS);
|
return BAN::Error::from_errno(EROFS);
|
||||||
return chmod_impl(mode);
|
|
||||||
|
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
|
||||||
|
mode |= m_mode & Inode::Mode::TYPE_MASK;
|
||||||
|
|
||||||
|
const auto old_mode = m_mode.exchange(mode);
|
||||||
|
|
||||||
|
if (auto ret = sync_inode(SyncType::Mode); ret.is_error())
|
||||||
|
{
|
||||||
|
m_mode.compare_exchange(mode, old_mode);
|
||||||
|
return ret.release_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Inode::chown(uid_t uid, gid_t gid)
|
BAN::ErrorOr<void> Inode::chown(uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
||||||
return BAN::Error::from_errno(EROFS);
|
return BAN::Error::from_errno(EROFS);
|
||||||
return chown_impl(uid, gid);
|
|
||||||
|
// TODO: unify uid and gid to a single atomic operation.
|
||||||
|
// this needs 64 bit atomic support from 32 bit target
|
||||||
|
|
||||||
|
const auto old_uid = m_uid.exchange(uid);
|
||||||
|
const auto old_gid = m_gid.exchange(gid);
|
||||||
|
|
||||||
|
if (auto ret = sync_inode(SyncType::UidGid); ret.is_error())
|
||||||
|
{
|
||||||
|
m_uid.compare_exchange(uid, old_uid);
|
||||||
|
m_gid.compare_exchange(gid, old_gid);
|
||||||
|
return ret.release_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Inode::utimens(const timespec times[2])
|
BAN::ErrorOr<void> Inode::utimens(const timespec times[2])
|
||||||
{
|
{
|
||||||
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
|
||||||
return BAN::Error::from_errno(EROFS);
|
return BAN::Error::from_errno(EROFS);
|
||||||
return utimens_impl(times);
|
|
||||||
|
// TODO: make these atomic
|
||||||
|
|
||||||
|
const auto old_atime = m_atime;
|
||||||
|
const auto old_mtime = m_mtime;
|
||||||
|
|
||||||
|
m_atime = times[0];
|
||||||
|
m_mtime = times[1];
|
||||||
|
|
||||||
|
if (auto ret = sync_inode(SyncType::Times); ret.is_error())
|
||||||
|
{
|
||||||
|
m_atime = old_atime;
|
||||||
|
m_mtime = old_mtime;
|
||||||
|
return ret.release_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Inode::fsync()
|
BAN::ErrorOr<void> Inode::fsync()
|
||||||
{
|
{
|
||||||
// TODO: should we sync shared data?
|
// TODO: should we sync MAP_SHARED data?
|
||||||
return fsync_impl();
|
TRY(sync_inode(SyncType::General));
|
||||||
|
TRY(sync_data());
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Inode::can_read() const
|
BAN::ErrorOr<long> Inode::ioctl(unsigned long request, void* arg)
|
||||||
{
|
{
|
||||||
return can_read_impl();
|
auto ret = ioctl_impl(request, arg);
|
||||||
}
|
if (!ret.is_error() || ret.error().get_error_code() != ENOTSUP)
|
||||||
|
return BAN::move(ret);
|
||||||
|
|
||||||
bool Inode::can_write() const
|
switch (request)
|
||||||
{
|
{
|
||||||
return can_write_impl();
|
case TIOCGWINSZ:
|
||||||
}
|
case TIOCSWINSZ:
|
||||||
|
case TCGETS:
|
||||||
bool Inode::has_error() const
|
case TCSETS:
|
||||||
{
|
case TCSETSW:
|
||||||
return has_error_impl();
|
case TCSETSF:
|
||||||
}
|
return BAN::Error::from_errno(EINVAL);
|
||||||
|
default:
|
||||||
bool Inode::has_hungup() const
|
return BAN::Error::from_errno(ENOTSUP);
|
||||||
{
|
}
|
||||||
return has_hungup_impl();
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ErrorOr<long> Inode::ioctl(int request, void* arg)
|
|
||||||
{
|
|
||||||
return ioctl_impl(request, arg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> Inode::add_epoll(class Epoll* epoll)
|
BAN::ErrorOr<void> Inode::add_epoll(class Epoll* epoll)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <BAN/HashMap.h>
|
||||||
#include <kernel/FS/Pipe.h>
|
#include <kernel/FS/Pipe.h>
|
||||||
#include <kernel/Lock/LockGuard.h>
|
#include <kernel/Lock/LockGuard.h>
|
||||||
#include <kernel/Thread.h>
|
#include <kernel/Thread.h>
|
||||||
@@ -12,33 +13,124 @@ namespace Kernel
|
|||||||
|
|
||||||
static constexpr size_t s_pipe_buffer_size = 0x10000;
|
static constexpr size_t s_pipe_buffer_size = 0x10000;
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<Inode>> Pipe::create(const Credentials& credentials)
|
static Mutex s_named_pipe_mutex;
|
||||||
|
static BAN::HashMap<BAN::RefPtr<Inode>, BAN::WeakPtr<Pipe>> s_named_pipes;
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::RefPtr<Inode>> Pipe::open(BAN::RefPtr<Inode> inode, int status_flags)
|
||||||
{
|
{
|
||||||
auto* pipe_ptr = new Pipe(credentials);
|
BAN::RefPtr<Pipe> pipe;
|
||||||
|
|
||||||
|
{
|
||||||
|
LockGuard _(s_named_pipe_mutex);
|
||||||
|
|
||||||
|
auto it = s_named_pipes.find(inode);
|
||||||
|
if (it == s_named_pipes.end())
|
||||||
|
it = TRY(s_named_pipes.insert(inode, {}));
|
||||||
|
|
||||||
|
if (!(pipe = it->value.lock()))
|
||||||
|
{
|
||||||
|
// FIXME: these should probably reference the underlying inode(?)
|
||||||
|
const struct stat st {
|
||||||
|
.st_dev = inode->dev(),
|
||||||
|
.st_ino = inode->ino(),
|
||||||
|
.st_mode = inode->mode().mode,
|
||||||
|
.st_nlink = inode->nlink(),
|
||||||
|
.st_uid = inode->uid(),
|
||||||
|
.st_gid = inode->gid(),
|
||||||
|
.st_rdev = inode->rdev(),
|
||||||
|
.st_size = inode->size(),
|
||||||
|
.st_atim = inode->atime(),
|
||||||
|
.st_mtim = inode->mtime(),
|
||||||
|
.st_ctim = inode->ctime(),
|
||||||
|
.st_blksize = inode->blksize(),
|
||||||
|
.st_blocks = inode->blocks(),
|
||||||
|
};
|
||||||
|
|
||||||
|
auto* pipe_ptr = new Pipe(st);
|
||||||
|
if (pipe_ptr == nullptr)
|
||||||
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
|
pipe = BAN::RefPtr<Pipe>::adopt(pipe_ptr);
|
||||||
|
pipe->m_buffer = TRY(ByteRingBuffer::create(s_pipe_buffer_size));
|
||||||
|
pipe->m_named_inode = inode;
|
||||||
|
|
||||||
|
it->value = TRY(pipe->get_weak_ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LockGuard _(pipe->m_mutex);
|
||||||
|
|
||||||
|
if (status_flags & O_RDONLY)
|
||||||
|
pipe->m_reading_count++;
|
||||||
|
if (status_flags & O_WRONLY)
|
||||||
|
pipe->m_writing_count++;
|
||||||
|
|
||||||
|
if (status_flags & O_NONBLOCK)
|
||||||
|
{
|
||||||
|
if ((status_flags & O_WRONLY) && pipe->m_writing_count == 0)
|
||||||
|
return BAN::Error::from_errno(ENXIO);
|
||||||
|
return BAN::RefPtr<Inode>(pipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& block_value = (status_flags & O_WRONLY) ? pipe->m_reading_count : pipe->m_writing_count;
|
||||||
|
while (block_value == 0)
|
||||||
|
TRY(Thread::current().block_or_eintr_indefinite(pipe->m_thread_blocker, &pipe->m_mutex));
|
||||||
|
return BAN::RefPtr<Inode>(pipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::RefPtr<Inode>> Pipe::create(uid_t uid, gid_t gid)
|
||||||
|
{
|
||||||
|
const timespec current_time = SystemTimer::get().real_time();
|
||||||
|
const struct stat st {
|
||||||
|
.st_dev = 0, // FIXME
|
||||||
|
.st_ino = 0, // FIXME
|
||||||
|
.st_mode = Mode::IFIFO | Mode::IRUSR | Mode::IWUSR,
|
||||||
|
.st_nlink = 0,
|
||||||
|
.st_uid = uid,
|
||||||
|
.st_gid = gid,
|
||||||
|
.st_rdev = 0, // FIXME
|
||||||
|
.st_size = 0,
|
||||||
|
.st_atim = current_time,
|
||||||
|
.st_mtim = current_time,
|
||||||
|
.st_ctim = current_time,
|
||||||
|
.st_blksize = PAGE_SIZE,
|
||||||
|
.st_blocks = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto* pipe_ptr = new Pipe(st);
|
||||||
if (pipe_ptr == nullptr)
|
if (pipe_ptr == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
auto pipe = BAN::RefPtr<Pipe>::adopt(pipe_ptr);
|
auto pipe = BAN::RefPtr<Pipe>::adopt(pipe_ptr);
|
||||||
pipe->m_buffer = TRY(ByteRingBuffer::create(s_pipe_buffer_size));
|
pipe->m_buffer = TRY(ByteRingBuffer::create(s_pipe_buffer_size));
|
||||||
|
pipe->m_reading_count++;
|
||||||
|
pipe->m_writing_count++;
|
||||||
return BAN::RefPtr<Inode>(pipe);
|
return BAN::RefPtr<Inode>(pipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pipe::Pipe(const Credentials& credentials)
|
Pipe::Pipe(const struct stat& st)
|
||||||
{
|
{
|
||||||
timespec current_time = SystemTimer::get().real_time();
|
m_ino = st.st_ino;
|
||||||
m_atime = current_time;
|
m_mode = st.st_mode;
|
||||||
m_mtime = current_time;
|
m_nlink = st.st_nlink;
|
||||||
m_ctime = current_time;
|
m_uid = st.st_uid;
|
||||||
m_uid = credentials.euid();
|
m_gid = st.st_gid;
|
||||||
m_gid = credentials.egid();
|
m_size = st.st_size;
|
||||||
|
m_atime = st.st_atim;
|
||||||
|
m_mtime = st.st_mtim;
|
||||||
|
m_ctime = st.st_ctim;
|
||||||
|
m_blksize = st.st_blksize;
|
||||||
|
m_blocks = st.st_blocks;
|
||||||
|
m_dev = st.st_dev;
|
||||||
|
m_rdev = st.st_rdev;
|
||||||
|
|
||||||
m_ino = 0; // FIXME
|
m_kind |= InodeKind::PIPE;
|
||||||
m_mode = { Mode::IFIFO | Mode::IRUSR | Mode::IWUSR };
|
}
|
||||||
m_nlink = 1;
|
|
||||||
m_size = 0;
|
Pipe::~Pipe()
|
||||||
m_blksize = 4096;
|
{
|
||||||
m_blocks = 0;
|
if (!m_named_inode)
|
||||||
m_dev = 0; // FIXME
|
return;
|
||||||
m_rdev = 0; // FIXME
|
LockGuard _(s_named_pipe_mutex);
|
||||||
|
s_named_pipes.remove(m_named_inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pipe::on_clone(int status_flags)
|
void Pipe::on_clone(int status_flags)
|
||||||
@@ -81,6 +173,44 @@ namespace Kernel
|
|||||||
m_thread_blocker.unblock();
|
m_thread_blocker.unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<void> Pipe::sync_inode(SyncType type)
|
||||||
|
{
|
||||||
|
if (!m_named_inode)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case SyncType::General:
|
||||||
|
break;
|
||||||
|
case SyncType::Mode:
|
||||||
|
TRY(m_named_inode->chmod(m_mode));
|
||||||
|
break;
|
||||||
|
case SyncType::UidGid:
|
||||||
|
TRY(m_named_inode->chown(m_uid, m_gid));
|
||||||
|
break;
|
||||||
|
case SyncType::Times:
|
||||||
|
const timespec times[] { m_atime, m_mtime };
|
||||||
|
TRY(m_named_inode->utimens(times));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_mode = m_named_inode->mode().mode;
|
||||||
|
|
||||||
|
m_uid = m_named_inode->uid();
|
||||||
|
m_gid = m_named_inode->gid();
|
||||||
|
|
||||||
|
m_atime = m_named_inode->atime();
|
||||||
|
m_mtime = m_named_inode->mtime();
|
||||||
|
m_ctime = m_named_inode->ctime();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<void> Pipe::sync_data()
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> Pipe::read_impl(off_t, BAN::ByteSpan buffer)
|
BAN::ErrorOr<size_t> Pipe::read_impl(off_t, BAN::ByteSpan buffer)
|
||||||
{
|
{
|
||||||
LockGuard _(m_mutex);
|
LockGuard _(m_mutex);
|
||||||
@@ -133,16 +263,9 @@ namespace Kernel
|
|||||||
return to_copy;
|
return to_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<void> Pipe::truncate_impl(size_t)
|
||||||
BAN::ErrorOr<long> Pipe::ioctl_impl(int cmd, void* arg)
|
|
||||||
{
|
{
|
||||||
switch (cmd)
|
return BAN::Error::from_errno(ENODEV);
|
||||||
{
|
|
||||||
case TIOCGWINSZ:
|
|
||||||
case TIOCSWINSZ:
|
|
||||||
return BAN::Error::from_errno(EINVAL);
|
|
||||||
}
|
|
||||||
return Inode::ioctl_impl(cmd, arg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,22 +16,23 @@ namespace Kernel
|
|||||||
MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
|
MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
|
||||||
|
|
||||||
auto meminfo_inode = MUST(ProcROInode::create_new(
|
auto meminfo_inode = MUST(ProcROInode::create_new(
|
||||||
[](off_t offset, BAN::ByteSpan buffer) -> size_t
|
[](off_t offset, BAN::ByteSpan buffer, void*) -> BAN::ErrorOr<size_t>
|
||||||
{
|
{
|
||||||
ASSERT(offset >= 0);
|
ASSERT(offset >= 0);
|
||||||
if ((size_t)offset >= sizeof(full_meminfo_t))
|
if ((size_t)offset >= sizeof(full_meminfo_t))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
full_meminfo_t meminfo;
|
const full_meminfo_t meminfo {
|
||||||
meminfo.page_size = PAGE_SIZE;
|
.page_size = PAGE_SIZE,
|
||||||
meminfo.free_pages = Heap::get().free_pages();
|
.free_pages = Heap::get().free_pages(),
|
||||||
meminfo.used_pages = Heap::get().used_pages();
|
.used_pages = Heap::get().used_pages(),
|
||||||
|
};
|
||||||
|
|
||||||
size_t bytes = BAN::Math::min<size_t>(sizeof(full_meminfo_t) - offset, buffer.size());
|
size_t bytes = BAN::Math::min<size_t>(sizeof(full_meminfo_t) - offset, buffer.size());
|
||||||
memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes);
|
memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes);
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
},
|
||||||
*s_instance, 0444, 0, 0
|
*s_instance, nullptr, 0444, 0, 0
|
||||||
));
|
));
|
||||||
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*meminfo_inode, "meminfo"_sv));
|
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*meminfo_inode, "meminfo"_sv));
|
||||||
|
|
||||||
@@ -48,6 +49,35 @@ namespace Kernel
|
|||||||
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*self_inode, "self"_sv));
|
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*self_inode, "self"_sv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProcFileSystem::post_scheduler_initialize()
|
||||||
|
{
|
||||||
|
MUST(s_instance->root_inode()->create_directory("cpu"_sv, Inode::Mode::IFDIR | 0555, 0, 0));
|
||||||
|
auto cpu_directory = MUST(s_instance->root_inode()->find_inode("cpu"_sv));
|
||||||
|
|
||||||
|
for (size_t i = 0; i < Processor::count(); i++)
|
||||||
|
{
|
||||||
|
auto cpu_inode = MUST(ProcROInode::create_new(
|
||||||
|
[](off_t offset, BAN::ByteSpan buffer, void* index_ptr) -> BAN::ErrorOr<size_t>
|
||||||
|
{
|
||||||
|
ASSERT(offset >= 0);
|
||||||
|
|
||||||
|
const size_t index = reinterpret_cast<uintptr_t>(index_ptr);
|
||||||
|
const auto load_stats = Processor::get_load_stats(index);
|
||||||
|
|
||||||
|
auto string = TRY(BAN::String::formatted("{} {}", load_stats.ns_idle, load_stats.ns_total));
|
||||||
|
if (static_cast<size_t>(offset) >= string.size())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
const size_t bytes = BAN::Math::min<size_t>(string.size() - offset, buffer.size());
|
||||||
|
memcpy(buffer.data(), string.data() + offset, bytes);
|
||||||
|
return bytes;
|
||||||
|
},
|
||||||
|
*s_instance, reinterpret_cast<void*>(i), 0444, 0, 0
|
||||||
|
));
|
||||||
|
MUST(cpu_directory->link_inode(MUST(BAN::String::formatted("{}", i)), cpu_inode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ProcFileSystem& ProcFileSystem::get()
|
ProcFileSystem& ProcFileSystem::get()
|
||||||
{
|
{
|
||||||
ASSERT(s_instance);
|
ASSERT(s_instance);
|
||||||
|
|||||||
@@ -16,12 +16,13 @@ namespace Kernel
|
|||||||
|
|
||||||
TRY(inode->link_inode(*inode, "."_sv));
|
TRY(inode->link_inode(*inode, "."_sv));
|
||||||
TRY(inode->link_inode(static_cast<TmpInode&>(*fs.root_inode()), ".."_sv));
|
TRY(inode->link_inode(static_cast<TmpInode&>(*fs.root_inode()), ".."_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_meminfo, fs, 0400)), "meminfo"_sv));
|
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_meminfo, fs, 0444)), "meminfo"_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_cmdline, fs, 0444)), "cmdline"_sv));
|
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_cmdline, fs, 0444)), "cmdline"_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_environ, fs, 0400)), "environ"_sv));
|
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_environ, fs, 0400)), "environ"_sv));
|
||||||
|
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_cputime, fs, 0444)), "cputime"_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_cwd, fs, 0777)), "cwd"_sv));
|
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_cwd, fs, 0777)), "cwd"_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_exe, fs, 0777)), "exe"_sv));
|
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_exe, fs, 0777)), "exe"_sv));
|
||||||
TRY(inode->link_inode(*MUST(ProcFDDirectoryInode::create_new(process, fs, 0777)), "fd"_sv));
|
TRY(inode->link_inode(*MUST(ProcFDDirectoryInode::create_new(process, fs, 0500)), "fd"_sv));
|
||||||
|
|
||||||
return inode;
|
return inode;
|
||||||
}
|
}
|
||||||
@@ -38,6 +39,7 @@ namespace Kernel
|
|||||||
(void)TmpDirectoryInode::unlink_impl("meminfo"_sv);
|
(void)TmpDirectoryInode::unlink_impl("meminfo"_sv);
|
||||||
(void)TmpDirectoryInode::unlink_impl("cmdline"_sv);
|
(void)TmpDirectoryInode::unlink_impl("cmdline"_sv);
|
||||||
(void)TmpDirectoryInode::unlink_impl("environ"_sv);
|
(void)TmpDirectoryInode::unlink_impl("environ"_sv);
|
||||||
|
(void)TmpDirectoryInode::unlink_impl("cputime"_sv);
|
||||||
(void)TmpDirectoryInode::unlink_impl("cwd"_sv);
|
(void)TmpDirectoryInode::unlink_impl("cwd"_sv);
|
||||||
(void)TmpDirectoryInode::unlink_impl("exe"_sv);
|
(void)TmpDirectoryInode::unlink_impl("exe"_sv);
|
||||||
(void)TmpDirectoryInode::unlink_impl("fd"_sv);
|
(void)TmpDirectoryInode::unlink_impl("fd"_sv);
|
||||||
@@ -95,19 +97,20 @@ namespace Kernel
|
|||||||
return (m_process.*m_callback)();
|
return (m_process.*m_callback)();
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create_new(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create_new(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem& fs, void* argument, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
|
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
|
||||||
|
|
||||||
auto* inode_ptr = new ProcROInode(callback, fs, inode_info);
|
auto* inode_ptr = new ProcROInode(callback, fs, argument, inode_info);
|
||||||
if (inode_ptr == nullptr)
|
if (inode_ptr == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
|
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcROInode::ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem& fs, const TmpInodeInfo& inode_info)
|
ProcROInode::ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem& fs, void* argument, const TmpInodeInfo& inode_info)
|
||||||
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
|
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
|
||||||
, m_callback(callback)
|
, m_callback(callback)
|
||||||
|
, m_argument(argument)
|
||||||
{
|
{
|
||||||
m_mode |= Inode::Mode::IFREG;
|
m_mode |= Inode::Mode::IFREG;
|
||||||
}
|
}
|
||||||
@@ -116,7 +119,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
if (offset < 0)
|
if (offset < 0)
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
return m_callback(offset, buffer);
|
return TRY(m_callback(offset, buffer, m_argument));
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> ProcSymlinkInode::create_new(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> ProcSymlinkInode::create_new(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user