Compare commits

..

13 Commits

Author SHA1 Message Date
d2c5242267 Kernel: Fix ByteRingBuffer->back() 2026-05-02 20:48:02 +03:00
96ae432bcf fixup 2026-05-02 20:05:13 +03:00
d08f7b1dee Kernel: Cleanup inline assembly accessing cpu specific data 2026-05-02 18:29:07 +03:00
23a0226f1b LibC: Mark exit as noreturn 2026-05-02 18:12:20 +03:00
33ea0f07b7 Kernel: Calculate internet checksum in host endian
No need to swap bytes of every 16 bit word in the packet, we can just do
one swap at the return
2026-05-02 18:11:29 +03:00
3874e0ed1e Kernel: Pass current cpu index as a GDT limit
I had no idea LSL was an instruction. This cleans up code to get the
current cpu by a lot and does not require extra segment usage :D
2026-05-02 18:10:10 +03:00
d49d260a09 BAN: Fix HashSet insertion and general cleanup
When inserting, look through the full hash chain before adding a new
entry, this fixes double insertion of the same key when there were a
hash collision

Instead of storing used and removed bits, store 2 bit state. This makes
the code cleaner and easier to not make mistakes
2026-05-02 16:01:06 +03:00
73b03860f4 Kernel: Use empty string instead of nullptr for non existing proc name 2026-05-02 15:54:37 +03:00
b9754859b2 Kernel: Remove kmalloc_vaddr_of
This is no longer needed. It was only used for x86_64 paging and AP
stack initialization
2026-05-02 15:53:29 +03:00
da50b654ab Kernel: Wrap syscall macro value in paranthesis 2026-05-02 15:52:18 +03:00
8869cc7b8c Kernel: Stop stacktrace dump on null bp
This makes stack traces not crash before IDT is initialized
2026-05-02 15:51:12 +03:00
d2b9b49cb0 Kernel: Rewrite paging and AP initialization
Initial step of paging now just prepares fast page for heap, actual page
table initialization happens after heap is initialized which allows
x86_64 to never depend on kmalloc for pages.

Processor's stacks are now also spawned with PMM/VMM allocated stacks
instead of kmalloc identity mapped.
2026-05-02 15:45:08 +03:00
21a2e7fd51 BAN: Fix HashSet 2026-05-02 13:12:22 +03:00
222 changed files with 3318 additions and 5191 deletions

View File

@@ -98,8 +98,7 @@ namespace BAN
struct EntryHash
{
template<detail::HashMapFindable<Key, HASH, COMP> U>
constexpr bool operator()(const U& a)
constexpr bool operator()(const Key& a)
{
return HASH()(a);
}
@@ -111,8 +110,7 @@ namespace BAN
struct EntryComp
{
template<detail::HashMapFindable<Key, HASH, COMP> U>
constexpr bool operator()(const Entry& a, const U& b)
constexpr bool operator()(const Entry& a, const Key& b)
{
return COMP()(a.key, b);
}

View File

@@ -21,47 +21,26 @@ namespace BAN::sort
namespace detail
{
template<typename It>
struct partition_pair
{
It lt;
It gt;
};
template<typename It, typename Comp>
partition_pair<It> partition(It begin, It end, Comp comp)
It partition(It begin, It end, Comp comp)
{
It pivot = next(begin, distance(begin, end) / 2);
It pivot = prev(end, 1);
It lt = begin;
It eq = begin;
It gt = end;
while (eq != gt)
It it1 = begin;
for (It it2 = begin; it2 != pivot; ++it2)
{
if (comp(*eq, *pivot))
if (comp(*it2, *pivot))
{
swap(*eq, *lt);
if (pivot == lt)
pivot = eq;
++lt;
++eq;
}
else if (comp(*pivot, *eq))
{
--gt;
swap(*eq, *gt);
if (pivot == gt)
pivot = eq;
}
else
{
++eq;
swap(*it1, *it2);
++it1;
}
}
return { lt, gt };
swap(*it1, *pivot);
return it1;
}
}
template<typename It, typename Comp = less<it_value_type_t<It>>>
@@ -69,9 +48,9 @@ namespace BAN::sort
{
if (distance(begin, end) <= 1)
return;
const auto [lt, gt] = detail::partition(begin, end, comp);
quick_sort(begin, lt, comp);
quick_sort(gt, end, comp);
It mid = detail::partition(begin, end, comp);
quick_sort(begin, mid, comp);
quick_sort(++mid, end, comp);
}
template<typename It, typename Comp = less<it_value_type_t<It>>>
@@ -106,9 +85,9 @@ namespace BAN::sort
return insertion_sort(begin, end, comp);
if (max_depth == 0)
return heap_sort(begin, end, comp);
const auto [lt, gt] = detail::partition(begin, end, comp);
intro_sort_impl(begin, lt, max_depth - 1, comp);
intro_sort_impl(gt, end, max_depth - 1, comp);
It mid = detail::partition(begin, end, comp);
intro_sort_impl(begin, mid, max_depth - 1, comp);
intro_sort_impl(++mid, end, max_depth - 1, comp);
}
}
@@ -137,20 +116,27 @@ namespace BAN::sort
template<typename It, size_t radix = 256>
requires is_unsigned_v<it_value_type_t<It>> && (radix > 0 && (radix & (radix - 1)) == 0)
void radix_sort(It begin, It end, BAN::Span<it_value_type_t<It>> storage)
BAN::ErrorOr<void> radix_sort(It begin, It end)
{
using value_type = it_value_type_t<It>;
const size_t len = distance(begin, end);
if (len <= 1)
return;
return {};
ASSERT(storage.size() >= len);
Vector<value_type> temp;
TRY(temp.resize(len));
Vector<size_t> counts;
TRY(counts.resize(radix));
constexpr size_t mask = radix - 1;
constexpr size_t shift = detail::lsb_index(radix);
for (size_t s = 0; s < sizeof(it_value_type_t<It>) * 8; s += shift)
for (size_t s = 0; s < sizeof(value_type) * 8; s += shift)
{
size_t counts[radix] {};
for (auto& cnt : counts)
cnt = 0;
for (It it = begin; it != end; ++it)
counts[(*it >> s) & mask]++;
@@ -160,27 +146,12 @@ namespace BAN::sort
for (It it = end; it != begin;)
{
--it;
storage[--counts[(*it >> s) & mask]] = *it;
temp[--counts[(*it >> s) & mask]] = *it;
}
It it = begin;
for (size_t j = 0; j < storage.size(); j++, ++it)
*it = storage[j];
for (size_t j = 0; j < temp.size(); j++)
*next(begin, j) = temp[j];
}
}
template<typename It, size_t radix = 256>
requires is_unsigned_v<it_value_type_t<It>> && (radix > 0 && (radix & (radix - 1)) == 0)
BAN::ErrorOr<void> radix_sort(It begin, It end)
{
const size_t len = distance(begin, end);
if (len <= 1)
return {};
Vector<it_value_type_t<It>> temp;
TRY(temp.resize(len));
radix_sort(begin, end, temp.span());
return {};
}

View File

@@ -11,7 +11,6 @@ set(KERNEL_SOURCES
kernel/Audio/Controller.cpp
kernel/Audio/HDAudio/AudioFunctionGroup.cpp
kernel/Audio/HDAudio/Controller.cpp
kernel/Banos.cpp
kernel/BootInfo.cpp
kernel/CPUID.cpp
kernel/Credentials.cpp
@@ -122,7 +121,6 @@ set(KERNEL_SOURCES
kernel/USB/USBManager.cpp
kernel/USB/XHCI/Controller.cpp
kernel/USB/XHCI/Device.cpp
kernel/UserCopy.cpp
icxxabi.cpp
)

View File

@@ -1,7 +1,7 @@
#include <kernel/BootInfo.h>
#include <kernel/CPUID.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/Memory/Heap.h>
#include <kernel/Memory/kmalloc.h>
#include <kernel/Memory/PageTable.h>
extern uint8_t g_kernel_start[];
@@ -26,6 +26,8 @@ namespace Kernel
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
static bool s_is_initialized = false;
static PageTable* s_kernel = nullptr;
static bool s_has_nxe = false;
static bool s_has_pge = false;
@@ -35,42 +37,24 @@ namespace Kernel
static uint64_t* s_fast_page_pt { nullptr };
alignas(PAGE_SIZE) static uint64_t s_fast_page_pt_storage[512] {};
static paddr_t allocate_zeroed_page_aligned_page()
static uint64_t* allocate_zeroed_page_aligned_page()
{
const paddr_t paddr = Heap::get().take_free_page();
ASSERT(paddr);
PageTable::with_fast_page(paddr, [] {
memset(PageTable::fast_page_as_ptr(), 0, PAGE_SIZE);
});
return paddr;
void* page = kmalloc(PAGE_SIZE, PAGE_SIZE, true);
ASSERT(page);
memset(page, 0, PAGE_SIZE);
return (uint64_t*)page;
}
static void unallocate_page(paddr_t paddr)
template<typename T>
static paddr_t V2P(const T vaddr)
{
Heap::get().release_page(paddr);
return (vaddr_t)vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
}
static uint64_t read_entry_from_table(paddr_t paddr, uint16_t entry)
template<typename T>
static uint64_t* P2V(const T paddr)
{
uint64_t result;
PageTable::with_fast_page(paddr & s_page_addr_mask, [&result, entry] {
result = PageTable::fast_page_as_sized<uint64_t>(entry);
});
return result;
}
static uint64_t write_entry_to_table(paddr_t paddr, uint16_t entry, uint64_t value)
{
uint64_t old_value;
PageTable::with_fast_page(paddr & s_page_addr_mask, [&old_value, entry, value] {
old_value = PageTable::fast_page_as_sized<uint64_t>(entry);
PageTable::fast_page_as_sized<uint64_t>(entry) = value;
});
return old_value;
return reinterpret_cast<uint64_t*>(reinterpret_cast<paddr_t>(paddr) - g_boot_info.kernel_paddr + KERNEL_OFFSET);
}
static inline PageTable::flags_t parse_flags(uint64_t entry)
@@ -158,10 +142,15 @@ namespace Kernel
s_kernel = new PageTable();
ASSERT(s_kernel);
s_kernel->m_highest_paging_struct = allocate_zeroed_page_aligned_page();
auto* pdpt = allocate_zeroed_page_aligned_page();
ASSERT(pdpt);
s_kernel->m_highest_paging_struct = V2P(pdpt);
s_kernel->map_kernel_memory();
s_global_pdpte = read_entry_from_table(s_kernel->m_highest_paging_struct, 3);
PageTable::with_fast_page(s_kernel->m_highest_paging_struct, [] {
s_global_pdpte = PageTable::fast_page_as_sized<paddr_t>(3);
});
// update fast page pt
{
@@ -172,21 +161,23 @@ namespace Kernel
const auto get_or_allocate_entry =
[](paddr_t table_paddr, uint16_t entry, uint64_t flags)
{
const uint64_t value = read_entry_from_table(table_paddr, entry);
if (value & Flags::Present)
return value & s_page_addr_mask;
uint64_t* table = P2V(table_paddr);
const paddr_t paddr = allocate_zeroed_page_aligned_page();
write_entry_to_table(table_paddr, entry, paddr | flags);
return paddr;
if (!(table[entry] & Flags::Present))
{
auto* vaddr = allocate_zeroed_page_aligned_page();
ASSERT(vaddr);
table[entry] = V2P(vaddr);
}
table[entry] |= flags;
return table[entry] & s_page_addr_mask;
};
const paddr_t pdpt = s_kernel->m_highest_paging_struct;
const paddr_t pd = get_or_allocate_entry(pdpt, pdpte, Flags::Present);
const paddr_t entry_paddr = reinterpret_cast<uintptr_t>(&s_fast_page_pt_storage) - KERNEL_OFFSET + g_boot_info.kernel_paddr;
write_entry_to_table(pd, pde, entry_paddr | PageTable::Flags::ReadWrite | PageTable::Flags::Present);
s_fast_page_pt = s_fast_page_pt_storage;
s_fast_page_pt = P2V(get_or_allocate_entry(pd, pde, Flags::ReadWrite | Flags::Present));
}
s_kernel->load();
@@ -206,37 +197,33 @@ namespace Kernel
void PageTable::map_kernel_memory()
{
// Map (phys_kernel_start -> phys_kernel_end) to (virt_kernel_start -> virt_kernel_end)
const vaddr_t kernel_start = reinterpret_cast<vaddr_t>(g_kernel_start);
map_range_at(
kernel_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
kernel_start,
V2P(g_kernel_start),
reinterpret_cast<vaddr_t>(g_kernel_start),
g_kernel_end - g_kernel_start,
Flags::Present
);
// Map executable kernel memory as executable
const vaddr_t kernel_execute_start = reinterpret_cast<vaddr_t>(g_kernel_execute_start);
map_range_at(
kernel_execute_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
kernel_execute_start,
V2P(g_kernel_execute_start),
reinterpret_cast<vaddr_t>(g_kernel_execute_start),
g_kernel_execute_end - g_kernel_execute_start,
Flags::Execute | Flags::Present
);
// Map writable kernel memory as writable
const vaddr_t kernel_writable_start = reinterpret_cast<vaddr_t>(g_kernel_writable_start);
map_range_at(
kernel_writable_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
kernel_writable_start,
V2P(g_kernel_writable_start),
reinterpret_cast<vaddr_t>(g_kernel_writable_start),
g_kernel_writable_end - g_kernel_writable_start,
Flags::ReadWrite | Flags::Present
);
// Map userspace memory
const vaddr_t kernel_userspace_start = reinterpret_cast<vaddr_t>(g_userspace_start);
map_range_at(
kernel_userspace_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
kernel_userspace_start,
V2P(g_userspace_start),
reinterpret_cast<vaddr_t>(g_userspace_start),
g_userspace_end - g_userspace_start,
Flags::Execute | Flags::UserSupervisor | Flags::Present
);
@@ -244,48 +231,26 @@ namespace Kernel
void PageTable::map_fast_page(paddr_t paddr)
{
map_fast_page(0, paddr);
ASSERT(paddr && paddr % PAGE_SIZE == 0);
ASSERT(s_fast_page_pt);
ASSERT(s_fast_page_lock.current_processor_has_lock());
ASSERT(!(*s_fast_page_pt & Flags::Present));
s_fast_page_pt[0] = paddr | Flags::ReadWrite | Flags::Present;
asm volatile("invlpg (%0)" :: "r"(fast_page()));
}
void PageTable::unmap_fast_page()
{
unmap_fast_page(0);
}
void* PageTable::map_fast_page(size_t index, paddr_t paddr)
{
ASSERT(paddr && paddr % PAGE_SIZE == 0);
ASSERT(index < 512);
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 & Flags::Present));
s_fast_page_pt[0] = 0;
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
void* address = reinterpret_cast<void*>(fast_page() + index * PAGE_SIZE);
asm volatile("invlpg (%0)" :: "r"(address));
return address;
}
void PageTable::unmap_fast_page(size_t index)
{
ASSERT(index < 512);
ASSERT(s_fast_page_pt);
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));
s_fast_page_pt[index] = 0;
asm volatile("invlpg (%0)" :: "r"(fast_page() + index * PAGE_SIZE));
asm volatile("invlpg (%0)" :: "r"(fast_page()));
}
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
@@ -295,16 +260,20 @@ namespace Kernel
if (page_table == nullptr)
return BAN::Error::from_errno(ENOMEM);
page_table->m_highest_paging_struct = allocate_zeroed_page_aligned_page();
uint64_t* pdpt = allocate_zeroed_page_aligned_page();
if (pdpt == nullptr)
{
delete page_table;
return BAN::Error::from_errno(ENOMEM);
}
PageTable::with_fast_page(page_table->m_highest_paging_struct, [] {
uint64_t* pdpt = &PageTable::fast_page_as<uint64_t>();
pdpt[0] = 0;
pdpt[1] = 0;
pdpt[2] = 0;
pdpt[3] = s_global_pdpte | Flags::Present;
static_assert(KERNEL_OFFSET == 0xC0000000);
});
page_table->m_highest_paging_struct = V2P(pdpt);
pdpt[0] = 0;
pdpt[1] = 0;
pdpt[2] = 0;
pdpt[3] = s_global_pdpte | Flags::Present;
static_assert(KERNEL_OFFSET == 0xC0000000);
return page_table;
}
@@ -314,22 +283,21 @@ namespace Kernel
if (m_highest_paging_struct == 0)
return;
const uint64_t pdpt = m_highest_paging_struct;
uint64_t* pdpt = P2V(m_highest_paging_struct);
for (uint32_t pdpte = 0; pdpte < 3; pdpte++)
{
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
if (!(pd & Flags::Present))
if (!(pdpt[pdpte] & Flags::Present))
continue;
uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
for (uint32_t pde = 0; pde < 512; pde++)
{
const uint64_t pt = read_entry_from_table(pd, pde);
if (!(pt & Flags::Present))
if (!(pd[pde] & Flags::Present))
continue;
unallocate_page(pt & s_page_addr_mask);
kfree(P2V(pd[pde] & s_page_addr_mask));
}
unallocate_page(pd & s_page_addr_mask);
kfree(pd);
}
unallocate_page(m_highest_paging_struct);
kfree(pdpt);
}
void PageTable::load()
@@ -347,10 +315,32 @@ namespace Kernel
const bool is_userspace = (vaddr < KERNEL_OFFSET);
if (is_userspace && this != &PageTable::current())
;
else if (pages >= full_tlb_flush_threshold)
invalidate_full_address_space(!is_userspace);
else for (size_t i = 0; i < pages; i++)
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
else if (pages <= 32 || !s_is_initialized)
{
for (size_t i = 0; i < pages; i++, vaddr += PAGE_SIZE)
asm volatile("invlpg (%0)" :: "r"(vaddr));
}
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)
{
@@ -365,34 +355,6 @@ 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)
{
ASSERT(vaddr);
@@ -412,13 +374,15 @@ namespace Kernel
if (is_page_free(vaddr))
Kernel::panic("trying to unmap unmapped page 0x{H}", vaddr);
const uint64_t pdpt = m_highest_paging_struct;
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
const uint64_t pt = read_entry_from_table(pd, pde);
uint64_t* pdpt = P2V(m_highest_paging_struct);
uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
const uint64_t old_entry = write_entry_to_table(pt, pte, 0);
const paddr_t old_paddr = pt[pte] & s_page_addr_mask;
if (invalidate && (old_entry & s_page_addr_mask))
pt[pte] = 0;
if (invalidate && old_paddr != 0)
invalidate_page(vaddr, true);
}
@@ -469,31 +433,28 @@ namespace Kernel
SpinLockGuard _(m_lock);
const uint64_t pdpt = m_highest_paging_struct;
uint64_t* pdpt = P2V(m_highest_paging_struct);
if (!(pdpt[pdpte] & Flags::Present))
pdpt[pdpte] = V2P(allocate_zeroed_page_aligned_page()) | Flags::Present;
uint64_t pd = read_entry_from_table(pdpt, pdpte);
if (!(pd & Flags::Present))
uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
if ((pd[pde] & uwr_flags) != uwr_flags)
{
pd = allocate_zeroed_page_aligned_page();
pd |= Flags::Present;
write_entry_to_table(pdpt, pdpte, pd);
}
uint64_t pt = read_entry_from_table(pd, pde);
if ((pt & uwr_flags) != uwr_flags)
{
if (!(pt & Flags::Present))
pt = allocate_zeroed_page_aligned_page();
pt |= uwr_flags;
write_entry_to_table(pd, pde, pt);
if (!(pd[pde] & Flags::Present))
pd[pde] = V2P(allocate_zeroed_page_aligned_page());
pd[pde] |= uwr_flags;
}
if (!(flags & Flags::Present))
uwr_flags &= ~Flags::Present;
const uint64_t old_entry = write_entry_to_table(pt, pte, paddr | uwr_flags | extra_flags);
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
if (invalidate && (old_entry & s_page_addr_mask))
const paddr_t old_paddr = pt[pte] & s_page_addr_mask;
pt[pte] = paddr | uwr_flags | extra_flags;
if (invalidate && old_paddr != 0)
invalidate_page(vaddr, true);
}
@@ -524,36 +485,31 @@ namespace Kernel
const uint32_t e_pde = ((vaddr + size - 1) >> 21) & 0x1FF;
const uint32_t e_pte = ((vaddr + size - 1) >> 12) & 0x1FF;
SpinLockGuard _0(m_lock);
SpinLockGuard _(m_lock);
SpinLockGuard _1(s_fast_page_lock);
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, m_highest_paging_struct));
const uint64_t* pdpt = P2V(m_highest_paging_struct);
for (; pdpte <= e_pdpte; pdpte++)
{
if (!(pdpt[pdpte] & Flags::Present))
continue;
const uint64_t* pd = static_cast<uint64_t*>(map_fast_page(1, pdpt[pdpte] & s_page_addr_mask));
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
for (; pde < 512; pde++)
{
if (pdpte == e_pdpte && pde > e_pde)
break;
if (!(pd[pde] & Flags::ReadWrite))
continue;
uint64_t* pt = static_cast<uint64_t*>(map_fast_page(2, pd[pde] & s_page_addr_mask));
uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
for (; pte < 512; pte++)
{
if (pdpte == e_pdpte && pde == e_pde && pte > e_pte)
break;
pt[pte] &= ~static_cast<uint64_t>(Flags::ReadWrite);
}
unmap_fast_page(2);
pte = 0;
}
unmap_fast_page(1);
pde = 0;
}
unmap_fast_page(0);
invalidate_range(vaddr, size / PAGE_SIZE, true);
}
@@ -568,17 +524,19 @@ namespace Kernel
SpinLockGuard _(m_lock);
const uint64_t pdpt = m_highest_paging_struct;
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
if (!(pd & Flags::Present))
const uint64_t* pdpt = P2V(m_highest_paging_struct);
if (!(pdpt[pdpte] & Flags::Present))
return 0;
const uint64_t pt = read_entry_from_table(pd, pde);
if (!(pt & Flags::Present))
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
if (!(pd[pde] & Flags::Present))
return 0;
return read_entry_from_table(pt, pte);
const uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
if (!(pt[pte] & Flags::Used))
return 0;
return pt[pte];
}
PageTable::flags_t PageTable::get_page_flags(vaddr_t vaddr) const
@@ -609,24 +567,30 @@ namespace Kernel
return true;
}
void PageTable::reserve_page(vaddr_t vaddr)
bool PageTable::reserve_page(vaddr_t vaddr, bool only_free, bool send_smp_message)
{
ASSERT(vaddr % PAGE_SIZE == 0);
SpinLockGuard _(m_lock);
ASSERT(is_page_free(vaddr));
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, false);
ASSERT(vaddr % PAGE_SIZE == 0);
if (only_free && !is_page_free(vaddr))
return false;
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, send_smp_message);
return true;
}
void PageTable::reserve_range(vaddr_t vaddr, size_t bytes)
bool PageTable::reserve_range(vaddr_t vaddr, size_t bytes, bool only_free)
{
if (size_t rem = bytes % PAGE_SIZE)
bytes += PAGE_SIZE - rem;
ASSERT(vaddr % PAGE_SIZE == 0);
SpinLockGuard _(m_lock);
ASSERT(is_range_free(vaddr, bytes));
if (only_free && !is_range_free(vaddr, bytes))
return false;
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
reserve_page(vaddr + offset);
reserve_page(vaddr + offset, true, false);
invalidate_range(vaddr, bytes / PAGE_SIZE, true);
return true;
}
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
@@ -648,58 +612,45 @@ namespace Kernel
SpinLockGuard _(m_lock);
auto state = s_fast_page_lock.lock();
// Try to find free page that can be mapped without
// allocations (page table with unused entries)
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, m_highest_paging_struct));
const uint64_t* pdpt = P2V(m_highest_paging_struct);
for (; pdpte <= e_pdpte; pdpte++)
{
if (!(pdpt[pdpte] & Flags::Present))
continue;
const uint64_t* pd = static_cast<uint64_t*>(map_fast_page(1, pdpt[pdpte] & s_page_addr_mask));
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
for (; pde < 512; pde++)
{
if (pdpte == e_pdpte && pde > e_pde)
break;
if (!(pd[pde] & Flags::Present))
continue;
const uint64_t* pt = static_cast<uint64_t*>(map_fast_page(2, pd[pde] & s_page_addr_mask));
const uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
for (; pte < 512; pte++)
{
if (pdpte == e_pdpte && pde == e_pde && pte > e_pte)
break;
if (pt[pte] & Flags::Used)
continue;
unmap_fast_page(2);
unmap_fast_page(1);
unmap_fast_page(0);
s_fast_page_lock.unlock(state);
vaddr_t vaddr = 0;
vaddr |= (vaddr_t)pdpte << 30;
vaddr |= (vaddr_t)pde << 21;
vaddr |= (vaddr_t)pte << 12;
reserve_page(vaddr);
ASSERT(reserve_page(vaddr));
return vaddr;
}
unmap_fast_page(2);
pte = 0;
}
unmap_fast_page(1);
pde = 0;
}
unmap_fast_page(0);
s_fast_page_lock.unlock(state);
// Find any free page
for (vaddr_t vaddr = first_address; vaddr < last_address; vaddr += PAGE_SIZE)
{
if (is_page_free(vaddr))
{
reserve_page(vaddr);
ASSERT(reserve_page(vaddr));
return vaddr;
}
}
@@ -732,7 +683,7 @@ namespace Kernel
}
if (valid)
{
reserve_range(vaddr, page_count * PAGE_SIZE);
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
return vaddr;
}
}
@@ -755,14 +706,12 @@ namespace Kernel
void PageTable::debug_dump()
{
SpinLockGuard _0(m_lock);
SpinLockGuard _(m_lock);
flags_t flags = 0;
vaddr_t start = 0;
SpinLockGuard _1(s_fast_page_lock);
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, m_highest_paging_struct));
const uint64_t* pdpt = P2V(m_highest_paging_struct);
for (uint32_t pdpte = 0; pdpte < 4; pdpte++)
{
if (!(pdpt[pdpte] & Flags::Present))
@@ -771,7 +720,7 @@ namespace Kernel
start = 0;
continue;
}
const uint64_t* pd = static_cast<uint64_t*>(map_fast_page(1, pdpt[pdpte] & s_page_addr_mask));
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
for (uint64_t pde = 0; pde < 512; pde++)
{
if (!(pd[pde] & Flags::Present))
@@ -780,7 +729,7 @@ namespace Kernel
start = 0;
continue;
}
const uint64_t* pt = static_cast<uint64_t*>(map_fast_page(2, pd[pde] & s_page_addr_mask));
const uint64_t* pt = P2V(pd[pde] & s_page_addr_mask);
for (uint64_t pte = 0; pte < 512; pte++)
{
if (parse_flags(pt[pte]) != flags)
@@ -798,11 +747,8 @@ namespace Kernel
start = (pdpte << 30) | (pde << 21) | (pte << 12);
}
}
unmap_fast_page(2);
}
unmap_fast_page(1);
}
unmap_fast_page(0);
}
}

View File

@@ -59,7 +59,7 @@ signal_trampoline:
addl $24, %esp
// restore sigmask
movl $79, %eax // SYS_SIGPROCMASK
movl $83, %eax // SYS_SIGPROCMASK
movl $3, %ebx // SIG_SETMASK
leal 72(%esp), %ecx // set
xorl %edx, %edx // oset

View File

@@ -24,6 +24,7 @@ namespace Kernel
SpinLock PageTable::s_fast_page_lock;
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_addr_mask = ~s_page_flag_mask;
@@ -368,48 +369,26 @@ namespace Kernel
void PageTable::map_fast_page(paddr_t paddr)
{
map_fast_page(0, paddr);
ASSERT(paddr && paddr % PAGE_SIZE == 0);
ASSERT(s_fast_page_pt);
ASSERT(s_fast_page_lock.current_processor_has_lock());
ASSERT(!(*s_fast_page_pt & Flags::Present));
s_fast_page_pt[0] = paddr | Flags::ReadWrite | Flags::Present;
asm volatile("invlpg (%0)" :: "r"(fast_page()));
}
void PageTable::unmap_fast_page()
{
unmap_fast_page(0);
}
void* PageTable::map_fast_page(size_t index, paddr_t paddr)
{
ASSERT(paddr && paddr % PAGE_SIZE == 0);
ASSERT(index < 512);
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 & Flags::Present));
s_fast_page_pt[0] = 0;
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
void* address = reinterpret_cast<void*>(fast_page() + index * PAGE_SIZE);
asm volatile("invlpg (%0)" :: "r"(address));
return address;
}
void PageTable::unmap_fast_page(size_t index)
{
ASSERT(index < 512);
ASSERT(s_fast_page_pt);
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));
s_fast_page_pt[index] = 0;
asm volatile("invlpg (%0)" :: "r"(fast_page() + index * PAGE_SIZE));
asm volatile("invlpg (%0)" :: "r"(fast_page()));
}
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
@@ -477,10 +456,32 @@ namespace Kernel
const bool is_userspace = (vaddr < KERNEL_OFFSET);
if (is_userspace && this != &PageTable::current())
;
else if (pages >= full_tlb_flush_threshold)
invalidate_full_address_space(!is_userspace);
else for (size_t i = 0; i < pages; i++)
asm volatile("invlpg (%0)" :: "r"(vaddr + i * PAGE_SIZE));
else if (pages <= 32 || !s_is_initialized)
{
for (size_t i = 0; i < pages; i++, vaddr += PAGE_SIZE)
asm volatile("invlpg (%0)" :: "r"(vaddr));
}
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)
{
@@ -495,34 +496,6 @@ 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)
{
ASSERT(vaddr);
@@ -564,70 +537,12 @@ namespace Kernel
{
ASSERT(vaddr % PAGE_SIZE == 0);
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;
const size_t page_count = range_page_count(vaddr, size);
SpinLockGuard _(m_lock);
uint64_t* pml4 = P2V(m_highest_paging_struct);
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);
for (vaddr_t page = 0; page < page_count; page++)
unmap_page(vaddr + page * PAGE_SIZE, false);
invalidate_range(vaddr, page_count, true);
}
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags, MemoryType memory_type, bool invalidate)
@@ -814,74 +729,33 @@ namespace Kernel
paddr_t PageTable::physical_address_of(vaddr_t addr) const
{
return get_page_data(addr) & s_page_addr_mask;
uint64_t page_data = get_page_data(addr);
return page_data & s_page_addr_mask;
}
void PageTable::reserve_page(vaddr_t vaddr)
bool PageTable::reserve_page(vaddr_t vaddr, bool only_free, bool invalidate)
{
ASSERT(vaddr % PAGE_SIZE == 0);
SpinLockGuard _(m_lock);
ASSERT(is_page_free(vaddr));
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, false);
ASSERT(vaddr % PAGE_SIZE == 0);
if (only_free && !is_page_free(vaddr))
return false;
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, invalidate);
return true;
}
void PageTable::reserve_range(vaddr_t vaddr, size_t bytes)
bool PageTable::reserve_range(vaddr_t vaddr, size_t bytes, bool only_free)
{
if (size_t rem = bytes % PAGE_SIZE)
bytes += PAGE_SIZE - rem;
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);
uint64_t* pml4 = P2V(m_highest_paging_struct);
for (;; pml4e++)
{
#define CHECK_IF_PRESENT(expr) \
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();
if (only_free && !is_range_free(vaddr, bytes))
return false;
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
reserve_page(vaddr + offset, true, false);
invalidate_range(vaddr, bytes / PAGE_SIZE, true);
return true;
}
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
@@ -895,7 +769,6 @@ namespace Kernel
ASSERT(is_canonical(first_address));
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);
@@ -945,7 +818,7 @@ namespace Kernel
vaddr |= static_cast<uint64_t>(pde) << 21;
vaddr |= static_cast<uint64_t>(pte) << 12;
vaddr = canonicalize(vaddr);
reserve_page(vaddr);
ASSERT(reserve_page(vaddr));
return vaddr;
}
pte = 0;
@@ -959,7 +832,7 @@ namespace Kernel
{
if (vaddr_t vaddr = canonicalize(uc_vaddr); is_page_free(vaddr))
{
reserve_page(vaddr);
ASSERT(reserve_page(vaddr));
return vaddr;
}
}
@@ -969,90 +842,44 @@ namespace Kernel
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 < reinterpret_cast<vaddr_t>(g_kernel_start))
first_address = reinterpret_cast<vaddr_t>(g_kernel_start);
if (const auto rem = first_address % PAGE_SIZE)
if (first_address >= KERNEL_OFFSET && first_address < (vaddr_t)g_kernel_start)
first_address = (vaddr_t)g_kernel_start;
if (size_t rem = first_address % PAGE_SIZE)
first_address += PAGE_SIZE - rem;
if (const auto rem = last_address % PAGE_SIZE)
if (size_t rem = last_address % PAGE_SIZE)
last_address -= rem;
ASSERT(is_canonical(first_address));
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);
const uint64_t* pml4 = P2V(m_highest_paging_struct);
for (; pml4e <= e_pml4e; pml4e++)
for (vaddr_t vaddr = first_address; vaddr < last_address;)
{
#define CHECK_IF_PRESENT(expr, advance) \
if (!((expr) & Flags::Present)) { \
if ((free_count += advance) >= page_count) \
goto found_free_region; \
continue; \
}
CHECK_IF_PRESENT(pml4[pml4e], 512 * 512 * 512);
const uint64_t* pdpt = P2V(pml4[pml4e] & s_page_addr_mask);
for (; pdpte < 512; pdpte++)
bool valid { true };
for (size_t page = 0; page < page_count; page++)
{
if (pml4e == e_pml4e && pdpte > e_pdpte)
break;
CHECK_IF_PRESENT(pdpt[pdpte], 512 * 512);
const uint64_t* pd = P2V(pdpt[pdpte] & s_page_addr_mask);
for (; pde < 512; pde++)
if (!is_canonical(vaddr + page * PAGE_SIZE))
{
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;
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;
}
pde = 0;
}
pdpte = 0;
#undef CHECK_IF_PRESENT
if (valid)
{
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
return vaddr;
}
}
return 0;
found_free_region:
reserve_range(vaddr, page_count * PAGE_SIZE);
return vaddr;
}
bool PageTable::is_page_free(vaddr_t page) const

View File

@@ -61,7 +61,7 @@ signal_trampoline:
addq $40, %rsp
// restore sigmask
movq $79, %rdi // SYS_SIGPROCMASK
movq $83, %rdi // SYS_SIGPROCMASK
movq $3, %rsi // SIG_SETMASK
leaq 192(%rsp), %rdx // set
xorq %r10, %r10 // oset

View File

@@ -41,18 +41,7 @@ SECTIONS
{
g_kernel_writable_start = .;
*(.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)
{
g_kernel_bss_start = .;

View File

@@ -1,27 +0,0 @@
#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)))

View File

@@ -1,15 +0,0 @@
#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)

View File

@@ -1,13 +0,0 @@
#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

View File

@@ -1,2 +0,0 @@
#pragma once
typedef unsigned long banos_revision_t;

View File

@@ -1,28 +0,0 @@
#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)

View File

@@ -14,6 +14,7 @@ namespace Kernel
public:
static BAN::ErrorOr<void> create(PCI::Device& pci_device);
dev_t rdev() const override { return m_rdev; }
BAN::StringView name() const override { return m_name; }
protected:
@@ -50,6 +51,7 @@ namespace Kernel
snd_volume_info m_volume_info {};
private:
const dev_t m_rdev;
char m_name[10] {};
};

View File

@@ -43,7 +43,7 @@ namespace Kernel
BAN::ErrorOr<void> enable_output_path(uint8_t index);
BAN::ErrorOr<void> disable_output_path(uint8_t index);
BAN::ErrorOr<void> reset_stream();
void reset_stream();
BAN::ErrorOr<void> recurse_output_paths(const HDAudio::AFGWidget& widget, BAN::Vector<const HDAudio::AFGWidget*>& path);

View File

@@ -1,10 +0,0 @@
#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);
}

View File

@@ -49,8 +49,6 @@
#define DEBUG_VTTY 1
#define DEBUG_DEVFS 0
#define DEBUG_PCI 0
#define DEBUG_SCHEDULER 0
#define DEBUG_PS2 1

View File

@@ -8,14 +8,15 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<DebugDevice>> create(mode_t, uid_t, gid_t);
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return "debug"_sv; }
protected:
DebugDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
: CharacterDevice(mode, uid, gid)
{
m_rdev = rdev;
}
, m_rdev(rdev)
{ }
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return 0; }
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override;
@@ -24,6 +25,9 @@ namespace Kernel
virtual bool can_write_impl() const override { return true; }
virtual bool has_error_impl() const override { return false; }
virtual bool has_hungup_impl() const override { return false; }
private:
const dev_t m_rdev;
};
}

View File

@@ -12,20 +12,24 @@ namespace Kernel
virtual ~Device() = default;
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)
{
(void)offset; (void)len; (void)status_flags;
return BAN::Error::from_errno(ENOTSUP);
}
virtual dev_t rdev() const override = 0;
virtual BAN::StringView name() const = 0;
protected:
Device(mode_t, uid_t, gid_t);
private:
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
BAN::ErrorOr<void> sync_data() final override { return {}; }
virtual BAN::ErrorOr<void> fsync_impl() final override { return BAN::Error::from_errno(EINVAL); }
};
class BlockDevice : public Device, public BAN::Weakable<BlockDevice>
@@ -41,7 +45,7 @@ namespace Kernel
BlockDevice(mode_t mode, uid_t uid, gid_t gid)
: Device(mode, uid, gid)
{
m_mode |= Inode::Mode::IFBLK;
m_inode_info.mode |= Inode::Mode::IFBLK;
}
};
@@ -51,7 +55,7 @@ namespace Kernel
CharacterDevice(mode_t mode, uid_t uid, gid_t gid)
: Device(mode, uid, gid)
{
m_mode |= Inode::Mode::IFCHR;
m_inode_info.mode |= Inode::Mode::IFCHR;
}
};

View File

@@ -30,6 +30,7 @@ namespace Kernel
virtual BAN::ErrorOr<BAN::UniqPtr<MemoryRegion>> mmap_region(PageTable&, off_t offset, size_t len, AddressRange, MemoryRegion::Type, PageTable::flags_t, int status_flags) override;
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return m_name.sv(); }
protected:
@@ -49,6 +50,7 @@ namespace Kernel
private:
const BAN::String m_name;
const dev_t m_rdev;
vaddr_t m_video_memory_vaddr { 0 };
const paddr_t m_video_memory_paddr;

View File

@@ -10,14 +10,15 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<NullDevice>> create(mode_t, uid_t, gid_t);
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return "null"_sv; }
protected:
NullDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
: CharacterDevice(mode, uid, gid)
{
m_rdev = rdev;
}
, m_rdev(rdev)
{ }
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return 0; }
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
@@ -27,6 +28,8 @@ namespace Kernel
virtual bool has_error_impl() const override { return false; }
virtual bool has_hungup_impl() const override { return false; }
private:
const dev_t m_rdev;
};
}

View File

@@ -8,14 +8,15 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<RandomDevice>> create(mode_t, uid_t, gid_t);
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return "random"_sv; }
protected:
RandomDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
: CharacterDevice(mode, uid, gid)
{
m_rdev = rdev;
}
, m_rdev(rdev)
{ }
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
@@ -24,6 +25,9 @@ namespace Kernel
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:
const dev_t m_rdev;
};
}

View File

@@ -8,14 +8,15 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<ZeroDevice>> create(mode_t, uid_t, gid_t);
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return "zero"_sv; }
protected:
ZeroDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
: CharacterDevice(mode, uid, gid)
{
m_rdev = rdev;
}
, m_rdev(rdev)
{ }
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
@@ -24,6 +25,9 @@ namespace Kernel
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:
const dev_t m_rdev;
};
}

View File

@@ -2,7 +2,6 @@
#include <BAN/HashMap.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
#include <sys/epoll.h>
@@ -21,7 +20,24 @@ namespace Kernel
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
private:
Epoll();
Epoll() = default;
public:
ino_t ino() const override { return 0; }
Mode mode() const override { return { Mode::IRUSR | Mode::IWUSR }; }
nlink_t nlink() const override { return 0; }
uid_t uid() const override { return 0; }
gid_t gid() const override { return 0; }
off_t size() const override { return 0; }
timespec atime() const override { return {}; }
timespec mtime() const override { return {}; }
timespec ctime() const override { return {}; }
blksize_t blksize() const override { return PAGE_SIZE; }
blkcnt_t blocks() const override { return 0; }
dev_t dev() const override { return 0; }
dev_t rdev() const override { return 0; }
bool is_epoll() const override { return true; }
const FileSystem* filesystem() const override { return nullptr; }
@@ -30,8 +46,7 @@ namespace Kernel
bool has_error_impl() const override { return false; }
bool has_hungup_impl() const override { return false; }
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
BAN::ErrorOr<void> sync_data() override { return {}; }
BAN::ErrorOr<void> fsync_impl() override { return {}; }
private:
struct ListenEventList
@@ -75,7 +90,6 @@ namespace Kernel
};
private:
Mutex m_mutex;
ThreadBlocker m_thread_blocker;
SpinLock m_ready_lock;
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t> m_ready_events;

View File

@@ -1,7 +1,6 @@
#pragma once
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
namespace Kernel
{
@@ -11,16 +10,26 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uint64_t initval, bool semaphore);
private:
EventFD(uint64_t initval, bool is_semaphore);
ino_t ino() const override { return 0; }
Mode mode() const override { return { Mode::IFCHR | Mode::IRUSR | Mode::IWUSR }; }
nlink_t nlink() const override { return ref_count(); }
uid_t uid() const override { return 0; }
gid_t gid() const override { return 0; }
off_t size() const override { return 0; }
timespec atime() const override { return {}; }
timespec mtime() const override { return {}; }
timespec ctime() const override { return {}; }
blksize_t blksize() const override { return 8; }
blkcnt_t blocks() const override { return 0; }
dev_t dev() const override { return 0; }
dev_t rdev() const override { return 0; }
const FileSystem* filesystem() const override { return nullptr; }
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
BAN::ErrorOr<void> sync_data() override { return {}; }
protected:
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) 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_write_impl() const override { return m_value < UINT64_MAX - 1; }
@@ -28,10 +37,15 @@ namespace Kernel
bool has_hungup_impl() const override { return false; }
private:
const bool m_is_semaphore;
BAN::Atomic<uint64_t> m_value;
EventFD(uint64_t initval, bool is_semaphore)
: m_is_semaphore(is_semaphore)
, m_value(initval)
{ }
private:
const bool m_is_semaphore;
uint64_t m_value;
Mutex m_mutex;
ThreadBlocker m_thread_blocker;
};

View File

@@ -79,12 +79,6 @@ namespace Kernel::Ext2
uint8_t __reserved[12];
};
struct InodeBlocks {
uint32_t block[15];
};
struct Osd2 {
uint32_t osd2[3];
};
struct Inode
{
uint16_t mode;
@@ -99,12 +93,12 @@ namespace Kernel::Ext2
uint32_t blocks;
uint32_t flags;
uint32_t osd1;
InodeBlocks block;
uint32_t block[15];
uint32_t generation;
uint32_t file_acl;
uint32_t dir_acl;
uint32_t faddr;
Osd2 osd2;
uint32_t osd2[3];
};
struct LinkedDirectoryEntry

View File

@@ -99,8 +99,7 @@ namespace Kernel
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
BAN::ErrorOr<void> release_block(uint32_t block);
BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> open_inode(ino_t);
void remove_from_cache(ino_t);
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>>& inode_cache() { return m_inode_cache; }
const Ext2::Superblock& superblock() const { return m_superblock; }
@@ -156,7 +155,6 @@ namespace Kernel
BAN::RefPtr<Inode> m_root_inode;
BAN::Vector<uint32_t> m_superblock_backups;
Mutex m_inode_cache_lock;
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>> m_inode_cache;
BlockBufferManager m_buffer_manager;

View File

@@ -4,7 +4,6 @@
#include <BAN/StringView.h>
#include <kernel/FS/Ext2/Definitions.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/RWLock.h>
namespace Kernel
{
@@ -16,12 +15,23 @@ namespace Kernel
public:
~Ext2Inode();
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; }
virtual const FileSystem* filesystem() const override;
private:
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
virtual BAN::ErrorOr<void> sync_data() override;
protected:
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<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
@@ -36,6 +46,10 @@ namespace Kernel
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<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_write_impl() const override { return true; }
@@ -43,96 +57,58 @@ namespace Kernel
virtual bool has_hungup_impl() const override { return false; }
private:
uint32_t block_group() const;
// Returns maximum number of data blocks in use
// 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(); }
BAN::ErrorOr<void> sync_inode_no_lock();
BAN::ErrorOr<BAN::Optional<uint32_t>> block_from_indirect_block(uint32_t& block, uint32_t index, uint32_t depth, bool allocate);
BAN::ErrorOr<BAN::Optional<uint32_t>> fs_block_of_data_block_index(uint32_t data_block_index, bool allocate);
BAN::ErrorOr<bool> is_directory_empty_no_lock();
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_no_lock(BAN::StringView);
BAN::ErrorOr<void> link_inode_to_directory(Ext2Inode&, BAN::StringView name);
BAN::ErrorOr<void> remove_inode_from_directory(BAN::StringView name, bool cleanup_directory);
BAN::ErrorOr<bool> is_directory_empty();
/* 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>> fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate);
BAN::ErrorOr<void> cleanup_indirect_block(uint32_t block, uint32_t depth);
BAN::ErrorOr<void> cleanup_default_links();
BAN::ErrorOr<void> cleanup_data_blocks();
BAN::ErrorOr<void> cleanup_from_fs();
/* needs write end of the lock */
BAN::ErrorOr<void> link_inode_to_directory_no_lock(Ext2Inode&, BAN::StringView name);
BAN::ErrorOr<void> remove_inode_from_directory_no_lock(BAN::StringView name, bool cleanup_directory);
BAN::ErrorOr<void> sync();
/* needs write end of the lock */
BAN::ErrorOr<void> cleanup_indirect_block_no_lock(uint32_t block, uint32_t depth);
BAN::ErrorOr<void> cleanup_default_links_no_lock();
BAN::ErrorOr<void> cleanup_data_blocks_no_lock();
BAN::ErrorOr<void> cleanup_from_fs_no_lock();
uint32_t block_group() const;
private:
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino);
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>);
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino)
: m_fs(fs)
, m_inode(inode)
, m_ino(ino)
{}
static BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> create(Ext2FS&, uint32_t);
private:
struct ScopedSync
{
ScopedSync(Ext2Inode& inode)
: inode(inode)
, inode_info(inode.m_inode)
{ }
~ScopedSync()
{
// TODO: there was some memcmp smarty pants stuff here.
// How do we wanna approach it?
if (auto ret = inode.sync_inode_no_lock(); ret.is_error())
if (memcmp(&inode.m_inode, &inode_info, sizeof(Ext2::Inode)) == 0)
return;
if (auto ret = inode.sync(); ret.is_error())
dwarnln("failed to sync inode: {}", ret.error());
}
Ext2Inode& inode;
Ext2::Inode inode_info;
};
private:
Ext2FS& m_fs;
RWLock m_lock;
Ext2::InodeBlocks m_ext2_blocks;
// NOTE: some fields from the original disk inode
// that we do not use, but we keep for serialise.
const uint32_t m_og_dtime;
const uint32_t m_og_flags;
const uint32_t m_og_osd1;
const uint32_t m_og_generation;
const uint32_t m_og_file_acl;
const uint32_t m_og_dir_acl;
const uint32_t m_og_faddr;
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;
Ext2::Inode m_inode;
const uint32_t m_ino;
friend class Ext2FS;
friend class BAN::RefPtr<Ext2Inode>;

View File

@@ -15,14 +15,25 @@ namespace Kernel
class FATInode final : public Inode, public BAN::Weakable<FATInode>
{
public:
virtual ino_t ino() const override { return m_ino; };
virtual Mode mode() const override { return Mode { ((m_entry.attr & FAT::FileAttr::DIRECTORY) ? Mode::IFDIR : Mode::IFREG) | 0777 }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return m_entry.file_size; }
virtual timespec atime() const override;
virtual timespec mtime() const override;
virtual timespec ctime() const override;
virtual blksize_t blksize() const override;
virtual blkcnt_t blocks() const override { return m_block_count; }
virtual dev_t dev() const override { return 0; }
virtual dev_t rdev() const override { return 0; }
virtual const FileSystem* filesystem() const override;
const FAT::DirectoryEntry& entry() const { return m_entry; }
private:
virtual BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
virtual BAN::ErrorOr<void> sync_data() override { return {}; }
protected:
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<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
@@ -32,6 +43,9 @@ namespace Kernel
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<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_write_impl() const override { return true; }
@@ -39,8 +53,12 @@ namespace Kernel
virtual bool has_hungup_impl() const override { return false; }
private:
FATInode(FATFS& fs, const FAT::DirectoryEntry& entry, ino_t ino, uint32_t block_count);
FATInode(FATFS& fs, const FAT::DirectoryEntry& entry, ino_t ino, uint32_t block_count)
: m_fs(fs)
, m_entry(entry)
, m_ino(ino)
, m_block_count(block_count)
{ }
~FATInode() {}
BAN::ErrorOr<void> for_each_directory_entry(BAN::ConstByteSpan, BAN::Function<BAN::Iteration(const FAT::DirectoryEntry&)>);
@@ -49,6 +67,7 @@ namespace Kernel
private:
FATFS& m_fs;
FAT::DirectoryEntry m_entry;
const ino_t m_ino;
uint32_t m_block_count;
friend class Ext2FS;

View File

@@ -10,6 +10,7 @@
#include <kernel/Credentials.h>
#include <kernel/Debug.h>
#include <kernel/Lock/Mutex.h>
#include <dirent.h>
#include <sys/socket.h>
@@ -62,24 +63,6 @@ namespace Kernel
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:
virtual ~Inode() {}
@@ -87,26 +70,24 @@ namespace Kernel
bool operator==(const Inode& other) const { return dev() == other.dev() && ino() == other.ino(); }
ino_t ino() const { return m_ino; }
Mode mode() const { return Mode(m_mode); }
nlink_t nlink() const { return m_nlink; }
uid_t uid() const { return m_uid; }
gid_t gid() const { return m_gid; }
off_t size() const { return m_size; }
timespec atime() const { return m_atime; }
timespec mtime() const { return m_mtime; }
timespec ctime() const { return m_ctime; }
blksize_t blksize() const { return m_blksize; }
blkcnt_t blocks() const { return m_blocks; }
dev_t dev() const { return m_dev; }
dev_t rdev() const { return m_rdev; }
virtual ino_t ino() const = 0;
virtual Mode mode() const = 0;
virtual nlink_t nlink() const = 0;
virtual uid_t uid() const = 0;
virtual gid_t gid() const = 0;
virtual off_t size() const = 0;
virtual timespec atime() const = 0;
virtual timespec mtime() const = 0;
virtual timespec ctime() const = 0;
virtual blksize_t blksize() const = 0;
virtual blkcnt_t blocks() const = 0;
virtual dev_t dev() const = 0;
virtual dev_t rdev() const = 0;
bool is_device() const { return m_kind & InodeKind::DEVICE; }
bool is_epoll() const { return m_kind & InodeKind::EPOLL; }
bool is_pipe() const { return m_kind & InodeKind::PIPE; }
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 bool is_device() const { return false; }
virtual bool is_epoll() const { return false; }
virtual bool is_pipe() const { return false; }
virtual bool is_tty() const { return false; }
virtual const FileSystem* filesystem() const = 0;
@@ -145,10 +126,10 @@ namespace Kernel
BAN::ErrorOr<void> fsync();
// Select/Non blocking API
bool can_read() const { return can_read_impl(); }
bool can_write() const { return can_write_impl(); }
bool has_error() const { return has_error_impl(); }
bool has_hungup() const { return has_hungup_impl(); }
bool can_read() const;
bool can_write() const;
bool has_error() const;
bool has_hungup() const;
BAN::ErrorOr<long> ioctl(int request, void* arg);
@@ -159,9 +140,6 @@ namespace Kernel
virtual void on_close(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:
// Directory API
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
@@ -192,6 +170,10 @@ namespace Kernel
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<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
virtual bool can_read_impl() const = 0;
@@ -202,33 +184,12 @@ namespace Kernel
virtual BAN::ErrorOr<long> ioctl_impl(int, void*) { return BAN::Error::from_errno(ENOTSUP); }
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<mode_t> m_mode;
BAN::Atomic<nlink_t> m_nlink;
BAN::Atomic<uid_t> m_uid;
BAN::Atomic<gid_t> m_gid;
BAN::Atomic<off_t> m_size;
// TODO: make these guys atomic :)
timespec m_atime;
timespec m_mtime;
timespec m_ctime;
BAN::Atomic<blksize_t> m_blksize;
BAN::Atomic<blkcnt_t> m_blocks;
BAN::Atomic<dev_t> m_dev;
BAN::Atomic<dev_t> m_rdev;
mutable PriorityMutex m_mutex;
private:
SpinLock m_shared_region_lock;
BAN::WeakPtr<SharedFileData> m_shared_region;
SpinLock m_epoll_lock;
BAN::LinkedList<class Epoll*> m_epolls;
friend class Epoll;
friend class FileBackedRegion;
friend class OpenFileDescriptorSet;

View File

@@ -1,34 +1,43 @@
#pragma once
#include <BAN/Array.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Memory/ByteRingBuffer.h>
#include <kernel/ThreadBlocker.h>
#include <sys/stat.h>
namespace Kernel
{
class Pipe final : public Inode, public BAN::Weakable<Pipe>
class Pipe : public Inode
{
public:
static BAN::ErrorOr<BAN::RefPtr<Inode>> open(BAN::RefPtr<Inode>, int status_flags);
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uid_t, gid_t);
~Pipe();
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(const Credentials&);
virtual bool is_pipe() const override { return true; }
void on_close(int status_flags) override;
void on_clone(int status_flags) override;
virtual ino_t ino() const override { return 0; } // FIXME
virtual Mode mode() const override { return { Mode::IFIFO | Mode::IRUSR | Mode::IWUSR }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return m_uid; }
virtual gid_t gid() const override { return m_gid; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return m_atime; }
virtual timespec mtime() const override { return m_mtime; }
virtual timespec ctime() const override { return m_ctime; }
virtual blksize_t blksize() const override { return 4096; }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return 0; } // FIXME
virtual dev_t rdev() const override { return 0; } // FIXME
virtual const FileSystem* filesystem() const override { return nullptr; }
private:
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
virtual BAN::ErrorOr<void> sync_data() override;
protected:
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<void> truncate_impl(size_t) override;
virtual BAN::ErrorOr<void> fsync_impl() final override { return {}; }
virtual bool can_read_impl() const override { return !m_buffer->empty(); }
virtual bool can_write_impl() const override { return true; }
@@ -36,18 +45,20 @@ namespace Kernel
virtual bool has_hungup_impl() const override { return m_writing_count == 0; }
private:
Pipe(const struct stat&);
Pipe(const Credentials&);
private:
Mutex m_mutex;
const uid_t m_uid;
const gid_t m_gid;
timespec m_atime {};
timespec m_mtime {};
timespec m_ctime {};
ThreadBlocker m_thread_blocker;
BAN::UniqPtr<ByteRingBuffer> m_buffer;
BAN::Atomic<uint32_t> m_writing_count { 0 };
BAN::Atomic<uint32_t> m_reading_count { 0 };
BAN::RefPtr<Inode> m_named_inode;
BAN::Atomic<uint32_t> m_writing_count { 1 };
BAN::Atomic<uint32_t> m_reading_count { 1 };
};
}

View File

@@ -13,8 +13,6 @@ namespace Kernel
static void initialize();
static ProcFileSystem& get();
void post_scheduler_initialize();
BAN::ErrorOr<void> on_process_create(Process&);
void on_process_delete(Process&);

View File

@@ -9,27 +9,34 @@ namespace Kernel
class ProcPidInode final : public TmpDirectoryInode
{
// FIXME: dynamically update ruid/rgid.
// Possibly just have a magic uid/gid of -1 or something
// which means use current process ID
public:
static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create_new(Process&, TmpFileSystem&, mode_t);
~ProcPidInode() = default;
virtual uid_t uid() const override { return m_process.credentials().ruid(); }
virtual gid_t gid() const override { return m_process.credentials().rgid(); }
void cleanup();
protected:
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override { return BAN::Error::from_errno(EPERM); }
private:
ProcPidInode(Process&, TmpFileSystem&, const TmpInodeInfo&);
private:
Process& m_process;
};
class ProcROProcessInode final : public TmpInode
{
//FIXME: dynamically update ruid/rgid
public:
static BAN::ErrorOr<BAN::RefPtr<ProcROProcessInode>> create_new(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem&, mode_t);
~ProcROProcessInode() = default;
virtual uid_t uid() const override { return m_process.credentials().ruid(); }
virtual gid_t gid() const override { return m_process.credentials().rgid(); }
protected:
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
@@ -52,11 +59,13 @@ namespace Kernel
class ProcSymlinkProcessInode final : public TmpInode
{
//FIXME: dynamically update ruid/rgid
public:
static BAN::ErrorOr<BAN::RefPtr<ProcSymlinkProcessInode>> create_new(Process& process, BAN::ErrorOr<BAN::String> (Process::*callback)() const, TmpFileSystem&, mode_t);
~ProcSymlinkProcessInode() = default;
virtual uid_t uid() const override { return m_process.credentials().ruid(); }
virtual gid_t gid() const override { return m_process.credentials().rgid(); }
protected:
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
@@ -76,7 +85,7 @@ namespace Kernel
class ProcROInode final : public TmpInode
{
public:
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);
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, mode_t, uid_t, gid_t);
~ProcROInode() = default;
protected:
@@ -92,11 +101,10 @@ namespace Kernel
virtual bool has_hungup_impl() const override { return false; }
private:
ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem&, void*, const TmpInodeInfo&);
ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, const TmpInodeInfo&);
private:
BAN::ErrorOr<size_t> (*m_callback)(off_t, BAN::ByteSpan, void*);
void* m_argument;
size_t (*m_callback)(off_t, BAN::ByteSpan);
};
class ProcSymlinkInode final : public TmpInode
@@ -124,10 +132,13 @@ namespace Kernel
class ProcFDDirectoryInode final : public TmpInode
{
//FIXME: dynamically update ruid/rgid
public:
static BAN::ErrorOr<BAN::RefPtr<ProcFDDirectoryInode>> create_new(Process&, TmpFileSystem&, mode_t);
~ProcFDDirectoryInode() = default;
virtual uid_t uid() const override { return m_process.credentials().ruid(); }
virtual gid_t gid() const override { return m_process.credentials().rgid(); }
protected:
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;

View File

@@ -30,19 +30,31 @@ namespace Kernel
};
public:
ino_t ino() const final override { ASSERT_NOT_REACHED(); }
Mode mode() const final override { return Mode(m_info.mode); }
nlink_t nlink() const final override { ASSERT_NOT_REACHED(); }
uid_t uid() const final override { return m_info.uid; }
gid_t gid() const final override { return m_info.gid; }
off_t size() const final override { ASSERT_NOT_REACHED(); }
timespec atime() const final override { ASSERT_NOT_REACHED(); }
timespec mtime() const final override { ASSERT_NOT_REACHED(); }
timespec ctime() const final override { ASSERT_NOT_REACHED(); }
blksize_t blksize() const final override { ASSERT_NOT_REACHED(); }
blkcnt_t blocks() const final override { ASSERT_NOT_REACHED(); }
dev_t dev() const final override { ASSERT_NOT_REACHED(); }
dev_t rdev() const final override { ASSERT_NOT_REACHED(); }
const FileSystem* filesystem() const final override { return nullptr; }
protected:
Socket(const Info& info)
{
m_mode = info.mode;
m_uid = info.uid;
m_gid = info.gid;
}
: m_info(info)
{}
BAN::ErrorOr<void> fsync_impl() final override { return {}; }
private:
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
BAN::ErrorOr<void> sync_data() final override { return {}; }
const Info m_info;
};
}

View File

@@ -11,8 +11,18 @@
namespace Kernel
{
struct TmpBlocks
struct TmpInodeInfo
{
mode_t mode { 0 };
uid_t uid { 0 };
gid_t gid { 0 };
timespec atime { 0, 0 };
timespec ctime { 0, 0 };
timespec mtime { 0, 0 };
nlink_t nlink { 0 };
size_t size { 0 };
blkcnt_t blocks { 0 };
#if ARCH(x86_64)
// 2x direct blocks
// 1x singly indirect
@@ -31,23 +41,8 @@ namespace Kernel
#else
#error
#endif
};
struct TmpInodeInfo
{
mode_t mode { 0 };
uid_t uid { 0 };
gid_t gid { 0 };
timespec atime { 0, 0 };
timespec ctime { 0, 0 };
timespec mtime { 0, 0 };
nlink_t nlink { 0 };
size_t size { 0 };
blkcnt_t blocks { 0 };
TmpBlocks tmp_blocks;
static constexpr size_t max_size =
TmpBlocks::direct_block_count * PAGE_SIZE +
direct_block_count * PAGE_SIZE +
(PAGE_SIZE / sizeof(paddr_t)) * PAGE_SIZE +
(PAGE_SIZE / sizeof(paddr_t)) * (PAGE_SIZE / sizeof(paddr_t)) * PAGE_SIZE +
(PAGE_SIZE / sizeof(paddr_t)) * (PAGE_SIZE / sizeof(paddr_t)) * (PAGE_SIZE / sizeof(paddr_t)) * PAGE_SIZE;

View File

@@ -4,7 +4,6 @@
#include <BAN/Optional.h>
#include <kernel/FS/Inode.h>
#include <kernel/FS/TmpFS/Definitions.h>
#include <kernel/Lock/Mutex.h>
namespace Kernel
{
@@ -24,37 +23,51 @@ namespace Kernel
class TmpInode : public Inode
{
public:
virtual ino_t ino() const override { return m_ino; }
virtual Mode mode() const override { return Mode(m_inode_info.mode); }
virtual nlink_t nlink() const override { return m_inode_info.nlink; }
virtual uid_t uid() const override { return m_inode_info.uid; }
virtual gid_t gid() const override { return m_inode_info.gid; }
virtual off_t size() const override { return m_inode_info.size; }
virtual timespec atime() const override { return m_inode_info.atime; }
virtual timespec mtime() const override { return m_inode_info.mtime; }
virtual timespec ctime() const override { return m_inode_info.ctime; }
virtual blksize_t blksize() const override { return PAGE_SIZE; }
virtual blkcnt_t blocks() const override { return m_inode_info.blocks; }
virtual dev_t dev() const override;
virtual dev_t rdev() const override { return 0; }
public:
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
virtual ~TmpInode();
~TmpInode();
virtual const FileSystem* filesystem() const override;
protected:
TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
void write_inode_to_fs();
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() { return {}; };
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();
virtual BAN::ErrorOr<void> prepare_unlink() { return {}; };
void free_all_blocks();
void free_indirect_blocks_no_lock(size_t block, uint32_t depth);
void free_indirect_blocks(size_t block, uint32_t depth);
BAN::Optional<size_t> block_index(size_t data_block_index);
BAN::Optional<size_t> block_index_from_indirect_no_lock(size_t block, size_t index, uint32_t depth);
BAN::Optional<size_t> block_index_from_indirect(size_t block, size_t index, uint32_t depth);
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);
private:
BAN::ErrorOr<void> sync_inode(SyncType) override;
BAN::ErrorOr<void> sync_data() override;
BAN::ErrorOr<size_t> block_index_from_indirect_with_allocation(size_t& block, size_t index, uint32_t depth);
protected:
TmpFileSystem& m_fs;
TmpBlocks m_tmp_blocks;
// TODO: try to reduce locking or replace this with rwlock(?)
Mutex m_lock;
TmpInodeInfo m_inode_info;
const ino_t m_ino;
// has to be able to increase link count
friend class TmpDirectoryInode;
@@ -66,7 +79,7 @@ namespace Kernel
static BAN::ErrorOr<BAN::RefPtr<TmpFileInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
~TmpFileInode();
private:
protected:
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<void> truncate_impl(size_t) override;
@@ -82,36 +95,13 @@ namespace Kernel
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
{
public:
static BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
~TmpSocketInode();
private:
protected:
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); }
@@ -133,7 +123,7 @@ namespace Kernel
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
~TmpSymlinkInode();
private:
protected:
BAN::ErrorOr<BAN::String> link_target_impl() override;
BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
@@ -159,7 +149,7 @@ namespace Kernel
protected:
TmpDirectoryInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() override;
virtual BAN::ErrorOr<void> prepare_unlink() override;
protected:
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override final;

View File

@@ -92,9 +92,8 @@ namespace Kernel
MountPoint* mount_from_root_inode(BAN::RefPtr<Inode>);
private:
Mutex m_mutex;
BAN::RefPtr<FileSystem> m_root_fs;
Mutex m_mount_point_lock;
BAN::Vector<MountPoint> m_mount_points;
friend class BAN::RefPtr<VirtualFileSystem>;

View File

@@ -22,6 +22,8 @@ namespace Kernel
InputDevice(Type type);
BAN::StringView name() const final override { return m_name; }
dev_t rdev() const final override { return m_rdev; }
protected:
void add_event(BAN::ConstByteSpan);
@@ -36,7 +38,8 @@ namespace Kernel
BAN::ErrorOr<size_t> read_non_block(BAN::ByteSpan);
private:
BAN::String m_name;
const dev_t m_rdev;
const BAN::String m_name;
const Type m_type;
@@ -61,7 +64,6 @@ namespace Kernel
{
public:
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();
@@ -75,8 +77,10 @@ namespace Kernel
bool has_hungup_impl() const override { return false; }
BAN::StringView name() const final override { return m_name; }
dev_t rdev() const final override { return m_rdev; }
private:
const dev_t m_rdev;
const BAN::StringView m_name;
ThreadBlocker m_thread_blocker;
@@ -100,7 +104,10 @@ namespace Kernel
bool has_hungup_impl() const override { return false; }
BAN::StringView name() const final override { return m_name; }
dev_t rdev() const final override { return m_rdev; }
private:
const dev_t m_rdev;
const BAN::StringView m_name;
ThreadBlocker m_thread_blocker;

View File

@@ -7,7 +7,7 @@
namespace Kernel
{
template<typename Lock> requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
template<typename Lock>
class LockGuard
{
BAN_NON_COPYABLE(LockGuard);

View File

@@ -21,7 +21,7 @@ namespace Kernel
virtual uint32_t lock_depth() const = 0;
};
class Mutex final : public BaseMutex
class Mutex : public BaseMutex
{
BAN_NON_COPYABLE(Mutex);
BAN_NON_MOVABLE(Mutex);
@@ -40,7 +40,6 @@ namespace Kernel
pid_t expected = -1;
while (!m_locker.compare_exchange(expected, tid))
{
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
Processor::yield();
expected = -1;
}
@@ -85,14 +84,13 @@ namespace Kernel
pid_t locker() const override { return m_locker; }
bool is_locked() const override { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; }
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
private:
BAN::Atomic<pid_t> m_locker { -1 };
uint32_t m_lock_depth { 0 };
};
class PriorityMutex final : public BaseMutex
class PriorityMutex : public BaseMutex
{
BAN_NON_COPYABLE(PriorityMutex);
BAN_NON_MOVABLE(PriorityMutex);
@@ -115,7 +113,6 @@ namespace Kernel
pid_t expected = -1;
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
{
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
Processor::yield();
expected = -1;
}
@@ -167,7 +164,6 @@ namespace Kernel
pid_t locker() const override { return m_locker; }
bool is_locked() const override { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; }
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
private:
BAN::Atomic<pid_t> m_locker { -1 };

View File

@@ -1,7 +1,7 @@
#pragma once
#include <kernel/Lock/SpinLock.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Lock/LockGuard.h>
namespace Kernel
{
@@ -15,60 +15,43 @@ namespace Kernel
void rd_lock()
{
SpinLockGuard _(m_lock);
while (m_writers_waiting > 0 || m_writer != -1)
{
SpinLockGuardAsMutex smutex(_);
m_thread_blocker.block_indefinite(&smutex);
}
LockGuard _(m_mutex);
while (m_writers_waiting > 0 || m_writer_active)
m_thread_blocker.block_indefinite(&m_mutex);
m_readers_active++;
}
void rd_unlock()
{
SpinLockGuard _(m_lock);
LockGuard _(m_mutex);
if (--m_readers_active == 0)
m_thread_blocker.unblock();
}
void wr_lock()
{
if (m_writer == Thread::current_tid())
{
m_writer_depth++;
return;
}
SpinLockGuard _(m_lock);
LockGuard _(m_mutex);
m_writers_waiting++;
while (m_readers_active > 0 || m_writer != -1)
{
SpinLockGuardAsMutex smutex(_);
m_thread_blocker.block_indefinite(&smutex);
}
while (m_readers_active > 0 || m_writer_active)
m_thread_blocker.block_indefinite(&m_mutex);
m_writers_waiting--;
m_writer = Thread::current_tid();
m_writer_depth = 1;
m_writer_active = true;
}
void wr_unlock()
{
if (--m_writer_depth != 0)
return;
SpinLockGuard _(m_lock);
m_writer = -1;
LockGuard _(m_mutex);
m_writer_active = false;
m_thread_blocker.unblock();
}
private:
SpinLock m_lock;
Mutex m_mutex;
ThreadBlocker m_thread_blocker;
uint32_t m_readers_active { 0 };
uint32_t m_writers_waiting { 0 };
pid_t m_writer { -1 };
uint32_t m_writer_depth { 0 };
bool m_writer_active { false };
};
class RWLockRDGuard

View File

@@ -46,7 +46,7 @@ namespace Kernel
uint32_t lock_depth() const override { return m_lock_depth; }
private:
Lock& m_lock;
SpinLock& m_lock;
uint32_t m_lock_depth { 0 };
InterruptState m_state;
const pid_t m_locker;

View File

@@ -1,7 +1,6 @@
#pragma once
#include <kernel/FS/Inode.h>
#include <kernel/Lock/RWLock.h>
#include <kernel/Memory/MemoryRegion.h>
namespace Kernel
@@ -11,15 +10,15 @@ namespace Kernel
{
~SharedFileData();
void sync_no_lock(size_t page_index);
void sync(size_t page_index);
RWLock rw_lock;
Mutex mutex;
// FIXME: this should probably be ordered tree like map
// for fast lookup and less memory usage
BAN::Vector<paddr_t> pages;
BAN::Vector<uint32_t> writers;
BAN::RefPtr<Inode> inode;
uint8_t page_buffer[PAGE_SIZE];
};
class FileBackedRegion final : public MemoryRegion

View File

@@ -14,12 +14,6 @@ namespace Kernel
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>
concept with_fast_page_callback_error = requires(F func)
{
@@ -51,10 +45,6 @@ namespace Kernel
WriteThrough,
};
static constexpr bool full_tlb_flush_threshold = 32;
static constexpr size_t reserved_fast_pages = 0x10;
public:
static void initialize_fast_page();
static void initialize_and_load();
@@ -82,18 +72,6 @@ namespace Kernel
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>
static BAN::ErrorOr<void> with_fast_page(paddr_t paddr, F callback)
{
@@ -145,8 +123,8 @@ namespace Kernel
bool is_page_free(vaddr_t) const;
bool is_range_free(vaddr_t, size_t bytes) const;
void reserve_page(vaddr_t);
void reserve_range(vaddr_t, size_t bytes);
bool reserve_page(vaddr_t, bool only_free = true, bool invalidate = true);
bool reserve_range(vaddr_t, size_t bytes, bool only_free = true);
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);
@@ -155,7 +133,6 @@ namespace Kernel
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_full_address_space(bool global);
InterruptState lock() const { return m_lock.lock(); }
void unlock(InterruptState state) const { m_lock.unlock(state); }
@@ -172,9 +149,6 @@ namespace Kernel
static void map_fast_page(paddr_t);
static void unmap_fast_page();
static void* map_fast_page(size_t index, paddr_t);
static void unmap_fast_page(size_t index);
private:
paddr_t m_highest_paging_struct { 0 };
mutable RecursiveSpinLock m_lock;

View File

@@ -1,8 +1,15 @@
#pragma once
#include <BAN/Optional.h>
#include <kernel/Memory/Types.h>
#include <stddef.h>
void kmalloc_initialize();
void kmalloc_dump_info();
void* kmalloc(size_t);
void* kmalloc(size_t size);
void* kmalloc(size_t size, size_t align, bool force_identity_map = false);
void kfree(void*);
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t);

View File

@@ -73,15 +73,11 @@ namespace Kernel
BAN::UniqPtr<DMARegion> m_tx_buffer_region;
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
BAN::Atomic<uint32_t> m_tx_head1 { 0 };
BAN::Atomic<uint32_t> m_tx_head2 { 0 };
SpinLock m_rx_lock;
ThreadBlocker m_rx_blocker;
SpinLock m_lock;
bool m_thread_should_die { false };
BAN::Atomic<bool> m_thread_is_dead { true };
ThreadBlocker m_thread_blocker;
BAN::MACAddress m_mac_address {};
bool m_link_up { false };

View File

@@ -57,6 +57,7 @@ namespace Kernel
virtual size_t payload_mtu() const = 0;
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return m_name; }
BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::ConstByteSpan payload)
@@ -65,11 +66,10 @@ namespace Kernel
}
virtual BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan> payload) = 0;
private:
BAN::ErrorOr<long> ioctl_impl(int, void*) override;
private:
const Type m_type;
const dev_t m_rdev;
char m_name[10];
BAN::IPv4Address m_ipv4_address { 0 };

View File

@@ -181,7 +181,6 @@ namespace Kernel
uint64_t m_time_wait_start_ms { 0 };
mutable Mutex m_mutex;
ThreadBlocker m_thread_blocker;
RecvWindowInfo m_recv_window;

View File

@@ -66,12 +66,9 @@ namespace Kernel
SpinLock m_packet_lock;
ThreadBlocker m_packet_thread_blocker;
SpinLock m_peer_address_lock;
sockaddr_storage m_peer_address {};
socklen_t m_peer_address_len { 0 };
Mutex m_bind_lock;
friend class BAN::RefPtr<UDPSocket>;
};

View File

@@ -43,16 +43,15 @@ namespace Kernel
UnixDomainSocket(Socket::Type, const Socket::Info&);
~UnixDomainSocket();
bool is_bound() const;
bool is_bound_to_unused() const;
BAN::ErrorOr<void> bind_to_unused_if_not_bound();
bool is_bound() const { return !m_bound_file.canonical_path.empty(); }
bool is_bound_to_unused() const { return !m_bound_file.inode; }
bool is_streaming() const;
private:
struct ConnectionInfo
{
BAN::Atomic<bool> listening { false };
bool listening { false };
BAN::Atomic<bool> connection_done { false };
mutable BAN::Atomic<bool> target_closed { false };
BAN::WeakPtr<UnixDomainSocket> connection;
@@ -63,7 +62,6 @@ namespace Kernel
struct ConnectionlessInfo
{
SpinLock lock;
BAN::String peer_address;
};
@@ -78,9 +76,7 @@ namespace Kernel
BAN::ErrorOr<size_t> add_packet(const msghdr&, PacketInfo&&, bool dont_block);
private:
const Socket::Type m_socket_type;
mutable Mutex m_bind_mutex;
const Socket::Type m_socket_type;
VirtualFileSystem::File m_bound_file;
BAN::Variant<ConnectionInfo, ConnectionlessInfo> m_info;

View File

@@ -58,8 +58,6 @@ namespace Kernel
BAN::ErrorOr<size_t> recvmsg(int socket, 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<BAN::String> path_of(int) const;
BAN::ErrorOr<BAN::RefPtr<Inode>> inode_of(int);

View File

@@ -66,13 +66,11 @@ namespace Kernel::PCI
};
public:
Device(uint8_t bus, uint8_t dev, uint8_t func)
: m_bus(bus)
, m_dev(dev)
, m_func(func)
{ }
Device() = default;
void set_location(uint8_t bus, uint8_t dev, uint8_t func);
void initialize(paddr_t pcie_paddr);
bool is_valid() const { return m_is_valid; }
uint32_t read_dword(uint8_t) const;
uint16_t read_word(uint8_t) const;
@@ -126,9 +124,10 @@ namespace Kernel::PCI
BAN::ErrorOr<uint8_t> find_intx_interrupt();
private:
const uint8_t m_bus { 0 };
const uint8_t m_dev { 0 };
const uint8_t m_func { 0 };
bool m_is_valid { false };
uint8_t m_bus { 0 };
uint8_t m_dev { 0 };
uint8_t m_func { 0 };
vaddr_t m_mmio_config { 0 };
@@ -162,8 +161,11 @@ namespace Kernel::PCI
template<typename F>
void for_each_device(F callback)
{
for (auto& dev : m_devices)
callback(dev);
for (auto& bus : m_buses)
for (auto& dev : bus)
for (auto& func : dev)
if (func.is_valid())
callback(func);
};
uint32_t read_config_dword(uint8_t bus, uint8_t dev, uint8_t func, uint8_t offset);
@@ -177,22 +179,19 @@ namespace Kernel::PCI
BAN::Optional<uint8_t> reserve_msi();
private:
struct PCIeInfo
{
paddr_t bus_paddr[256];
};
PCIManager() = default;
void check_function(const PCIeInfo&, uint8_t bus, uint8_t dev, uint8_t func);
void check_device(const PCIeInfo&, uint8_t bus, uint8_t dev);
void check_bus(const PCIeInfo&, uint8_t bus);
void check_all_buses(const PCIeInfo&);
PCIManager() : m_bus_pcie_paddr(0) {}
void check_function(uint8_t bus, uint8_t dev, uint8_t func);
void check_device(uint8_t bus, uint8_t dev);
void check_bus(uint8_t bus);
void check_all_buses();
void initialize_impl();
private:
static constexpr uint8_t m_msi_count = IRQ_MSI_END - IRQ_MSI_BASE;
BAN::Vector<Device> m_devices;
using PCIBus = BAN::Array<BAN::Array<Device, 8>, 32>;
BAN::Array<PCIBus, 256> m_buses;
BAN::Array<paddr_t, 256> m_bus_pcie_paddr;
bool m_is_pcie { false };
SpinLock m_reserved_msi_lock;
BAN::Array<uint8_t, m_msi_count / 8> m_reserved_msi_bitmap;

View File

@@ -56,7 +56,6 @@ namespace Kernel
pid_t pid() const { return m_pid; }
bool is_session_leader() const { return pid() == sid(); }
bool is_pgrpg_in_this_session(pid_t) const;
const char* name() const { return m_cmdline.empty() ? "<unknown>" : m_cmdline.front().data(); }
@@ -64,6 +63,9 @@ namespace Kernel
BAN::ErrorOr<long> sys_exit(int status);
BAN::ErrorOr<long> sys_tcgetattr(int fildes, termios*);
BAN::ErrorOr<long> sys_tcsetattr(int fildes, int optional_actions, const termios*);
BAN::ErrorOr<long> sys_fork(uintptr_t rsp, uintptr_t rip);
BAN::ErrorOr<long> sys_exec(const char* path, const char* const* argv, const char* const* envp);
@@ -100,14 +102,13 @@ namespace Kernel
BAN::ErrorOr<long> open_inode(VirtualFileSystem::File&&, int flags);
BAN::ErrorOr<void> create_file(int fd, const char* path, mode_t) const;
BAN::ErrorOr<long> sys_openat(int fd, const char* path, int flags, mode_t);
BAN::ErrorOr<void> create_file_or_dir(int fd, const char* path, mode_t mode) const;
BAN::ErrorOr<long> sys_openat(int, const char* path, int, mode_t);
BAN::ErrorOr<long> sys_close(int fd);
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_access(const char* path, int amode);
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_create_dir(const char*, mode_t);
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_unlinkat(int fd, const char* path, int flag);
@@ -185,6 +186,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_smo_map(SharedMemoryObjectManager::Key);
BAN::ErrorOr<long> sys_ttyname(int fildes, char* name, size_t namesize);
BAN::ErrorOr<long> sys_isatty(int fildes);
BAN::ErrorOr<long> sys_posix_openpt(int flags);
BAN::ErrorOr<long> sys_ptsname(int fildes, char* buffer, size_t buffer_len);
@@ -215,14 +217,14 @@ namespace Kernel
BAN::ErrorOr<long> sys_pthread_join(pthread_t thread, void** value);
BAN::ErrorOr<long> sys_pthread_self();
BAN::ErrorOr<long> sys_pthread_kill(pthread_t thread, int signal);
BAN::ErrorOr<long> sys_pthread_detach(pthread_t thread);
BAN::ErrorOr<long> sys_tcgetpgrp(int fd);
BAN::ErrorOr<long> sys_tcsetpgrp(int fd, pid_t pgid);
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
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; }
static Process& current() { return Thread::current().process(); }
@@ -280,12 +282,13 @@ namespace Kernel
// You must hold reader end of m_mapped_region_lock when calling this.
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<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<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);
uint64_t signal_pending_mask() const

View File

@@ -29,13 +29,6 @@ namespace Kernel
BAN_NON_MOVABLE(Processor);
public:
struct TLBEntry
{
vaddr_t vaddr;
size_t page_count;
class PageTable* page_table;
};
struct SMPMessage
{
enum class Type
@@ -50,19 +43,18 @@ namespace Kernel
Type type;
union
{
TLBEntry flush_tlb;
struct
{
uintptr_t vaddr;
size_t page_count;
void* page_table;
} flush_tlb;
SchedulerQueue::Node* new_thread;
SchedulerQueue::Node* unblock_thread;
bool dummy;
};
};
struct LoadStats
{
uint64_t ns_idle;
uint64_t ns_total;
};
public:
static Processor& create(ProcessorID id);
static Processor& initialize();
@@ -78,6 +70,9 @@ namespace Kernel
static void set_smp_enabled() { s_is_smp_enabled = true; }
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 bool current_is_bsp() { return current_id() == bsp_id(); }
@@ -119,8 +114,6 @@ namespace Kernel
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 LoadStats get_load_stats(size_t index);
static void yield();
static Scheduler& scheduler() { return *read_gs_sized<Scheduler*>(offsetof(Processor, m_scheduler)); }
@@ -137,7 +130,7 @@ namespace Kernel
static void handle_ipi();
static void handle_smp_messages();
static bool send_smp_message(ProcessorID, const SMPMessage&, bool send_ipi = true);
static void send_smp_message(ProcessorID, const SMPMessage&, bool send_ipi = true);
static void broadcast_smp_message(const SMPMessage&);
static void load_segments();
@@ -185,13 +178,11 @@ namespace Kernel
asm volatile("mov %[value], %%gs:%a[offset]" :: [value]"r"(value), [offset]"ir"(offset) : "memory");
}
void lock_tlb_lock();
void unlock_tlb_lock();
private:
static ProcessorID s_bsp_id;
static BAN::Atomic<uint8_t> s_processor_count;
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 vaddr_t s_shared_page_vaddr;
@@ -211,19 +202,15 @@ namespace Kernel
Scheduler* m_scheduler { nullptr };
BAN::Atomic<bool> m_load_stat_lock;
uint64_t m_load_start_ns { 0 };
LoadStats m_load_stats {};
uint64_t m_start_ns { 0 };
uint64_t m_idle_ns { 0 };
uint64_t m_last_update_ns { 0 };
uint64_t m_next_update_ns { 0 };
BAN::Atomic<SMPMessage*> m_smp_pending { nullptr };
BAN::Atomic<SMPMessage*> m_smp_free { 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 };
friend class BAN::Array<Processor, 0xFF>;

View File

@@ -31,6 +31,8 @@ namespace Kernel
BAN::StringView model() const { return m_model; }
BAN::StringView name() const override { return m_name; }
dev_t rdev() const override { return m_rdev; }
protected:
ATABaseDevice();
BAN::ErrorOr<void> initialize(BAN::Span<const uint16_t> identify_data);
@@ -44,6 +46,8 @@ namespace Kernel
bool m_has_lba;
char m_model[41];
char m_name[4] {};
const dev_t m_rdev;
};
}

View File

@@ -20,6 +20,7 @@ namespace Kernel
NVMeQueue& io_queue() { return *m_io_queue; }
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return m_name; }
protected:
@@ -50,6 +51,7 @@ namespace Kernel
BAN::Vector<BAN::RefPtr<NVMeNamespace>> m_namespaces;
char m_name[20];
const dev_t m_rdev;
};
}

View File

@@ -16,6 +16,7 @@ namespace Kernel
virtual uint32_t sector_size() const override { return m_block_size; }
virtual uint64_t total_size() const override { return m_block_size * m_block_count; }
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::StringView name() const override { return m_name; }
private:
@@ -34,6 +35,7 @@ namespace Kernel
const uint64_t m_block_count;
char m_name[10] {};
const dev_t m_rdev;
};
}

View File

@@ -42,6 +42,11 @@ namespace Kernel
char m_label[36 * 4 + 1];
const BAN::String m_name;
public:
virtual bool is_partition() const override { return true; }
virtual dev_t rdev() const override { return m_rdev; }
protected:
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
@@ -51,6 +56,7 @@ namespace Kernel
virtual bool has_hungup_impl() const override { return false; }
private:
const dev_t m_rdev;
};
}

View File

@@ -14,10 +14,7 @@ namespace Kernel
public:
StorageDevice()
: BlockDevice(0660, 0, 0)
{
m_kind |= InodeKind::STORAGE;
}
{ }
virtual ~StorageDevice();
BAN::ErrorOr<void> initialize_partitions(BAN::StringView name_prefix);
@@ -38,6 +35,7 @@ namespace Kernel
size_t drop_disk_cache();
BAN::ErrorOr<void> sync_disk_cache();
virtual bool is_storage_device() const override { return true; }
protected:
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t lba, uint64_t sector_count, BAN::ByteSpan) = 0;

View File

@@ -13,6 +13,7 @@ namespace Kernel
public:
static BAN::ErrorOr<BAN::RefPtr<PseudoTerminalMaster>> create(mode_t, uid_t, gid_t);
dev_t rdev() const override { return m_rdev; }
BAN::StringView name() const override { return "<ptmx>"_sv; }
BAN::ErrorOr<BAN::RefPtr<PseudoTerminalSlave>> slave();
@@ -47,6 +48,8 @@ namespace Kernel
size_t m_buffer_tail { 0 };
size_t m_buffer_size { 0 };
const dev_t m_rdev;
friend class PseudoTerminalSlave;
friend class BAN::RefPtr<PseudoTerminalMaster>;
};
@@ -64,7 +67,7 @@ namespace Kernel
bool putchar_impl(uint8_t ch) override;
bool can_write_impl() const override;
bool has_hungup_impl() const override { return master_has_closed(); }
bool has_hungup_impl() const override { return !m_master.valid(); }
private:
PseudoTerminalSlave(BAN::String&& name, uint32_t number, mode_t, uid_t, gid_t);

View File

@@ -33,6 +33,9 @@ namespace Kernel
public:
virtual BAN::ErrorOr<void> set_font(LibFont::Font&&) { return BAN::Error::from_errno(EINVAL); }
void set_foreground_pgrp(pid_t pgrp) { m_foreground_pgrp = pgrp; }
pid_t foreground_pgrp() const { return m_foreground_pgrp; }
BAN::ErrorOr<void> tty_ctrl(int command, int flags);
// for kprint
@@ -43,14 +46,28 @@ namespace Kernel
static void keyboard_task(void*);
static void initialize_devices();
bool should_receive_input() const { return m_tty_ctrl.receive_input; }
void on_key_event(LibInput::RawKeyEvent);
void on_key_event(LibInput::KeyEvent);
void handle_input_byte(uint8_t);
void get_termios(termios* termios) { *termios = m_termios; }
// FIXME: validate termios
BAN::ErrorOr<void> set_termios(const termios* termios) { m_termios = *termios; return {}; }
virtual bool is_tty() const override { return true; }
virtual dev_t rdev() const final override { return m_rdev; }
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; }
protected:
@@ -59,50 +76,43 @@ namespace Kernel
virtual bool putchar_impl(uint8_t ch) = 0;
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> write_impl(off_t, BAN::ConstByteSpan) final 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 BAN::ErrorOr<long> ioctl_impl(int, void*) override;
void update_winsize(unsigned short cols, unsigned short rows);
private:
bool putchar(uint8_t ch);
void do_backspace();
termios get_termios();
protected:
termios m_termios;
private:
BAN::Atomic<pid_t> m_foreground_pgrp { 0 };
const dev_t m_rdev;
pid_t m_foreground_pgrp { 0 };
struct tty_ctrl_t
{
BAN::Atomic<bool> draw_graphics { true };
BAN::Atomic<bool> receive_input { true };
bool draw_graphics { true };
bool receive_input { true };
ThreadBlocker thread_blocker;
};
tty_ctrl_t m_tty_ctrl;
struct Buffer
{
BAN::UniqPtr<ByteRingBuffer> buffer;
BAN::Atomic<bool> flush { false };
bool flush { false };
ThreadBlocker thread_blocker;
};
Buffer m_output;
winsize m_winsize {};
SpinLock m_termios_lock;
termios m_termios;
protected:
Mutex m_mutex;
Mutex m_write_lock;
RecursiveSpinLock m_write_lock;
ThreadBlocker m_write_blocker;
};

View File

@@ -117,9 +117,6 @@ namespace Kernel
const Process& process() const;
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; }
uint64_t cpu_time_ns() const;
@@ -179,7 +176,6 @@ namespace Kernel
State m_state { State::NotStarted };
Process* m_process { nullptr };
bool m_is_userspace { false };
BAN::Atomic<bool> m_is_detached { false };
bool m_delete_process { false };
vaddr_t m_fsbase { 0 };

View File

@@ -62,8 +62,8 @@ namespace Kernel
Mutex m_command_mutex;
BAN::Atomic<bool> m_has_initialized_leds { false };
BAN::Atomic<uint8_t> m_led_state { 0b0001 };
BAN::Atomic<uint8_t> m_rumble_strength { 0x00 };
uint8_t m_led_state { 0b0001 };
uint8_t m_rumble_strength { 0x00 };
friend class BAN::RefPtr<USBJoystick>;
};

View File

@@ -11,8 +11,6 @@ namespace Kernel
BAN_NON_MOVABLE(USBKeyboard);
public:
BAN::ErrorOr<void> initialize() override;
void start_report() override;
void stop_report() override;
@@ -40,8 +38,6 @@ namespace Kernel
uint16_t m_toggle_mask { 0 };
uint16_t m_led_mask { 0 };
BAN::UniqPtr<DMARegion> m_led_region;
BAN::Vector<USBHID::Report> m_outputs;
BAN::Optional<uint8_t> m_repeat_scancode;

View File

@@ -15,6 +15,7 @@ namespace Kernel
uint32_t sector_size() const override { return m_block_size; }
uint64_t total_size() const override { return m_block_size * m_block_count; }
dev_t rdev() const override { return m_rdev; }
BAN::StringView name() const override { return m_name; }
private:
@@ -33,6 +34,7 @@ namespace Kernel
const uint64_t m_block_count;
const uint32_t m_block_size;
const dev_t m_rdev;
const char m_name[4];
friend class BAN::RefPtr<USBSCSIDevice>;

View File

@@ -1,12 +0,0 @@
#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);
};

View File

@@ -57,7 +57,7 @@ namespace Kernel::ACPI
m_last_value = target_conv.value().as.integer.value;
}
auto target_str = TRY(BAN::String::formatted("{}", m_last_value.load()));
auto target_str = TRY(BAN::String::formatted("{}", m_last_value));
if (static_cast<size_t>(offset) >= target_str.size())
return 0;
@@ -67,8 +67,8 @@ namespace Kernel::ACPI
return ncopy;
}
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(EINVAL); }
BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(EINVAL); }
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(EINVAL); }
BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(EINVAL); }
bool can_read_impl() const override { return true; }
bool can_write_impl() const override { return false; }
@@ -90,8 +90,8 @@ namespace Kernel::ACPI
AML::NameString m_method_name;
size_t m_result_index;
BAN::Atomic<uint64_t> m_last_read_ms = 0;
BAN::Atomic<uint64_t> m_last_value = 0;
uint64_t m_last_read_ms = 0;
uint64_t m_last_value = 0;
};
BAN::ErrorOr<void> BatterySystem::initialize(AML::Namespace& acpi_namespace)

View File

@@ -15,8 +15,8 @@ namespace Kernel
AudioController::AudioController()
: CharacterDevice(0644, 0, 0)
, m_rdev(makedev(DeviceNumber::AudioController, s_next_audio_minor++))
{
m_rdev = makedev(DeviceNumber::AudioController, s_next_audio_minor++);
char* ptr = m_name;
BAN::Formatter::print([&ptr](char c) { *ptr++ = c; }, "audio{}", minor(m_rdev));
}

View File

@@ -1,7 +1,6 @@
#include <kernel/Audio/HDAudio/AudioFunctionGroup.h>
#include <kernel/Audio/HDAudio/Registers.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Timer/Timer.h>
#include <BAN/Sort.h>
@@ -203,12 +202,12 @@ namespace Kernel
ASSERT(m_stream_index == 0xFF);
m_stream_index = TRY(m_controller->allocate_stream(HDAudio::StreamType::Output, this));
TRY(reset_stream());
reset_stream();
return {};
}
BAN::ErrorOr<void> HDAudioFunctionGroup::reset_stream()
void HDAudioFunctionGroup::reset_stream()
{
using Regs = HDAudio::Regs;
@@ -220,23 +219,13 @@ namespace Kernel
// stop stream
bar.write8(base + Regs::SDCTL, bar.read8(base + Regs::SDCTL) & 0xFD);
const auto timeout_ms = SystemTimer::get().ms_since_boot() + 100;
// reset stream
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE) | 1);
while (!(bar.read8(base + Regs::SDCTL) & 1))
{
if (SystemTimer::get().ms_since_boot() > timeout_ms)
return BAN::Error::from_errno(ETIMEDOUT);
Processor::pause();
}
bar.write8(base + Regs::SDCTL, (bar.read8(base + Regs::SDCTL) & 0xFE));
while ((bar.read8(base + Regs::SDCTL) & 1))
{
if (SystemTimer::get().ms_since_boot() > timeout_ms)
return BAN::Error::from_errno(ETIMEDOUT);
Processor::pause();
}
// set bdl address, total size and lvi
const paddr_t bdl_paddr = m_bdl_region->paddr() + bdl_offset();
@@ -255,8 +244,6 @@ namespace Kernel
m_bdl_head = 0;
m_bdl_tail = 0;
m_stream_running = false;
return {};
}
BAN::ErrorOr<void> HDAudioFunctionGroup::initialize_output()
@@ -634,13 +621,7 @@ namespace Kernel
m_bdl_tail = (m_bdl_tail + 1) % m_bdl_entry_count;
if (m_bdl_tail == m_bdl_head)
{
if (auto ret = reset_stream(); ret.is_error())
{
dwarnln("failed to reset HDA stream: {}", ret.error());
return;
}
}
reset_stream();
queue_bdl_data();
}

View File

@@ -1,215 +0,0 @@
#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;
}
}

View File

@@ -11,7 +11,6 @@ namespace Kernel
MUST(DevFileSystem::get().allocate_inode(create_inode_info(mode, uid, gid))),
create_inode_info(mode, uid, gid)
)
{
m_kind |= InodeKind::DEVICE;
}
{ }
}

View File

@@ -52,14 +52,13 @@ namespace Kernel
FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp)
: CharacterDevice(mode, uid, gid)
, m_name(MUST(BAN::String::formatted("fb{}", minor(rdev))))
, m_rdev(rdev)
, m_video_memory_paddr(paddr)
, m_width(width)
, m_height(height)
, m_pitch(pitch)
, m_bpp(bpp)
{
m_rdev = rdev;
}
{ }
FramebufferDevice::~FramebufferDevice()
{
@@ -356,7 +355,51 @@ namespace Kernel
void do_msync(uint32_t first_pixel, uint32_t pixel_count)
{
m_framebuffer->sync_pixels_linear(first_pixel, pixel_count);
if (!Processor::get_should_print_cpu_load())
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:

View File

@@ -14,24 +14,6 @@ namespace Kernel
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()
{
for (auto& [inode, _] : m_listening_events)
@@ -171,6 +153,8 @@ namespace Kernel
REMOVE_IT();
{
LockGuard inode_locker(inode->m_mutex);
#define CHECK_EVENT_BIT(mask, func) \
if ((events & mask) && !inode->func()) \
events &= ~mask;

View File

@@ -102,7 +102,7 @@ namespace Kernel
uint64_t next_sync_ms { sync_interval_ms };
while (true)
{
devfs->m_device_lock.lock();
LockGuard _(devfs->m_device_lock);
while (!devfs->m_should_sync)
{
const uint64_t current_ms = SystemTimer::get().ms_since_boot();
@@ -110,17 +110,12 @@ namespace Kernel
break;
devfs->m_sync_thread_blocker.block_with_timeout_ms(next_sync_ms - current_ms, &devfs->m_device_lock);
}
BAN::Vector<BAN::RefPtr<StorageDevice>> storage_devices;
for (auto& device : devfs->m_devices)
if (device->is_storage_device())
MUST(storage_devices.push_back(static_cast<StorageDevice*>(device.ptr())));
devfs->m_device_lock.unlock();
if (auto ret = static_cast<StorageDevice*>(device.ptr())->sync_disk_cache(); ret.is_error())
dwarnln("disk sync: {}", ret.error());
for (auto& device : storage_devices)
if (auto ret = device->sync_disk_cache(); ret.is_error())
dwarnln("disk sync: {}", ret.error());
LockGuard _(devfs->m_device_lock);
next_sync_ms = SystemTimer::get().ms_since_boot() + sync_interval_ms;
devfs->m_should_sync = false;
devfs->m_sync_done.unblock();
@@ -153,7 +148,7 @@ namespace Kernel
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
MUST(m_devices.push_back(device));
dprintln_if(DEBUG_DEVFS, "Added device /dev/{}", device->name());
dprintln("Added device /dev/{}", device->name());
}
void DevFileSystem::remove_device(BAN::RefPtr<Device> device)
@@ -170,7 +165,7 @@ namespace Kernel
}
}
dprintln_if(DEBUG_DEVFS, "Removed device /dev/{}", device->name());
dprintln("Removed device /dev/{}", device->name());
}
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<TmpInode> inode)

View File

@@ -1,5 +1,4 @@
#include <kernel/FS/EventFD.h>
#include <kernel/Lock/LockGuard.h>
#include <sys/epoll.h>
@@ -14,44 +13,21 @@ namespace Kernel
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)
{
if (buffer.size() < sizeof(uint64_t))
return BAN::Error::from_errno(EINVAL);
LockGuard _(m_mutex);
while (m_value == 0)
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
const uint64_t read_value = m_is_semaphore ? 1 : m_value.load();
const uint64_t read_value = m_is_semaphore ? 1 : m_value;
m_value -= read_value;
buffer.as<uint64_t>() = read_value;
epoll_notify(EPOLLOUT);
m_thread_blocker.unblock();
return sizeof(uint64_t);
}
@@ -64,8 +40,6 @@ namespace Kernel
if (write_value == UINT64_MAX)
return BAN::Error::from_errno(EINVAL);
LockGuard _(m_mutex);
while (m_value + write_value < m_value)
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
@@ -74,8 +48,6 @@ namespace Kernel
if (m_value > 0)
epoll_notify(EPOLLIN);
m_thread_blocker.unblock();
return sizeof(uint64_t);
}

View File

@@ -167,36 +167,10 @@ namespace Kernel
BAN::ErrorOr<void> Ext2FS::initialize_root_inode()
{
m_root_inode = TRY(open_inode(Ext2::Enum::ROOT_INO));
m_root_inode = TRY(Ext2Inode::create(*this, Ext2::Enum::ROOT_INO));
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)
{
auto bgd_buffer = TRY(m_buffer_manager.get_buffer());
@@ -307,7 +281,7 @@ namespace Kernel
auto& inode = inode_buffer.span().slice(inode_location.offset).as<Ext2::Inode>();
#if EXT2_VERIFY_NO_BLOCKS
static const char zero_buffer[sizeof(inode.block)] {};
ASSERT(memcmp(inode.block.block, zero_buffer, sizeof(inode.block)) == 0);
ASSERT(memcmp(inode.block, zero_buffer, sizeof(inode.block)) == 0);
#endif
bool is_directory = Inode::Mode(inode.mode).ifdir();
memset(&inode, 0x00, m_superblock.inode_size);
@@ -333,27 +307,31 @@ namespace Kernel
BAN::ErrorOr<void> Ext2FS::read_block(uint32_t block, BlockBufferWrapper& buffer)
{
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
const uint32_t block_size = this->block_size();
const uint32_t sectors_per_block = block_size / sector_size;
ASSERT(block >= superblock().first_data_block + 1);
ASSERT(buffer.size() >= block_size);
TRY(m_block_device->read_blocks(block * sectors_per_block, sectors_per_block, buffer.span()));
return {};
}
BAN::ErrorOr<void> Ext2FS::write_block(uint32_t block, const BlockBufferWrapper& buffer)
{
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
const uint32_t block_size = this->block_size();
const uint32_t sectors_per_block = block_size / sector_size;
ASSERT(block >= superblock().first_data_block + 1);
ASSERT(buffer.size() >= block_size);
TRY(m_block_device->write_blocks(block * sectors_per_block, sectors_per_block, buffer.span()));
return {};
}
@@ -361,6 +339,8 @@ namespace Kernel
{
auto superblock_buffer = TRY(get_block_buffer());
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
ASSERT(1024 % sector_size == 0);
@@ -384,6 +364,8 @@ namespace Kernel
BAN::ErrorOr<void> Ext2FS::sync_block(uint32_t block)
{
LockGuard _(m_mutex);
const uint32_t sector_size = m_block_device->blksize();
const uint32_t block_size = this->block_size();
const uint32_t sectors_per_block = block_size / sector_size;

File diff suppressed because it is too large Load Diff

View File

@@ -23,30 +23,27 @@ namespace Kernel
return BAN::to_unix_time(ban_time);
}
static timespec fat_date_to_timespec(FAT::Date date, FAT::Time time)
blksize_t FATInode::blksize() const
{
const time_t epoch = fat_date_to_epoch(date, time);
return m_fs.inode_block_size(this);
}
timespec FATInode::atime() const
{
const time_t epoch = fat_date_to_epoch(m_entry.last_access_date, {});
return timespec { .tv_sec = epoch, .tv_nsec = 0 };
}
FATInode::FATInode(FATFS& fs, const FAT::DirectoryEntry& entry, ino_t ino, uint32_t block_count)
: m_fs(fs)
, m_entry(entry)
, m_block_count(block_count)
timespec FATInode::mtime() const
{
m_ino = ino;
m_mode = ((m_entry.attr & FAT::FileAttr::DIRECTORY) ? Mode::IFDIR : Mode::IFREG) | 0777;
m_nlink = 1;
m_uid = 0;
m_gid = 0;
m_size = m_entry.file_size;
m_blksize = fs.inode_block_size(this);
m_atime = fat_date_to_timespec(m_entry.last_access_date, {});
m_mtime = fat_date_to_timespec(m_entry.write_date, m_entry.write_time);
m_ctime = fat_date_to_timespec(m_entry.creation_date, m_entry.creation_time);
m_blocks = m_block_count;
m_dev = 0;
m_rdev = 0;
const time_t epoch = fat_date_to_epoch(m_entry.write_date, m_entry.write_time);
return timespec { .tv_sec = epoch, .tv_nsec = 0 };
}
timespec FATInode::ctime() const
{
const time_t epoch = fat_date_to_epoch(m_entry.creation_date, m_entry.creation_time);
return timespec { .tv_sec = epoch, .tv_nsec = 0 };
}
const FileSystem* FATInode::filesystem() const

View File

@@ -5,14 +5,14 @@
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<FileSystem>> FileSystem::from_block_device(BAN::RefPtr<BlockDevice> block_device)
{
if (auto res = Ext2FS::probe(block_device); !res.is_error() && res.value())
return BAN::RefPtr<FileSystem>(TRY(Ext2FS::create(block_device)));
if (auto res = FATFS::probe(block_device); !res.is_error() && res.value())
return BAN::RefPtr<FileSystem>(TRY(FATFS::create(block_device)));
dprintln("Unsupported filesystem");
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<BAN::RefPtr<FileSystem>> FileSystem::from_block_device(BAN::RefPtr<BlockDevice> block_device)
{
if (auto res = Ext2FS::probe(block_device); !res.is_error() && res.value())
return BAN::RefPtr<FileSystem>(TRY(Ext2FS::create(block_device)));
if (auto res = FATFS::probe(block_device); !res.is_error() && res.value())
return BAN::RefPtr<FileSystem>(TRY(FATFS::create(block_device)));
dprintln("Unsupported filesystem");
return BAN::Error::from_errno(ENOTSUP);
}
}

View File

@@ -5,7 +5,6 @@
#include <kernel/Memory/FileBackedRegion.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
namespace Kernel
@@ -63,6 +62,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> Inode::find_inode(BAN::StringView name)
{
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
return find_inode_impl(name);
@@ -70,6 +70,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::list_next_inodes(off_t offset, struct dirent* list, size_t list_len)
{
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
return list_next_inodes_impl(offset, list, list_len);
@@ -77,6 +78,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::create_file(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (Mode(mode).ifdir())
@@ -88,6 +90,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::create_directory(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (!Mode(mode).ifdir())
@@ -99,6 +102,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::link_inode(BAN::StringView name, BAN::RefPtr<Inode> inode)
{
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (inode->mode().ifdir())
@@ -112,6 +116,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::rename_inode(BAN::RefPtr<Inode> old_parent, BAN::StringView old_name, BAN::StringView new_name)
{
LockGuard _(m_mutex);
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (!old_parent->mode().ifdir())
@@ -125,6 +130,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::unlink(BAN::StringView name)
{
LockGuard _(m_mutex);
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (name == "."_sv || name == ".."_sv)
@@ -136,6 +142,7 @@ namespace Kernel
BAN::ErrorOr<BAN::String> Inode::link_target()
{
LockGuard _(m_mutex);
if (!mode().iflnk())
return BAN::Error::from_errno(EINVAL);
return link_target_impl();
@@ -143,6 +150,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::set_link_target(BAN::StringView target)
{
LockGuard _(m_mutex);
if (!mode().iflnk())
return BAN::Error::from_errno(EINVAL);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
@@ -152,6 +160,7 @@ namespace Kernel
BAN::ErrorOr<long> Inode::accept(sockaddr* address, socklen_t* address_len, int flags)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return accept_impl(address, address_len, flags);
@@ -159,6 +168,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::bind(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return bind_impl(address, address_len);
@@ -166,6 +176,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::connect(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return connect_impl(address, address_len);
@@ -173,6 +184,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::listen(int backlog)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return listen_impl(backlog);
@@ -180,6 +192,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::recvmsg(msghdr& message, int flags)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return recvmsg_impl(message, flags);
@@ -187,6 +200,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::sendmsg(const msghdr& message, int flags)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return sendmsg_impl(message, flags);
@@ -194,6 +208,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::getsockname(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return getsockname_impl(address, address_len);
@@ -201,6 +216,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::getpeername(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return getpeername_impl(address, address_len);
@@ -208,6 +224,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::getsockopt(int level, int option, void* value, socklen_t* value_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return getsockopt_impl(level, option, value, value_len);
@@ -215,6 +232,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::setsockopt(int level, int option, const void* value, socklen_t value_len)
{
LockGuard _(m_mutex);
if (!mode().ifsock())
return BAN::Error::from_errno(ENOTSOCK);
return setsockopt_impl(level, option, value, value_len);
@@ -222,6 +240,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::read(off_t offset, BAN::ByteSpan buffer)
{
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
return read_impl(offset, buffer);
@@ -229,6 +248,7 @@ namespace Kernel
BAN::ErrorOr<size_t> Inode::write(off_t offset, BAN::ConstByteSpan buffer)
{
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
@@ -238,6 +258,7 @@ namespace Kernel
BAN::ErrorOr<void> Inode::truncate(size_t size)
{
LockGuard _(m_mutex);
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
@@ -247,93 +268,66 @@ namespace Kernel
BAN::ErrorOr<void> Inode::chmod(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
LockGuard _(m_mutex);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
return BAN::Error::from_errno(EROFS);
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 {};
return chmod_impl(mode);
}
BAN::ErrorOr<void> Inode::chown(uid_t uid, gid_t gid)
{
LockGuard _(m_mutex);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
return BAN::Error::from_errno(EROFS);
// 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 {};
return chown_impl(uid, gid);
}
BAN::ErrorOr<void> Inode::utimens(const timespec times[2])
{
LockGuard _(m_mutex);
if (auto* fs = filesystem(); fs && (fs->flag() & ST_RDONLY))
return BAN::Error::from_errno(EROFS);
// 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 {};
return utimens_impl(times);
}
BAN::ErrorOr<void> Inode::fsync()
{
// TODO: should we sync MAP_SHARED data?
TRY(sync_inode(SyncType::General));
TRY(sync_data());
return {};
LockGuard _(m_mutex);
if (auto shared = m_shared_region.lock())
for (size_t i = 0; i < shared->pages.size(); i++)
shared->sync(i);
return fsync_impl();
}
bool Inode::can_read() const
{
LockGuard _(m_mutex);
return can_read_impl();
}
bool Inode::can_write() const
{
LockGuard _(m_mutex);
return can_write_impl();
}
bool Inode::has_error() const
{
LockGuard _(m_mutex);
return has_error_impl();
}
bool Inode::has_hungup() const
{
LockGuard _(m_mutex);
return has_hungup_impl();
}
BAN::ErrorOr<long> Inode::ioctl(int request, void* arg)
{
auto ret = ioctl_impl(request, arg);
if (!ret.is_error() || ret.error().get_error_code() != ENOTSUP)
return BAN::move(ret);
switch (request)
{
case TIOCGWINSZ:
case TIOCSWINSZ:
case TCGETS:
case TCSETS:
case TCSETSW:
case TCSETSF:
return BAN::Error::from_errno(EINVAL);
default:
return BAN::Error::from_errno(ENOTSUP);
}
LockGuard _(m_mutex);
return ioctl_impl(request, arg);
}
BAN::ErrorOr<void> Inode::add_epoll(class Epoll* epoll)

View File

@@ -1,4 +1,3 @@
#include <BAN/HashMap.h>
#include <kernel/FS/Pipe.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Thread.h>
@@ -6,131 +5,30 @@
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
namespace Kernel
{
static constexpr size_t s_pipe_buffer_size = 0x10000;
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)
BAN::ErrorOr<BAN::RefPtr<Inode>> Pipe::create(const Credentials& 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);
auto* pipe_ptr = new Pipe(credentials);
if (pipe_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto pipe = BAN::RefPtr<Pipe>::adopt(pipe_ptr);
pipe->m_buffer = TRY(ByteRingBuffer::create(s_pipe_buffer_size));
pipe->m_reading_count++;
pipe->m_writing_count++;
return BAN::RefPtr<Inode>(pipe);
}
Pipe::Pipe(const struct stat& st)
Pipe::Pipe(const Credentials& credentials)
: m_uid(credentials.euid())
, m_gid(credentials.egid())
{
m_ino = st.st_ino;
m_mode = st.st_mode;
m_nlink = st.st_nlink;
m_uid = st.st_uid;
m_gid = st.st_gid;
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_kind |= InodeKind::PIPE;
}
Pipe::~Pipe()
{
if (!m_named_inode)
return;
LockGuard _(s_named_pipe_mutex);
s_named_pipes.remove(m_named_inode);
timespec current_time = SystemTimer::get().real_time();
m_atime = current_time;
m_mtime = current_time;
m_ctime = current_time;
}
void Pipe::on_clone(int status_flags)
@@ -173,48 +71,8 @@ namespace Kernel
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)
{
LockGuard _(m_mutex);
while (m_buffer->empty())
{
if (m_writing_count == 0)
@@ -237,8 +95,6 @@ namespace Kernel
BAN::ErrorOr<size_t> Pipe::write_impl(off_t, BAN::ConstByteSpan buffer)
{
LockGuard _(m_mutex);
while (m_buffer->full())
{
if (m_reading_count == 0)
@@ -263,9 +119,4 @@ namespace Kernel
return to_copy;
}
BAN::ErrorOr<void> Pipe::truncate_impl(size_t)
{
return BAN::Error::from_errno(ENODEV);
}
}

View File

@@ -16,23 +16,22 @@ namespace Kernel
MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
auto meminfo_inode = MUST(ProcROInode::create_new(
[](off_t offset, BAN::ByteSpan buffer, void*) -> BAN::ErrorOr<size_t>
[](off_t offset, BAN::ByteSpan buffer) -> size_t
{
ASSERT(offset >= 0);
if ((size_t)offset >= sizeof(full_meminfo_t))
return 0;
const full_meminfo_t meminfo {
.page_size = PAGE_SIZE,
.free_pages = Heap::get().free_pages(),
.used_pages = Heap::get().used_pages(),
};
full_meminfo_t meminfo;
meminfo.page_size = PAGE_SIZE;
meminfo.free_pages = Heap::get().free_pages();
meminfo.used_pages = Heap::get().used_pages();
size_t bytes = BAN::Math::min<size_t>(sizeof(full_meminfo_t) - offset, buffer.size());
memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes);
return bytes;
},
*s_instance, nullptr, 0444, 0, 0
*s_instance, 0444, 0, 0
));
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*meminfo_inode, "meminfo"_sv));
@@ -49,35 +48,6 @@ namespace Kernel
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()
{
ASSERT(s_instance);

View File

@@ -28,9 +28,8 @@ namespace Kernel
ProcPidInode::ProcPidInode(Process& process, TmpFileSystem& fs, const TmpInodeInfo& inode_info)
: TmpDirectoryInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_process(process)
{
m_uid = process.credentials().ruid();
m_gid = process.credentials().rgid();
}
void ProcPidInode::cleanup()
@@ -58,9 +57,7 @@ namespace Kernel
, m_process(process)
, m_callback(callback)
{
m_mode |= Inode::Mode::IFREG;
m_uid = process.credentials().ruid();
m_gid = process.credentials().rgid();
m_inode_info.mode |= Inode::Mode::IFREG;
}
BAN::ErrorOr<size_t> ProcROProcessInode::read_impl(off_t offset, BAN::ByteSpan buffer)
@@ -85,9 +82,7 @@ namespace Kernel
, m_process(process)
, m_callback(callback)
{
m_mode |= Inode::Mode::IFLNK;
m_uid = process.credentials().ruid();
m_gid = process.credentials().rgid();
m_inode_info.mode |= Inode::Mode::IFLNK;
}
BAN::ErrorOr<BAN::String> ProcSymlinkProcessInode::link_target_impl()
@@ -95,29 +90,28 @@ namespace Kernel
return (m_process.*m_callback)();
}
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)
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)
{
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
auto* inode_ptr = new ProcROInode(callback, fs, argument, inode_info);
auto* inode_ptr = new ProcROInode(callback, fs, inode_info);
if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
}
ProcROInode::ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem& fs, void* argument, const TmpInodeInfo& inode_info)
ProcROInode::ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem& fs, const TmpInodeInfo& inode_info)
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_callback(callback)
, m_argument(argument)
{
m_mode |= Inode::Mode::IFREG;
m_inode_info.mode |= Inode::Mode::IFREG;
}
BAN::ErrorOr<size_t> ProcROInode::read_impl(off_t offset, BAN::ByteSpan buffer)
{
if (offset < 0)
return BAN::Error::from_errno(EINVAL);
return TRY(m_callback(offset, buffer, m_argument));
return m_callback(offset, buffer);
}
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)
@@ -136,7 +130,7 @@ namespace Kernel
, m_destructor(destructor)
, m_data(data)
{
m_mode |= Inode::Mode::IFLNK;
m_inode_info.mode |= Inode::Mode::IFLNK;
}
ProcSymlinkInode::~ProcSymlinkInode()
@@ -164,8 +158,6 @@ namespace Kernel
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_process(process)
{
m_uid = process.credentials().ruid();
m_gid = process.credentials().rgid();
ASSERT(mode().ifdir());
}

View File

@@ -131,7 +131,7 @@ namespace Kernel
PageTable::with_fast_page(inode_location.paddr, [&] {
auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
ASSERT(inode_info.nlink == 0);
for (auto paddr : inode_info.tmp_blocks.block)
for (auto paddr : inode_info.block)
ASSERT(paddr == 0);
inode_info = {};
});

View File

@@ -48,6 +48,11 @@ namespace Kernel
return &m_fs;
}
dev_t TmpInode::dev() const
{
return m_fs.rdev();
}
BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpInode::create_from_existing(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info)
{
TmpInode* inode_ptr = nullptr;
@@ -69,21 +74,9 @@ namespace Kernel
TmpInode::TmpInode(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info)
: m_fs(fs)
, m_inode_info(info)
, m_ino(ino)
{
m_ino = ino;
m_rdev = 0;
m_blksize = PAGE_SIZE;
m_mode = info.mode;
m_nlink = info.nlink;
m_uid = info.uid;
m_gid = info.gid;
m_size = info.size;
m_atime = info.atime;
m_mtime = info.mtime;
m_ctime = info.ctime;
m_blocks = info.blocks;
m_dev = fs.rdev();
m_tmp_blocks = info.tmp_blocks;
// FIXME: this should be able to fail
MUST(fs.add_to_cache(this));
}
@@ -92,60 +85,61 @@ namespace Kernel
{
if (nlink() > 0)
{
write_inode_to_fs();
sync();
return;
}
free_all_blocks();
m_fs.delete_inode(ino());
}
void TmpInode::write_inode_to_fs()
{
TmpInodeInfo info = {
.mode = m_mode.load(),
.uid = m_uid.load(),
.gid = m_gid.load(),
.atime = m_atime,
.ctime = m_ctime,
.mtime = m_mtime,
.nlink = m_nlink.load(),
.size = static_cast<size_t>(size()),
.blocks = m_blocks.load(),
.tmp_blocks = m_tmp_blocks,
};
m_fs.write_inode(m_ino, info);
}
BAN::ErrorOr<void> TmpInode::sync_inode(SyncType)
BAN::ErrorOr<void> TmpInode::chmod_impl(mode_t new_mode)
{
ASSERT(!(new_mode & Inode::Mode::TYPE_MASK));
m_inode_info.mode &= Inode::Mode::TYPE_MASK;
m_inode_info.mode |= new_mode;
return {};
}
BAN::ErrorOr<void> TmpInode::sync_data()
BAN::ErrorOr<void> TmpInode::chown_impl(uid_t new_uid, gid_t new_gid)
{
m_inode_info.uid = new_uid;
m_inode_info.gid = new_gid;
return {};
}
BAN::ErrorOr<void> TmpInode::utimens_impl(const timespec times[2])
{
if (times[0].tv_nsec != UTIME_OMIT)
m_inode_info.atime = times[0];
if (times[1].tv_nsec != UTIME_OMIT)
m_inode_info.atime = times[1];
return {};
}
void TmpInode::sync()
{
m_fs.write_inode(m_ino, m_inode_info);
}
void TmpInode::free_all_blocks()
{
LockGuard _(m_lock);
if (mode().iflnk() && static_cast<size_t>(size()) <= sizeof(TmpBlocks::block))
if (mode().iflnk() && m_inode_info.size <= sizeof(TmpInodeInfo::block))
goto free_all_blocks_done;
for (size_t i = 0; i < TmpBlocks::direct_block_count; i++)
if (m_tmp_blocks.block[i])
m_fs.free_block(m_tmp_blocks.block[i]);
if (size_t block = m_tmp_blocks.block[TmpBlocks::direct_block_count + 0])
free_indirect_blocks_no_lock(block, 1);
if (size_t block = m_tmp_blocks.block[TmpBlocks::direct_block_count + 1])
free_indirect_blocks_no_lock(block, 2);
if (size_t block = m_tmp_blocks.block[TmpBlocks::direct_block_count + 2])
free_indirect_blocks_no_lock(block, 3);
for (size_t i = 0; i < TmpInodeInfo::direct_block_count; i++)
if (m_inode_info.block[i])
m_fs.free_block(m_inode_info.block[i]);
if (size_t block = m_inode_info.block[TmpInodeInfo::direct_block_count + 0])
free_indirect_blocks(block, 1);
if (size_t block = m_inode_info.block[TmpInodeInfo::direct_block_count + 1])
free_indirect_blocks(block, 2);
if (size_t block = m_inode_info.block[TmpInodeInfo::direct_block_count + 2])
free_indirect_blocks(block, 3);
free_all_blocks_done:
for (auto& block : m_tmp_blocks.block)
for (auto& block : m_inode_info.block)
block = 0;
}
void TmpInode::free_indirect_blocks_no_lock(size_t block, uint32_t depth)
void TmpInode::free_indirect_blocks(size_t block, uint32_t depth)
{
ASSERT(block != 0);
@@ -166,7 +160,7 @@ namespace Kernel
if (next_block == 0)
continue;
free_indirect_blocks_no_lock(next_block, depth - 1);
free_indirect_blocks(next_block, depth - 1);
}
m_fs.free_block(block);
@@ -174,33 +168,31 @@ namespace Kernel
BAN::Optional<size_t> TmpInode::block_index(size_t data_block_index)
{
LockGuard _(m_lock);
if (data_block_index < TmpBlocks::direct_block_count)
if (data_block_index < TmpInodeInfo::direct_block_count)
{
if (m_tmp_blocks.block[data_block_index] == 0)
if (m_inode_info.block[data_block_index] == 0)
return {};
return m_tmp_blocks.block[data_block_index];
return m_inode_info.block[data_block_index];
}
data_block_index -= TmpBlocks::direct_block_count;
data_block_index -= TmpInodeInfo::direct_block_count;
const size_t indices_per_block = blksize() / sizeof(size_t);
if (data_block_index < indices_per_block)
return block_index_from_indirect_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 0], data_block_index, 1);
return block_index_from_indirect(m_inode_info.block[TmpInodeInfo::direct_block_count + 0], data_block_index, 1);
data_block_index -= indices_per_block;
if (data_block_index < indices_per_block * indices_per_block)
return block_index_from_indirect_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 1], data_block_index, 2);
return block_index_from_indirect(m_inode_info.block[TmpInodeInfo::direct_block_count + 1], data_block_index, 2);
data_block_index -= indices_per_block * indices_per_block;
if (data_block_index < indices_per_block * indices_per_block * indices_per_block)
return block_index_from_indirect_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 2], data_block_index, 3);
return block_index_from_indirect(m_inode_info.block[TmpInodeInfo::direct_block_count + 2], data_block_index, 3);
ASSERT_NOT_REACHED();
}
BAN::Optional<size_t> TmpInode::block_index_from_indirect_no_lock(size_t block, size_t index, uint32_t depth)
BAN::Optional<size_t> TmpInode::block_index_from_indirect(size_t block, size_t index, uint32_t depth)
{
if (block == 0)
return {};
@@ -223,46 +215,44 @@ namespace Kernel
if (depth == 1)
return next_block;
return block_index_from_indirect_no_lock(next_block, index, depth - 1);
return block_index_from_indirect(next_block, index, depth - 1);
}
BAN::ErrorOr<size_t> TmpInode::block_index_with_allocation(size_t data_block_index)
{
LockGuard _(m_lock);
if (data_block_index < TmpBlocks::direct_block_count)
if (data_block_index < TmpInodeInfo::direct_block_count)
{
if (m_tmp_blocks.block[data_block_index] == 0)
if (m_inode_info.block[data_block_index] == 0)
{
m_tmp_blocks.block[data_block_index] = TRY(m_fs.allocate_block());
m_blocks++;
m_inode_info.block[data_block_index] = TRY(m_fs.allocate_block());
m_inode_info.blocks++;
}
return m_tmp_blocks.block[data_block_index];
return m_inode_info.block[data_block_index];
}
data_block_index -= TmpBlocks::direct_block_count;
data_block_index -= TmpInodeInfo::direct_block_count;
const size_t indices_per_block = blksize() / sizeof(size_t);
if (data_block_index < indices_per_block)
return block_index_from_indirect_with_allocation_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 0], data_block_index, 1);
return block_index_from_indirect_with_allocation(m_inode_info.block[TmpInodeInfo::direct_block_count + 0], data_block_index, 1);
data_block_index -= indices_per_block;
if (data_block_index < indices_per_block * indices_per_block)
return block_index_from_indirect_with_allocation_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 1], data_block_index, 2);
return block_index_from_indirect_with_allocation(m_inode_info.block[TmpInodeInfo::direct_block_count + 1], data_block_index, 2);
data_block_index -= indices_per_block * indices_per_block;
if (data_block_index < indices_per_block * indices_per_block * indices_per_block)
return block_index_from_indirect_with_allocation_no_lock(m_tmp_blocks.block[TmpBlocks::direct_block_count + 2], data_block_index, 3);
return block_index_from_indirect_with_allocation(m_inode_info.block[TmpInodeInfo::direct_block_count + 2], data_block_index, 3);
ASSERT_NOT_REACHED();
}
BAN::ErrorOr<size_t> TmpInode::block_index_from_indirect_with_allocation_no_lock(size_t& block, size_t index, uint32_t depth)
BAN::ErrorOr<size_t> TmpInode::block_index_from_indirect_with_allocation(size_t& block, size_t index, uint32_t depth)
{
if (block == 0)
{
block = TRY(m_fs.allocate_block());
m_blocks++;
m_inode_info.blocks++;
}
ASSERT(depth >= 1);
@@ -280,7 +270,7 @@ namespace Kernel
if (next_block == 0)
{
next_block = TRY(m_fs.allocate_block());
m_blocks++;
m_inode_info.blocks++;
m_fs.with_block_buffer(block, [&](BAN::ByteSpan block_buffer) {
block_buffer.as_span<size_t>()[(index / divisor) % indices_per_block] = next_block;
@@ -290,7 +280,7 @@ namespace Kernel
if (depth == 1)
return next_block;
return block_index_from_indirect_with_allocation_no_lock(next_block, index, depth - 1);
return block_index_from_indirect_with_allocation(next_block, index, depth - 1);
}
/* FILE INODE */
@@ -319,8 +309,6 @@ namespace Kernel
BAN::ErrorOr<size_t> TmpFileInode::read_impl(off_t offset, BAN::ByteSpan out_buffer)
{
LockGuard _(m_lock);
if (offset >= size() || out_buffer.size() == 0)
return 0;
@@ -351,12 +339,7 @@ namespace Kernel
BAN::ErrorOr<size_t> TmpFileInode::write_impl(off_t offset, BAN::ConstByteSpan in_buffer)
{
if (offset < 0)
return BAN::Error::from_errno(EINVAL);
if (BAN::Math::will_addition_overflow<size_t>(offset, in_buffer.size()))
return BAN::Error::from_errno(EOVERFLOW);
LockGuard _(m_lock);
// FIXME: handle overflow
if (offset + in_buffer.size() > (size_t)size())
TRY(truncate_impl(offset + in_buffer.size()));
@@ -387,36 +370,12 @@ namespace Kernel
{
// FIXME: if size is decreased, we should probably free
// unused blocks
m_size = new_size;
m_inode_info.size = new_size;
return {};
}
/* FIFO INODE */
BAN::ErrorOr<BAN::RefPtr<TmpFIFOInode>> TmpFIFOInode::create_new(TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
auto info = create_inode_info(Mode::IFIFO | mode, uid, gid);
ino_t ino = TRY(fs.allocate_inode(info));
auto* inode_ptr = new TmpFIFOInode(fs, ino, info);
if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<TmpFIFOInode>::adopt(inode_ptr);
}
TmpFIFOInode::TmpFIFOInode(TmpFileSystem& fs, ino_t ino, const TmpInodeInfo& info)
: TmpInode(fs, ino, info)
{
ASSERT(mode().ififo());
}
TmpFIFOInode::~TmpFIFOInode()
{
}
/* SOCKET INODE */
BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> TmpSocketInode::create_new(TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
auto info = create_inode_info(Mode::IFSOCK | mode, uid, gid);
@@ -468,15 +427,13 @@ namespace Kernel
BAN::ErrorOr<void> TmpSymlinkInode::set_link_target_impl(BAN::StringView new_target)
{
LockGuard _(m_lock);
free_all_blocks();
m_size = 0;
m_inode_info.size = 0;
if (new_target.size() <= sizeof(TmpBlocks::block))
if (new_target.size() <= sizeof(TmpInodeInfo::block))
{
memcpy(m_tmp_blocks.block.data(), new_target.data(), new_target.size());
m_size = new_target.size();
memcpy(m_inode_info.block.data(), new_target.data(), new_target.size());
m_inode_info.size = new_target.size();
return {};
}
@@ -490,7 +447,7 @@ namespace Kernel
memcpy(bytespan.data(), new_target.data() + i * blksize(), byte_count);
});
m_size += byte_count;
m_inode_info.size += byte_count;
}
return {};
@@ -498,14 +455,12 @@ namespace Kernel
BAN::ErrorOr<BAN::String> TmpSymlinkInode::link_target_impl()
{
LockGuard _(m_lock);
BAN::String result;
TRY(result.resize(size()));
if ((size_t)size() <= sizeof(TmpBlocks::block))
if ((size_t)size() <= sizeof(TmpInodeInfo::block))
{
memcpy(result.data(), m_tmp_blocks.block.data(), size());
memcpy(result.data(), m_inode_info.block.data(), size());
return result;
}
@@ -567,7 +522,7 @@ namespace Kernel
{
}
BAN::ErrorOr<void> TmpDirectoryInode::prepare_unlink_no_lock()
BAN::ErrorOr<void> TmpDirectoryInode::prepare_unlink()
{
ino_t dot_ino = 0;
ino_t dotdot_ino = 0;
@@ -594,14 +549,14 @@ namespace Kernel
{
auto inode = TRY(m_fs.open_inode(dot_ino));
ASSERT(inode->nlink() > 0);
inode->m_nlink--;
inode->m_inode_info.nlink--;
}
if (dotdot_ino)
{
auto inode = TRY(m_fs.open_inode(dotdot_ino));
ASSERT(inode->nlink() > 0);
inode->m_nlink--;
inode->m_inode_info.nlink--;
}
return {};
@@ -609,15 +564,15 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> TmpDirectoryInode::find_inode_impl(BAN::StringView name)
{
LockGuard _(m_lock);
ino_t result = 0;
for_each_valid_entry([&](TmpDirectoryEntry& entry) {
if (entry.name_sv() != name)
return BAN::Iteration::Continue;
result = entry.ino;
return BAN::Iteration::Break;
});
if (result == 0)
return BAN::Error::from_errno(ENOENT);
@@ -633,8 +588,6 @@ namespace Kernel
return BAN::Error::from_errno(ENOBUFS);
}
LockGuard _(m_lock);
auto block_index = this->block_index(data_block_index);
// if we reach a non-allocated block, it marks the end
@@ -687,9 +640,6 @@ namespace Kernel
case Mode::IFLNK:
new_inode = TRY(TmpSymlinkInode::create_new(m_fs, mode, uid, gid, ""_sv));
break;
case Mode::IFIFO:
new_inode = TRY(TmpFIFOInode::create_new(m_fs, mode, uid, gid));
break;
case Mode::IFSOCK:
new_inode = TRY(TmpSocketInode::create_new(m_fs, mode, uid, gid));
break;
@@ -714,8 +664,6 @@ namespace Kernel
ASSERT(!inode->mode().ifdir());
ASSERT(&m_fs == inode->filesystem());
LockGuard _(m_lock);
if (!find_inode_impl(name).is_error())
return BAN::Error::from_errno(EEXIST);
@@ -732,17 +680,16 @@ namespace Kernel
auto* tmp_parent = static_cast<TmpDirectoryInode*>(old_parent.ptr());
// FIXME: is this a possible deadlock?
LockGuard _0(tmp_parent->m_lock);
LockGuard _1(m_lock);
// FIXME: possible deadlock :)
LockGuard _(tmp_parent->m_mutex);
auto old_inode = TRY(tmp_parent->find_inode_impl(old_name));
auto* tmp_inode = static_cast<TmpInode*>(old_inode.ptr());
if (auto find_result = find_inode_impl(new_name); find_result.is_error())
if (auto replace_or_error = find_inode_impl(new_name); replace_or_error.is_error())
{
if (find_result.error().get_error_code() != ENOENT)
return find_result.release_error();
if (replace_or_error.error().get_error_code() != ENOENT)
return replace_or_error.release_error();
}
else
{
@@ -764,15 +711,15 @@ namespace Kernel
BAN::ErrorOr<void> TmpDirectoryInode::unlink_inode(BAN::StringView name, bool cleanup)
{
LockGuard _(m_lock);
ino_t entry_ino = 0;
for_each_valid_entry([&](TmpDirectoryEntry& entry) {
if (entry.name_sv() != name)
return BAN::Iteration::Continue;
entry_ino = entry.ino;
return BAN::Iteration::Break;
});
if (entry_ino == 0)
return BAN::Error::from_errno(ENOENT);
@@ -781,8 +728,8 @@ namespace Kernel
ASSERT(inode->nlink() > 0);
if (cleanup)
TRY(inode->prepare_unlink_no_lock());
inode->m_nlink--;
TRY(inode->prepare_unlink());
inode->m_inode_info.nlink--;
if (inode->nlink() == 0)
m_fs.remove_from_cache(inode);
@@ -802,8 +749,6 @@ namespace Kernel
{
static constexpr size_t directory_entry_alignment = sizeof(TmpDirectoryEntry);
LockGuard _(m_lock);
auto find_result = find_inode_impl(name);
if (!find_result.is_error())
return BAN::Error::from_errno(EEXIST);
@@ -865,7 +810,7 @@ namespace Kernel
if (done)
{
// add link to linked inode
inode.m_nlink++;
inode.m_inode_info.nlink++;
return {};
}
}
@@ -894,10 +839,10 @@ namespace Kernel
});
// increase current size
m_size += blksize();
m_inode_info.size += blksize();
// add link to linked inode
inode.m_nlink++;
inode.m_inode_info.nlink++;
return {};
}

View File

@@ -1,6 +1,5 @@
#include <BAN/ScopeGuard.h>
#include <kernel/FS/USTARModule.h>
#include <kernel/Memory/PageTable.h>
#include <kernel/Timer/Timer.h>
#include <LibDEFLATE/Decompressor.h>

View File

@@ -210,14 +210,14 @@ namespace Kernel
if (!file.inode->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
LockGuard _(m_mount_point_lock);
LockGuard _(m_mutex);
TRY(m_mount_points.emplace_back(file_system, BAN::move(file)));
return {};
}
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_host_inode(BAN::RefPtr<Inode> inode)
{
LockGuard _(m_mount_point_lock);
LockGuard _(m_mutex);
for (MountPoint& mount : m_mount_points)
if (*mount.host.inode == *inode)
return &mount;
@@ -226,52 +226,53 @@ namespace Kernel
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_root_inode(BAN::RefPtr<Inode> inode)
{
LockGuard _(m_mount_point_lock);
LockGuard _(m_mutex);
for (MountPoint& mount : m_mount_points)
if (*mount.target->root_inode() == *inode)
return &mount;
return nullptr;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage="
BAN::ErrorOr<VirtualFileSystem::File> VirtualFileSystem::file_from_relative_path(BAN::RefPtr<Inode> root_inode, const File& parent, const Credentials& credentials, BAN::StringView path, int flags)
{
LockGuard _(m_mutex);
auto inode = parent.inode;
ASSERT(inode);
char canonical_buf[PATH_MAX];
size_t canonical_len = parent.canonical_path.size();
if (!parent.canonical_path.empty() && parent.canonical_path.back() == '/')
canonical_len--;
memcpy(canonical_buf, parent.canonical_path.data(), canonical_len);
ASSERT(canonical_len == 0 || canonical_buf[canonical_len - 1] != '/');
BAN::String canonical_path;
TRY(canonical_path.append(parent.canonical_path));
if (!canonical_path.empty() && canonical_path.back() == '/')
canonical_path.pop_back();
ASSERT(canonical_path.empty() || canonical_path.back() != '/');
char path_storage[PATH_MAX];
BAN::Vector<BAN::String> path_parts;
const auto append_string_view_in_reverse =
[&path_parts](BAN::StringView path) -> BAN::ErrorOr<void>
{
auto split_path = TRY(path.split('/'));
TRY(path_parts.reserve(path_parts.size() + split_path.size()));
for (size_t i = split_path.size(); i > 0; i--)
{
TRY(path_parts.emplace_back());
TRY(path_parts.back().append(split_path[i - 1]));
}
return {};
};
TRY(append_string_view_in_reverse(path));
size_t link_depth = 0;
for (;;)
while (!path_parts.empty())
{
const auto path_part = [&path] {
size_t off = 0;
while (off < path.size() && path[off] == '/')
off++;
BAN::String path_part = BAN::move(path_parts.back());
path_parts.pop_back();
size_t len = 0;
while (off + len < path.size() && path[off + len] != '/')
len++;
const auto result = path.substring(off, len);
path = path.substring(off + len);
return result;
}();
if (path_part.empty())
break;
if (path_part == "."_sv)
if (path_part.empty() || path_part == "."_sv)
continue;
auto orig_inode = inode;
auto orig = inode;
// resolve file name
{
@@ -288,26 +289,26 @@ namespace Kernel
if (path_part == ".."_sv)
{
while (canonical_len && canonical_buf[canonical_len - 1] != '/')
canonical_len--;
if (canonical_len)
canonical_len--;
if (!canonical_path.empty())
{
ASSERT(canonical_path.front() == '/');
while (canonical_path.back() != '/')
canonical_path.pop_back();
canonical_path.pop_back();
}
}
else
{
if (auto* mount_point = mount_from_host_inode(inode))
inode = mount_point->target->root_inode();
if (canonical_len + 1 + path_part.size() > sizeof(canonical_buf))
return BAN::Error::from_errno(ENAMETOOLONG);
canonical_buf[canonical_len++] = '/';
memcpy(canonical_buf + canonical_len, path_part.data(), path_part.size());
canonical_len += path_part.size();
TRY(canonical_path.push_back('/'));
TRY(canonical_path.append(path_part));
}
}
if (!inode->mode().iflnk())
continue;
if ((flags & O_NOFOLLOW) && !path.find([](char ch) { return ch != '/'; }).has_value())
if ((flags & O_NOFOLLOW) && path_parts.empty())
continue;
// resolve symbolic links
@@ -319,26 +320,21 @@ namespace Kernel
if (link_target.front() == '/')
{
inode = root_inode;
canonical_len = 0;
canonical_path.clear();
}
else
{
inode = orig_inode;
inode = orig;
while (canonical_len && canonical_buf[canonical_len - 1] != '/')
canonical_len--;
if (canonical_len)
canonical_len--;
while (canonical_path.back() != '/')
canonical_path.pop_back();
canonical_path.pop_back();
}
if (link_target.size() + path.size() > sizeof(path_storage))
return BAN::Error::from_errno(ENAMETOOLONG);
memcpy(path_storage, link_target.data(), link_target.size());
memcpy(path_storage + link_target.size(), path.data(), path.size());
path = BAN::StringView(path_storage, link_target.size() + path.size());
TRY(append_string_view_in_reverse(link_target.sv()));
link_depth++;
if (link_depth > SYMLOOP_MAX)
if (link_depth > 100)
return BAN::Error::from_errno(ELOOP);
}
}
@@ -346,14 +342,17 @@ namespace Kernel
if (!inode->can_access(credentials, flags))
return BAN::Error::from_errno(EACCES);
if (canonical_len == 0)
canonical_buf[canonical_len++] = '/';
if (canonical_path.empty())
TRY(canonical_path.push_back('/'));
BAN::String canonical_path;
TRY(canonical_path.append(BAN::StringView(canonical_buf, canonical_len)));
File file;
file.inode = inode;
file.canonical_path = BAN::move(canonical_path);
return File(BAN::move(inode), BAN::move(canonical_path));
if (file.canonical_path.empty())
TRY(file.canonical_path.push_back('/'));
return file;
}
#pragma GCC diagnostic pop
}

View File

@@ -256,18 +256,6 @@ namespace Kernel
break;
}
case ISR::GeneralProtectionFault:
{
const uint8_t* ip = reinterpret_cast<const uint8_t*>(interrupt_stack->ip);
for (const auto& safe_user : s_safe_user_page_faults)
{
if (ip < safe_user.ip_start || ip >= safe_user.ip_end)
continue;
interrupt_stack->ip = reinterpret_cast<vaddr_t>(safe_user.ip_fault);
return;
}
break;
}
case ISR::DeviceNotAvailable:
{
if (pid == 0 || !Thread::current().is_userspace())

View File

@@ -1,10 +1,7 @@
#include <BAN/CircularQueue.h>
#include <kernel/Device/DeviceNumbers.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Input/InputDevice.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Terminal/TTY.h>
#include <LibInput/Joystick.h>
#include <LibInput/KeyEvent.h>
@@ -16,21 +13,14 @@
namespace Kernel
{
static SpinLock s_keyboard_lock;
static BAN::Vector<BAN::WeakPtr<InputDevice>> s_keyboards;
static BAN::RefPtr<KeyboardDevice> s_keyboard_device;
static SpinLock s_mouse_lock;
static BAN::Vector<BAN::WeakPtr<InputDevice>> s_mice;
static BAN::RefPtr<MouseDevice> s_mouse_device;
static SpinLock s_joystick_lock;
static BAN::Vector<BAN::WeakPtr<InputDevice>> s_joysticks;
static SpinLock s_tty_keyboard_event_lock;
static ThreadBlocker s_tty_keyboard_event_blocker;
static BAN::CircularQueue<LibInput::RawKeyEvent, 128> s_tty_keyboard_events;
static const char* get_name_format(InputDevice::Type type)
{
switch (type)
@@ -50,29 +40,20 @@ namespace Kernel
switch (type)
{
case InputDevice::Type::Keyboard:
{
SpinLockGuard _(s_keyboard_lock);
for (size_t i = 0; i < s_keyboards.size(); i++)
if (!s_keyboards[i].valid())
return makedev(DeviceNumber::Keyboard, i + 1);
return makedev(DeviceNumber::Keyboard, s_keyboards.size() + 1);
}
case InputDevice::Type::Mouse:
{
SpinLockGuard _(s_mouse_lock);
for (size_t i = 0; i < s_mice.size(); i++)
if (!s_mice[i].valid())
return makedev(DeviceNumber::Mouse, i + 1);
return makedev(DeviceNumber::Mouse, s_mice.size() + 1);
}
case InputDevice::Type::Joystick:
{
SpinLockGuard _(s_joystick_lock);
for (size_t i = 0; i < s_joysticks.size(); i++)
if (!s_joysticks[i].valid())
return makedev(DeviceNumber::Joystick, i + 1);
return makedev(DeviceNumber::Joystick, s_joysticks.size() + 1);
}
}
ASSERT_NOT_REACHED();
}
@@ -93,39 +74,30 @@ namespace Kernel
InputDevice::InputDevice(Type type)
: CharacterDevice(0440, 0, 901)
, m_rdev(get_rdev(type))
, m_name(MUST(BAN::String::formatted(get_name_format(type), minor(m_rdev) - 1)))
, m_type(type)
, m_event_size(get_event_size(type))
{
m_rdev = get_rdev(type);
m_name = MUST(BAN::String::formatted(get_name_format(type), minor(m_rdev) - 1));
MUST(m_event_buffer.resize(m_event_size * m_max_event_count, 0));
switch (m_type)
{
case Type::Keyboard:
{
SpinLockGuard _(s_keyboard_lock);
if (s_keyboards.size() < minor(m_rdev))
MUST(s_keyboards.resize(minor(m_rdev)));
s_keyboards[minor(m_rdev) - 1] = MUST(get_weak_ptr());
break;
}
case Type::Mouse:
{
SpinLockGuard _(s_mouse_lock);
if (s_mice.size() < minor(m_rdev))
MUST(s_mice.resize(minor(m_rdev)));
s_mice[minor(m_rdev) - 1] = MUST(get_weak_ptr());
break;
}
case Type::Joystick:
{
SpinLockGuard _(s_joystick_lock);
if (s_joysticks.size() < minor(m_rdev))
MUST(s_joysticks.resize(minor(m_rdev)));
s_joysticks[minor(m_rdev) - 1] = MUST(get_weak_ptr());
break;
}
}
}
@@ -197,6 +169,9 @@ namespace Kernel
}
else switch (key_event.keycode)
{
case LibInput::keycode_function(1):
Processor::toggle_should_print_cpu_load();
break;
case LibInput::keycode_function(11):
DevFileSystem::get().initiate_disk_cache_drop();
break;
@@ -205,15 +180,6 @@ namespace Kernel
break;
}
}
if (TTY::current()->should_receive_input())
{
SpinLockGuard _(s_tty_keyboard_event_lock);
if (!s_tty_keyboard_events.full())
s_tty_keyboard_events.push(key_event);
s_tty_keyboard_event_blocker.unblock();
return;
}
}
if (m_event_count == m_max_event_count)
@@ -274,41 +240,6 @@ namespace Kernel
}
static void tty_keyboard_thread(void*)
{
static BAN::Atomic<bool> initialized = false;
[[maybe_unused]] bool old_initialized = initialized.exchange(true);
ASSERT(old_initialized == false);
for (;;)
{
LibInput::RawKeyEvent event;
{
SpinLockGuard guard(s_tty_keyboard_event_lock);
if (s_tty_keyboard_events.empty())
{
SpinLockGuardAsMutex smutex(guard);
s_tty_keyboard_event_blocker.block_indefinite(&smutex);
continue;
}
event = s_tty_keyboard_events.front();
s_tty_keyboard_events.pop();
}
TTY::current()->on_key_event(event);
}
}
BAN::ErrorOr<void> KeyboardDevice::initialize_tty_thread()
{
auto* thread = TRY(Thread::create_kernel(tty_keyboard_thread, nullptr));
ASSERT(thread);
TRY(Processor::scheduler().add_thread(thread));
return {};
}
BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> KeyboardDevice::create(mode_t mode, uid_t uid, gid_t gid)
{
@@ -318,16 +249,13 @@ namespace Kernel
KeyboardDevice::KeyboardDevice(mode_t mode, uid_t uid, gid_t gid)
: CharacterDevice(mode, uid, gid)
, m_rdev(makedev(DeviceNumber::Keyboard, 0))
, m_name("keyboard"_sv)
{
m_rdev = makedev(DeviceNumber::Keyboard, 0);
}
{}
void KeyboardDevice::notify()
{
epoll_notify(EPOLLIN);
SpinLockGuard _(s_keyboard_lock);
m_thread_blocker.unblock();
}
@@ -336,7 +264,6 @@ namespace Kernel
if (buffer.size() < sizeof(LibInput::RawKeyEvent))
return BAN::Error::from_errno(ENOBUFS);
SpinLockGuard keyboard_guard(s_keyboard_lock);
for (;;)
{
for (auto& weak_keyboard : s_keyboards)
@@ -350,14 +277,13 @@ namespace Kernel
return bytes;
}
SpinLockGuardAsMutex smutex(keyboard_guard);
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &smutex));
// FIXME: race condition as notify doesn't lock mutex
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
}
}
bool KeyboardDevice::can_read_impl() const
{
SpinLockGuard _(s_keyboard_lock);
for (auto& weak_keyboard : s_keyboards)
if (auto keyboard = weak_keyboard.lock())
if (keyboard->can_read())
@@ -375,16 +301,13 @@ namespace Kernel
MouseDevice::MouseDevice(mode_t mode, uid_t uid, gid_t gid)
: CharacterDevice(mode, uid, gid)
, m_rdev(makedev(DeviceNumber::Mouse, 0))
, m_name("mouse"_sv)
{
m_rdev = makedev(DeviceNumber::Mouse, 0);
}
{}
void MouseDevice::notify()
{
epoll_notify(EPOLLIN);
SpinLockGuard _(s_mouse_lock);
m_thread_blocker.unblock();
}
@@ -393,7 +316,6 @@ namespace Kernel
if (buffer.size() < sizeof(LibInput::MouseEvent))
return BAN::Error::from_errno(ENOBUFS);
SpinLockGuard mouse_guard(s_mouse_lock);
for (;;)
{
for (auto& weak_mouse : s_mice)
@@ -407,14 +329,13 @@ namespace Kernel
return bytes;
}
SpinLockGuardAsMutex smutex(mouse_guard);
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &smutex));
// FIXME: race condition as notify doesn't lock mutex
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
}
}
bool MouseDevice::can_read_impl() const
{
SpinLockGuard _(s_mouse_lock);
for (auto& weak_mouse : s_mice)
if (auto mouse = weak_mouse.lock())
if (mouse->can_read())

View File

@@ -25,12 +25,10 @@ namespace Kernel
const paddr_t paddr = Heap::get().take_free_page();
if (paddr == 0)
return BAN::Error::from_errno(ENOMEM);
PageTable::kernel().map_page_at(paddr, buffer->m_vaddr + i * PAGE_SIZE, PageTable::ReadWrite | PageTable::Present, PageTable::MemoryType::Normal, false);
PageTable::kernel().map_page_at(paddr, buffer->m_vaddr + size + i * PAGE_SIZE, PageTable::ReadWrite | PageTable::Present, PageTable::MemoryType::Normal, false);
PageTable::kernel().map_page_at(paddr, buffer->m_vaddr + i * PAGE_SIZE, PageTable::ReadWrite | PageTable::Present);
PageTable::kernel().map_page_at(paddr, buffer->m_vaddr + size + i * PAGE_SIZE, PageTable::ReadWrite | PageTable::Present);
}
PageTable::kernel().invalidate_range(buffer->m_vaddr, page_count * 2, true);
return buffer;
}
@@ -38,11 +36,13 @@ namespace Kernel
{
if (m_vaddr == 0)
return;
for (size_t i = 0; i < m_capacity / PAGE_SIZE; i++)
if (const paddr_t paddr = PageTable::kernel().physical_address_of(m_vaddr + i * PAGE_SIZE))
Heap::get().release_page(paddr);
{
const paddr_t paddr = PageTable::kernel().physical_address_of(m_vaddr + i * PAGE_SIZE);
if (paddr == 0)
break;
Heap::get().release_page(paddr);
}
PageTable::kernel().unmap_range(m_vaddr, m_capacity * 2);
}

View File

@@ -2,12 +2,8 @@
#include <kernel/Memory/FileBackedRegion.h>
#include <kernel/Memory/Heap.h>
#include <BAN/ScopeGuard.h>
#include <sys/mman.h>
#pragma GCC diagnostic ignored "-Wstack-usage="
namespace Kernel
{
@@ -35,13 +31,11 @@ namespace Kernel
if (type == Type::PRIVATE)
TRY(region->m_dirty_pages.resize(BAN::Math::div_round_up<size_t>(size, PAGE_SIZE)));
SpinLockGuard _(inode->m_shared_region_lock);
LockGuard _(inode->m_mutex);
if (!(region->m_shared_data = inode->m_shared_region.lock()))
{
auto shared_data = TRY(BAN::RefPtr<SharedFileData>::create());
const size_t page_count = BAN::Math::div_round_up<size_t>(inode->size(), PAGE_SIZE);
TRY(shared_data->pages.resize(page_count, 0));
TRY(shared_data->writers.resize(page_count, 0));
TRY(shared_data->pages.resize(BAN::Math::div_round_up<size_t>(inode->size(), PAGE_SIZE)));
shared_data->inode = inode;
inode->m_shared_region = TRY(shared_data->get_weak_ptr());
region->m_shared_data = BAN::move(shared_data);
@@ -61,44 +55,37 @@ namespace Kernel
{
if (m_vaddr == 0)
return;
switch (m_type)
{
case Type::PRIVATE:
for (paddr_t dirty_page : m_dirty_pages)
if (dirty_page)
Heap::get().release_page(dirty_page);
break;
case Type::SHARED:
const size_t page_count = BAN::Math::div_round_up<size_t>(size(), PAGE_SIZE);
for (size_t i = 0; i < page_count; i++)
if (m_page_table.get_page_flags(m_vaddr + i * PAGE_SIZE) & PageTable::Flags::ReadWrite)
BAN::atomic_sub_fetch(m_shared_data->writers[m_offset / PAGE_SIZE + i], 1);
break;
}
for (paddr_t dirty_page : m_dirty_pages)
if (dirty_page)
Heap::get().release_page(dirty_page);
}
SharedFileData::~SharedFileData()
{
// TODO: validate that this is not locked
// no-one should be referencing this anymore
[[maybe_unused]] bool success = mutex.try_lock();
ASSERT(success);
for (size_t i = 0; i < pages.size(); i++)
{
if (pages[i] == 0)
continue;
sync_no_lock(i);
sync(i);
Heap::get().release_page(pages[i]);
}
mutex.unlock();
}
void SharedFileData::sync_no_lock(size_t page_index)
void SharedFileData::sync(size_t page_index)
{
if (pages[page_index] == 0 || BAN::atomic_load(writers[page_index]) > 0)
ASSERT(mutex.is_locked());
if (pages[page_index] == 0)
return;
uint8_t page_buffer[PAGE_SIZE];
PageTable::with_per_cpu_fast_page(pages[page_index], [&](void* addr) {
memcpy(page_buffer, addr, PAGE_SIZE);
PageTable::with_fast_page(pages[page_index], [&] {
memcpy(page_buffer, PageTable::fast_page_as_ptr(), PAGE_SIZE);
});
const size_t write_size = BAN::Math::min<size_t>(PAGE_SIZE, inode->size() - page_index * PAGE_SIZE);
@@ -108,18 +95,18 @@ namespace Kernel
BAN::ErrorOr<void> FileBackedRegion::msync(vaddr_t address, size_t size, int flags)
{
// TODO: maybe do something with the flags :)
(void)flags;
if (flags != MS_SYNC)
dprintln("async file backed mmap msync");
if (m_type != Type::SHARED)
return {};
const vaddr_t first_page = BAN::Math::max(m_vaddr, address) & PAGE_ADDR_MASK;
const vaddr_t last_page = BAN::Math::div_round_up(BAN::Math::min(m_vaddr + m_size, address + size), PAGE_SIZE) * PAGE_SIZE;
const vaddr_t first_page = address & PAGE_ADDR_MASK;
const vaddr_t last_page = BAN::Math::div_round_up<vaddr_t>(address + size, PAGE_SIZE) * PAGE_SIZE;
RWLockRDGuard _(m_shared_data->rw_lock);
LockGuard _(m_shared_data->mutex);
for (vaddr_t page_addr = first_page; page_addr < last_page; page_addr += PAGE_SIZE)
m_shared_data->sync_no_lock((m_offset + page_addr - m_vaddr) / PAGE_SIZE);
if (contains(page_addr))
m_shared_data->sync((page_addr - m_vaddr) / PAGE_SIZE);
return {};
}
@@ -138,43 +125,29 @@ namespace Kernel
if (m_page_table.physical_address_of(vaddr) == 0)
{
ASSERT(m_shared_data);
uint8_t page_buffer[PAGE_SIZE];
m_shared_data->rw_lock.rd_lock();
LockGuard _(m_shared_data->mutex);
bool shared_data_has_correct_page = false;
if (m_shared_data->pages[shared_page_index] == 0)
{
m_shared_data->rw_lock.rd_unlock();
m_shared_data->pages[shared_page_index] = Heap::get().take_free_page();
if (m_shared_data->pages[shared_page_index] == 0)
return BAN::Error::from_errno(ENOMEM);
const size_t offset = (vaddr - m_vaddr) + m_offset;
ASSERT(offset % PAGE_SIZE == 0);
ASSERT(offset % 4096 == 0);
const size_t bytes = BAN::Math::min<size_t>(m_inode->size() - offset, PAGE_SIZE);
TRY(m_inode->read(offset, BAN::ByteSpan(page_buffer, bytes)));
memset(page_buffer + bytes, 0, PAGE_SIZE - bytes);
memset(m_shared_data->page_buffer, 0x00, PAGE_SIZE);
TRY(m_inode->read(offset, BAN::ByteSpan(m_shared_data->page_buffer, bytes)));
shared_data_has_correct_page = true;
{
RWLockWRGuard _(m_shared_data->rw_lock);
if (m_shared_data->pages[shared_page_index] == 0)
{
m_shared_data->pages[shared_page_index] = Heap::get().take_free_page();
if (m_shared_data->pages[shared_page_index] == 0)
return BAN::Error::from_errno(ENOMEM);
PageTable::with_per_cpu_fast_page(m_shared_data->pages[shared_page_index], [&](void* addr) {
memcpy(addr, page_buffer, PAGE_SIZE);
});
shared_data_has_correct_page = true;
}
}
m_shared_data->rw_lock.rd_lock();
PageTable::with_fast_page(m_shared_data->pages[shared_page_index], [&] {
memcpy(PageTable::fast_page_as_ptr(), m_shared_data->page_buffer, PAGE_SIZE);
});
}
BAN::ScopeGuard _([this] { m_shared_data->rw_lock.rd_unlock(); });
if (m_type == Type::PRIVATE && wants_write)
{
const paddr_t paddr = Heap::get().take_free_page();
@@ -182,39 +155,50 @@ namespace Kernel
return BAN::Error::from_errno(ENOMEM);
if (!shared_data_has_correct_page)
{
PageTable::with_per_cpu_fast_page(m_shared_data->pages[shared_page_index], [&](void* addr) {
memcpy(page_buffer, addr, PAGE_SIZE);
PageTable::with_fast_page(m_shared_data->pages[shared_page_index], [&] {
memcpy(m_shared_data->page_buffer, PageTable::fast_page_as_ptr(), PAGE_SIZE);
});
}
PageTable::with_per_cpu_fast_page(paddr, [&](void* addr) {
memcpy(addr, page_buffer, PAGE_SIZE);
PageTable::with_fast_page(paddr, [&] {
memcpy(PageTable::fast_page_as_ptr(), m_shared_data->page_buffer, PAGE_SIZE);
});
m_dirty_pages[local_page_index] = paddr;
m_page_table.map_page_at(paddr, vaddr, m_flags);
}
else
{
const paddr_t paddr = m_shared_data->pages[shared_page_index];
auto flags = m_flags;
if (m_type == Type::PRIVATE)
flags &= ~PageTable::Flags::ReadWrite;
if (flags & PageTable::Flags::ReadWrite)
BAN::atomic_add_fetch(m_shared_data->writers[shared_page_index], 1);
m_page_table.map_page_at(m_shared_data->pages[shared_page_index], vaddr, flags);
m_page_table.map_page_at(paddr, vaddr, flags);
}
}
else
{
ASSERT(m_type == Type::PRIVATE && wants_write);
// page does not need remappings
if (m_type != Type::PRIVATE || !wants_write)
return false;
ASSERT(writable());
// page is already mapped as writable
if (m_page_table.get_page_flags(vaddr) & PageTable::Flags::ReadWrite)
return false;
const paddr_t paddr = Heap::get().take_free_page();
if (paddr == 0)
return BAN::Error::from_errno(ENOMEM);
ASSERT(&m_page_table == &PageTable::current());
PageTable::with_per_cpu_fast_page(paddr, [vaddr](void* addr) {
memcpy(addr, reinterpret_cast<void*>(vaddr), PAGE_SIZE);
});
ASSERT(m_shared_data);
LockGuard _(m_shared_data->mutex);
ASSERT(m_shared_data->pages[shared_page_index]);
PageTable::with_fast_page(m_shared_data->pages[shared_page_index], [&] {
memcpy(m_shared_data->page_buffer, PageTable::fast_page_as_ptr(), PAGE_SIZE);
});
PageTable::with_fast_page(paddr, [&] {
memcpy(PageTable::fast_page_as_ptr(), m_shared_data->page_buffer, PAGE_SIZE);
});
m_dirty_pages[local_page_index] = paddr;
m_page_table.map_page_at(paddr, vaddr, m_flags);
}
@@ -241,8 +225,8 @@ namespace Kernel
return BAN::Error::from_errno(ENOMEM);
ASSERT(&m_page_table == &PageTable::current() || &m_page_table == &PageTable::kernel());
PageTable::with_per_cpu_fast_page(paddr, [&](void* addr) {
memcpy(addr, reinterpret_cast<void*>(vaddr), PAGE_SIZE);
PageTable::with_fast_page(paddr, [&] {
memcpy(PageTable::fast_page_as_ptr(), reinterpret_cast<void*>(vaddr), PAGE_SIZE);
});
result->m_page_table.map_page_at(paddr, vaddr, m_flags);

View File

@@ -64,8 +64,8 @@ namespace Kernel
return BAN::Error::from_errno(ENOMEM);
m_page_table.map_page_at(paddr, vaddr, m_flags);
PageTable::with_per_cpu_fast_page(paddr, [](void* addr) {
memset(addr, 0x00, PAGE_SIZE);
PageTable::with_fast_page(paddr, [] {
memset(PageTable::fast_page_as_ptr(), 0x00, PAGE_SIZE);
});
return true;
@@ -93,8 +93,8 @@ namespace Kernel
m_page_table.map_page_at(paddr, vaddr, m_flags);
ASSERT(&m_page_table == &PageTable::current());
PageTable::with_per_cpu_fast_page(physical_page->paddr, [vaddr](void* addr) {
memcpy(reinterpret_cast<void*>(vaddr), addr, PAGE_SIZE);
PageTable::with_fast_page(physical_page->paddr, [vaddr] {
memcpy(reinterpret_cast<void*>(vaddr), PageTable::fast_page_as_ptr(), PAGE_SIZE);
});
if (--physical_page->ref_count == 0)
@@ -164,9 +164,9 @@ namespace Kernel
size_t written = 0;
while (written < buffer_size)
{
const vaddr_t write_vaddr = m_vaddr + offset_into_region + written;
const vaddr_t page_offset = write_vaddr % PAGE_SIZE;
const size_t bytes = BAN::Math::min<size_t>(buffer_size - written, PAGE_SIZE - page_offset);
vaddr_t write_vaddr = m_vaddr + offset_into_region + written;
vaddr_t page_offset = write_vaddr % PAGE_SIZE;
size_t bytes = BAN::Math::min<size_t>(buffer_size - written, PAGE_SIZE - page_offset);
if (!(m_page_table.get_page_flags(write_vaddr & PAGE_ADDR_MASK) & PageTable::ReadWrite))
{
@@ -180,8 +180,8 @@ namespace Kernel
const paddr_t paddr = m_page_table.physical_address_of(write_vaddr & PAGE_ADDR_MASK);
ASSERT(paddr);
PageTable::with_per_cpu_fast_page(paddr, [&](void* addr) {
memcpy(static_cast<uint8_t*>(addr) + page_offset, (void*)(buffer + written), bytes);
PageTable::with_fast_page(paddr, [&] {
memcpy(PageTable::fast_page_as_ptr(page_offset), (void*)(buffer + written), bytes);
});
written += bytes;

View File

@@ -21,8 +21,8 @@ namespace Kernel
const size_t bitmap_page_count = BAN::Math::div_round_up<size_t>(m_page_count, bits_per_page);
for (size_t i = 0; i < bitmap_page_count; i++)
{
PageTable::with_per_cpu_fast_page(paddr + i * PAGE_SIZE, [](void* addr) {
memset(addr, 0, PAGE_SIZE);
PageTable::with_fast_page(paddr + i * PAGE_SIZE, [] {
memset(PageTable::fast_page_as_ptr(), 0, PAGE_SIZE);
});
}
@@ -40,15 +40,15 @@ namespace Kernel
BAN::Optional<size_t> page_matched_bit;
const paddr_t current_paddr = m_paddr + i * PAGE_SIZE;
PageTable::with_per_cpu_fast_page(current_paddr, [&page_matched_bit](void* addr) {
PageTable::with_fast_page(current_paddr, [&page_matched_bit] {
for (size_t j = 0; j < PAGE_SIZE / sizeof(size_t); j++)
{
static_assert(sizeof(size_t) == sizeof(long));
auto& current = static_cast<size_t*>(addr)[j];
const size_t current = PageTable::fast_page_as_sized<volatile size_t>(j);
if (current == BAN::numeric_limits<size_t>::max())
continue;
const int ctz = __builtin_ctzl(~current);
current |= static_cast<size_t>(1) << ctz;
PageTable::fast_page_as_sized<volatile size_t>(j) = current | (static_cast<size_t>(1) << ctz);
page_matched_bit = j * sizeof(size_t) * 8 + ctz;
return;
}
@@ -75,14 +75,15 @@ namespace Kernel
const size_t paddr_index = (paddr - m_paddr) / PAGE_SIZE;
PageTable::with_per_cpu_fast_page(m_paddr + paddr_index / bits_per_page * PAGE_SIZE, [paddr_index] (void* addr) {
PageTable::with_fast_page(m_paddr + paddr_index / bits_per_page * PAGE_SIZE, [paddr_index] {
const size_t bitmap_bit = paddr_index % bits_per_page;
const size_t byte = bitmap_bit / 8;
const size_t bit = bitmap_bit % 8;
uint8_t& bitmap_byte = static_cast<uint8_t*>(addr)[byte];
volatile uint8_t& bitmap_byte = PageTable::fast_page_as_sized<volatile uint8_t>(byte);
ASSERT(bitmap_byte & (1u << bit));
bitmap_byte &= ~(1u << bit);
bitmap_byte = bitmap_byte & ~(1u << bit);
});
m_free_pages++;
@@ -102,8 +103,8 @@ namespace Kernel
const size_t bit = bit_index % 8;
uint8_t current;
PageTable::with_per_cpu_fast_page(m_paddr + page_index * PAGE_SIZE, [&current, byte](void* addr) {
current = static_cast<uint8_t*>(addr)[byte];
PageTable::with_fast_page(m_paddr + page_index * PAGE_SIZE, [&current, byte] {
current = PageTable::fast_page_as_sized<volatile uint8_t>(byte);
});
return current & (1u << bit);
@@ -116,9 +117,9 @@ namespace Kernel
const size_t bit_index = buffer_bit % bits_per_page;
const size_t byte = bit_index / 8;
const size_t bit = bit_index % 8;
PageTable::with_per_cpu_fast_page(m_paddr + page_index * PAGE_SIZE, [byte, bit](void* addr) {
uint8_t& current = static_cast<uint8_t*>(addr)[byte];
current |= 1u << bit;
PageTable::with_fast_page(m_paddr + page_index * PAGE_SIZE, [byte, bit] {
volatile uint8_t& current = PageTable::fast_page_as_sized<volatile uint8_t>(byte);
current = current | (1u << bit);
});
};

View File

@@ -68,9 +68,8 @@ namespace Kernel
PageTable::with_fast_page(paddr, [] {
memset(PageTable::fast_page_as_ptr(), 0, PAGE_SIZE);
});
m_page_table.map_page_at(paddr, vaddr() + i * PAGE_SIZE, m_flags, PageTable::MemoryType::Normal, false);
m_page_table.map_page_at(paddr, vaddr() + i * PAGE_SIZE, m_flags);
}
m_page_table.invalidate_range(m_vaddr, page_count, true);
return {};
}

View File

@@ -1,292 +1,438 @@
#include <kernel/Memory/Heap.h>
#include <BAN/Errors.h>
#include <kernel/BootInfo.h>
#include <kernel/Memory/kmalloc.h>
#include <kernel/Memory/PageTable.h>
static constexpr size_t s_allocator_chunk_size { 64 };
static constexpr size_t s_allocator_align { alignof(max_align_t) };
#define MB (1 << 20)
static constexpr size_t s_max_allocator_count { 128 };
extern uint8_t g_kernel_end[];
static constexpr size_t s_allocator_default_size { 128 * 1024 };
static constexpr size_t s_allocator_dynamic_size { 16 * 1024 * 1024 };
static constexpr size_t s_kmalloc_min_align = alignof(max_align_t);
alignas(s_allocator_align) static uint8_t s_default_allocator_memory[s_allocator_default_size] {};
static constexpr size_t s_kmalloc_size = 48 * MB;
static constexpr size_t s_kmalloc_fixed_size = 16 * MB;
static uint8_t s_kmalloc_storage[s_kmalloc_size + s_kmalloc_fixed_size];
// NOTE: 128 KiB + 127 * 16 MiB ~= 2 GiB
// This is should be more than enough for kmalloc :^)
struct BitmapAllocator
struct kmalloc_node
{
struct Header
{
size_t chunks { 0 };
uint8_t padding[s_allocator_align - sizeof(chunks)];
};
void set_align(ptrdiff_t align) { m_align = align; }
void set_end(uintptr_t end) { m_size = end - (uintptr_t)m_data; }
void set_used(bool used) { m_used = used; }
uint32_t bitmap_chunks { 0 };
uint32_t total_chunks { 0 };
uint32_t free_chunks { 0 };
uint32_t allocations { 0 };
uint8_t* base { nullptr };
bool can_align(uint32_t align) { return align < m_size; }
bool can_fit_before() { return m_align > sizeof(kmalloc_node); }
bool can_fit_after(size_t new_size) { return data() + new_size < end() - sizeof(kmalloc_node); }
static size_t needed_chunks(size_t size)
void split_in_align()
{
return BAN::Math::div_round_up(sizeof(BitmapAllocator::Header) + size, s_allocator_chunk_size);
uintptr_t node_end = end();
set_end(data() - sizeof(kmalloc_node));
set_align(0);
auto* next = after();
next->set_end(node_end);
next->set_align(0);
}
void initialize_default()
void split_after_size(size_t size)
{
constexpr size_t bitmap_bytes = BAN::Math::div_round_up(s_allocator_default_size, s_allocator_chunk_size * 8);
constexpr size_t bitmap_chunks = BAN::Math::div_round_up(bitmap_bytes, s_allocator_chunk_size);
constexpr size_t usable_chunks = s_allocator_default_size / s_allocator_chunk_size - bitmap_chunks;
uintptr_t node_end = end();
set_end(data() + size);
this->bitmap_chunks = bitmap_chunks;
this->total_chunks = usable_chunks;
this->free_chunks = usable_chunks;
this->base = s_default_allocator_memory;
memset(this->base, 0, bitmap_chunks * s_allocator_chunk_size);
auto* next = after();
next->set_end(node_end);
next->set_align(0);
}
bool initialize_dynamic()
bool used() { return m_used; }
uintptr_t size_no_align() { return m_size; }
uintptr_t size() { return size_no_align() - m_align; }
uintptr_t data_no_align() { return (uintptr_t)m_data; }
uintptr_t data() { return data_no_align() + m_align; }
uintptr_t end() { return data_no_align() + m_size; }
kmalloc_node* after() { return (kmalloc_node*)end(); }
private:
uint32_t m_size;
uint32_t m_align;
bool m_used;
uint8_t m_padding[s_kmalloc_min_align - sizeof(m_size) - sizeof(m_align) - sizeof(m_used)];
uint8_t m_data[0];
};
static_assert(sizeof(kmalloc_node) == s_kmalloc_min_align);
struct kmalloc_info
{
const uintptr_t base = (uintptr_t)s_kmalloc_storage;
const size_t size = s_kmalloc_size;
const uintptr_t end = base + size;
kmalloc_node* first() { return (kmalloc_node*)base; }
kmalloc_node* from_address(void* addr)
{
using namespace Kernel;
const size_t page_count = s_allocator_dynamic_size / PAGE_SIZE;
const vaddr_t vaddr = PageTable::kernel().reserve_free_contiguous_pages(page_count, KERNEL_OFFSET);
if (vaddr == 0)
return false;
for (size_t i = 0; i < page_count; i++)
{
const paddr_t paddr = Heap::get().take_free_page();
if (paddr == 0)
{
for (size_t j = 0; j < i; j++)
Heap::get().release_page(PageTable::kernel().physical_address_of(vaddr + j * PAGE_SIZE));
PageTable::kernel().unmap_range(vaddr, page_count * PAGE_SIZE);
return false;
}
PageTable::kernel().map_page_at(paddr, vaddr + i * PAGE_SIZE, PageTable::ReadWrite | PageTable::Present);
}
constexpr size_t bitmap_bytes = BAN::Math::div_round_up(s_allocator_dynamic_size, s_allocator_chunk_size * 8);
constexpr size_t bitmap_chunks = BAN::Math::div_round_up(bitmap_bytes, s_allocator_chunk_size);
constexpr size_t usable_chunks = s_allocator_dynamic_size / s_allocator_chunk_size - bitmap_chunks;
this->bitmap_chunks = bitmap_chunks;
this->total_chunks = usable_chunks;
this->free_chunks = usable_chunks;
this->base = reinterpret_cast<uint8_t*>(vaddr);
memset(this->base, 0, bitmap_chunks * s_allocator_chunk_size);
return true;
}
uint8_t* data_start() { return base + bitmap_chunks * s_allocator_chunk_size; }
const uint8_t* data_start() const { return base + bitmap_chunks * s_allocator_chunk_size; }
size_t get_first_chunk(void* ptr) const
{
return (static_cast<uint8_t*>(ptr) - sizeof(Header) - data_start()) / s_allocator_chunk_size;
}
bool contains(void* ptr) const
{
if (ptr < data_start() + sizeof(Header))
return false;
return get_first_chunk(ptr) < total_chunks;
}
bool get_bit(size_t index) const
{
ASSERT(index < total_chunks);
const size_t byte = index / 8;
const size_t bit = index % 8;
return (base[byte] >> bit) & 1;
}
void set_bit(size_t index, bool value)
{
ASSERT(index < total_chunks);
const size_t byte = index / 8;
const size_t bit = index % 8;
if (value)
base[byte] |= 1 << bit;
else
base[byte] &= ~(1 << bit);
}
size_t find_unset_bit(size_t index) const
{
// NOTE: We could optimize other bitmap functions than this
// but this one is the bottle neck so it doesn't matter
static_assert(sizeof(unsigned long long) == sizeof(uint64_t));
if (index >= total_chunks)
return index;
if (const auto rem = index % 64)
{
const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + (index - rem) / 8) >> rem;
if (qword != (1ull << (64 - rem)) - 1)
return index + __builtin_ctzll(~qword);
index += 64 - rem;
}
while (index < total_chunks)
{
const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + index / 8);
if (qword != UINT64_MAX)
return index + __builtin_ctzll(~qword);
index += 64;
}
return index;
}
size_t count_unset_bits(size_t index, size_t wanted) const
{
size_t count = 0;
for (; index + count < total_chunks && count < wanted; count++)
if (get_bit(index + count))
break;
return count;
}
Header& header_from_chunk(size_t index)
{
return *reinterpret_cast<Header*>(data_start() + index * s_allocator_chunk_size);
}
Header& header_from_ptr(void* ptr)
{
return *reinterpret_cast<Header*>(static_cast<uint8_t*>(ptr) - sizeof(Header));
}
void* allocate(size_t needed_chunks)
{
ASSERT(needed_chunks > 0);
if (needed_chunks > free_chunks)
return nullptr;
for (size_t i = find_unset_bit(0); i <= total_chunks - needed_chunks; i = find_unset_bit(i))
{
if (const size_t count = count_unset_bits(i, needed_chunks); count < needed_chunks)
{
i += count + 1;
continue;
}
for (size_t j = 0; j < needed_chunks; j++)
set_bit(i + j, true);
auto& header = header_from_chunk(i);
header.chunks = needed_chunks;
free_chunks -= header.chunks;
allocations++;
return &header + 1;
}
for (auto* node = first(); node->end() < end; node = node->after())
if (node->data() == (uintptr_t)addr)
return node;
return nullptr;
}
void free(void* ptr)
bool contains(uintptr_t addr) const
{
ASSERT(contains(ptr));
const size_t first_chunk = get_first_chunk(ptr);
auto& header = header_from_ptr(ptr);
for (size_t i = 0; i < header.chunks; i++)
set_bit(first_chunk + i, false);
free_chunks += header.chunks;
allocations--;
return base <= addr && addr < end;
}
};
static uint8_t s_allocator_storage[s_max_allocator_count * sizeof(BitmapAllocator)];
static BitmapAllocator* s_allocators[s_max_allocator_count] {};
size_t used = 0;
size_t free = size;
};
static kmalloc_info s_kmalloc_info;
static Kernel::SpinLock s_kmalloc_lock;
template<size_t SIZE>
struct kmalloc_fixed_node
{
uint8_t data[SIZE - 2 * sizeof(uint16_t)];
uint16_t prev = NULL;
uint16_t next = NULL;
static constexpr uint16_t invalid = ~0;
};
struct kmalloc_fixed_info
{
using node = kmalloc_fixed_node<64>;
const uintptr_t base = s_kmalloc_info.end;
const size_t size = s_kmalloc_fixed_size;
const uintptr_t end = base + size;
const size_t node_count = size / sizeof(node);
node* free_list_head = NULL;
node* used_list_head = NULL;
node* node_at(size_t index) { return (node*)(base + index * sizeof(node)); }
uint16_t index_of(const node* p) { return ((uintptr_t)p - base) / sizeof(node); }
size_t used = 0;
size_t free = size;
};
static kmalloc_fixed_info s_kmalloc_fixed_info;
void kmalloc_initialize()
{
auto& allocator = reinterpret_cast<BitmapAllocator*>(s_allocator_storage)[0];
new (&allocator) BitmapAllocator();
allocator.initialize_default();
s_allocators[0] = &allocator;
}
dprintln("kmalloc {8H}->{8H}", (uintptr_t)s_kmalloc_storage, (uintptr_t)s_kmalloc_storage + sizeof(s_kmalloc_storage));
static void kmalloc_dump_info()
{
ASSERT(s_kmalloc_lock.current_processor_has_lock());
dwarnln("kmalloc info");
for (size_t i = 0; i < s_max_allocator_count && s_allocators[i]; i++)
// initialize fixed size allocations
{
dwarnln(" allocator {}", i);
dwarnln(" total size: {}", s_allocators[i]->total_chunks * s_allocator_chunk_size);
dwarnln(" free size: {}", s_allocators[i]->free_chunks * s_allocator_chunk_size);
dwarnln(" allocations: {}", s_allocators[i]->allocations);
auto& info = s_kmalloc_fixed_info;
for (size_t i = 0; i < info.node_count; i++)
{
auto* node = info.node_at(i);
node->next = i - 1;
node->prev = i + 1;
}
info.node_at(0)->next = kmalloc_fixed_info::node::invalid;
info.node_at(info.node_count - 1)->prev = kmalloc_fixed_info::node::invalid;
info.free_list_head = info.node_at(0);
info.used_list_head = nullptr;
}
// initial general allocations
{
auto& info = s_kmalloc_info;
auto* node = info.first();
node->set_end(info.end);
node->set_align(0);
node->set_used(false);
}
}
void* kmalloc(size_t size)
void kmalloc_dump_info()
{
const size_t needed_chunks = BitmapAllocator::needed_chunks(size);
Kernel::SpinLockGuard _(s_kmalloc_lock);
dprintln("kmalloc: 0x{8H}->0x{8H}", s_kmalloc_info.base, s_kmalloc_info.end);
dprintln(" used: 0x{8H}", s_kmalloc_info.used);
dprintln(" free: 0x{8H}", s_kmalloc_info.free);
dprintln("kmalloc fixed {} byte: 0x{8H}->0x{8H}", sizeof(kmalloc_fixed_info::node), s_kmalloc_fixed_info.base, s_kmalloc_fixed_info.end);
dprintln(" used: 0x{8H}", s_kmalloc_fixed_info.used);
dprintln(" free: 0x{8H}", s_kmalloc_fixed_info.free);
}
static bool is_corrupted()
{
Kernel::SpinLockGuard _(s_kmalloc_lock);
auto& info = s_kmalloc_info;
auto* temp = info.first();
while (reinterpret_cast<uintptr_t>(temp) != info.end)
{
if (!info.contains(reinterpret_cast<uintptr_t>(temp)))
return true;
if (!info.contains(temp->end() - 1))
return true;
if (temp->after() <= temp)
return true;
temp = temp->after();
}
return false;
}
[[maybe_unused]] static void debug_dump()
{
Kernel::SpinLockGuard _(s_kmalloc_lock);
auto& info = s_kmalloc_info;
uint32_t used = 0;
uint32_t free = 0;
for (auto* node = info.first(); node->data() <= info.end; node = node->after())
{
(node->used() ? used : free) += sizeof(kmalloc_node) + node->size_no_align();
dprintln("{} node {H} -> {H}", node->used() ? "used" : "free", node->data(), node->end());
}
dprintln("total used: {}", used);
dprintln("total free: {}", free);
dprintln(" {}", used + free);
}
static void* kmalloc_fixed()
{
Kernel::SpinLockGuard _(s_kmalloc_lock);
auto& info = s_kmalloc_fixed_info;
if (!info.free_list_head)
return nullptr;
// allocate the node on top of free list
auto* node = info.free_list_head;
ASSERT(node->next == kmalloc_fixed_info::node::invalid);
// remove the node from free list
if (info.free_list_head->prev != kmalloc_fixed_info::node::invalid)
{
info.free_list_head = info.node_at(info.free_list_head->prev);
info.free_list_head->next = kmalloc_fixed_info::node::invalid;
}
else
{
derrorln("removing free list, allocated {}", info.used);
info.free_list_head = nullptr;
}
node->prev = kmalloc_fixed_info::node::invalid;
node->next = kmalloc_fixed_info::node::invalid;
// move the node to the top of used nodes
if (info.used_list_head)
{
info.used_list_head->next = info.index_of(node);
node->prev = info.index_of(info.used_list_head);
}
info.used_list_head = node;
info.used += sizeof(kmalloc_fixed_info::node);
info.free -= sizeof(kmalloc_fixed_info::node);
return (void*)node->data;
}
static void* kmalloc_impl(size_t size, size_t align)
{
ASSERT(align % s_kmalloc_min_align == 0);
ASSERT(size % s_kmalloc_min_align == 0);
Kernel::SpinLockGuard _(s_kmalloc_lock);
for (size_t i = 0; i < s_max_allocator_count; i++)
auto& info = s_kmalloc_info;
for (auto* node = info.first(); info.contains(reinterpret_cast<uintptr_t>(node)); node = node->after())
{
if (auto* allocator = s_allocators[i])
{
if (void* result = allocator->allocate(needed_chunks))
return result;
if (node->used())
continue;
}
auto& new_allocator = reinterpret_cast<BitmapAllocator*>(s_allocator_storage)[i];
new (&new_allocator) BitmapAllocator();
if (!new_allocator.initialize_dynamic())
if (auto* next = node->after(); info.contains(reinterpret_cast<uintptr_t>(next)))
if (!next->used())
node->set_end(next->end());
if (node->size_no_align() < size)
continue;
ptrdiff_t needed_align = 0;
if (ptrdiff_t rem = node->data_no_align() % align)
needed_align = align - rem;
if (!node->can_align(needed_align))
continue;
node->set_align(needed_align);
ASSERT(node->data() % align == 0);
if (node->size() < size)
continue;
if (node->can_fit_before())
{
new_allocator.~BitmapAllocator();
break;
node->split_in_align();
node->set_used(false);
node = node->after();
ASSERT(node->data() % align == 0);
}
s_allocators[i] = &new_allocator;
node->set_used(true);
if (void* result = new_allocator.allocate(needed_chunks))
return result;
if (node->can_fit_after(size))
{
node->split_after_size(size);
node->after()->set_used(false);
ASSERT(node->data() % align == 0);
}
break;
info.used += sizeof(kmalloc_node) + node->size_no_align();
info.free -= sizeof(kmalloc_node) + node->size_no_align();
return (void*)node->data();
}
dwarnln("failed to allocate {} bytes", size);
kmalloc_dump_info();
return nullptr;
}
void kfree(void* ptr)
void* kmalloc(size_t size)
{
if (ptr == nullptr)
return kmalloc(size, s_kmalloc_min_align, false);
}
static constexpr bool is_power_of_two(size_t value)
{
if (value == 0)
return false;
return (value & (value - 1)) == 0;
}
void* kmalloc(size_t size, size_t align, bool force_identity_map)
{
// currently kmalloc is always identity mapped
(void)force_identity_map;
if (size == 0)
size = 1;
const kmalloc_info& info = s_kmalloc_info;
ASSERT(is_power_of_two(align));
if (align < s_kmalloc_min_align)
align = s_kmalloc_min_align;
ASSERT(align <= PAGE_SIZE);
if (size == 0 || size >= info.size)
goto no_memory;
// if the size fits into fixed node, we will try to use that since it is faster
if (align == s_kmalloc_min_align && size <= sizeof(kmalloc_fixed_info::node::data))
if (void* result = kmalloc_fixed())
return result;
if (ptrdiff_t rem = size % s_kmalloc_min_align)
size += s_kmalloc_min_align - rem;
if (void* res = kmalloc_impl(size, align))
return res;
no_memory:
dwarnln("could not allocate {H} bytes ({} aligned)", size, align);
dwarnln(" {6H} free (fixed)", s_kmalloc_fixed_info.free);
dwarnln(" {6H} free", s_kmalloc_info.free);
//Debug::dump_stack_trace();
ASSERT(!is_corrupted());
return nullptr;
}
void kfree(void* address)
{
if (address == nullptr)
return;
uintptr_t address_uint = (uintptr_t)address;
ASSERT(address_uint % s_kmalloc_min_align == 0);
Kernel::SpinLockGuard _(s_kmalloc_lock);
for (size_t i = 0; i < s_max_allocator_count && s_allocators[i]; i++)
if (s_allocators[i]->contains(ptr))
return s_allocators[i]->free(ptr);
if (s_kmalloc_fixed_info.base <= address_uint && address_uint < s_kmalloc_fixed_info.end)
{
auto& info = s_kmalloc_fixed_info;
ASSERT(info.used_list_head);
// get node from fixed info buffer
auto* node = (kmalloc_fixed_info::node*)address;
ASSERT(node->next < info.node_count || node->next == kmalloc_fixed_info::node::invalid);
ASSERT(node->prev < info.node_count || node->prev == kmalloc_fixed_info::node::invalid);
// remove from used list
if (node->prev != kmalloc_fixed_info::node::invalid)
info.node_at(node->prev)->next = node->next;
if (node->next != kmalloc_fixed_info::node::invalid)
info.node_at(node->next)->prev = node->prev;
if (info.used_list_head == node)
info.used_list_head = info.used_list_head->prev != kmalloc_fixed_info::node::invalid ? info.node_at(info.used_list_head->prev) : nullptr;
// add to free list
node->next = kmalloc_fixed_info::node::invalid;
node->prev = kmalloc_fixed_info::node::invalid;
if (info.free_list_head)
{
info.free_list_head->next = info.index_of(node);
node->prev = info.index_of(info.free_list_head);
}
info.free_list_head = node;
info.used -= sizeof(kmalloc_fixed_info::node);
info.free += sizeof(kmalloc_fixed_info::node);
}
else if (s_kmalloc_info.base <= address_uint && address_uint < s_kmalloc_info.end)
{
auto& info = s_kmalloc_info;
auto* node = info.from_address(address);
ASSERT(node);
ASSERT(node->data() == (uintptr_t)address);
ASSERT(node->used());
ptrdiff_t size = node->size_no_align();
if (auto* next = node->after(); next->end() <= info.end)
if (!next->used())
node->set_end(node->after()->end());
node->set_used(false);
info.used -= sizeof(kmalloc_node) + size;
info.free += sizeof(kmalloc_node) + size;
}
else
{
Kernel::panic("Trying to free a pointer {8H} outsize of kmalloc memory", address);
}
ASSERT_NOT_REACHED();
}
static bool is_kmalloc_vaddr(Kernel::vaddr_t vaddr)
{
using namespace Kernel;
if (vaddr < reinterpret_cast<vaddr_t>(s_kmalloc_storage))
return false;
if (vaddr >= reinterpret_cast<vaddr_t>(s_kmalloc_storage) + sizeof(s_kmalloc_storage))
return false;
return true;
}
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t vaddr)
{
using namespace Kernel;
if (!is_kmalloc_vaddr(vaddr))
return {};
return vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
}

View File

@@ -59,7 +59,7 @@ namespace Kernel
E1000::~E1000()
{
m_thread_should_die = true;
m_rx_blocker.unblock();
m_thread_blocker.unblock();
while (!m_thread_is_dead)
Processor::yield();
@@ -200,7 +200,7 @@ namespace Kernel
rctrl |= RCTL_BAM;
rctrl |= RCTL_SECRC;
rctrl |= RCTL_BSIZE_8192;
write32(REG_RCTL, rctrl);
write32(REG_RCTL, rctrl);
return {};
}
@@ -214,7 +214,6 @@ namespace Kernel
for (size_t i = 0; i < E1000_TX_DESCRIPTOR_COUNT; i++)
{
tx_descriptors[i].addr = m_tx_buffer_region->paddr() + E1000_TX_BUFFER_SIZE * i;
tx_descriptors[i].status = 0xFF; /* NOTE: set status to non-zero so send_bytes doesn't think these are initially pending */
tx_descriptors[i].cmd = 0;
}
@@ -240,18 +239,15 @@ namespace Kernel
{
if (!link_up())
return 0;
switch (read32(REG_STATUS) & STATUS_SPEED_MASK)
{
case STATUS_SPEED_10MB:
return 10;
case STATUS_SPEED_100MB:
return 100;
case STATUS_SPEED_1000MB1:
case STATUS_SPEED_1000MB2:
return 1000;
}
uint32_t speed = read32(REG_STATUS) & STATUS_SPEED_MASK;
if (speed == STATUS_SPEED_10MB)
return 10;
if (speed == STATUS_SPEED_100MB)
return 100;
if (speed == STATUS_SPEED_1000MB1)
return 1000;
if (speed == STATUS_SPEED_1000MB2)
return 1000;
return 0;
}
@@ -281,11 +277,9 @@ namespace Kernel
BAN::ErrorOr<void> E1000::send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::Span<const BAN::ConstByteSpan> payload)
{
const uint32_t tx_current = m_tx_head1.fetch_add(1) % E1000_TX_DESCRIPTOR_COUNT;
SpinLockGuard _(m_lock);
auto& descriptor = reinterpret_cast<volatile e1000_tx_desc*>(m_tx_descriptor_region->vaddr())[tx_current];
while (descriptor.status == 0)
Processor::yield();
size_t tx_current = read32(REG_TDT) % E1000_TX_DESCRIPTOR_COUNT;
auto* tx_buffer = reinterpret_cast<uint8_t*>(m_tx_buffer_region->vaddr() + E1000_TX_BUFFER_SIZE * tx_current);
@@ -302,12 +296,15 @@ namespace Kernel
packet_size += buffer.size();
}
auto& descriptor = reinterpret_cast<volatile e1000_tx_desc*>(m_tx_descriptor_region->vaddr())[tx_current];
descriptor.length = packet_size;
descriptor.status = 0;
descriptor.cmd = CMD_EOP | CMD_IFCS | CMD_RS;
if (tx_current == m_tx_head2.fetch_add(1) % E1000_TX_DESCRIPTOR_COUNT)
write32(REG_TDT, (tx_current + 1) % E1000_TX_DESCRIPTOR_COUNT);
// FIXME: there isnt really any reason to wait for transmission
write32(REG_TDT, (tx_current + 1) % E1000_TX_DESCRIPTOR_COUNT);
while (descriptor.status == 0)
Processor::pause();
dprintln_if(DEBUG_E1000, "sent {} bytes", packet_size);
@@ -316,7 +313,7 @@ namespace Kernel
void E1000::receive_thread()
{
SpinLockGuard _(m_rx_lock);
SpinLockGuard _(m_lock);
while (!m_thread_should_die)
{
@@ -331,21 +328,21 @@ namespace Kernel
dprintln_if(DEBUG_E1000, "got {} bytes", (uint16_t)descriptor.length);
m_rx_lock.unlock(InterruptState::Enabled);
m_lock.unlock(InterruptState::Enabled);
NetworkManager::get().on_receive(*this, BAN::ConstByteSpan {
reinterpret_cast<const uint8_t*>(m_rx_buffer_region->vaddr() + rx_current * E1000_RX_BUFFER_SIZE),
descriptor.length
});
m_rx_lock.lock();
m_lock.lock();
descriptor.status = 0;
write32(REG_RDT0, rx_current);
}
SpinLockAsMutex smutex(m_rx_lock, InterruptState::Enabled);
m_rx_blocker.block_indefinite(&smutex);
SpinLockAsMutex smutex(m_lock, InterruptState::Enabled);
m_thread_blocker.block_indefinite(&smutex);
}
m_thread_is_dead = true;
@@ -358,8 +355,8 @@ namespace Kernel
if (icr & (ICR_RxQ0 | ICR_RXT0))
{
SpinLockGuard _(m_rx_lock);
m_rx_blocker.unblock();
SpinLockGuard _(m_lock);
m_thread_blocker.unblock();
}
}

View File

@@ -3,8 +3,6 @@
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Networking/NetworkInterface.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/sysmacros.h>
#include <string.h>
@@ -27,10 +25,10 @@ namespace Kernel
}
NetworkInterface::NetworkInterface(Type type)
: CharacterDevice(0664, 0, 0)
: CharacterDevice(0400, 0, 0)
, m_type(type)
, m_rdev(get_rdev(type))
{
m_rdev = get_rdev(type);
switch (type)
{
case Type::Ethernet:
@@ -45,95 +43,4 @@ namespace Kernel
}
}
BAN::ErrorOr<long> NetworkInterface::ioctl_impl(int request, void* arg)
{
if (arg == nullptr)
{
dprintln("No argument provided");
return BAN::Error::from_errno(EINVAL);
}
auto* ifreq = reinterpret_cast<struct ifreq*>(arg);
switch (request)
{
case SIOCGIFADDR:
{
auto& ifru_addr = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_addr);
ifru_addr.sin_family = AF_INET;
ifru_addr.sin_addr.s_addr = get_ipv4_address().raw;
return 0;
}
case SIOCSIFADDR:
{
auto& ifru_addr = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_addr);
if (ifru_addr.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
set_ipv4_address(BAN::IPv4Address { ifru_addr.sin_addr.s_addr });
dprintln("IPv4 address set to {}", get_ipv4_address());
return 0;
}
case SIOCGIFNETMASK:
{
auto& ifru_netmask = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_netmask);
ifru_netmask.sin_family = AF_INET;
ifru_netmask.sin_addr.s_addr = get_netmask().raw;
return 0;
}
case SIOCSIFNETMASK:
{
auto& ifru_netmask = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_netmask);
if (ifru_netmask.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
set_netmask(BAN::IPv4Address { ifru_netmask.sin_addr.s_addr });
dprintln("Netmask set to {}", get_netmask());
return 0;
}
case SIOCGIFGWADDR:
{
auto& ifru_gwaddr = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_gwaddr);
ifru_gwaddr.sin_family = AF_INET;
ifru_gwaddr.sin_addr.s_addr = get_gateway().raw;
return 0;
}
case SIOCSIFGWADDR:
{
auto& ifru_gwaddr = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_gwaddr);
if (ifru_gwaddr.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
set_gateway(BAN::IPv4Address { ifru_gwaddr.sin_addr.s_addr });
dprintln("Gateway set to {}", get_gateway());
return 0;
}
case SIOCGIFHWADDR:
{
auto mac_address = get_mac_address();
ifreq->ifr_ifru.ifru_hwaddr.sa_family = AF_INET;
memcpy(ifreq->ifr_ifru.ifru_hwaddr.sa_data, &mac_address, sizeof(mac_address));
return 0;
}
case SIOCGIFNAME:
{
auto& ifrn_name = ifreq->ifr_ifrn.ifrn_name;
ASSERT(name().size() < sizeof(ifrn_name));
memcpy(ifrn_name, name().data(), name().size());
ifrn_name[name().size()] = '\0';
return 0;
}
case SIOCGIFFLAGS:
{
int flags = 0;
if (link_up())
flags |= IFF_UP | IFF_RUNNING;
if (m_type == Type::Loopback)
flags |= IFF_LOOPBACK;
ifreq->ifr_ifru.ifru_flags = flags;
return 0;
}
}
return CharacterDevice::ioctl_impl(request, arg);
}
}

View File

@@ -110,20 +110,83 @@ namespace Kernel
BAN::ErrorOr<long> NetworkSocket::ioctl_impl(int request, void* arg)
{
if (!arg)
{
dprintln("No argument provided");
return BAN::Error::from_errno(EINVAL);
}
auto interface = TRY(this->interface(nullptr, 0));
auto* ifreq = reinterpret_cast<struct ifreq*>(arg);
switch (request)
{
case SIOCGIFADDR:
{
auto& ifru_addr = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_addr);
ifru_addr.sin_family = AF_INET;
ifru_addr.sin_addr.s_addr = interface->get_ipv4_address().raw;
return 0;
}
case SIOCSIFADDR:
{
auto& ifru_addr = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_addr);
if (ifru_addr.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
interface->set_ipv4_address(BAN::IPv4Address { ifru_addr.sin_addr.s_addr });
dprintln("IPv4 address set to {}", interface->get_ipv4_address());
return 0;
}
case SIOCGIFNETMASK:
{
auto& ifru_netmask = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_netmask);
ifru_netmask.sin_family = AF_INET;
ifru_netmask.sin_addr.s_addr = interface->get_netmask().raw;
return 0;
}
case SIOCSIFNETMASK:
{
auto& ifru_netmask = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_netmask);
if (ifru_netmask.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
interface->set_netmask(BAN::IPv4Address { ifru_netmask.sin_addr.s_addr });
dprintln("Netmask set to {}", interface->get_netmask());
return 0;
}
case SIOCGIFGWADDR:
{
auto& ifru_gwaddr = *reinterpret_cast<sockaddr_in*>(&ifreq->ifr_ifru.ifru_gwaddr);
ifru_gwaddr.sin_family = AF_INET;
ifru_gwaddr.sin_addr.s_addr = interface->get_gateway().raw;
return 0;
}
case SIOCSIFGWADDR:
{
auto& ifru_gwaddr = *reinterpret_cast<const sockaddr_in*>(&ifreq->ifr_ifru.ifru_gwaddr);
if (ifru_gwaddr.sin_family != AF_INET)
return BAN::Error::from_errno(EADDRNOTAVAIL);
interface->set_gateway(BAN::IPv4Address { ifru_gwaddr.sin_addr.s_addr });
dprintln("Gateway set to {}", interface->get_gateway());
return 0;
}
case SIOCGIFHWADDR:
{
auto mac_address = interface->get_mac_address();
ifreq->ifr_ifru.ifru_hwaddr.sa_family = AF_INET;
memcpy(ifreq->ifr_ifru.ifru_hwaddr.sa_data, &mac_address, sizeof(mac_address));
return 0;
}
case SIOCGIFNAME:
return TRY(interface(nullptr, 0))->ioctl(request, arg);
{
auto& ifrn_name = ifreq->ifr_ifrn.ifrn_name;
ASSERT(interface->name().size() < sizeof(ifrn_name));
memcpy(ifrn_name, interface->name().data(), interface->name().size());
ifrn_name[interface->name().size()] = '\0';
return 0;
}
default:
return BAN::Error::from_errno(EINVAL);
}
return Socket::ioctl_impl(request, arg);
}
BAN::ErrorOr<void> NetworkSocket::getsockname_impl(sockaddr* address, socklen_t* address_len)

View File

@@ -66,8 +66,6 @@ namespace Kernel
BAN::ErrorOr<long> TCPSocket::accept_impl(sockaddr* address, socklen_t* address_len, int flags)
{
LockGuard _(m_mutex);
if (m_state != State::Listen)
return BAN::Error::from_errno(EINVAL);
@@ -173,8 +171,6 @@ namespace Kernel
BAN::ErrorOr<void> TCPSocket::listen_impl(int backlog)
{
LockGuard _(m_mutex);
if (!is_bound())
return BAN::Error::from_errno(EDESTADDRREQ);
if (m_connection_info.has_value())
@@ -189,8 +185,6 @@ namespace Kernel
BAN::ErrorOr<void> TCPSocket::bind_impl(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_mutex);
if (is_bound())
return BAN::Error::from_errno(EINVAL);
return m_network_layer.bind_socket_to_address(this, address, address_len);
@@ -210,8 +204,6 @@ namespace Kernel
message.msg_controllen = 0;
}
LockGuard _(m_mutex);
if (!m_has_connected)
return BAN::Error::from_errno(ENOTCONN);
@@ -269,8 +261,6 @@ namespace Kernel
if (CMSG_FIRSTHDR(&message))
dwarnln("ignoring sendmsg control message");
LockGuard _(m_mutex);
if (!m_has_connected)
return BAN::Error::from_errno(ENOTCONN);
@@ -301,7 +291,6 @@ namespace Kernel
BAN::ErrorOr<void> TCPSocket::getpeername_impl(sockaddr* address, socklen_t* address_len)
{
LockGuard _(m_mutex);
if (!m_has_connected && m_state != State::Established)
return BAN::Error::from_errno(ENOTCONN);
ASSERT(m_connection_info.has_value());
@@ -313,8 +302,6 @@ namespace Kernel
BAN::ErrorOr<void> TCPSocket::getsockopt_impl(int level, int option, void* value, socklen_t* value_len)
{
LockGuard _(m_mutex);
int result;
switch (level)
@@ -364,8 +351,6 @@ namespace Kernel
BAN::ErrorOr<void> TCPSocket::setsockopt_impl(int level, int option, const void* value, socklen_t value_len)
{
LockGuard _(m_mutex);
switch (level)
{
case SOL_SOCKET:
@@ -416,7 +401,6 @@ namespace Kernel
bool TCPSocket::can_read_impl() const
{
LockGuard _(m_mutex);
if (m_has_connected && !m_has_sent_zero && m_state != State::Established && m_state != State::Listen)
return true;
if (m_state == State::Listen)
@@ -426,7 +410,6 @@ namespace Kernel
bool TCPSocket::can_write_impl() const
{
LockGuard _(m_mutex);
if (m_state != State::Established)
return false;
return !m_send_window.buffer->full();
@@ -434,7 +417,6 @@ namespace Kernel
bool TCPSocket::has_hungup_impl() const
{
LockGuard _(m_mutex);
return m_has_connected && m_state != State::Established;
}

View File

@@ -1,4 +1,3 @@
#include <kernel/Lock/LockGuard.h>
#include <kernel/Lock/SpinLockAsMutex.h>
#include <kernel/Memory/Heap.h>
#include <kernel/Networking/UDPSocket.h>
@@ -59,6 +58,9 @@ namespace Kernel
void UDPSocket::receive_packet(BAN::ConstByteSpan packet, const sockaddr* sender, socklen_t sender_len)
{
(void)sender_len;
//auto& header = packet.as<const UDPHeader>();
auto payload = packet.slice(sizeof(UDPHeader));
SpinLockGuard _(m_packet_lock);
@@ -93,8 +95,6 @@ namespace Kernel
{
if (address_len > static_cast<socklen_t>(sizeof(m_peer_address)))
address_len = sizeof(m_peer_address);
SpinLockGuard _(m_peer_address_lock);
memcpy(&m_peer_address, address, address_len);
m_peer_address_len = address_len;
return {};
@@ -102,7 +102,6 @@ namespace Kernel
BAN::ErrorOr<void> UDPSocket::bind_impl(const sockaddr* address, socklen_t address_len)
{
LockGuard _(m_bind_lock);
if (is_bound())
return BAN::Error::from_errno(EINVAL);
return m_network_layer.bind_socket_to_address(this, address, address_len);
@@ -184,11 +183,8 @@ namespace Kernel
if (CMSG_FIRSTHDR(&message))
dwarnln("ignoring sendmsg control message");
{
LockGuard _(m_bind_lock);
if (!is_bound())
TRY(m_network_layer.bind_socket_with_target(this, static_cast<sockaddr*>(message.msg_name), message.msg_namelen));
}
if (!is_bound())
TRY(m_network_layer.bind_socket_with_target(this, static_cast<sockaddr*>(message.msg_name), message.msg_namelen));
const size_t total_send_size =
[&message]() -> size_t {
@@ -212,7 +208,6 @@ namespace Kernel
socklen_t address_len;
if (!message.msg_name || message.msg_namelen == 0)
{
SpinLockGuard _(m_peer_address_lock);
if (m_peer_address_len == 0)
return BAN::Error::from_errno(EDESTADDRREQ);
address = reinterpret_cast<sockaddr*>(&m_peer_address);

Some files were not shown because too many files have changed in this diff Show More