Compare commits
1 Commits
main
...
fc0f3157ea
| Author | SHA1 | Date | |
|---|---|---|---|
| fc0f3157ea |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,4 +3,3 @@
|
|||||||
build/
|
build/
|
||||||
base/
|
base/
|
||||||
script/fakeroot-context
|
script/fakeroot-context
|
||||||
tools/update-image-perms
|
|
||||||
|
|||||||
@@ -9,35 +9,29 @@
|
|||||||
#include <BAN/Formatter.h>
|
#include <BAN/Formatter.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define __debug_putchar [](int c) { putc_unlocked(c, stddbg); }
|
#define __debug_putchar [](int c) { putc(c, stddbg); }
|
||||||
|
|
||||||
#define dprintln(...) \
|
#define dprintln(...) \
|
||||||
do { \
|
do { \
|
||||||
flockfile(stddbg); \
|
|
||||||
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
||||||
BAN::Formatter::print(__debug_putchar,"\n"); \
|
BAN::Formatter::print(__debug_putchar,"\n"); \
|
||||||
fflush(stddbg); \
|
fflush(stddbg); \
|
||||||
funlockfile(stddbg); \
|
|
||||||
} while (false)
|
} while (false)
|
||||||
|
|
||||||
#define dwarnln(...) \
|
#define dwarnln(...) \
|
||||||
do { \
|
do { \
|
||||||
flockfile(stddbg); \
|
|
||||||
BAN::Formatter::print(__debug_putchar, "\e[33m"); \
|
BAN::Formatter::print(__debug_putchar, "\e[33m"); \
|
||||||
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
||||||
BAN::Formatter::print(__debug_putchar, "\e[m\n"); \
|
BAN::Formatter::print(__debug_putchar, "\e[m\n"); \
|
||||||
fflush(stddbg); \
|
fflush(stddbg); \
|
||||||
funlockfile(stddbg); \
|
|
||||||
} while(false)
|
} while(false)
|
||||||
|
|
||||||
#define derrorln(...) \
|
#define derrorln(...) \
|
||||||
do { \
|
do { \
|
||||||
flockfile(stddbg); \
|
|
||||||
BAN::Formatter::print(__debug_putchar, "\e[31m"); \
|
BAN::Formatter::print(__debug_putchar, "\e[31m"); \
|
||||||
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
||||||
BAN::Formatter::print(__debug_putchar, "\e[m\n"); \
|
BAN::Formatter::print(__debug_putchar, "\e[m\n"); \
|
||||||
fflush(stddbg); \
|
fflush(stddbg); \
|
||||||
funlockfile(stddbg); \
|
|
||||||
} while(false)
|
} while(false)
|
||||||
|
|
||||||
#define dprintln_if(cond, ...) \
|
#define dprintln_if(cond, ...) \
|
||||||
|
|||||||
@@ -1,156 +1,49 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/HashSet.h>
|
#include <BAN/Hash.h>
|
||||||
|
#include <BAN/LinkedList.h>
|
||||||
|
#include <BAN/Vector.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename HashSetIt, typename HashMap, typename Entry>
|
template<typename Key, typename T, typename HASH = BAN::hash<Key>>
|
||||||
class HashMapIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
HashMapIterator() = default;
|
|
||||||
|
|
||||||
Entry& operator*()
|
|
||||||
{
|
|
||||||
return const_cast<Entry&>(m_iterator.operator*());
|
|
||||||
}
|
|
||||||
const Entry& operator*() const
|
|
||||||
{
|
|
||||||
return m_iterator.operator*();
|
|
||||||
}
|
|
||||||
|
|
||||||
Entry* operator->()
|
|
||||||
{
|
|
||||||
return const_cast<Entry*>(m_iterator.operator->());
|
|
||||||
}
|
|
||||||
const Entry* operator->() const
|
|
||||||
{
|
|
||||||
return m_iterator.operator->();
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapIterator& operator++()
|
|
||||||
{
|
|
||||||
++m_iterator;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
HashMapIterator operator++(int)
|
|
||||||
{
|
|
||||||
auto temp = *this;
|
|
||||||
++(*this);
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(HashMapIterator other) const
|
|
||||||
{
|
|
||||||
return m_iterator == other.m_iterator;
|
|
||||||
}
|
|
||||||
bool operator!=(HashMapIterator other) const
|
|
||||||
{
|
|
||||||
return m_iterator != other.m_iterator;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
explicit HashMapIterator(HashSetIt it)
|
|
||||||
: m_iterator(it)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
private:
|
|
||||||
HashSetIt m_iterator;
|
|
||||||
friend HashMap;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
template<typename T, typename Key, typename HASH, typename COMP>
|
|
||||||
concept HashMapFindable = requires(const Key& a, const T& b) { COMP()(a, b); HASH()(b); };
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Key, typename T, typename HASH = BAN::hash<Key>, typename COMP = BAN::equal<Key>>
|
|
||||||
class HashMap
|
class HashMap
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct Entry
|
struct Entry
|
||||||
{
|
{
|
||||||
const Key key;
|
template<typename... Args>
|
||||||
T value;
|
Entry(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
|
: key(key)
|
||||||
Entry() = delete;
|
, value(forward<Args>(args)...)
|
||||||
Entry& operator=(const Entry&) = delete;
|
{}
|
||||||
Entry& operator=(Entry&&) = delete;
|
|
||||||
|
|
||||||
Entry(const Entry& other)
|
|
||||||
: key(other.key)
|
|
||||||
, value(other.value)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
Entry(Entry&& other)
|
|
||||||
: key(BAN::move(const_cast<Key&>(other.key)))
|
|
||||||
, value(BAN::move(other.value))
|
|
||||||
{ }
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
Entry(Key&& key, Args&&... args)
|
Entry(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
: key(BAN::move(key))
|
: key(BAN::move(key))
|
||||||
, value(BAN::forward<Args>(args)...)
|
, value(forward<Args>(args)...)
|
||||||
{ }
|
{}
|
||||||
};
|
|
||||||
|
|
||||||
struct EntryHash
|
Key key;
|
||||||
{
|
T value;
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
|
||||||
constexpr bool operator()(const U& a)
|
|
||||||
{
|
|
||||||
return HASH()(a);
|
|
||||||
}
|
|
||||||
constexpr bool operator()(const Entry& a)
|
|
||||||
{
|
|
||||||
return HASH()(a.key);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct EntryComp
|
|
||||||
{
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
|
||||||
constexpr bool operator()(const Entry& a, const U& b)
|
|
||||||
{
|
|
||||||
return COMP()(a.key, b);
|
|
||||||
}
|
|
||||||
constexpr bool operator()(const Entry& a, const Entry& b)
|
|
||||||
{
|
|
||||||
return COMP()(a.key, b.key);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using key_type = Key;
|
using key_type = Key;
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
using iterator = HashMapIterator<typename HashSet<Entry, EntryHash, EntryComp>::iterator, HashMap, Entry>;
|
using iterator = IteratorDouble<Entry, Vector, LinkedList, HashMap>;
|
||||||
using const_iterator = HashMapIterator<typename HashSet<Entry, EntryHash, EntryComp>::const_iterator, HashMap, const Entry>;
|
using const_iterator = ConstIteratorDouble<Entry, Vector, LinkedList, HashMap>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HashMap() = default;
|
HashMap() = default;
|
||||||
~HashMap() { clear(); }
|
HashMap(const HashMap<Key, T, HASH>&);
|
||||||
|
HashMap(HashMap<Key, T, HASH>&&);
|
||||||
|
~HashMap();
|
||||||
|
|
||||||
HashMap(const HashMap& other) { *this = other; }
|
HashMap<Key, T, HASH>& operator=(const HashMap<Key, T, HASH>&);
|
||||||
HashMap& operator=(const HashMap& other)
|
HashMap<Key, T, HASH>& operator=(HashMap<Key, T, HASH>&&);
|
||||||
{
|
|
||||||
m_hash_set = other.m_hash_set;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap(HashMap&& other) { *this = BAN::move(other); }
|
|
||||||
HashMap& operator=(HashMap&& other)
|
|
||||||
{
|
|
||||||
m_hash_set = BAN::move(other.m_hash_set);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator begin() { return iterator(m_hash_set.begin()); }
|
|
||||||
iterator end() { return iterator(m_hash_set.end()); }
|
|
||||||
const_iterator begin() const { return const_iterator(m_hash_set.begin()); }
|
|
||||||
const_iterator end() const { return const_iterator(m_hash_set.end()); }
|
|
||||||
|
|
||||||
ErrorOr<iterator> insert(const Key& key, const T& value) { return emplace(key, value); }
|
ErrorOr<iterator> insert(const Key& key, const T& value) { return emplace(key, value); }
|
||||||
ErrorOr<iterator> insert(const Key& key, T&& value) { return emplace(key, move(value)); }
|
ErrorOr<iterator> insert(const Key& key, T&& value) { return emplace(key, move(value)); }
|
||||||
@@ -164,100 +57,263 @@ namespace BAN
|
|||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
ErrorOr<iterator> emplace(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
|
ErrorOr<iterator> emplace(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
{ return emplace(Key(key), BAN::forward<Args>(args)...); }
|
{ return emplace(Key(key), forward<Args>(args)...); }
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
ErrorOr<iterator> emplace(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
|
ErrorOr<iterator> emplace(Key&&, Args&&...) requires is_constructible_v<T, Args...>;
|
||||||
{
|
|
||||||
ASSERT(!contains(key));
|
|
||||||
auto it = TRY(m_hash_set.insert(Entry { BAN::move(key), T(BAN::forward<Args>(args)...) }));
|
|
||||||
return iterator(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
ErrorOr<iterator> emplace_or_assign(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
|
ErrorOr<iterator> emplace_or_assign(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
{ return emplace_or_assign(Key(key), BAN::forward<Args>(args)...); }
|
{ return emplace_or_assign(Key(key), forward<Args>(args)...); }
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
ErrorOr<iterator> emplace_or_assign(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
|
ErrorOr<iterator> emplace_or_assign(Key&&, Args&&...) requires is_constructible_v<T, Args...>;
|
||||||
{
|
|
||||||
if (auto it = m_hash_set.find(key); it != m_hash_set.end())
|
|
||||||
{
|
|
||||||
const_cast<T&>(it->value) = T(BAN::forward<Args>(args)...);
|
|
||||||
return iterator(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto it = TRY(m_hash_set.insert(Entry { BAN::move(key), T(BAN::forward<Args>(args)...) }));
|
iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); }
|
||||||
return iterator(it);
|
iterator end() { return iterator(m_buckets.end(), m_buckets.end()); }
|
||||||
}
|
const_iterator begin() const { return const_iterator(m_buckets.end(), m_buckets.begin()); }
|
||||||
|
const_iterator end() const { return const_iterator(m_buckets.end(), m_buckets.end()); }
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
ErrorOr<void> reserve(size_type);
|
||||||
void remove(const U& key)
|
|
||||||
{
|
|
||||||
if (auto it = find(key); it != end())
|
|
||||||
remove(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator remove(iterator it)
|
void remove(const Key&);
|
||||||
{
|
void remove(iterator it);
|
||||||
return iterator(m_hash_set.remove(it.m_iterator));
|
void clear();
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
T& operator[](const Key&);
|
||||||
iterator find(const U& key)
|
const T& operator[](const Key&) const;
|
||||||
{
|
|
||||||
return iterator(m_hash_set.find(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
iterator find(const Key& key);
|
||||||
const_iterator find(const U& key) const
|
const_iterator find(const Key& key) const;
|
||||||
{
|
bool contains(const Key&) const;
|
||||||
return const_iterator(m_hash_set.find(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear()
|
bool empty() const;
|
||||||
{
|
size_type size() const;
|
||||||
m_hash_set.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<void> reserve(size_type size)
|
|
||||||
{
|
|
||||||
return m_hash_set.reserve(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
|
||||||
T& operator[](const U& key)
|
|
||||||
{
|
|
||||||
return find(key)->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
|
||||||
const T& operator[](const U& key) const
|
|
||||||
{
|
|
||||||
return find(key)->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashMapFindable<Key, HASH, COMP> U>
|
|
||||||
bool contains(const U& key) const
|
|
||||||
{
|
|
||||||
return find(key) != end();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_type capacity() const
|
|
||||||
{
|
|
||||||
return m_hash_set.capacity();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_type size() const
|
|
||||||
{
|
|
||||||
return m_hash_set.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const
|
|
||||||
{
|
|
||||||
return m_hash_set.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HashSet<Entry, EntryHash, EntryComp> m_hash_set;
|
ErrorOr<void> rebucket(size_type);
|
||||||
|
LinkedList<Entry>& get_bucket(const Key&);
|
||||||
|
const LinkedList<Entry>& get_bucket(const Key&) const;
|
||||||
|
Vector<LinkedList<Entry>>::iterator get_bucket_iterator(const Key&);
|
||||||
|
Vector<LinkedList<Entry>>::const_iterator get_bucket_iterator(const Key&) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector<LinkedList<Entry>> m_buckets;
|
||||||
|
size_type m_size = 0;
|
||||||
|
|
||||||
|
friend iterator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
HashMap<Key, T, HASH>::HashMap(const HashMap<Key, T, HASH>& other)
|
||||||
|
{
|
||||||
|
*this = other;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
HashMap<Key, T, HASH>::HashMap(HashMap<Key, T, HASH>&& other)
|
||||||
|
{
|
||||||
|
*this = move(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
HashMap<Key, T, HASH>::~HashMap()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
HashMap<Key, T, HASH>& HashMap<Key, T, HASH>::operator=(const HashMap<Key, T, HASH>& other)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
m_buckets = other.m_buckets;
|
||||||
|
m_size = other.m_size;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
HashMap<Key, T, HASH>& HashMap<Key, T, HASH>::operator=(HashMap<Key, T, HASH>&& other)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
m_buckets = move(other.m_buckets);
|
||||||
|
m_size = other.m_size;
|
||||||
|
other.m_size = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
template<typename... Args>
|
||||||
|
ErrorOr<typename HashMap<Key, T, HASH>::iterator> HashMap<Key, T, HASH>::emplace(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
|
{
|
||||||
|
ASSERT(!contains(key));
|
||||||
|
TRY(rebucket(m_size + 1));
|
||||||
|
|
||||||
|
auto bucket_it = get_bucket_iterator(key);
|
||||||
|
TRY(bucket_it->emplace_back(move(key), forward<Args>(args)...));
|
||||||
|
m_size++;
|
||||||
|
|
||||||
|
return iterator(m_buckets.end(), bucket_it, prev(bucket_it->end(), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
template<typename... Args>
|
||||||
|
ErrorOr<typename HashMap<Key, T, HASH>::iterator> HashMap<Key, T, HASH>::emplace_or_assign(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
|
||||||
|
{
|
||||||
|
if (empty())
|
||||||
|
return emplace(move(key), forward<Args>(args)...);
|
||||||
|
|
||||||
|
auto bucket_it = get_bucket_iterator(key);
|
||||||
|
for (auto entry_it = bucket_it->begin(); entry_it != bucket_it->end(); entry_it++)
|
||||||
|
{
|
||||||
|
if (entry_it->key != key)
|
||||||
|
continue;
|
||||||
|
entry_it->value = T(forward<Args>(args)...);
|
||||||
|
return iterator(m_buckets.end(), bucket_it, entry_it);
|
||||||
|
}
|
||||||
|
|
||||||
|
return emplace(move(key), forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashMap<Key, T, HASH>::reserve(size_type size)
|
||||||
|
{
|
||||||
|
TRY(rebucket(size));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
void HashMap<Key, T, HASH>::remove(const Key& key)
|
||||||
|
{
|
||||||
|
auto it = find(key);
|
||||||
|
if (it != end())
|
||||||
|
remove(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
void HashMap<Key, T, HASH>::remove(iterator it)
|
||||||
|
{
|
||||||
|
it.outer_current()->remove(it.inner_current());
|
||||||
|
m_size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
void HashMap<Key, T, HASH>::clear()
|
||||||
|
{
|
||||||
|
m_buckets.clear();
|
||||||
|
m_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
T& HashMap<Key, T, HASH>::operator[](const Key& key)
|
||||||
|
{
|
||||||
|
ASSERT(!empty());
|
||||||
|
auto& bucket = get_bucket(key);
|
||||||
|
for (Entry& entry : bucket)
|
||||||
|
if (entry.key == key)
|
||||||
|
return entry.value;
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
const T& HashMap<Key, T, HASH>::operator[](const Key& key) const
|
||||||
|
{
|
||||||
|
ASSERT(!empty());
|
||||||
|
const auto& bucket = get_bucket(key);
|
||||||
|
for (const Entry& entry : bucket)
|
||||||
|
if (entry.key == key)
|
||||||
|
return entry.value;
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
typename HashMap<Key, T, HASH>::iterator HashMap<Key, T, HASH>::find(const Key& key)
|
||||||
|
{
|
||||||
|
if (empty())
|
||||||
|
return end();
|
||||||
|
auto bucket_it = get_bucket_iterator(key);
|
||||||
|
for (auto it = bucket_it->begin(); it != bucket_it->end(); it++)
|
||||||
|
if (it->key == key)
|
||||||
|
return iterator(m_buckets.end(), bucket_it, it);
|
||||||
|
return end();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
typename HashMap<Key, T, HASH>::const_iterator HashMap<Key, T, HASH>::find(const Key& key) const
|
||||||
|
{
|
||||||
|
if (empty())
|
||||||
|
return end();
|
||||||
|
auto bucket_it = get_bucket_iterator(key);
|
||||||
|
for (auto it = bucket_it->begin(); it != bucket_it->end(); it++)
|
||||||
|
if (it->key == key)
|
||||||
|
return const_iterator(m_buckets.end(), bucket_it, it);
|
||||||
|
return end();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
bool HashMap<Key, T, HASH>::contains(const Key& key) const
|
||||||
|
{
|
||||||
|
return find(key) != end();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
bool HashMap<Key, T, HASH>::empty() const
|
||||||
|
{
|
||||||
|
return m_size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
typename HashMap<Key, T, HASH>::size_type HashMap<Key, T, HASH>::size() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashMap<Key, T, HASH>::rebucket(size_type bucket_count)
|
||||||
|
{
|
||||||
|
if (m_buckets.size() >= bucket_count)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
|
||||||
|
Vector<LinkedList<Entry>> new_buckets;
|
||||||
|
TRY(new_buckets.resize(new_bucket_count));
|
||||||
|
|
||||||
|
for (auto& bucket : m_buckets)
|
||||||
|
{
|
||||||
|
for (auto it = bucket.begin(); it != bucket.end();)
|
||||||
|
{
|
||||||
|
size_type new_bucket_index = HASH()(it->key) % new_buckets.size();
|
||||||
|
it = bucket.move_element_to_other_linked_list(new_buckets[new_bucket_index], new_buckets[new_bucket_index].end(), it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_buckets = move(new_buckets);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
LinkedList<typename HashMap<Key, T, HASH>::Entry>& HashMap<Key, T, HASH>::get_bucket(const Key& key)
|
||||||
|
{
|
||||||
|
return *get_bucket_iterator(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
const LinkedList<typename HashMap<Key, T, HASH>::Entry>& HashMap<Key, T, HASH>::get_bucket(const Key& key) const
|
||||||
|
{
|
||||||
|
return *get_bucket_iterator(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
Vector<LinkedList<typename HashMap<Key, T, HASH>::Entry>>::iterator HashMap<Key, T, HASH>::get_bucket_iterator(const Key& key)
|
||||||
|
{
|
||||||
|
ASSERT(!m_buckets.empty());
|
||||||
|
auto index = HASH()(key) % m_buckets.size();
|
||||||
|
return next(m_buckets.begin(), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T, typename HASH>
|
||||||
|
Vector<LinkedList<typename HashMap<Key, T, HASH>::Entry>>::const_iterator HashMap<Key, T, HASH>::get_bucket_iterator(const Key& key) const
|
||||||
|
{
|
||||||
|
ASSERT(!m_buckets.empty());
|
||||||
|
auto index = HASH()(key) % m_buckets.size();
|
||||||
|
return next(m_buckets.begin(), index);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,358 +2,198 @@
|
|||||||
|
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
#include <BAN/Hash.h>
|
#include <BAN/Hash.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
|
#include <BAN/LinkedList.h>
|
||||||
#include <BAN/Math.h>
|
#include <BAN/Math.h>
|
||||||
#include <BAN/Move.h>
|
#include <BAN/Move.h>
|
||||||
#include <BAN/New.h>
|
#include <BAN/Vector.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename HashSet, typename Bucket, typename T>
|
template<typename T, typename HASH = hash<T>>
|
||||||
class HashSetIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
HashSetIterator() = default;
|
|
||||||
|
|
||||||
const T& operator*() const
|
|
||||||
{
|
|
||||||
ASSERT(m_bucket);
|
|
||||||
return *m_bucket->element();
|
|
||||||
}
|
|
||||||
|
|
||||||
const T* operator->() const
|
|
||||||
{
|
|
||||||
ASSERT(m_bucket);
|
|
||||||
return m_bucket->element();
|
|
||||||
}
|
|
||||||
|
|
||||||
HashSetIterator& operator++()
|
|
||||||
{
|
|
||||||
ASSERT(m_bucket);
|
|
||||||
m_bucket++;
|
|
||||||
skip_to_valid_bucket();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
HashSetIterator operator++(int)
|
|
||||||
{
|
|
||||||
auto temp = *this;
|
|
||||||
++(*this);
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(HashSetIterator other) const
|
|
||||||
{
|
|
||||||
return m_bucket == other.m_bucket;
|
|
||||||
}
|
|
||||||
bool operator!=(HashSetIterator other) const
|
|
||||||
{
|
|
||||||
return m_bucket != other.m_bucket;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
explicit HashSetIterator(Bucket* bucket)
|
|
||||||
: m_bucket(bucket)
|
|
||||||
{
|
|
||||||
if (m_bucket != nullptr)
|
|
||||||
skip_to_valid_bucket();
|
|
||||||
}
|
|
||||||
|
|
||||||
void skip_to_valid_bucket()
|
|
||||||
{
|
|
||||||
while (m_bucket->state != Bucket::USED && !m_bucket->end)
|
|
||||||
m_bucket++;
|
|
||||||
if (m_bucket->end)
|
|
||||||
m_bucket = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Bucket* m_bucket { nullptr };
|
|
||||||
friend HashSet;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
template<typename T, typename U, typename HASH, typename COMP>
|
|
||||||
concept HashSetFindable = requires(const U& a, const T& b) { COMP()(a, b); HASH()(b); };
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename HASH = BAN::hash<T>, typename COMP = BAN::equal<T>>
|
|
||||||
class HashSet
|
class HashSet
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
struct Bucket
|
|
||||||
{
|
|
||||||
static constexpr uint8_t UNUSED = 0;
|
|
||||||
static constexpr uint8_t USED = 1;
|
|
||||||
static constexpr uint8_t REMOVED = 2;
|
|
||||||
|
|
||||||
alignas(T) uint8_t storage[sizeof(T)];
|
|
||||||
hash_t hash;
|
|
||||||
uint8_t state : 2;
|
|
||||||
uint8_t chain_start : 1;
|
|
||||||
uint8_t end : 1;
|
|
||||||
|
|
||||||
T* element() { return reinterpret_cast<T*>(storage); }
|
|
||||||
const T* element() const { return reinterpret_cast<const T*>(storage); }
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using iterator = HashSetIterator<HashSet, Bucket, T>;
|
using iterator = IteratorDouble<T, Vector, LinkedList, HashSet>;
|
||||||
using const_iterator = HashSetIterator<HashSet, const Bucket, const T>;
|
using const_iterator = ConstIteratorDouble<T, Vector, LinkedList, HashSet>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HashSet() = default;
|
HashSet() = default;
|
||||||
~HashSet() { clear(); }
|
HashSet(const HashSet&);
|
||||||
|
HashSet(HashSet&&);
|
||||||
|
|
||||||
HashSet(const HashSet& other) { *this = other; }
|
HashSet& operator=(const HashSet&);
|
||||||
HashSet& operator=(const HashSet& other)
|
HashSet& operator=(HashSet&&);
|
||||||
{
|
|
||||||
clear();
|
|
||||||
|
|
||||||
MUST(reserve(other.size()));
|
ErrorOr<void> insert(const T&);
|
||||||
for (auto& bucket : other)
|
ErrorOr<void> insert(T&&);
|
||||||
MUST(insert(bucket));
|
void remove(const T&);
|
||||||
|
void clear();
|
||||||
|
|
||||||
return *this;
|
ErrorOr<void> reserve(size_type);
|
||||||
}
|
|
||||||
|
|
||||||
HashSet(HashSet&& other) { *this = BAN::move(other); }
|
iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); }
|
||||||
HashSet& operator=(HashSet&& other)
|
iterator end() { return iterator(m_buckets.end(), m_buckets.end()); }
|
||||||
{
|
const_iterator begin() const { return const_iterator(m_buckets.end(), m_buckets.begin()); }
|
||||||
clear();
|
const_iterator end() const { return const_iterator(m_buckets.end(), m_buckets.end()); }
|
||||||
|
|
||||||
m_buckets = other.m_buckets;
|
bool contains(const T&) const;
|
||||||
m_capacity = other.m_capacity;
|
|
||||||
m_size = other.m_size;
|
|
||||||
m_removed = other.m_removed;
|
|
||||||
|
|
||||||
other.m_buckets = nullptr;
|
size_type size() const;
|
||||||
other.m_capacity = 0;
|
bool empty() const;
|
||||||
other.m_size = 0;
|
|
||||||
other.m_removed = 0;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator begin() { return iterator(m_buckets); }
|
|
||||||
iterator end() { return iterator(nullptr); }
|
|
||||||
const_iterator begin() const { return const_iterator(m_buckets); }
|
|
||||||
const_iterator end() const { return const_iterator(nullptr); }
|
|
||||||
|
|
||||||
ErrorOr<iterator> insert(const T& value)
|
|
||||||
{
|
|
||||||
return insert(T(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<iterator> insert(T&& value)
|
|
||||||
{
|
|
||||||
if (should_rehash_with_size(m_size + 1))
|
|
||||||
TRY(rehash(m_size * 2));
|
|
||||||
return insert_impl(BAN::move(value), HASH()(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashSetFindable<T, HASH, COMP> U>
|
|
||||||
void remove(const U& value)
|
|
||||||
{
|
|
||||||
if (auto it = find(value); it != end())
|
|
||||||
remove(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator remove(iterator it)
|
|
||||||
{
|
|
||||||
auto& bucket = *it.m_bucket;
|
|
||||||
bucket.element()->~T();
|
|
||||||
bucket.state = Bucket::REMOVED;
|
|
||||||
m_size--;
|
|
||||||
m_removed++;
|
|
||||||
return iterator(&bucket);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashSetFindable<T, HASH, COMP> U>
|
|
||||||
iterator find(const U& value)
|
|
||||||
{
|
|
||||||
return iterator(const_cast<Bucket*>(find_impl(value).m_bucket));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashSetFindable<T, HASH, COMP> U>
|
|
||||||
const_iterator find(const U& value) const
|
|
||||||
{
|
|
||||||
return find_impl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear()
|
|
||||||
{
|
|
||||||
if (m_buckets == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (size_type i = 0; i < m_capacity; i++)
|
|
||||||
if (m_buckets[i].state == Bucket::USED)
|
|
||||||
m_buckets[i].element()->~T();
|
|
||||||
|
|
||||||
BAN::deallocator(m_buckets);
|
|
||||||
m_buckets = nullptr;
|
|
||||||
m_capacity = 0;
|
|
||||||
m_size = 0;
|
|
||||||
m_removed = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<void> reserve(size_type size)
|
|
||||||
{
|
|
||||||
if (should_rehash_with_size(size))
|
|
||||||
TRY(rehash(size * 2));
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashSetFindable<T, HASH, COMP> U>
|
|
||||||
bool contains(const U& value) const
|
|
||||||
{
|
|
||||||
return find(value) != end();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_type capacity() const
|
|
||||||
{
|
|
||||||
return m_capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_type size() const
|
|
||||||
{
|
|
||||||
return m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const
|
|
||||||
{
|
|
||||||
return m_size == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ErrorOr<void> rehash(size_type new_capacity)
|
ErrorOr<void> rebucket(size_type);
|
||||||
{
|
LinkedList<T>& get_bucket(const T&);
|
||||||
new_capacity = BAN::Math::max<size_t>(16, BAN::Math::max(new_capacity, m_size + 1));
|
const LinkedList<T>& get_bucket(const T&) const;
|
||||||
new_capacity = BAN::Math::round_up_to_power_of_two(new_capacity);
|
|
||||||
|
|
||||||
void* new_buckets = BAN::allocator((new_capacity + 1) * sizeof(Bucket));
|
|
||||||
if (new_buckets == nullptr)
|
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
|
||||||
memset(new_buckets, 0, (new_capacity + 1) * sizeof(Bucket));
|
|
||||||
|
|
||||||
Bucket* old_buckets = m_buckets;
|
|
||||||
const size_type old_capacity = m_capacity;
|
|
||||||
|
|
||||||
m_buckets = static_cast<Bucket*>(new_buckets);
|
|
||||||
m_capacity = new_capacity;
|
|
||||||
m_size = 0;
|
|
||||||
m_removed = 0;
|
|
||||||
|
|
||||||
for (size_type i = 0; i < old_capacity; i++)
|
|
||||||
{
|
|
||||||
auto& old_bucket = old_buckets[i];
|
|
||||||
if (old_bucket.state != Bucket::USED)
|
|
||||||
continue;
|
|
||||||
insert_impl(BAN::move(*old_bucket.element()), old_bucket.hash);
|
|
||||||
old_bucket.element()->~T();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_buckets[m_capacity].end = true;
|
|
||||||
|
|
||||||
BAN::deallocator(old_buckets);
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<detail::HashSetFindable<T, HASH, COMP> U>
|
|
||||||
const_iterator find_impl(const U& value) const
|
|
||||||
{
|
|
||||||
if (m_capacity == 0)
|
|
||||||
return end();
|
|
||||||
|
|
||||||
bool first = true;
|
|
||||||
const hash_t orig_hash = HASH()(value);
|
|
||||||
for (auto hash = orig_hash;; hash = get_next_hash_in_chain(hash, orig_hash), first = false)
|
|
||||||
{
|
|
||||||
auto& bucket = m_buckets[hash & (m_capacity - 1)];
|
|
||||||
if (bucket.state == Bucket::USED && bucket.hash == orig_hash && COMP()(*bucket.element(), value))
|
|
||||||
return const_iterator(&bucket);
|
|
||||||
if (bucket.state == Bucket::UNUSED)
|
|
||||||
return end();
|
|
||||||
if (!first && bucket.chain_start)
|
|
||||||
return end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator insert_impl(T&& value, hash_t orig_hash)
|
|
||||||
{
|
|
||||||
ASSERT(!should_rehash_with_size(m_size + 1));
|
|
||||||
|
|
||||||
Bucket* target = nullptr;
|
|
||||||
|
|
||||||
bool first = true;
|
|
||||||
for (auto hash = orig_hash;; hash = get_next_hash_in_chain(hash, orig_hash), first = false)
|
|
||||||
{
|
|
||||||
auto& bucket = m_buckets[hash & (m_capacity - 1)];
|
|
||||||
|
|
||||||
if (!first)
|
|
||||||
bucket.chain_start = false;
|
|
||||||
|
|
||||||
if (bucket.state == Bucket::USED)
|
|
||||||
{
|
|
||||||
if (bucket.hash != orig_hash || !COMP()(*bucket.element(), value))
|
|
||||||
continue;
|
|
||||||
target = &bucket;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target == nullptr)
|
|
||||||
target = &bucket;
|
|
||||||
|
|
||||||
if (bucket.state == Bucket::UNUSED)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (target->state)
|
|
||||||
{
|
|
||||||
case Bucket::USED:
|
|
||||||
target->element()->~T();
|
|
||||||
break;
|
|
||||||
case Bucket::REMOVED:
|
|
||||||
m_removed--;
|
|
||||||
[[fallthrough]];
|
|
||||||
case Bucket::UNUSED:
|
|
||||||
m_size++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
target->chain_start = first && target->state == Bucket::UNUSED;
|
|
||||||
target->hash = orig_hash;
|
|
||||||
target->state = Bucket::USED;
|
|
||||||
|
|
||||||
new (target->element()) T(BAN::move(value));
|
|
||||||
|
|
||||||
return iterator(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool should_rehash_with_size(size_type size) const
|
|
||||||
{
|
|
||||||
if (m_capacity < 16)
|
|
||||||
return true;
|
|
||||||
if (size + m_removed > m_capacity / 4 * 3)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash_t get_next_hash_in_chain(hash_t prev_hash, hash_t orig_hash) const
|
|
||||||
{
|
|
||||||
// TODO: does this even provide better performance than `return prev_hash + 1`
|
|
||||||
// when using "good" hash functions
|
|
||||||
return prev_hash * 1103515245 + (orig_hash | 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Bucket* m_buckets { nullptr };
|
Vector<LinkedList<T>> m_buckets;
|
||||||
size_type m_capacity { 0 };
|
size_type m_size = 0;
|
||||||
size_type m_size { 0 };
|
|
||||||
size_type m_removed { 0 };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
HashSet<T, HASH>::HashSet(const HashSet& other)
|
||||||
|
: m_buckets(other.m_buckets)
|
||||||
|
, m_size(other.m_size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
HashSet<T, HASH>::HashSet(HashSet&& other)
|
||||||
|
: m_buckets(move(other.m_buckets))
|
||||||
|
, m_size(other.m_size)
|
||||||
|
{
|
||||||
|
other.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
HashSet<T, HASH>& HashSet<T, HASH>::operator=(const HashSet& other)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
m_buckets = other.m_buckets;
|
||||||
|
m_size = other.m_size;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
HashSet<T, HASH>& HashSet<T, HASH>::operator=(HashSet&& other)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
m_buckets = move(other.m_buckets);
|
||||||
|
m_size = other.m_size;
|
||||||
|
other.clear();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashSet<T, HASH>::insert(const T& key)
|
||||||
|
{
|
||||||
|
return insert(move(T(key)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashSet<T, HASH>::insert(T&& key)
|
||||||
|
{
|
||||||
|
if (!empty() && get_bucket(key).contains(key))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
TRY(rebucket(m_size + 1));
|
||||||
|
TRY(get_bucket(key).push_back(move(key)));
|
||||||
|
m_size++;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
void HashSet<T, HASH>::remove(const T& key)
|
||||||
|
{
|
||||||
|
if (empty()) return;
|
||||||
|
auto& bucket = get_bucket(key);
|
||||||
|
for (auto it = bucket.begin(); it != bucket.end(); it++)
|
||||||
|
{
|
||||||
|
if (*it == key)
|
||||||
|
{
|
||||||
|
bucket.remove(it);
|
||||||
|
m_size--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
void HashSet<T, HASH>::clear()
|
||||||
|
{
|
||||||
|
m_buckets.clear();
|
||||||
|
m_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashSet<T, HASH>::reserve(size_type size)
|
||||||
|
{
|
||||||
|
TRY(rebucket(size));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
bool HashSet<T, HASH>::contains(const T& key) const
|
||||||
|
{
|
||||||
|
if (empty()) return false;
|
||||||
|
return get_bucket(key).contains(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
typename HashSet<T, HASH>::size_type HashSet<T, HASH>::size() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
bool HashSet<T, HASH>::empty() const
|
||||||
|
{
|
||||||
|
return m_size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
ErrorOr<void> HashSet<T, HASH>::rebucket(size_type bucket_count)
|
||||||
|
{
|
||||||
|
if (m_buckets.size() >= bucket_count)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
size_type new_bucket_count = Math::max<size_type>(bucket_count, m_buckets.size() * 2);
|
||||||
|
Vector<LinkedList<T>> new_buckets;
|
||||||
|
if (new_buckets.resize(new_bucket_count).is_error())
|
||||||
|
return Error::from_errno(ENOMEM);
|
||||||
|
|
||||||
|
for (auto& bucket : m_buckets)
|
||||||
|
{
|
||||||
|
for (auto it = bucket.begin(); it != bucket.end();)
|
||||||
|
{
|
||||||
|
size_type new_bucket_index = HASH()(*it) % new_buckets.size();
|
||||||
|
it = bucket.move_element_to_other_linked_list(new_buckets[new_bucket_index], new_buckets[new_bucket_index].end(), it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_buckets = move(new_buckets);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
LinkedList<T>& HashSet<T, HASH>::get_bucket(const T& key)
|
||||||
|
{
|
||||||
|
ASSERT(!m_buckets.empty());
|
||||||
|
size_type index = HASH()(key) % m_buckets.size();
|
||||||
|
return m_buckets[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename HASH>
|
||||||
|
const LinkedList<T>& HashSet<T, HASH>::get_bucket(const T& key) const
|
||||||
|
{
|
||||||
|
ASSERT(!m_buckets.empty());
|
||||||
|
size_type index = HASH()(key) % m_buckets.size();
|
||||||
|
return m_buckets[index];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ namespace BAN
|
|||||||
|
|
||||||
struct IPv4Address
|
struct IPv4Address
|
||||||
{
|
{
|
||||||
constexpr IPv4Address()
|
|
||||||
: IPv4Address(0)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
constexpr IPv4Address(uint32_t u32_address)
|
constexpr IPv4Address(uint32_t u32_address)
|
||||||
{
|
{
|
||||||
raw = u32_address;
|
raw = u32_address;
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#define _ban_count_args_impl(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, ...) _9
|
|
||||||
#define _ban_count_args(...) _ban_count_args_impl(__VA_ARGS__ __VA_OPT__(,) 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
|
||||||
|
|
||||||
#define _ban_concat_impl(a, b) a##b
|
|
||||||
#define _ban_concat(a, b) _ban_concat_impl(a, b)
|
|
||||||
|
|
||||||
#define _ban_stringify_impl(x) #x
|
|
||||||
#define _ban_stringify(x) _ban_stringify_impl(x)
|
|
||||||
|
|
||||||
#define _ban_fe_0(f)
|
|
||||||
#define _ban_fe_1(f, _0) f(0, _0)
|
|
||||||
#define _ban_fe_2(f, _0, _1) f(0, _0) f(1, _1)
|
|
||||||
#define _ban_fe_3(f, _0, _1, _2) f(0, _0) f(1, _1) f(2, _2)
|
|
||||||
#define _ban_fe_4(f, _0, _1, _2, _3) f(0, _0) f(1, _1) f(2, _2) f(3, _3)
|
|
||||||
#define _ban_fe_5(f, _0, _1, _2, _3, _4) f(0, _0) f(1, _1) f(2, _2) f(3, _3) f(4, _4)
|
|
||||||
#define _ban_fe_6(f, _0, _1, _2, _3, _4, _5) f(0, _0) f(1, _1) f(2, _2) f(3, _3) f(4, _4) f(5, _5)
|
|
||||||
#define _ban_fe_7(f, _0, _1, _2, _3, _4, _5, _6) f(0, _0) f(1, _1) f(2, _2) f(3, _3) f(4, _4) f(5, _5) f(6, _6)
|
|
||||||
#define _ban_fe_8(f, _0, _1, _2, _3, _4, _5, _6, _7) f(0, _0) f(1, _1) f(2, _2) f(3, _3) f(4, _4) f(5, _5) f(6, _6) f(7, _7)
|
|
||||||
#define _ban_fe_9(f, _0, _1, _2, _3, _4, _5, _6, _7, _8) f(0, _0) f(1, _1) f(2, _2) f(3, _3) f(4, _4) f(5, _5) f(6, _6) f(7, _7) f(8, _8)
|
|
||||||
#define _ban_for_each(f, ...) _ban_concat(_ban_fe_, _ban_count_args(__VA_ARGS__))(f __VA_OPT__(,) __VA_ARGS__)
|
|
||||||
|
|
||||||
#define _ban_fe_comma_0(f)
|
|
||||||
#define _ban_fe_comma_1(f, _0) f(0, _0)
|
|
||||||
#define _ban_fe_comma_2(f, _0, _1) f(0, _0), f(1, _1)
|
|
||||||
#define _ban_fe_comma_3(f, _0, _1, _2) f(0, _0), f(1, _1), f(2, _2)
|
|
||||||
#define _ban_fe_comma_4(f, _0, _1, _2, _3) f(0, _0), f(1, _1), f(2, _2), f(3, _3)
|
|
||||||
#define _ban_fe_comma_5(f, _0, _1, _2, _3, _4) f(0, _0), f(1, _1), f(2, _2), f(3, _3), f(4, _4)
|
|
||||||
#define _ban_fe_comma_6(f, _0, _1, _2, _3, _4, _5) f(0, _0), f(1, _1), f(2, _2), f(3, _3), f(4, _4), f(5, _5)
|
|
||||||
#define _ban_fe_comma_7(f, _0, _1, _2, _3, _4, _5, _6) f(0, _0), f(1, _1), f(2, _2), f(3, _3), f(4, _4), f(5, _5), f(6, _6)
|
|
||||||
#define _ban_fe_comma_8(f, _0, _1, _2, _3, _4, _5, _6, _7) f(0, _0), f(1, _1), f(2, _2), f(3, _3), f(4, _4), f(5, _5), f(6, _6), f(7, _7)
|
|
||||||
#define _ban_fe_comma_9(f, _0, _1, _2, _3, _4, _5, _6, _7, _8) f(0, _0), f(1, _1), f(2, _2), f(3, _3), f(4, _4), f(5, _5), f(6, _6), f(7, _7), f(8, _8)
|
|
||||||
#define _ban_for_each_comma(f, ...) _ban_concat(_ban_fe_comma_, _ban_count_args(__VA_ARGS__))(f __VA_OPT__(,) __VA_ARGS__)
|
|
||||||
|
|
||||||
#define _ban_get_0(a0, ...) a0
|
|
||||||
#define _ban_get_1(a0, a1, ...) a1
|
|
||||||
#define _ban_get_2(a0, a1, a2, ...) a2
|
|
||||||
#define _ban_get_3(a0, a1, a2, a3, ...) a3
|
|
||||||
#define _ban_get_4(a0, a1, a2, a3, a4, ...) a4
|
|
||||||
#define _ban_get_5(a0, a1, a2, a3, a4, a5, ...) a5
|
|
||||||
#define _ban_get_6(a0, a1, a2, a3, a4, a5, a6, ...) a6
|
|
||||||
#define _ban_get_7(a0, a1, a2, a3, a4, a5, a6, a7, ...) a7
|
|
||||||
#define _ban_get_8(a0, a1, a2, a3, a4, a5, a6, a7, a8, ...) a8
|
|
||||||
#define _ban_get_9(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, ...) a9
|
|
||||||
#define _ban_get(n, ...) _ban_concat(_ban_get_, n)(__VA_ARGS__)
|
|
||||||
@@ -36,11 +36,12 @@ namespace BAN::Math
|
|||||||
template<integral T>
|
template<integral T>
|
||||||
inline constexpr T gcd(T a, T b)
|
inline constexpr T gcd(T a, T b)
|
||||||
{
|
{
|
||||||
|
T t;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
T temp = b;
|
t = b;
|
||||||
b = a % b;
|
b = a % b;
|
||||||
a = temp;
|
a = t;
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
@@ -65,36 +66,25 @@ namespace BAN::Math
|
|||||||
return (x & (x - 1)) == 0;
|
return (x & (x - 1)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<integral T> requires(sizeof(T) <= 8)
|
template<BAN::integral T>
|
||||||
inline constexpr T round_up_to_power_of_two(T x)
|
static constexpr bool will_multiplication_overflow(T a, T b)
|
||||||
{
|
{
|
||||||
x--;
|
if (a == 0 || b == 0)
|
||||||
x |= x >> 1;
|
return false;
|
||||||
x |= x >> 2;
|
if ((a > 0) == (b > 0))
|
||||||
x |= x >> 4;
|
return a > BAN::numeric_limits<T>::max() / b;
|
||||||
if constexpr(sizeof(T) >= 2)
|
else
|
||||||
x |= x >> 8;
|
return a < BAN::numeric_limits<T>::min() / b;
|
||||||
if constexpr(sizeof(T) >= 4)
|
|
||||||
x |= x >> 16;
|
|
||||||
if constexpr(sizeof(T) >= 8)
|
|
||||||
x |= x >> 32;
|
|
||||||
return x + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<integral T>
|
template<BAN::integral T>
|
||||||
__attribute__((always_inline))
|
static constexpr bool will_addition_overflow(T a, T b)
|
||||||
inline constexpr bool will_multiplication_overflow(T a, T b)
|
|
||||||
{
|
{
|
||||||
T dummy;
|
if (a > 0 && b > 0)
|
||||||
return __builtin_mul_overflow(a, b, &dummy);
|
return a > BAN::numeric_limits<T>::max() - b;
|
||||||
}
|
if (a < 0 && b < 0)
|
||||||
|
return a < BAN::numeric_limits<T>::min() - b;
|
||||||
template<integral T>
|
return false;
|
||||||
__attribute__((always_inline))
|
|
||||||
inline constexpr bool will_addition_overflow(T a, T b)
|
|
||||||
{
|
|
||||||
T dummy;
|
|
||||||
return __builtin_add_overflow(a, b, &dummy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -108,19 +98,6 @@ namespace BAN::Math
|
|||||||
return sizeof(T) * 8 - __builtin_clzll(x) - 1;
|
return sizeof(T) * 8 - __builtin_clzll(x) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is ugly but my clangd does not like including
|
|
||||||
// intrinsic headers at all
|
|
||||||
#if !defined(__SSE__) || !defined(__SSE2__)
|
|
||||||
#pragma GCC push_options
|
|
||||||
#ifndef __SSE__
|
|
||||||
#pragma GCC target("sse")
|
|
||||||
#endif
|
|
||||||
#ifndef __SSE2__
|
|
||||||
#pragma GCC target("sse2")
|
|
||||||
#endif
|
|
||||||
#define BAN_MATH_POP_OPTIONS
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T floor(T x)
|
inline constexpr T floor(T x)
|
||||||
{
|
{
|
||||||
@@ -182,23 +159,7 @@ namespace BAN::Math
|
|||||||
"jne 1b;"
|
"jne 1b;"
|
||||||
: "+t"(a)
|
: "+t"(a)
|
||||||
: "u"(b)
|
: "u"(b)
|
||||||
: "ax", "cc"
|
: "ax"
|
||||||
);
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<floating_point T>
|
|
||||||
inline constexpr T remainder(T a, T b)
|
|
||||||
{
|
|
||||||
asm(
|
|
||||||
"1:"
|
|
||||||
"fprem1;"
|
|
||||||
"fnstsw %%ax;"
|
|
||||||
"testb $4, %%ah;"
|
|
||||||
"jne 1b;"
|
|
||||||
: "+t"(a)
|
|
||||||
: "u"(b)
|
|
||||||
: "ax", "cc"
|
|
||||||
);
|
);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
@@ -206,7 +167,7 @@ namespace BAN::Math
|
|||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
static T modf(T x, T* iptr)
|
static T modf(T x, T* iptr)
|
||||||
{
|
{
|
||||||
const T frac = BAN::Math::fmod<T>(x, (T)1.0);
|
const T frac = BAN::Math::fmod<T>(x, 1);
|
||||||
*iptr = x - frac;
|
*iptr = x - frac;
|
||||||
return frac;
|
return frac;
|
||||||
}
|
}
|
||||||
@@ -214,15 +175,15 @@ namespace BAN::Math
|
|||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T frexp(T num, int* exp)
|
inline constexpr T frexp(T num, int* exp)
|
||||||
{
|
{
|
||||||
if (num == (T)0.0)
|
if (num == 0.0)
|
||||||
{
|
{
|
||||||
*exp = 0;
|
*exp = 0;
|
||||||
return (T)0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
T e;
|
T _exp;
|
||||||
asm("fxtract" : "+t"(num), "=u"(e));
|
asm("fxtract" : "+t"(num), "=u"(_exp));
|
||||||
*exp = (int)e + 1;
|
*exp = (int)_exp + 1;
|
||||||
return num / (T)2.0;
|
return num / (T)2.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,7 +251,6 @@ namespace BAN::Math
|
|||||||
"fstp %%st(1);"
|
"fstp %%st(1);"
|
||||||
: "+t"(x)
|
: "+t"(x)
|
||||||
);
|
);
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,9 +263,18 @@ namespace BAN::Math
|
|||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T pow(T x, T y)
|
inline constexpr T pow(T x, T y)
|
||||||
{
|
{
|
||||||
if (x == (T)0.0)
|
asm(
|
||||||
return (T)0.0;
|
"fyl2x;"
|
||||||
return exp2<T>(y * log2<T>(x));
|
"fld1;"
|
||||||
|
"fld %%st(1);"
|
||||||
|
"fprem;"
|
||||||
|
"f2xm1;"
|
||||||
|
"faddp;"
|
||||||
|
"fscale;"
|
||||||
|
: "+t"(x), "+u"(y)
|
||||||
|
);
|
||||||
|
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
@@ -341,27 +310,16 @@ namespace BAN::Math
|
|||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T sqrt(T x)
|
inline constexpr T sqrt(T x)
|
||||||
{
|
{
|
||||||
if constexpr(BAN::is_same_v<T, float>)
|
asm("fsqrt" : "+t"(x));
|
||||||
{
|
return x;
|
||||||
using v4sf = float __attribute__((vector_size(16)));
|
|
||||||
return __builtin_ia32_sqrtss((v4sf) { x, 0.0f, 0.0f, 0.0f })[0];
|
|
||||||
}
|
|
||||||
else if constexpr(BAN::is_same_v<T, double>)
|
|
||||||
{
|
|
||||||
using v2df = double __attribute__((vector_size(16)));
|
|
||||||
return __builtin_ia32_sqrtsd((v2df) { x, 0.0 })[0];
|
|
||||||
}
|
|
||||||
else if constexpr(BAN::is_same_v<T, long double>)
|
|
||||||
{
|
|
||||||
asm("fsqrt" : "+t"(x));
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T cbrt(T value)
|
inline constexpr T cbrt(T value)
|
||||||
{
|
{
|
||||||
return pow<T>(value, (T)1.0 / (T)3.0);
|
if (value == 0.0)
|
||||||
|
return 0.0;
|
||||||
|
return pow<T>(value, 1.0 / 3.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
@@ -388,21 +346,30 @@ namespace BAN::Math
|
|||||||
inline constexpr T tan(T x)
|
inline constexpr T tan(T x)
|
||||||
{
|
{
|
||||||
T one, ret;
|
T one, ret;
|
||||||
asm("fptan" : "=t"(one), "=u"(ret) : "0"(x));
|
asm(
|
||||||
|
"fptan"
|
||||||
|
: "=t"(one), "=u"(ret)
|
||||||
|
: "0"(x)
|
||||||
|
);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T atan2(T y, T x)
|
inline constexpr T atan2(T y, T x)
|
||||||
{
|
{
|
||||||
asm("fpatan" : "+t"(x) : "u"(y) : "st(1)");
|
asm(
|
||||||
|
"fpatan"
|
||||||
|
: "+t"(x)
|
||||||
|
: "u"(y)
|
||||||
|
: "st(1)"
|
||||||
|
);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T atan(T x)
|
inline constexpr T atan(T x)
|
||||||
{
|
{
|
||||||
return atan2<T>(x, (T)1.0);
|
return atan2<T>(x, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
@@ -411,10 +378,10 @@ namespace BAN::Math
|
|||||||
if (x == (T)0.0)
|
if (x == (T)0.0)
|
||||||
return (T)0.0;
|
return (T)0.0;
|
||||||
if (x == (T)1.0)
|
if (x == (T)1.0)
|
||||||
return +numbers::pi_v<T> / (T)2.0;
|
return numbers::pi_v<T> / (T)2.0;
|
||||||
if (x == (T)-1.0)
|
if (x == (T)-1.0)
|
||||||
return -numbers::pi_v<T> / (T)2.0;
|
return -numbers::pi_v<T> / (T)2.0;
|
||||||
return (T)2.0 * atan<T>(x / ((T)1.0 + sqrt<T>((T)1.0 - x * x)));
|
return (T)2.0 * atan<T>(x / (T(1.0) + sqrt<T>((T)1.0 - x * x)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
@@ -444,7 +411,7 @@ namespace BAN::Math
|
|||||||
template<floating_point T>
|
template<floating_point T>
|
||||||
inline constexpr T tanh(T x)
|
inline constexpr T tanh(T x)
|
||||||
{
|
{
|
||||||
const T exp_px = exp<T>(+x);
|
const T exp_px = exp<T>(x);
|
||||||
const T exp_nx = exp<T>(-x);
|
const T exp_nx = exp<T>(-x);
|
||||||
return (exp_px - exp_nx) / (exp_px + exp_nx);
|
return (exp_px - exp_nx) / (exp_px + exp_nx);
|
||||||
}
|
}
|
||||||
@@ -473,9 +440,4 @@ namespace BAN::Math
|
|||||||
return sqrt<T>(x * x + y * y);
|
return sqrt<T>(x * x + y * y);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BAN_MATH_POP_OPTIONS
|
|
||||||
#undef BAN_MATH_POP_OPTIONS
|
|
||||||
#pragma GCC pop_options
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,10 @@
|
|||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
#if defined(__is_kernel)
|
#if defined(__is_kernel)
|
||||||
static constexpr void*(*allocator)(size_t) = kmalloc;
|
static constexpr void*(&allocator)(size_t) = kmalloc;
|
||||||
static constexpr void*(*reallocator)(void*, size_t) = nullptr;
|
static constexpr void(&deallocator)(void*) = kfree;
|
||||||
static constexpr void(*deallocator)(void*) = kfree;
|
|
||||||
#else
|
#else
|
||||||
static constexpr void*(*allocator)(size_t) = malloc;
|
static constexpr void*(&allocator)(size_t) = malloc;
|
||||||
static constexpr void*(*reallocator)(void*, size_t) = realloc;
|
static constexpr void(&deallocator)(void*) = free;
|
||||||
static constexpr void(*deallocator)(void*) = free;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#include <BAN/Atomic.h>
|
#include <BAN/Atomic.h>
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
#include <BAN/Hash.h>
|
|
||||||
#include <BAN/Move.h>
|
#include <BAN/Move.h>
|
||||||
#include <BAN/NoCopyMove.h>
|
#include <BAN/NoCopyMove.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
@@ -129,9 +129,14 @@ namespace BAN
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
T* ptr() const { return m_pointer; }
|
T* ptr() { ASSERT(!empty()); return m_pointer; }
|
||||||
T& operator*() const { ASSERT(!empty()); return *ptr(); }
|
const T* ptr() const { ASSERT(!empty()); return m_pointer; }
|
||||||
T* operator->() const { ASSERT(!empty()); return ptr(); }
|
|
||||||
|
T& operator*() { return *ptr(); }
|
||||||
|
const T& operator*() const { return *ptr(); }
|
||||||
|
|
||||||
|
T* operator->() { return ptr(); }
|
||||||
|
const T* operator->() const { return ptr(); }
|
||||||
|
|
||||||
bool operator==(RefPtr other) const { return m_pointer == other.m_pointer; }
|
bool operator==(RefPtr other) const { return m_pointer == other.m_pointer; }
|
||||||
bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; }
|
bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; }
|
||||||
@@ -153,13 +158,4 @@ namespace BAN
|
|||||||
friend class RefPtr;
|
friend class RefPtr;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct hash<RefPtr<T>>
|
|
||||||
{
|
|
||||||
constexpr hash_t operator()(const RefPtr<T>& ptr) const
|
|
||||||
{
|
|
||||||
return hash<T*>()(ptr.ptr());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,66 +21,36 @@ namespace BAN::sort
|
|||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename It>
|
|
||||||
struct partition_pair
|
|
||||||
{
|
|
||||||
It lt;
|
|
||||||
It gt;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename It, typename Comp>
|
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 it1 = begin;
|
||||||
It eq = begin;
|
for (It it2 = begin; it2 != pivot; ++it2)
|
||||||
It gt = end;
|
|
||||||
|
|
||||||
while (eq != gt)
|
|
||||||
{
|
{
|
||||||
if (comp(*eq, *pivot))
|
if (comp(*it2, *pivot))
|
||||||
{
|
{
|
||||||
swap(*eq, *lt);
|
swap(*it1, *it2);
|
||||||
if (pivot == lt)
|
++it1;
|
||||||
pivot = eq;
|
|
||||||
++lt;
|
|
||||||
++eq;
|
|
||||||
}
|
|
||||||
else if (comp(*pivot, *eq))
|
|
||||||
{
|
|
||||||
--gt;
|
|
||||||
swap(*eq, *gt);
|
|
||||||
if (pivot == gt)
|
|
||||||
pivot = eq;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++eq;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { lt, gt };
|
swap(*it1, *pivot);
|
||||||
|
|
||||||
|
return it1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
||||||
void quick_sort(It begin, It end, Comp comp = {})
|
void quick_sort(It begin, It end, Comp comp = {})
|
||||||
{
|
{
|
||||||
while (distance(begin, end) > 1)
|
if (distance(begin, end) <= 1)
|
||||||
{
|
return;
|
||||||
const auto [lt, gt] = detail::partition(begin, end, comp);
|
It mid = detail::partition(begin, end, comp);
|
||||||
if (distance(begin, lt) < distance(gt, end))
|
quick_sort(begin, mid, comp);
|
||||||
{
|
quick_sort(++mid, end, comp);
|
||||||
quick_sort(begin, lt);
|
|
||||||
begin = gt;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
quick_sort(gt, end);
|
|
||||||
end = lt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
template<typename It, typename Comp = less<it_value_type_t<It>>>
|
||||||
@@ -111,28 +81,13 @@ namespace BAN::sort
|
|||||||
template<typename It, typename Comp>
|
template<typename It, typename Comp>
|
||||||
void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp)
|
void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < max_depth; i++)
|
if (distance(begin, end) <= 16)
|
||||||
{
|
return insertion_sort(begin, end, comp);
|
||||||
const size_t len = distance(begin, end);
|
if (max_depth == 0)
|
||||||
if (len <= 1)
|
return heap_sort(begin, end, comp);
|
||||||
return;
|
It mid = detail::partition(begin, end, comp);
|
||||||
if (len <= 16)
|
intro_sort_impl(begin, mid, max_depth - 1, comp);
|
||||||
return insertion_sort(begin, end, comp);
|
intro_sort_impl(++mid, end, max_depth - 1, comp);
|
||||||
|
|
||||||
const auto [lt, gt] = detail::partition(begin, end, comp);
|
|
||||||
if (distance(begin, lt) < distance(gt, end))
|
|
||||||
{
|
|
||||||
intro_sort_impl(begin, lt, max_depth - 1, comp);
|
|
||||||
begin = gt;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
intro_sort_impl(gt, end, max_depth - 1, comp);
|
|
||||||
end = lt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
heap_sort(begin, end, comp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -161,20 +116,27 @@ namespace BAN::sort
|
|||||||
|
|
||||||
template<typename It, size_t radix = 256>
|
template<typename It, size_t radix = 256>
|
||||||
requires is_unsigned_v<it_value_type_t<It>> && (radix > 0 && (radix & (radix - 1)) == 0)
|
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);
|
const size_t len = distance(begin, end);
|
||||||
if (len <= 1)
|
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 mask = radix - 1;
|
||||||
constexpr size_t shift = detail::lsb_index(radix);
|
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)
|
for (It it = begin; it != end; ++it)
|
||||||
counts[(*it >> s) & mask]++;
|
counts[(*it >> s) & mask]++;
|
||||||
|
|
||||||
@@ -184,27 +146,12 @@ namespace BAN::sort
|
|||||||
for (It it = end; it != begin;)
|
for (It it = end; it != begin;)
|
||||||
{
|
{
|
||||||
--it;
|
--it;
|
||||||
storage[--counts[(*it >> s) & mask]] = *it;
|
temp[--counts[(*it >> s) & mask]] = *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
It it = begin;
|
for (size_t j = 0; j < temp.size(); j++)
|
||||||
for (size_t j = 0; j < storage.size(); j++, ++it)
|
*next(begin, j) = temp[j];
|
||||||
*it = storage[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 {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ namespace BAN
|
|||||||
|
|
||||||
value_type* data() const
|
value_type* data() const
|
||||||
{
|
{
|
||||||
|
ASSERT(m_data);
|
||||||
return m_data;
|
return m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +84,7 @@ namespace BAN
|
|||||||
|
|
||||||
Span slice(size_type start, size_type length = ~size_type(0)) const
|
Span slice(size_type start, size_type length = ~size_type(0)) const
|
||||||
{
|
{
|
||||||
|
ASSERT(m_data);
|
||||||
ASSERT(start <= m_size);
|
ASSERT(start <= m_size);
|
||||||
if (length == ~size_type(0))
|
if (length == ~size_type(0))
|
||||||
length = m_size - start;
|
length = m_size - start;
|
||||||
@@ -100,22 +102,3 @@ namespace BAN
|
|||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <BAN/Formatter.h>
|
|
||||||
|
|
||||||
namespace BAN::Formatter
|
|
||||||
{
|
|
||||||
|
|
||||||
template<typename F, typename T>
|
|
||||||
void print_argument(F putc, const Span<T>& span, const ValueFormat& format)
|
|
||||||
{
|
|
||||||
putc('[');
|
|
||||||
for (typename Span<T>::size_type i = 0; i < span.size(); i++)
|
|
||||||
{
|
|
||||||
if (i != 0) putc(',');
|
|
||||||
print_argument(putc, span[i], format);
|
|
||||||
}
|
|
||||||
putc(']');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ namespace BAN
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using value_type = char;
|
|
||||||
using iterator = IteratorSimple<char, String>;
|
using iterator = IteratorSimple<char, String>;
|
||||||
using const_iterator = ConstIteratorSimple<char, String>;
|
using const_iterator = ConstIteratorSimple<char, String>;
|
||||||
static constexpr size_type sso_capacity = 15;
|
static constexpr size_type sso_capacity = 15;
|
||||||
@@ -353,9 +352,10 @@ namespace BAN::Formatter
|
|||||||
{
|
{
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void print_argument(F putc, const String& string, const ValueFormat& format)
|
void print_argument(F putc, const String& string, const ValueFormat&)
|
||||||
{
|
{
|
||||||
print_argument(putc, string.sv(), format);
|
for (String::size_type i = 0; i < string.size(); i++)
|
||||||
|
putc(string[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ namespace BAN
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using value_type = char;
|
|
||||||
using const_iterator = ConstIteratorSimple<char, StringView>;
|
using const_iterator = ConstIteratorSimple<char, StringView>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -247,12 +246,10 @@ namespace BAN::Formatter
|
|||||||
{
|
{
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void print_argument(F putc, const StringView& sv, const ValueFormat& format)
|
void print_argument(F putc, const StringView& sv, const ValueFormat&)
|
||||||
{
|
{
|
||||||
for (StringView::size_type i = 0; i < sv.size(); i++)
|
for (StringView::size_type i = 0; i < sv.size(); i++)
|
||||||
putc(sv[i]);
|
putc(sv[i]);
|
||||||
for (int i = sv.size(); i < format.fill; i++)
|
|
||||||
putc(' ');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,9 +61,6 @@ namespace BAN
|
|||||||
template<typename T> struct is_move_constructible { static constexpr bool value = is_constructible_v<T, T&&>; };
|
template<typename T> struct is_move_constructible { static constexpr bool value = is_constructible_v<T, T&&>; };
|
||||||
template<typename T> inline constexpr bool is_move_constructible_v = is_move_constructible<T>::value;
|
template<typename T> inline constexpr bool is_move_constructible_v = is_move_constructible<T>::value;
|
||||||
|
|
||||||
template<typename T> struct is_trivially_copyable { static constexpr bool value = __is_trivially_copyable(T); };
|
|
||||||
template<typename T> inline constexpr bool is_trivially_copyable_v = is_trivially_copyable<T>::value;
|
|
||||||
|
|
||||||
template<typename T> struct is_integral { static constexpr bool value = requires (T t, T* p, void (*f)(T)) { reinterpret_cast<T>(t); f(0); p + t; }; };
|
template<typename T> struct is_integral { static constexpr bool value = requires (T t, T* p, void (*f)(T)) { reinterpret_cast<T>(t); f(0); p + t; }; };
|
||||||
template<typename T> inline constexpr bool is_integral_v = is_integral<T>::value;
|
template<typename T> inline constexpr bool is_integral_v = is_integral<T>::value;
|
||||||
template<typename T> concept integral = is_integral_v<T>;
|
template<typename T> concept integral = is_integral_v<T>;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
#include <BAN/Hash.h>
|
|
||||||
#include <BAN/NoCopyMove.h>
|
#include <BAN/NoCopyMove.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
@@ -54,12 +53,32 @@ namespace BAN
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
T* ptr() const { return m_pointer; }
|
T& operator*()
|
||||||
T& operator*() const { ASSERT(!empty()); return *ptr(); }
|
{
|
||||||
T* operator->() const { ASSERT(!empty()); return ptr(); }
|
ASSERT(m_pointer);
|
||||||
|
return *m_pointer;
|
||||||
|
}
|
||||||
|
|
||||||
bool empty() const { return m_pointer == nullptr; }
|
const T& operator*() const
|
||||||
explicit operator bool() const { return m_pointer; }
|
{
|
||||||
|
ASSERT(m_pointer);
|
||||||
|
return *m_pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
T* operator->()
|
||||||
|
{
|
||||||
|
ASSERT(m_pointer);
|
||||||
|
return m_pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const T* operator->() const
|
||||||
|
{
|
||||||
|
ASSERT(m_pointer);
|
||||||
|
return m_pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
T* ptr() { return m_pointer; }
|
||||||
|
const T* ptr() const { return m_pointer; }
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
@@ -68,6 +87,8 @@ namespace BAN
|
|||||||
m_pointer = nullptr;
|
m_pointer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator bool() const { return m_pointer != nullptr; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T* m_pointer = nullptr;
|
T* m_pointer = nullptr;
|
||||||
|
|
||||||
@@ -75,13 +96,4 @@ namespace BAN
|
|||||||
friend class UniqPtr;
|
friend class UniqPtr;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct hash<UniqPtr<T>>
|
|
||||||
{
|
|
||||||
constexpr hash_t operator()(const UniqPtr<T>& ptr) const
|
|
||||||
{
|
|
||||||
return hash<T*>()(ptr.ptr());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,16 +126,14 @@ namespace BAN
|
|||||||
Variant(Variant&& other)
|
Variant(Variant&& other)
|
||||||
: m_index(other.m_index)
|
: m_index(other.m_index)
|
||||||
{
|
{
|
||||||
if (other.has_value())
|
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
||||||
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
|
||||||
other.clear();
|
other.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Variant(const Variant& other)
|
Variant(const Variant& other)
|
||||||
: m_index(other.m_index)
|
: m_index(other.m_index)
|
||||||
{
|
{
|
||||||
if (other.has_value())
|
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
||||||
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -159,13 +157,12 @@ namespace BAN
|
|||||||
|
|
||||||
Variant& operator=(Variant&& other)
|
Variant& operator=(Variant&& other)
|
||||||
{
|
{
|
||||||
if (m_index == other.m_index && m_index != invalid_index())
|
if (m_index == other.m_index)
|
||||||
detail::move_assign<Ts...>(m_index, other.m_storage, m_storage);
|
detail::move_assign<Ts...>(m_index, other.m_storage, m_storage);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
if (other.has_value())
|
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
||||||
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
|
||||||
m_index = other.m_index;
|
m_index = other.m_index;
|
||||||
}
|
}
|
||||||
other.clear();
|
other.clear();
|
||||||
@@ -174,13 +171,12 @@ namespace BAN
|
|||||||
|
|
||||||
Variant& operator=(const Variant& other)
|
Variant& operator=(const Variant& other)
|
||||||
{
|
{
|
||||||
if (m_index == other.m_index && m_index != invalid_index())
|
if (m_index == other.m_index)
|
||||||
detail::copy_assign<Ts...>(m_index, other.m_storage, m_storage);
|
detail::copy_assign<Ts...>(m_index, other.m_storage, m_storage);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
if (other.has_value())
|
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
||||||
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
|
|
||||||
m_index = other.m_index;
|
m_index = other.m_index;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
@@ -381,46 +381,19 @@ namespace BAN
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
ErrorOr<void> Vector<T>::ensure_capacity(size_type size)
|
ErrorOr<void> Vector<T>::ensure_capacity(size_type size)
|
||||||
{
|
{
|
||||||
static_assert(alignof(T) <= alignof(max_align_t), "over aligned types not supported");
|
|
||||||
|
|
||||||
if (m_capacity >= size)
|
if (m_capacity >= size)
|
||||||
return {};
|
return {};
|
||||||
|
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
|
||||||
const size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
|
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
|
||||||
|
if (new_data == nullptr)
|
||||||
if constexpr (BAN::is_trivially_copyable_v<T>)
|
return Error::from_errno(ENOMEM);
|
||||||
|
for (size_type i = 0; i < m_size; i++)
|
||||||
{
|
{
|
||||||
if constexpr (BAN::reallocator)
|
new (new_data + i) T(move(m_data[i]));
|
||||||
{
|
m_data[i].~T();
|
||||||
auto* new_data = static_cast<T*>(BAN::reallocator(m_data, new_cap * sizeof(T)));
|
|
||||||
if (new_data == nullptr)
|
|
||||||
return Error::from_errno(ENOMEM);
|
|
||||||
m_data = new_data;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto* new_data = static_cast<T*>(BAN::allocator(new_cap * sizeof(T)));
|
|
||||||
if (new_data == nullptr)
|
|
||||||
return Error::from_errno(ENOMEM);
|
|
||||||
memcpy(new_data, m_data, m_size * sizeof(T));
|
|
||||||
BAN::deallocator(m_data);
|
|
||||||
m_data = new_data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
BAN::deallocator(m_data);
|
||||||
{
|
m_data = new_data;
|
||||||
auto* new_data = static_cast<T*>(BAN::allocator(new_cap * sizeof(T)));
|
|
||||||
if (new_data == nullptr)
|
|
||||||
return Error::from_errno(ENOMEM);
|
|
||||||
for (size_type i = 0; i < m_size; i++)
|
|
||||||
{
|
|
||||||
new (new_data + i) T(move(m_data[i]));
|
|
||||||
m_data[i].~T();
|
|
||||||
}
|
|
||||||
BAN::deallocator(m_data);
|
|
||||||
m_data = new_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_capacity = new_cap;
|
m_capacity = new_cap;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ To change the bootloader you can set environment variable BANAN\_BOOTLOADER; sup
|
|||||||
|
|
||||||
To run with UEFI set environment variable BANAN\_UEFI\_BOOT=1. You will also have to set OVMF\_PATH to the correct OVMF (default */usr/share/ovmf/x64/OVMF.fd*).
|
To run with UEFI set environment variable BANAN\_UEFI\_BOOT=1. You will also have to set OVMF\_PATH to the correct OVMF (default */usr/share/ovmf/x64/OVMF.fd*).
|
||||||
|
|
||||||
To build an image with no physical root filesystem, but an initrd set environment variable BANAN\_INITRD=1. This can be used when testing on hardware with unsupported USB controller. If BANAN\_INITRD is set to a value larger than 1, initrd will be gzip compressed.
|
To build an image with no physical root filesystem, but an initrd set environment variable BANAN\_INITRD=1. This can be used when testing on hardware with unsupported USB controller.
|
||||||
|
|
||||||
If you have corrupted your disk image or want to create new one, you can either manually delete *build/banan-os.img* and build system will automatically create you a new one or you can run the following command.
|
If you have corrupted your disk image or want to create new one, you can either manually delete *build/banan-os.img* and build system will automatically create you a new one or you can run the following command.
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
Binary file not shown.
@@ -11,7 +11,6 @@ set(KERNEL_SOURCES
|
|||||||
kernel/Audio/Controller.cpp
|
kernel/Audio/Controller.cpp
|
||||||
kernel/Audio/HDAudio/AudioFunctionGroup.cpp
|
kernel/Audio/HDAudio/AudioFunctionGroup.cpp
|
||||||
kernel/Audio/HDAudio/Controller.cpp
|
kernel/Audio/HDAudio/Controller.cpp
|
||||||
kernel/Banos.cpp
|
|
||||||
kernel/BootInfo.cpp
|
kernel/BootInfo.cpp
|
||||||
kernel/CPUID.cpp
|
kernel/CPUID.cpp
|
||||||
kernel/Credentials.cpp
|
kernel/Credentials.cpp
|
||||||
@@ -26,7 +25,6 @@ set(KERNEL_SOURCES
|
|||||||
kernel/Epoll.cpp
|
kernel/Epoll.cpp
|
||||||
kernel/Errors.cpp
|
kernel/Errors.cpp
|
||||||
kernel/FS/DevFS/FileSystem.cpp
|
kernel/FS/DevFS/FileSystem.cpp
|
||||||
kernel/FS/EventFD.cpp
|
|
||||||
kernel/FS/Ext2/FileSystem.cpp
|
kernel/FS/Ext2/FileSystem.cpp
|
||||||
kernel/FS/Ext2/Inode.cpp
|
kernel/FS/Ext2/Inode.cpp
|
||||||
kernel/FS/FAT/FileSystem.cpp
|
kernel/FS/FAT/FileSystem.cpp
|
||||||
@@ -52,7 +50,6 @@ set(KERNEL_SOURCES
|
|||||||
kernel/InterruptController.cpp
|
kernel/InterruptController.cpp
|
||||||
kernel/kernel.cpp
|
kernel/kernel.cpp
|
||||||
kernel/Lock/SpinLock.cpp
|
kernel/Lock/SpinLock.cpp
|
||||||
kernel/Memory/ByteRingBuffer.cpp
|
|
||||||
kernel/Memory/DMARegion.cpp
|
kernel/Memory/DMARegion.cpp
|
||||||
kernel/Memory/FileBackedRegion.cpp
|
kernel/Memory/FileBackedRegion.cpp
|
||||||
kernel/Memory/Heap.cpp
|
kernel/Memory/Heap.cpp
|
||||||
@@ -122,7 +119,6 @@ set(KERNEL_SOURCES
|
|||||||
kernel/USB/USBManager.cpp
|
kernel/USB/USBManager.cpp
|
||||||
kernel/USB/XHCI/Controller.cpp
|
kernel/USB/XHCI/Controller.cpp
|
||||||
kernel/USB/XHCI/Device.cpp
|
kernel/USB/XHCI/Device.cpp
|
||||||
kernel/UserCopy.cpp
|
|
||||||
icxxabi.cpp
|
icxxabi.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -141,8 +137,6 @@ if("${BANAN_ARCH}" STREQUAL "x86_64")
|
|||||||
arch/x86_64/Signal.S
|
arch/x86_64/Signal.S
|
||||||
arch/x86_64/Syscall.S
|
arch/x86_64/Syscall.S
|
||||||
arch/x86_64/Thread.S
|
arch/x86_64/Thread.S
|
||||||
arch/x86_64/User.S
|
|
||||||
arch/x86_64/Yield.S
|
|
||||||
)
|
)
|
||||||
elseif("${BANAN_ARCH}" STREQUAL "i686")
|
elseif("${BANAN_ARCH}" STREQUAL "i686")
|
||||||
set(KERNEL_SOURCES
|
set(KERNEL_SOURCES
|
||||||
@@ -153,8 +147,6 @@ elseif("${BANAN_ARCH}" STREQUAL "i686")
|
|||||||
arch/i686/Signal.S
|
arch/i686/Signal.S
|
||||||
arch/i686/Syscall.S
|
arch/i686/Syscall.S
|
||||||
arch/i686/Thread.S
|
arch/i686/Thread.S
|
||||||
arch/i686/User.S
|
|
||||||
arch/i686/Yield.S
|
|
||||||
)
|
)
|
||||||
else()
|
else()
|
||||||
message(FATAL_ERROR "unsupported architecure ${BANAN_ARCH}")
|
message(FATAL_ERROR "unsupported architecure ${BANAN_ARCH}")
|
||||||
@@ -170,7 +162,10 @@ set(BAN_SOURCES
|
|||||||
set(KLIBC_SOURCES
|
set(KLIBC_SOURCES
|
||||||
klibc/ctype.cpp
|
klibc/ctype.cpp
|
||||||
klibc/string.cpp
|
klibc/string.cpp
|
||||||
klibc/arch/${BANAN_ARCH}/string.S
|
|
||||||
|
# Ehhh don't do this but for now libc uses the same stuff kernel can use
|
||||||
|
# This won't work after libc starts using sse implemetations tho
|
||||||
|
../userspace/libraries/LibC/arch/${BANAN_ARCH}/string.S
|
||||||
)
|
)
|
||||||
|
|
||||||
set(LIBDEFLATE_SOURCE
|
set(LIBDEFLATE_SOURCE
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <kernel/BootInfo.h>
|
#include <kernel/BootInfo.h>
|
||||||
#include <kernel/CPUID.h>
|
#include <kernel/CPUID.h>
|
||||||
#include <kernel/Lock/SpinLock.h>
|
#include <kernel/Lock/SpinLock.h>
|
||||||
#include <kernel/Memory/Heap.h>
|
#include <kernel/Memory/kmalloc.h>
|
||||||
#include <kernel/Memory/PageTable.h>
|
#include <kernel/Memory/PageTable.h>
|
||||||
|
|
||||||
extern uint8_t g_kernel_start[];
|
extern uint8_t g_kernel_start[];
|
||||||
@@ -16,16 +16,11 @@ extern uint8_t g_kernel_writable_end[];
|
|||||||
extern uint8_t g_userspace_start[];
|
extern uint8_t g_userspace_start[];
|
||||||
extern uint8_t g_userspace_end[];
|
extern uint8_t g_userspace_end[];
|
||||||
|
|
||||||
extern uint64_t g_boot_fast_page_pt[];
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
SpinLock PageTable::s_fast_page_lock;
|
SpinLock PageTable::s_fast_page_lock;
|
||||||
|
|
||||||
constexpr uint64_t s_page_flag_mask = 0x8000000000000FFF;
|
|
||||||
constexpr uint64_t s_page_addr_mask = ~s_page_flag_mask;
|
|
||||||
|
|
||||||
static PageTable* s_kernel = nullptr;
|
static PageTable* s_kernel = nullptr;
|
||||||
static bool s_has_nxe = false;
|
static bool s_has_nxe = false;
|
||||||
static bool s_has_pge = false;
|
static bool s_has_pge = false;
|
||||||
@@ -33,46 +28,6 @@ namespace Kernel
|
|||||||
|
|
||||||
static paddr_t s_global_pdpte = 0;
|
static paddr_t s_global_pdpte = 0;
|
||||||
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void unallocate_page(paddr_t paddr)
|
|
||||||
{
|
|
||||||
Heap::get().release_page(paddr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t read_entry_from_table(paddr_t paddr, uint16_t entry)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline PageTable::flags_t parse_flags(uint64_t entry)
|
static inline PageTable::flags_t parse_flags(uint64_t entry)
|
||||||
{
|
{
|
||||||
using Flags = PageTable::Flags;
|
using Flags = PageTable::Flags;
|
||||||
@@ -91,22 +46,31 @@ namespace Kernel
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::initialize_fast_page()
|
void PageTable::initialize_pre_heap()
|
||||||
{
|
|
||||||
s_fast_page_pt = g_boot_fast_page_pt;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void detect_cpu_features()
|
|
||||||
{
|
{
|
||||||
if (CPUID::has_nxe())
|
if (CPUID::has_nxe())
|
||||||
s_has_nxe = true;
|
s_has_nxe = true;
|
||||||
|
|
||||||
if (CPUID::has_pge())
|
if (CPUID::has_pge())
|
||||||
s_has_pge = true;
|
s_has_pge = true;
|
||||||
|
|
||||||
if (CPUID::has_pat())
|
if (CPUID::has_pat())
|
||||||
s_has_pat = true;
|
s_has_pat = true;
|
||||||
|
|
||||||
|
ASSERT(s_kernel == nullptr);
|
||||||
|
s_kernel = new PageTable();
|
||||||
|
ASSERT(s_kernel);
|
||||||
|
|
||||||
|
s_kernel->initialize_kernel();
|
||||||
|
s_kernel->initial_load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::enable_cpu_features()
|
void PageTable::initialize_post_heap()
|
||||||
|
{
|
||||||
|
// NOTE: this is no-op as our 32 bit target does not use hhdm
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageTable::initial_load()
|
||||||
{
|
{
|
||||||
if (s_has_nxe)
|
if (s_has_nxe)
|
||||||
{
|
{
|
||||||
@@ -147,49 +111,8 @@ namespace Kernel
|
|||||||
"movl %%eax, %%cr0;"
|
"movl %%eax, %%cr0;"
|
||||||
::: "rax"
|
::: "rax"
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
void PageTable::initialize_and_load()
|
load();
|
||||||
{
|
|
||||||
detect_cpu_features();
|
|
||||||
enable_cpu_features();
|
|
||||||
|
|
||||||
ASSERT(s_kernel == nullptr);
|
|
||||||
s_kernel = new PageTable();
|
|
||||||
ASSERT(s_kernel);
|
|
||||||
|
|
||||||
s_kernel->m_highest_paging_struct = allocate_zeroed_page_aligned_page();
|
|
||||||
s_kernel->map_kernel_memory();
|
|
||||||
|
|
||||||
s_global_pdpte = read_entry_from_table(s_kernel->m_highest_paging_struct, 3);
|
|
||||||
|
|
||||||
// update fast page pt
|
|
||||||
{
|
|
||||||
constexpr vaddr_t vaddr = fast_page();
|
|
||||||
constexpr uint16_t pdpte = (vaddr >> 30) & 0x1FF;
|
|
||||||
constexpr uint16_t pde = (vaddr >> 21) & 0x1FF;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
const paddr_t paddr = allocate_zeroed_page_aligned_page();
|
|
||||||
write_entry_to_table(table_paddr, entry, paddr | flags);
|
|
||||||
return paddr;
|
|
||||||
};
|
|
||||||
|
|
||||||
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_kernel->load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PageTable& PageTable::kernel()
|
PageTable& PageTable::kernel()
|
||||||
@@ -203,89 +126,122 @@ namespace Kernel
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::map_kernel_memory()
|
static uint64_t* allocate_zeroed_page_aligned_page()
|
||||||
{
|
{
|
||||||
|
void* page = kmalloc(PAGE_SIZE, PAGE_SIZE, true);
|
||||||
|
ASSERT(page);
|
||||||
|
memset(page, 0, PAGE_SIZE);
|
||||||
|
return (uint64_t*)page;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static paddr_t V2P(const T vaddr)
|
||||||
|
{
|
||||||
|
return (vaddr_t)vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static vaddr_t P2V(const T paddr)
|
||||||
|
{
|
||||||
|
return (paddr_t)paddr - g_boot_info.kernel_paddr + KERNEL_OFFSET;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageTable::initialize_kernel()
|
||||||
|
{
|
||||||
|
ASSERT(s_global_pdpte == 0);
|
||||||
|
s_global_pdpte = V2P(allocate_zeroed_page_aligned_page());
|
||||||
|
|
||||||
|
map_kernel_memory();
|
||||||
|
|
||||||
|
prepare_fast_page();
|
||||||
|
|
||||||
// Map (phys_kernel_start -> phys_kernel_end) to (virt_kernel_start -> virt_kernel_end)
|
// 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);
|
ASSERT((vaddr_t)g_kernel_start % PAGE_SIZE == 0);
|
||||||
map_range_at(
|
map_range_at(
|
||||||
kernel_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
|
V2P(g_kernel_start),
|
||||||
kernel_start,
|
(vaddr_t)g_kernel_start,
|
||||||
g_kernel_end - g_kernel_start,
|
g_kernel_end - g_kernel_start,
|
||||||
Flags::Present
|
Flags::Present
|
||||||
);
|
);
|
||||||
|
|
||||||
// Map executable kernel memory as executable
|
// Map executable kernel memory as executable
|
||||||
const vaddr_t kernel_execute_start = reinterpret_cast<vaddr_t>(g_kernel_execute_start);
|
|
||||||
map_range_at(
|
map_range_at(
|
||||||
kernel_execute_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
|
V2P(g_kernel_execute_start),
|
||||||
kernel_execute_start,
|
(vaddr_t)g_kernel_execute_start,
|
||||||
g_kernel_execute_end - g_kernel_execute_start,
|
g_kernel_execute_end - g_kernel_execute_start,
|
||||||
Flags::Execute | Flags::Present
|
Flags::Execute | Flags::Present
|
||||||
);
|
);
|
||||||
|
|
||||||
// Map writable kernel memory as writable
|
// Map writable kernel memory as writable
|
||||||
const vaddr_t kernel_writable_start = reinterpret_cast<vaddr_t>(g_kernel_writable_start);
|
|
||||||
map_range_at(
|
map_range_at(
|
||||||
kernel_writable_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
|
V2P(g_kernel_writable_start),
|
||||||
kernel_writable_start,
|
(vaddr_t)g_kernel_writable_start,
|
||||||
g_kernel_writable_end - g_kernel_writable_start,
|
g_kernel_writable_end - g_kernel_writable_start,
|
||||||
Flags::ReadWrite | Flags::Present
|
Flags::ReadWrite | Flags::Present
|
||||||
);
|
);
|
||||||
|
|
||||||
// Map userspace memory
|
// Map userspace memory
|
||||||
const vaddr_t kernel_userspace_start = reinterpret_cast<vaddr_t>(g_userspace_start);
|
|
||||||
map_range_at(
|
map_range_at(
|
||||||
kernel_userspace_start - KERNEL_OFFSET + g_boot_info.kernel_paddr,
|
V2P(g_userspace_start),
|
||||||
kernel_userspace_start,
|
(vaddr_t)g_userspace_start,
|
||||||
g_userspace_end - g_userspace_start,
|
g_userspace_end - g_userspace_start,
|
||||||
Flags::Execute | Flags::UserSupervisor | Flags::Present
|
Flags::Execute | Flags::UserSupervisor | Flags::Present
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PageTable::prepare_fast_page()
|
||||||
|
{
|
||||||
|
constexpr uint64_t pdpte = (fast_page() >> 30) & 0x1FF;
|
||||||
|
constexpr uint64_t pde = (fast_page() >> 21) & 0x1FF;
|
||||||
|
|
||||||
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
|
ASSERT(pdpt[pdpte] & Flags::Present);
|
||||||
|
|
||||||
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte]) & PAGE_ADDR_MASK);
|
||||||
|
ASSERT(!(pd[pde] & Flags::Present));
|
||||||
|
pd[pde] = V2P(allocate_zeroed_page_aligned_page()) | Flags::ReadWrite | Flags::Present;
|
||||||
|
}
|
||||||
|
|
||||||
void PageTable::map_fast_page(paddr_t paddr)
|
void PageTable::map_fast_page(paddr_t paddr)
|
||||||
{
|
{
|
||||||
map_fast_page(0, paddr);
|
ASSERT(s_kernel);
|
||||||
|
ASSERT(paddr);
|
||||||
|
ASSERT(paddr % PAGE_SIZE == 0);
|
||||||
|
|
||||||
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
|
|
||||||
|
constexpr uint64_t pdpte = (fast_page() >> 30) & 0x1FF;
|
||||||
|
constexpr uint64_t pde = (fast_page() >> 21) & 0x1FF;
|
||||||
|
constexpr uint64_t pte = (fast_page() >> 12) & 0x1FF;
|
||||||
|
|
||||||
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(s_kernel->m_highest_paging_struct));
|
||||||
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
|
uint64_t* pt = reinterpret_cast<uint64_t*>(P2V(pd[pde] & PAGE_ADDR_MASK));
|
||||||
|
|
||||||
|
ASSERT(!(pt[pte] & Flags::Present));
|
||||||
|
pt[pte] = paddr | Flags::ReadWrite | Flags::Present;
|
||||||
|
|
||||||
|
invalidate(fast_page(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::unmap_fast_page()
|
void PageTable::unmap_fast_page()
|
||||||
{
|
{
|
||||||
unmap_fast_page(0);
|
ASSERT(s_kernel);
|
||||||
}
|
|
||||||
|
|
||||||
void* PageTable::map_fast_page(size_t index, paddr_t paddr)
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
||||||
{
|
|
||||||
ASSERT(paddr && paddr % PAGE_SIZE == 0);
|
|
||||||
|
|
||||||
ASSERT(index < 512);
|
constexpr uint64_t pdpte = (fast_page() >> 30) & 0x1FF;
|
||||||
ASSERT(s_fast_page_pt);
|
constexpr uint64_t pde = (fast_page() >> 21) & 0x1FF;
|
||||||
|
constexpr uint64_t pte = (fast_page() >> 12) & 0x1FF;
|
||||||
|
|
||||||
if (index < reserved_fast_pages)
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(s_kernel->m_highest_paging_struct));
|
||||||
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
else
|
uint64_t* pt = reinterpret_cast<uint64_t*>(P2V(pd[pde] & PAGE_ADDR_MASK));
|
||||||
ASSERT(Processor::get_interrupt_state() == InterruptState::Disabled);
|
|
||||||
|
|
||||||
ASSERT(!(s_fast_page_pt[index] & Flags::Present));
|
ASSERT(pt[pte] & Flags::Present);
|
||||||
s_fast_page_pt[index] = paddr | Flags::ReadWrite | Flags::Present;
|
pt[pte] = 0;
|
||||||
|
|
||||||
void* address = reinterpret_cast<void*>(fast_page() + index * PAGE_SIZE);
|
invalidate(fast_page(), false);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
|
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
|
||||||
@@ -294,63 +250,63 @@ namespace Kernel
|
|||||||
PageTable* page_table = new PageTable;
|
PageTable* page_table = new PageTable;
|
||||||
if (page_table == nullptr)
|
if (page_table == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
|
page_table->map_kernel_memory();
|
||||||
page_table->m_highest_paging_struct = allocate_zeroed_page_aligned_page();
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
return page_table;
|
return page_table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PageTable::map_kernel_memory()
|
||||||
|
{
|
||||||
|
ASSERT(s_kernel);
|
||||||
|
ASSERT(s_global_pdpte);
|
||||||
|
|
||||||
|
ASSERT(m_highest_paging_struct == 0);
|
||||||
|
m_highest_paging_struct = V2P(kmalloc(32, 32, true));
|
||||||
|
ASSERT(m_highest_paging_struct);
|
||||||
|
|
||||||
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
|
pdpt[0] = 0;
|
||||||
|
pdpt[1] = 0;
|
||||||
|
pdpt[2] = 0;
|
||||||
|
pdpt[3] = s_global_pdpte | Flags::Present;
|
||||||
|
static_assert(KERNEL_OFFSET == 0xC0000000);
|
||||||
|
}
|
||||||
|
|
||||||
PageTable::~PageTable()
|
PageTable::~PageTable()
|
||||||
{
|
{
|
||||||
if (m_highest_paging_struct == 0)
|
if (m_highest_paging_struct == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const uint64_t pdpt = m_highest_paging_struct;
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
|
|
||||||
for (uint32_t pdpte = 0; pdpte < 3; pdpte++)
|
for (uint32_t pdpte = 0; pdpte < 3; pdpte++)
|
||||||
{
|
{
|
||||||
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
|
if (!(pdpt[pdpte] & Flags::Present))
|
||||||
if (!(pd & Flags::Present))
|
|
||||||
continue;
|
continue;
|
||||||
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
for (uint32_t pde = 0; pde < 512; pde++)
|
for (uint32_t pde = 0; pde < 512; pde++)
|
||||||
{
|
{
|
||||||
const uint64_t pt = read_entry_from_table(pd, pde);
|
if (!(pd[pde] & Flags::Present))
|
||||||
if (!(pt & Flags::Present))
|
|
||||||
continue;
|
continue;
|
||||||
unallocate_page(pt & s_page_addr_mask);
|
kfree(reinterpret_cast<uint64_t*>(P2V(pd[pde] & PAGE_ADDR_MASK)));
|
||||||
}
|
}
|
||||||
unallocate_page(pd & s_page_addr_mask);
|
kfree(pd);
|
||||||
}
|
}
|
||||||
unallocate_page(m_highest_paging_struct);
|
kfree(pdpt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::load()
|
void PageTable::load()
|
||||||
{
|
{
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
ASSERT(m_highest_paging_struct < 0x100000000);
|
ASSERT(m_highest_paging_struct < 0x100000000);
|
||||||
asm volatile("movl %0, %%cr3" :: "r"(static_cast<uint32_t>(m_highest_paging_struct)));
|
const uint32_t pdpt_lo = m_highest_paging_struct;
|
||||||
|
asm volatile("movl %0, %%cr3" :: "r"(pdpt_lo));
|
||||||
Processor::set_current_page_table(this);
|
Processor::set_current_page_table(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::invalidate_range(vaddr_t vaddr, size_t pages, bool send_smp_message)
|
void PageTable::invalidate(vaddr_t vaddr, bool send_smp_message)
|
||||||
{
|
{
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
asm volatile("invlpg (%0)" :: "r"(vaddr) : "memory");
|
||||||
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));
|
|
||||||
|
|
||||||
if (send_smp_message)
|
if (send_smp_message)
|
||||||
{
|
{
|
||||||
@@ -358,42 +314,13 @@ namespace Kernel
|
|||||||
.type = Processor::SMPMessage::Type::FlushTLB,
|
.type = Processor::SMPMessage::Type::FlushTLB,
|
||||||
.flush_tlb = {
|
.flush_tlb = {
|
||||||
.vaddr = vaddr,
|
.vaddr = vaddr,
|
||||||
.page_count = pages,
|
.page_count = 1
|
||||||
.page_table = vaddr < KERNEL_OFFSET ? this : nullptr,
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::invalidate_full_address_space(bool global)
|
void PageTable::unmap_page(vaddr_t vaddr, bool send_smp_message)
|
||||||
{
|
|
||||||
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);
|
ASSERT(vaddr);
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
@@ -412,14 +339,12 @@ namespace Kernel
|
|||||||
if (is_page_free(vaddr))
|
if (is_page_free(vaddr))
|
||||||
Kernel::panic("trying to unmap unmapped page 0x{H}", vaddr);
|
Kernel::panic("trying to unmap unmapped page 0x{H}", vaddr);
|
||||||
|
|
||||||
const uint64_t pdpt = m_highest_paging_struct;
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
const uint64_t pt = read_entry_from_table(pd, pde);
|
uint64_t* pt = reinterpret_cast<uint64_t*>(P2V(pd[pde] & PAGE_ADDR_MASK));
|
||||||
|
|
||||||
const uint64_t old_entry = write_entry_to_table(pt, pte, 0);
|
pt[pte] = 0;
|
||||||
|
invalidate(vaddr, send_smp_message);
|
||||||
if (invalidate && (old_entry & s_page_addr_mask))
|
|
||||||
invalidate_page(vaddr, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::unmap_range(vaddr_t vaddr, size_t size)
|
void PageTable::unmap_range(vaddr_t vaddr, size_t size)
|
||||||
@@ -431,10 +356,17 @@ namespace Kernel
|
|||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
for (vaddr_t page = 0; page < page_count; page++)
|
for (vaddr_t page = 0; page < page_count; page++)
|
||||||
unmap_page(vaddr + page * PAGE_SIZE, false);
|
unmap_page(vaddr + page * PAGE_SIZE, false);
|
||||||
invalidate_range(vaddr, page_count, true);
|
|
||||||
|
Processor::broadcast_smp_message({
|
||||||
|
.type = Processor::SMPMessage::Type::FlushTLB,
|
||||||
|
.flush_tlb = {
|
||||||
|
.vaddr = vaddr,
|
||||||
|
.page_count = page_count
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags, MemoryType memory_type, bool invalidate)
|
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags, MemoryType memory_type, bool send_smp_message)
|
||||||
{
|
{
|
||||||
ASSERT(vaddr);
|
ASSERT(vaddr);
|
||||||
ASSERT(vaddr != fast_page());
|
ASSERT(vaddr != fast_page());
|
||||||
@@ -469,32 +401,25 @@ namespace Kernel
|
|||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
const uint64_t pdpt = m_highest_paging_struct;
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(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);
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
if (!(pd & Flags::Present))
|
if ((pd[pde] & uwr_flags) != uwr_flags)
|
||||||
{
|
{
|
||||||
pd = allocate_zeroed_page_aligned_page();
|
if (!(pd[pde] & Flags::Present))
|
||||||
pd |= Flags::Present;
|
pd[pde] = V2P(allocate_zeroed_page_aligned_page());
|
||||||
write_entry_to_table(pdpt, pdpte, pd);
|
pd[pde] |= uwr_flags;
|
||||||
}
|
|
||||||
|
|
||||||
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 (!(flags & Flags::Present))
|
if (!(flags & Flags::Present))
|
||||||
uwr_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 = reinterpret_cast<uint64_t*>(P2V(pd[pde] & PAGE_ADDR_MASK));
|
||||||
|
pt[pte] = paddr | uwr_flags | extra_flags;
|
||||||
|
|
||||||
if (invalidate && (old_entry & s_page_addr_mask))
|
invalidate(vaddr, send_smp_message);
|
||||||
invalidate_page(vaddr, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageTable::map_range_at(paddr_t paddr, vaddr_t vaddr, size_t size, flags_t flags, MemoryType memory_type)
|
void PageTable::map_range_at(paddr_t paddr, vaddr_t vaddr, size_t size, flags_t flags, MemoryType memory_type)
|
||||||
@@ -508,54 +433,14 @@ namespace Kernel
|
|||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
for (size_t page = 0; page < page_count; page++)
|
for (size_t page = 0; page < page_count; page++)
|
||||||
map_page_at(paddr + page * PAGE_SIZE, vaddr + page * PAGE_SIZE, flags, memory_type, false);
|
map_page_at(paddr + page * PAGE_SIZE, vaddr + page * PAGE_SIZE, flags, memory_type, false);
|
||||||
invalidate_range(vaddr, page_count, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PageTable::remove_writable_from_range(vaddr_t vaddr, size_t size)
|
Processor::broadcast_smp_message({
|
||||||
{
|
.type = Processor::SMPMessage::Type::FlushTLB,
|
||||||
ASSERT(vaddr);
|
.flush_tlb = {
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
.vaddr = vaddr,
|
||||||
|
.page_count = page_count
|
||||||
uint32_t pdpte = (vaddr >> 30) & 0x1FF;
|
|
||||||
uint32_t pde = (vaddr >> 21) & 0x1FF;
|
|
||||||
uint32_t pte = (vaddr >> 12) & 0x1FF;
|
|
||||||
|
|
||||||
const uint32_t e_pdpte = ((vaddr + size - 1) >> 30) & 0x1FF;
|
|
||||||
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 _1(s_fast_page_lock);
|
|
||||||
|
|
||||||
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, 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));
|
|
||||||
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));
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t PageTable::get_page_data(vaddr_t vaddr) const
|
uint64_t PageTable::get_page_data(vaddr_t vaddr) const
|
||||||
@@ -568,17 +453,19 @@ namespace Kernel
|
|||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
const uint64_t pdpt = m_highest_paging_struct;
|
uint64_t* pdpt = (uint64_t*)P2V(m_highest_paging_struct);
|
||||||
|
if (!(pdpt[pdpte] & Flags::Present))
|
||||||
const uint64_t pd = read_entry_from_table(pdpt, pdpte);
|
|
||||||
if (!(pd & Flags::Present))
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
const uint64_t pt = read_entry_from_table(pd, pde);
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
||||||
if (!(pt & Flags::Present))
|
if (!(pd[pde] & Flags::Present))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return read_entry_from_table(pt, pte);
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
||||||
|
if (!(pt[pte] & Flags::Used))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return pt[pte];
|
||||||
}
|
}
|
||||||
|
|
||||||
PageTable::flags_t PageTable::get_page_flags(vaddr_t vaddr) const
|
PageTable::flags_t PageTable::get_page_flags(vaddr_t vaddr) const
|
||||||
@@ -588,7 +475,8 @@ namespace Kernel
|
|||||||
|
|
||||||
paddr_t PageTable::physical_address_of(vaddr_t vaddr) const
|
paddr_t PageTable::physical_address_of(vaddr_t vaddr) const
|
||||||
{
|
{
|
||||||
return get_page_data(vaddr) & s_page_addr_mask;
|
uint64_t page_data = get_page_data(vaddr);
|
||||||
|
return (page_data & PAGE_ADDR_MASK) & ~(1ull << 63);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageTable::is_page_free(vaddr_t vaddr) const
|
bool PageTable::is_page_free(vaddr_t vaddr) const
|
||||||
@@ -609,24 +497,35 @@ namespace Kernel
|
|||||||
return true;
|
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);
|
SpinLockGuard _(m_lock);
|
||||||
ASSERT(is_page_free(vaddr));
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
map_page_at(0, vaddr, Flags::Reserved, MemoryType::Normal, false);
|
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)
|
if (size_t rem = bytes % PAGE_SIZE)
|
||||||
bytes += PAGE_SIZE - rem;
|
bytes += PAGE_SIZE - rem;
|
||||||
ASSERT(vaddr % PAGE_SIZE == 0);
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
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)
|
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
|
||||||
reserve_page(vaddr + offset);
|
reserve_page(vaddr + offset, true, false);
|
||||||
|
Processor::broadcast_smp_message({
|
||||||
|
.type = Processor::SMPMessage::Type::FlushTLB,
|
||||||
|
.flush_tlb = {
|
||||||
|
.vaddr = vaddr,
|
||||||
|
.page_count = bytes / PAGE_SIZE,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
||||||
@@ -638,73 +537,61 @@ namespace Kernel
|
|||||||
if (size_t rem = last_address % PAGE_SIZE)
|
if (size_t rem = last_address % PAGE_SIZE)
|
||||||
last_address -= rem;
|
last_address -= rem;
|
||||||
|
|
||||||
uint32_t pdpte = (first_address >> 30) & 0x1FF;
|
const uint32_t s_pdpte = (first_address >> 30) & 0x1FF;
|
||||||
uint32_t pde = (first_address >> 21) & 0x1FF;
|
const uint32_t s_pde = (first_address >> 21) & 0x1FF;
|
||||||
uint32_t pte = (first_address >> 12) & 0x1FF;
|
const uint32_t s_pte = (first_address >> 12) & 0x1FF;
|
||||||
|
|
||||||
const uint32_t e_pdpte = ((last_address - 1) >> 30) & 0x1FF;
|
const uint32_t e_pdpte = (last_address >> 30) & 0x1FF;
|
||||||
const uint32_t e_pde = ((last_address - 1) >> 21) & 0x1FF;
|
const uint32_t e_pde = (last_address >> 21) & 0x1FF;
|
||||||
const uint32_t e_pte = ((last_address - 1) >> 12) & 0x1FF;
|
const uint32_t e_pte = (last_address >> 12) & 0x1FF;
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
auto state = s_fast_page_lock.lock();
|
|
||||||
|
|
||||||
// Try to find free page that can be mapped without
|
// Try to find free page that can be mapped without
|
||||||
// allocations (page table with unused entries)
|
// allocations (page table with unused entries)
|
||||||
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, m_highest_paging_struct));
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
for (; pdpte <= e_pdpte; pdpte++)
|
for (uint32_t pdpte = s_pdpte; pdpte < 4; pdpte++)
|
||||||
{
|
{
|
||||||
|
if (pdpte > e_pdpte)
|
||||||
|
break;
|
||||||
if (!(pdpt[pdpte] & Flags::Present))
|
if (!(pdpt[pdpte] & Flags::Present))
|
||||||
continue;
|
continue;
|
||||||
const uint64_t* pd = static_cast<uint64_t*>(map_fast_page(1, pdpt[pdpte] & s_page_addr_mask));
|
uint64_t* pd = reinterpret_cast<uint64_t*>(P2V(pdpt[pdpte] & PAGE_ADDR_MASK));
|
||||||
for (; pde < 512; pde++)
|
for (uint32_t pde = s_pde; pde < 512; pde++)
|
||||||
{
|
{
|
||||||
if (pdpte == e_pdpte && pde > e_pde)
|
if (pdpte == e_pdpte && pde > e_pde)
|
||||||
break;
|
break;
|
||||||
if (!(pd[pde] & Flags::Present))
|
if (!(pd[pde] & Flags::Present))
|
||||||
continue;
|
continue;
|
||||||
const uint64_t* pt = static_cast<uint64_t*>(map_fast_page(2, pd[pde] & s_page_addr_mask));
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
||||||
for (; pte < 512; pte++)
|
for (uint32_t pte = s_pte; pte < 512; pte++)
|
||||||
{
|
{
|
||||||
if (pdpte == e_pdpte && pde == e_pde && pte > e_pte)
|
if (pdpte == e_pdpte && pde == e_pde && pte >= e_pte)
|
||||||
break;
|
break;
|
||||||
if (pt[pte] & Flags::Used)
|
if (!(pt[pte] & Flags::Used))
|
||||||
continue;
|
{
|
||||||
|
vaddr_t vaddr = 0;
|
||||||
unmap_fast_page(2);
|
vaddr |= (vaddr_t)pdpte << 30;
|
||||||
unmap_fast_page(1);
|
vaddr |= (vaddr_t)pde << 21;
|
||||||
unmap_fast_page(0);
|
vaddr |= (vaddr_t)pte << 12;
|
||||||
s_fast_page_lock.unlock(state);
|
ASSERT(reserve_page(vaddr));
|
||||||
|
return vaddr;
|
||||||
vaddr_t vaddr = 0;
|
}
|
||||||
vaddr |= (vaddr_t)pdpte << 30;
|
|
||||||
vaddr |= (vaddr_t)pde << 21;
|
|
||||||
vaddr |= (vaddr_t)pte << 12;
|
|
||||||
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
|
// Find any free page
|
||||||
for (vaddr_t vaddr = first_address; vaddr < last_address; vaddr += PAGE_SIZE)
|
for (vaddr_t vaddr = first_address; vaddr < last_address; vaddr += PAGE_SIZE)
|
||||||
{
|
{
|
||||||
if (is_page_free(vaddr))
|
if (is_page_free(vaddr))
|
||||||
{
|
{
|
||||||
reserve_page(vaddr);
|
ASSERT(reserve_page(vaddr));
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddr_t PageTable::reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address)
|
vaddr_t PageTable::reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address)
|
||||||
@@ -732,12 +619,12 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
if (valid)
|
if (valid)
|
||||||
{
|
{
|
||||||
reserve_range(vaddr, page_count * PAGE_SIZE);
|
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
|
||||||
return vaddr;
|
return vaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_range(vaddr_t start, vaddr_t end, PageTable::flags_t flags)
|
static void dump_range(vaddr_t start, vaddr_t end, PageTable::flags_t flags)
|
||||||
@@ -755,14 +642,12 @@ namespace Kernel
|
|||||||
|
|
||||||
void PageTable::debug_dump()
|
void PageTable::debug_dump()
|
||||||
{
|
{
|
||||||
SpinLockGuard _0(m_lock);
|
SpinLockGuard _(m_lock);
|
||||||
|
|
||||||
flags_t flags = 0;
|
flags_t flags = 0;
|
||||||
vaddr_t start = 0;
|
vaddr_t start = 0;
|
||||||
|
|
||||||
SpinLockGuard _1(s_fast_page_lock);
|
uint64_t* pdpt = reinterpret_cast<uint64_t*>(P2V(m_highest_paging_struct));
|
||||||
|
|
||||||
const uint64_t* pdpt = static_cast<uint64_t*>(map_fast_page(0, m_highest_paging_struct));
|
|
||||||
for (uint32_t pdpte = 0; pdpte < 4; pdpte++)
|
for (uint32_t pdpte = 0; pdpte < 4; pdpte++)
|
||||||
{
|
{
|
||||||
if (!(pdpt[pdpte] & Flags::Present))
|
if (!(pdpt[pdpte] & Flags::Present))
|
||||||
@@ -771,7 +656,7 @@ namespace Kernel
|
|||||||
start = 0;
|
start = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const uint64_t* pd = static_cast<uint64_t*>(map_fast_page(1, pdpt[pdpte] & s_page_addr_mask));
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
||||||
for (uint64_t pde = 0; pde < 512; pde++)
|
for (uint64_t pde = 0; pde < 512; pde++)
|
||||||
{
|
{
|
||||||
if (!(pd[pde] & Flags::Present))
|
if (!(pd[pde] & Flags::Present))
|
||||||
@@ -780,7 +665,7 @@ namespace Kernel
|
|||||||
start = 0;
|
start = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const uint64_t* pt = static_cast<uint64_t*>(map_fast_page(2, pd[pde] & s_page_addr_mask));
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
||||||
for (uint64_t pte = 0; pte < 512; pte++)
|
for (uint64_t pte = 0; pte < 512; pte++)
|
||||||
{
|
{
|
||||||
if (parse_flags(pt[pte]) != flags)
|
if (parse_flags(pt[pte]) != flags)
|
||||||
@@ -798,11 +683,8 @@ namespace Kernel
|
|||||||
start = (pdpte << 30) | (pde << 21) | (pte << 12);
|
start = (pdpte << 30) | (pde << 21) | (pte << 12);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unmap_fast_page(2);
|
|
||||||
}
|
}
|
||||||
unmap_fast_page(1);
|
|
||||||
}
|
}
|
||||||
unmap_fast_page(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
.section .userspace, "ax"
|
.section .userspace, "ax"
|
||||||
|
|
||||||
// stack contains
|
// stack contains
|
||||||
// (4 bytes) return address (on return stack)
|
// return address
|
||||||
// (4 bytes) return stack
|
// return stack
|
||||||
// (4 bytes) return rflags
|
// return rflags
|
||||||
// (8 bytes) restore sigmask
|
// siginfo_t
|
||||||
// (36 bytes) siginfo_t
|
// signal number
|
||||||
// (4 bytes) signal number
|
// signal handler
|
||||||
// (4 bytes) signal handler
|
|
||||||
|
|
||||||
.global signal_trampoline
|
.global signal_trampoline
|
||||||
signal_trampoline:
|
signal_trampoline:
|
||||||
@@ -19,10 +18,6 @@ signal_trampoline:
|
|||||||
pushl %eax
|
pushl %eax
|
||||||
pushl %ebp
|
pushl %ebp
|
||||||
|
|
||||||
movl 84(%esp), %eax
|
|
||||||
pushl %eax; addl $4, (%esp)
|
|
||||||
pushl (%eax)
|
|
||||||
|
|
||||||
// FIXME: populate these
|
// FIXME: populate these
|
||||||
xorl %eax, %eax
|
xorl %eax, %eax
|
||||||
pushl %eax // stack
|
pushl %eax // stack
|
||||||
@@ -33,9 +28,9 @@ signal_trampoline:
|
|||||||
pushl %eax // link
|
pushl %eax // link
|
||||||
|
|
||||||
movl %esp, %edx // ucontext
|
movl %esp, %edx // ucontext
|
||||||
leal 68(%esp), %esi // siginfo
|
leal 60(%esp), %esi // siginfo
|
||||||
movl 64(%esp), %edi // signal number
|
movl 56(%esp), %edi // signal number
|
||||||
movl 60(%esp), %eax // handlers
|
movl 52(%esp), %eax // handlers
|
||||||
|
|
||||||
// align stack to 16 bytes
|
// align stack to 16 bytes
|
||||||
movl %esp, %ebp
|
movl %esp, %ebp
|
||||||
@@ -58,15 +53,7 @@ signal_trampoline:
|
|||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
addl $24, %esp
|
addl $24, %esp
|
||||||
|
|
||||||
// restore sigmask
|
|
||||||
movl $78, %eax // SYS_SIGPROCMASK
|
|
||||||
movl $3, %ebx // SIG_SETMASK
|
|
||||||
leal 72(%esp), %ecx // set
|
|
||||||
xorl %edx, %edx // oset
|
|
||||||
int $0xF0
|
|
||||||
|
|
||||||
// restore registers
|
// restore registers
|
||||||
addl $8, %esp
|
|
||||||
popl %ebp
|
popl %ebp
|
||||||
popl %eax
|
popl %eax
|
||||||
popl %ebx
|
popl %ebx
|
||||||
@@ -75,8 +62,8 @@ signal_trampoline:
|
|||||||
popl %edi
|
popl %edi
|
||||||
popl %esi
|
popl %esi
|
||||||
|
|
||||||
// skip handler, number, siginfo_t, sigmask
|
// skip handler, number, siginfo_t
|
||||||
addl $52, %esp
|
addl $44, %esp
|
||||||
|
|
||||||
// restore flags
|
// restore flags
|
||||||
popf
|
popf
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ read_ip:
|
|||||||
# void start_kernel_thread()
|
# void start_kernel_thread()
|
||||||
.global start_kernel_thread
|
.global start_kernel_thread
|
||||||
start_kernel_thread:
|
start_kernel_thread:
|
||||||
|
call get_thread_start_sp
|
||||||
|
movl %eax, %esp
|
||||||
|
|
||||||
# STACK LAYOUT
|
# STACK LAYOUT
|
||||||
# on_exit arg
|
# on_exit arg
|
||||||
# on_exit func
|
# on_exit func
|
||||||
@@ -31,6 +34,11 @@ start_kernel_thread:
|
|||||||
|
|
||||||
.global start_userspace_thread
|
.global start_userspace_thread
|
||||||
start_userspace_thread:
|
start_userspace_thread:
|
||||||
|
call load_thread_sse
|
||||||
|
|
||||||
|
call get_thread_start_sp
|
||||||
|
movl %eax, %esp
|
||||||
|
|
||||||
movw $(0x20 | 3), %bx
|
movw $(0x20 | 3), %bx
|
||||||
movw %bx, %ds
|
movw %bx, %ds
|
||||||
movw %bx, %es
|
movw %bx, %es
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
# bool safe_user_memcpy(void*, const void*, size_t)
|
|
||||||
.global safe_user_memcpy
|
|
||||||
.global safe_user_memcpy_end
|
|
||||||
.global safe_user_memcpy_fault
|
|
||||||
safe_user_memcpy:
|
|
||||||
xorl %eax, %eax
|
|
||||||
xchgl 4(%esp), %edi
|
|
||||||
xchgl 8(%esp), %esi
|
|
||||||
movl 12(%esp), %ecx
|
|
||||||
movl %edi, %edx
|
|
||||||
rep movsb
|
|
||||||
movl 4(%esp), %edi
|
|
||||||
movl 8(%esp), %esi
|
|
||||||
incl %eax
|
|
||||||
safe_user_memcpy_fault:
|
|
||||||
ret
|
|
||||||
safe_user_memcpy_end:
|
|
||||||
|
|
||||||
# bool safe_user_strncpy(void*, const void*, size_t)
|
|
||||||
.global safe_user_strncpy
|
|
||||||
.global safe_user_strncpy_end
|
|
||||||
.global safe_user_strncpy_fault
|
|
||||||
safe_user_strncpy:
|
|
||||||
xchgl 4(%esp), %edi
|
|
||||||
xchgl 8(%esp), %esi
|
|
||||||
movl 12(%esp), %ecx
|
|
||||||
|
|
||||||
testl %ecx, %ecx
|
|
||||||
jz safe_user_strncpy_fault
|
|
||||||
|
|
||||||
.safe_user_strncpy_loop:
|
|
||||||
movb (%esi), %al
|
|
||||||
movb %al, (%edi)
|
|
||||||
testb %al, %al
|
|
||||||
jz .safe_user_strncpy_done
|
|
||||||
|
|
||||||
incl %edi
|
|
||||||
incl %esi
|
|
||||||
decl %ecx
|
|
||||||
jnz .safe_user_strncpy_loop
|
|
||||||
|
|
||||||
safe_user_strncpy_fault:
|
|
||||||
xorl %eax, %eax
|
|
||||||
jmp .safe_user_strncpy_return
|
|
||||||
|
|
||||||
.safe_user_strncpy_done:
|
|
||||||
movl $1, %eax
|
|
||||||
|
|
||||||
.safe_user_strncpy_return:
|
|
||||||
movl 4(%esp), %edi
|
|
||||||
movl 8(%esp), %esi
|
|
||||||
ret
|
|
||||||
|
|
||||||
safe_user_strncpy_end:
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
.global asm_yield_trampoline
|
|
||||||
asm_yield_trampoline:
|
|
||||||
leal 4(%esp), %ecx
|
|
||||||
movl 4(%esp), %esp
|
|
||||||
|
|
||||||
pushl -4(%ecx)
|
|
||||||
pushl %ecx
|
|
||||||
pushl %eax
|
|
||||||
pushl %ebx
|
|
||||||
pushl %esi
|
|
||||||
pushl %edi
|
|
||||||
pushl %ebp
|
|
||||||
|
|
||||||
pushl %esp
|
|
||||||
call scheduler_on_yield_trampoline
|
|
||||||
addl $4, %esp
|
|
||||||
|
|
||||||
popl %ebp
|
|
||||||
popl %edi
|
|
||||||
popl %esi
|
|
||||||
popl %ebx
|
|
||||||
popl %eax
|
|
||||||
movl 4(%esp), %ecx
|
|
||||||
movl 0(%esp), %esp
|
|
||||||
jmp *%ecx
|
|
||||||
@@ -11,14 +11,13 @@
|
|||||||
|
|
||||||
.code32
|
.code32
|
||||||
|
|
||||||
// video mode info, page align modules
|
# multiboot2 header
|
||||||
.set multiboot_flags, (1 << 2) | (1 << 0)
|
|
||||||
|
|
||||||
.section .multiboot, "aw"
|
.section .multiboot, "aw"
|
||||||
|
.align 8
|
||||||
multiboot_start:
|
multiboot_start:
|
||||||
.long 0x1BADB002
|
.long 0x1BADB002
|
||||||
.long multiboot_flags
|
.long (1 << 2) # page align modules
|
||||||
.long -(0x1BADB002 + multiboot_flags)
|
.long -(0x1BADB002 + (1 << 2))
|
||||||
|
|
||||||
.long 0
|
.long 0
|
||||||
.long 0
|
.long 0
|
||||||
@@ -31,8 +30,7 @@ multiboot_start:
|
|||||||
.long FB_HEIGHT
|
.long FB_HEIGHT
|
||||||
.long FB_BPP
|
.long FB_BPP
|
||||||
multiboot_end:
|
multiboot_end:
|
||||||
|
.align 8
|
||||||
.section .multiboot2, "aw"
|
|
||||||
multiboot2_start:
|
multiboot2_start:
|
||||||
.long 0xE85250D6
|
.long 0xE85250D6
|
||||||
.long 0
|
.long 0
|
||||||
@@ -68,6 +66,7 @@ multiboot2_start:
|
|||||||
multiboot2_end:
|
multiboot2_end:
|
||||||
|
|
||||||
.section .bananboot, "aw"
|
.section .bananboot, "aw"
|
||||||
|
.align 8
|
||||||
bananboot_start:
|
bananboot_start:
|
||||||
.long 0xBABAB007
|
.long 0xBABAB007
|
||||||
.long -(0xBABAB007 + FB_WIDTH + FB_HEIGHT + FB_BPP)
|
.long -(0xBABAB007 + FB_WIDTH + FB_HEIGHT + FB_BPP)
|
||||||
@@ -98,7 +97,8 @@ bananboot_end:
|
|||||||
boot_pdpt:
|
boot_pdpt:
|
||||||
.long V2P(boot_pd) + (PG_PRESENT)
|
.long V2P(boot_pd) + (PG_PRESENT)
|
||||||
.long 0
|
.long 0
|
||||||
.skip 2 * 8
|
.quad 0
|
||||||
|
.quad 0
|
||||||
.long V2P(boot_pd) + (PG_PRESENT)
|
.long V2P(boot_pd) + (PG_PRESENT)
|
||||||
.long 0
|
.long 0
|
||||||
.align 4096
|
.align 4096
|
||||||
@@ -111,16 +111,13 @@ boot_pd:
|
|||||||
.endr
|
.endr
|
||||||
boot_pts:
|
boot_pts:
|
||||||
.set i, 0
|
.set i, 0
|
||||||
.rept 511
|
.rept 512
|
||||||
.rept 512
|
.rept 512
|
||||||
.long i + (PG_READ_WRITE | PG_PRESENT)
|
.long i + (PG_READ_WRITE | PG_PRESENT)
|
||||||
.long 0
|
.long 0
|
||||||
.set i, i + 0x1000
|
.set i, i + 0x1000
|
||||||
.endr
|
.endr
|
||||||
.endr
|
.endr
|
||||||
.global g_boot_fast_page_pt
|
|
||||||
g_boot_fast_page_pt:
|
|
||||||
.skip 512 * 8
|
|
||||||
|
|
||||||
boot_gdt:
|
boot_gdt:
|
||||||
.quad 0x0000000000000000 # null descriptor
|
.quad 0x0000000000000000 # null descriptor
|
||||||
@@ -276,7 +273,7 @@ system_halt:
|
|||||||
jmp 1b
|
jmp 1b
|
||||||
|
|
||||||
|
|
||||||
#define AP_REL(vaddr) ((vaddr) - ap_trampoline + 0xF000)
|
#define AP_V2P(vaddr) ((vaddr) - ap_trampoline + 0xF000)
|
||||||
|
|
||||||
.section .ap_init, "ax"
|
.section .ap_init, "ax"
|
||||||
|
|
||||||
@@ -286,27 +283,21 @@ ap_trampoline:
|
|||||||
jmp 1f
|
jmp 1f
|
||||||
|
|
||||||
.align 8
|
.align 8
|
||||||
ap_stack_paddr:
|
ap_stack_ptr:
|
||||||
.skip 4
|
|
||||||
ap_stack_vaddr:
|
|
||||||
.skip 4
|
|
||||||
ap_prepare_paging:
|
|
||||||
.skip 4
|
|
||||||
ap_page_table:
|
|
||||||
.skip 4
|
|
||||||
ap_ready:
|
|
||||||
.skip 4
|
.skip 4
|
||||||
|
ap_stack_loaded:
|
||||||
|
.skip 1
|
||||||
|
|
||||||
1: cli; cld
|
1: cli; cld
|
||||||
ljmpl $0x00, $AP_REL(ap_cs_clear)
|
ljmpl $0x00, $AP_V2P(ap_cs_clear)
|
||||||
|
|
||||||
ap_cs_clear:
|
ap_cs_clear:
|
||||||
# load ap gdt and enter protected mode
|
# load ap gdt and enter protected mode
|
||||||
lgdt AP_REL(ap_gdtr)
|
lgdt AP_V2P(ap_gdtr)
|
||||||
movl %cr0, %eax
|
movl %cr0, %eax
|
||||||
orb $1, %al
|
orb $1, %al
|
||||||
movl %eax, %cr0
|
movl %eax, %cr0
|
||||||
ljmpl $0x08, $AP_REL(ap_protected_mode)
|
ljmpl $0x08, $AP_V2P(ap_protected_mode)
|
||||||
|
|
||||||
.code32
|
.code32
|
||||||
ap_protected_mode:
|
ap_protected_mode:
|
||||||
@@ -315,7 +306,8 @@ ap_protected_mode:
|
|||||||
movw %ax, %ss
|
movw %ax, %ss
|
||||||
movw %ax, %es
|
movw %ax, %es
|
||||||
|
|
||||||
movl AP_REL(ap_stack_paddr), %esp
|
movl AP_V2P(ap_stack_ptr), %esp
|
||||||
|
movb $1, AP_V2P(ap_stack_loaded)
|
||||||
|
|
||||||
leal V2P(enable_sse), %ecx; call *%ecx
|
leal V2P(enable_sse), %ecx; call *%ecx
|
||||||
leal V2P(enable_tsc), %ecx; call *%ecx
|
leal V2P(enable_tsc), %ecx; call *%ecx
|
||||||
@@ -323,28 +315,24 @@ ap_protected_mode:
|
|||||||
|
|
||||||
# load boot gdt and enter long mode
|
# load boot gdt and enter long mode
|
||||||
lgdt V2P(boot_gdtr)
|
lgdt V2P(boot_gdtr)
|
||||||
ljmpl $0x08, $AP_REL(ap_flush_gdt)
|
ljmpl $0x08, $AP_V2P(ap_flush_gdt)
|
||||||
|
|
||||||
ap_flush_gdt:
|
ap_flush_gdt:
|
||||||
movl $ap_higher_half, %ecx
|
# move stack pointer to higher half
|
||||||
|
movl %esp, %esp
|
||||||
|
addl $KERNEL_OFFSET, %esp
|
||||||
|
|
||||||
|
# jump to higher half
|
||||||
|
leal ap_higher_half, %ecx
|
||||||
jmp *%ecx
|
jmp *%ecx
|
||||||
|
|
||||||
ap_higher_half:
|
ap_higher_half:
|
||||||
movl AP_REL(ap_prepare_paging), %eax
|
|
||||||
call *%eax
|
|
||||||
|
|
||||||
# load AP's initial values
|
|
||||||
movl AP_REL(ap_stack_vaddr), %esp
|
|
||||||
movl AP_REL(ap_page_table), %eax
|
|
||||||
movl $1, AP_REL(ap_ready)
|
|
||||||
movl %eax, %cr3
|
|
||||||
|
|
||||||
# clear rbp for stacktrace
|
# clear rbp for stacktrace
|
||||||
xorl %ebp, %ebp
|
xorl %ebp, %ebp
|
||||||
|
|
||||||
1: pause
|
1: pause
|
||||||
cmpb $0, g_ap_startup_done
|
cmpb $0, g_ap_startup_done
|
||||||
je 1b
|
jz 1b
|
||||||
|
|
||||||
lock incb g_ap_running_count
|
lock incb g_ap_running_count
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
.macro intr_header, n
|
.macro maybe_load_kernel_segments, n
|
||||||
pushal
|
testb $3, \n(%esp)
|
||||||
testb $3, \n+8*4(%esp)
|
jz 1f; jnp 1f
|
||||||
jz 1f
|
|
||||||
movw $0x10, %ax
|
movw $0x10, %ax
|
||||||
movw %ax, %ds
|
movw %ax, %ds
|
||||||
movw %ax, %es
|
movw %ax, %es
|
||||||
movw %ax, %fs
|
movw %ax, %fs
|
||||||
|
|
||||||
movw $0x28, %ax
|
movw $0x28, %ax
|
||||||
movw %ax, %gs
|
movw %ax, %gs
|
||||||
1: cld
|
1:
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
.macro intr_footer, n
|
.macro maybe_load_userspace_segments, n
|
||||||
testb $3, \n+8*4(%esp)
|
testb $3, \n(%esp)
|
||||||
jz 1f
|
jz 1f; jnp 1f
|
||||||
call cpp_check_signal
|
|
||||||
movw $(0x20 | 3), %bx
|
movw $(0x20 | 3), %bx
|
||||||
movw %bx, %ds
|
movw %bx, %ds
|
||||||
movw %bx, %es
|
movw %bx, %es
|
||||||
@@ -22,15 +23,22 @@
|
|||||||
movw %bx, %fs
|
movw %bx, %fs
|
||||||
movw $(0x38 | 3), %bx
|
movw $(0x38 | 3), %bx
|
||||||
movw %bx, %gs
|
movw %bx, %gs
|
||||||
1: popal
|
1:
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
isr_stub:
|
isr_stub:
|
||||||
intr_header 12
|
pushal
|
||||||
|
maybe_load_kernel_segments 44
|
||||||
|
cld
|
||||||
|
|
||||||
movl 32(%esp), %edi // isr number
|
movl %cr0, %eax; pushl %eax
|
||||||
movl 36(%esp), %esi // error code
|
movl %cr2, %eax; pushl %eax
|
||||||
leal 40(%esp), %edx // interrupt stack ptr
|
movl %cr3, %eax; pushl %eax
|
||||||
|
movl %cr4, %eax; pushl %eax
|
||||||
|
|
||||||
|
movl 48(%esp), %edi // isr number
|
||||||
|
movl 52(%esp), %esi // error code
|
||||||
|
leal 56(%esp), %edx // interrupt stack ptr
|
||||||
movl %esp, %ecx // register ptr
|
movl %esp, %ecx // register ptr
|
||||||
|
|
||||||
# stack frame for stack trace
|
# stack frame for stack trace
|
||||||
@@ -48,14 +56,17 @@ isr_stub:
|
|||||||
call cpp_isr_handler
|
call cpp_isr_handler
|
||||||
|
|
||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
addl $8, %esp
|
addl $24, %esp
|
||||||
|
|
||||||
intr_footer 12
|
maybe_load_userspace_segments 44
|
||||||
|
popal
|
||||||
addl $8, %esp
|
addl $8, %esp
|
||||||
iret
|
iret
|
||||||
|
|
||||||
irq_stub:
|
irq_stub:
|
||||||
intr_header 12
|
pushal
|
||||||
|
maybe_load_kernel_segments 44
|
||||||
|
cld
|
||||||
|
|
||||||
movl 32(%esp), %edi # interrupt number
|
movl 32(%esp), %edi # interrupt number
|
||||||
|
|
||||||
@@ -68,13 +79,38 @@ irq_stub:
|
|||||||
|
|
||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
|
|
||||||
intr_footer 12
|
maybe_load_userspace_segments 44
|
||||||
|
popal
|
||||||
addl $8, %esp
|
addl $8, %esp
|
||||||
iret
|
iret
|
||||||
|
|
||||||
|
.global asm_yield_handler
|
||||||
|
asm_yield_handler:
|
||||||
|
# This can only be called from kernel, so no segment saving is needed
|
||||||
|
pushal
|
||||||
|
cld
|
||||||
|
|
||||||
|
leal 32(%esp), %edi # interrupt stack ptr
|
||||||
|
movl %esp, %esi # interrupt registers ptr
|
||||||
|
|
||||||
|
movl %esp, %ebp
|
||||||
|
andl $-16, %esp
|
||||||
|
|
||||||
|
subl $8, %esp
|
||||||
|
pushl %esi
|
||||||
|
pushl %edi
|
||||||
|
call cpp_yield_handler
|
||||||
|
|
||||||
|
movl %ebp, %esp
|
||||||
|
|
||||||
|
popal
|
||||||
|
iret
|
||||||
|
|
||||||
.global asm_ipi_handler
|
.global asm_ipi_handler
|
||||||
asm_ipi_handler:
|
asm_ipi_handler:
|
||||||
intr_header 4
|
pushal
|
||||||
|
maybe_load_kernel_segments 36
|
||||||
|
cld
|
||||||
|
|
||||||
movl %esp, %ebp
|
movl %esp, %ebp
|
||||||
andl $-16, %esp
|
andl $-16, %esp
|
||||||
@@ -83,12 +119,15 @@ asm_ipi_handler:
|
|||||||
|
|
||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
|
|
||||||
intr_footer 4
|
maybe_load_userspace_segments 36
|
||||||
|
popal
|
||||||
iret
|
iret
|
||||||
|
|
||||||
.global asm_timer_handler
|
.global asm_timer_handler
|
||||||
asm_timer_handler:
|
asm_timer_handler:
|
||||||
intr_header 4
|
pushal
|
||||||
|
maybe_load_kernel_segments 36
|
||||||
|
cld
|
||||||
|
|
||||||
movl %esp, %ebp
|
movl %esp, %ebp
|
||||||
andl $-16, %esp
|
andl $-16, %esp
|
||||||
@@ -97,7 +136,8 @@ asm_timer_handler:
|
|||||||
|
|
||||||
movl %ebp, %esp
|
movl %ebp, %esp
|
||||||
|
|
||||||
intr_footer 4
|
maybe_load_userspace_segments 36
|
||||||
|
popal
|
||||||
iret
|
iret
|
||||||
|
|
||||||
.macro isr n
|
.macro isr n
|
||||||
|
|||||||
@@ -11,21 +11,20 @@ SECTIONS
|
|||||||
{
|
{
|
||||||
g_kernel_execute_start = .;
|
g_kernel_execute_start = .;
|
||||||
*(.multiboot)
|
*(.multiboot)
|
||||||
*(.multiboot2)
|
|
||||||
*(.bananboot)
|
*(.bananboot)
|
||||||
*(.text.*)
|
*(.text.*)
|
||||||
}
|
}
|
||||||
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
|
||||||
{
|
|
||||||
g_ap_init_addr = .;
|
|
||||||
*(.ap_init)
|
|
||||||
g_kernel_execute_end = .;
|
|
||||||
}
|
|
||||||
.userspace ALIGN(4K) : AT(ADDR(.userspace) - KERNEL_OFFSET)
|
.userspace ALIGN(4K) : AT(ADDR(.userspace) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_userspace_start = .;
|
g_userspace_start = .;
|
||||||
*(.userspace)
|
*(.userspace)
|
||||||
g_userspace_end = .;
|
g_userspace_end = .;
|
||||||
|
g_kernel_execute_end = .;
|
||||||
|
}
|
||||||
|
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
||||||
|
{
|
||||||
|
g_ap_init_addr = .;
|
||||||
|
*(.ap_init)
|
||||||
}
|
}
|
||||||
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,12 @@
|
|||||||
.section .userspace, "ax"
|
.section .userspace, "ax"
|
||||||
|
|
||||||
// stack contains
|
// stack contains
|
||||||
// (8 bytes) return address (on return stack)
|
// return address
|
||||||
// (8 bytes) return stack
|
// return stack
|
||||||
// (8 bytes) return rflags
|
// return rflags
|
||||||
// (8 bytes) restore sigmask
|
// siginfo_t
|
||||||
// (56 bytes) siginfo_t
|
// signal number
|
||||||
// (8 bytes) signal number
|
// signal handler
|
||||||
// (8 bytes) signal handler
|
|
||||||
|
|
||||||
.global signal_trampoline
|
.global signal_trampoline
|
||||||
signal_trampoline:
|
signal_trampoline:
|
||||||
@@ -27,10 +26,6 @@ signal_trampoline:
|
|||||||
pushq %rax
|
pushq %rax
|
||||||
pushq %rbp
|
pushq %rbp
|
||||||
|
|
||||||
movq 208(%rsp), %rax
|
|
||||||
pushq %rax; addq $(128 + 8), (%rsp)
|
|
||||||
pushq (%rax)
|
|
||||||
|
|
||||||
// FIXME: populate these
|
// FIXME: populate these
|
||||||
xorq %rax, %rax
|
xorq %rax, %rax
|
||||||
pushq %rax // stack
|
pushq %rax // stack
|
||||||
@@ -40,9 +35,9 @@ signal_trampoline:
|
|||||||
pushq %rax // link
|
pushq %rax // link
|
||||||
|
|
||||||
movq %rsp, %rdx // ucontext
|
movq %rsp, %rdx // ucontext
|
||||||
leaq 192(%rsp), %rsi // siginfo
|
leaq 176(%rsp), %rsi // siginfo
|
||||||
movq 184(%rsp), %rdi // signal number
|
movq 168(%rsp), %rdi // signal number
|
||||||
movq 176(%rsp), %rax // handler
|
movq 160(%rsp), %rax // handler
|
||||||
|
|
||||||
// align stack to 16 bytes
|
// align stack to 16 bytes
|
||||||
movq %rsp, %rbp
|
movq %rsp, %rbp
|
||||||
@@ -60,15 +55,7 @@ signal_trampoline:
|
|||||||
movq %rbp, %rsp
|
movq %rbp, %rsp
|
||||||
addq $40, %rsp
|
addq $40, %rsp
|
||||||
|
|
||||||
// restore sigmask
|
|
||||||
movq $78, %rdi // SYS_SIGPROCMASK
|
|
||||||
movq $3, %rsi // SIG_SETMASK
|
|
||||||
leaq 192(%rsp), %rdx // set
|
|
||||||
xorq %r10, %r10 // oset
|
|
||||||
syscall
|
|
||||||
|
|
||||||
// restore registers
|
// restore registers
|
||||||
addq $16, %rsp
|
|
||||||
popq %rbp
|
popq %rbp
|
||||||
popq %rax
|
popq %rax
|
||||||
popq %rbx
|
popq %rbx
|
||||||
@@ -85,13 +72,13 @@ signal_trampoline:
|
|||||||
popq %r14
|
popq %r14
|
||||||
popq %r15
|
popq %r15
|
||||||
|
|
||||||
// skip handler, number, siginfo_t, sigmask
|
// skip handler, number, siginfo_t
|
||||||
addq $80, %rsp
|
addq $72, %rsp
|
||||||
|
|
||||||
// restore flags
|
// restore flags
|
||||||
popfq
|
popfq
|
||||||
|
|
||||||
movq (%rsp), %rsp
|
movq (%rsp), %rsp
|
||||||
|
|
||||||
// return over red-zone
|
// return over red-zone and siginfo_t
|
||||||
ret $128
|
ret $128
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ sys_fork_trampoline:
|
|||||||
|
|
||||||
call read_ip
|
call read_ip
|
||||||
testq %rax, %rax
|
testq %rax, %rax
|
||||||
jz .done
|
je .done
|
||||||
|
|
||||||
movq %rax, %rsi
|
movq %rax, %rsi
|
||||||
movq %rsp, %rdi
|
movq %rsp, %rdi
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ read_ip:
|
|||||||
# void start_kernel_thread()
|
# void start_kernel_thread()
|
||||||
.global start_kernel_thread
|
.global start_kernel_thread
|
||||||
start_kernel_thread:
|
start_kernel_thread:
|
||||||
|
call get_thread_start_sp
|
||||||
|
movq %rax, %rsp
|
||||||
|
|
||||||
# STACK LAYOUT
|
# STACK LAYOUT
|
||||||
# on_exit arg
|
# on_exit arg
|
||||||
# on_exit func
|
# on_exit func
|
||||||
@@ -24,5 +27,11 @@ start_kernel_thread:
|
|||||||
|
|
||||||
.global start_userspace_thread
|
.global start_userspace_thread
|
||||||
start_userspace_thread:
|
start_userspace_thread:
|
||||||
|
call load_thread_sse
|
||||||
|
|
||||||
|
call get_thread_start_sp
|
||||||
|
movq %rax, %rsp
|
||||||
|
|
||||||
swapgs
|
swapgs
|
||||||
|
|
||||||
iretq
|
iretq
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
# bool safe_user_memcpy(void*, const void*, size_t)
|
|
||||||
.global safe_user_memcpy
|
|
||||||
.global safe_user_memcpy_end
|
|
||||||
.global safe_user_memcpy_fault
|
|
||||||
safe_user_memcpy:
|
|
||||||
xorq %rax, %rax
|
|
||||||
movq %rdx, %rcx
|
|
||||||
rep movsb
|
|
||||||
incq %rax
|
|
||||||
safe_user_memcpy_fault:
|
|
||||||
ret
|
|
||||||
safe_user_memcpy_end:
|
|
||||||
|
|
||||||
# bool safe_user_strncpy(void*, const void*, size_t)
|
|
||||||
.global safe_user_strncpy
|
|
||||||
.global safe_user_strncpy_end
|
|
||||||
.global safe_user_strncpy_fault
|
|
||||||
safe_user_strncpy:
|
|
||||||
movq %rdx, %rcx
|
|
||||||
testq %rcx, %rcx
|
|
||||||
jz safe_user_strncpy_fault
|
|
||||||
|
|
||||||
.safe_user_strncpy_align_loop:
|
|
||||||
testb $0x7, %sil
|
|
||||||
jz .safe_user_strncpy_align_done
|
|
||||||
|
|
||||||
movb (%rsi), %al
|
|
||||||
movb %al, (%rdi)
|
|
||||||
testb %al, %al
|
|
||||||
jz .safe_user_strncpy_done
|
|
||||||
|
|
||||||
incq %rdi
|
|
||||||
incq %rsi
|
|
||||||
decq %rcx
|
|
||||||
jnz .safe_user_strncpy_align_loop
|
|
||||||
jmp safe_user_strncpy_fault
|
|
||||||
|
|
||||||
.safe_user_strncpy_align_done:
|
|
||||||
movq $0x0101010101010101, %r8
|
|
||||||
movq $0x8080808080808080, %r9
|
|
||||||
|
|
||||||
.safe_user_strncpy_qword_loop:
|
|
||||||
cmpq $8, %rcx
|
|
||||||
jb .safe_user_strncpy_qword_done
|
|
||||||
|
|
||||||
movq (%rsi), %rax
|
|
||||||
movq %rax, %r10
|
|
||||||
movq %rax, %r11
|
|
||||||
|
|
||||||
# https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
|
|
||||||
subq %r8, %r10
|
|
||||||
notq %r11
|
|
||||||
andq %r11, %r10
|
|
||||||
andq %r9, %r10
|
|
||||||
jnz .safe_user_strncpy_byte_loop
|
|
||||||
|
|
||||||
movq %rax, (%rdi)
|
|
||||||
|
|
||||||
addq $8, %rdi
|
|
||||||
addq $8, %rsi
|
|
||||||
subq $8, %rcx
|
|
||||||
jnz .safe_user_strncpy_qword_loop
|
|
||||||
jmp safe_user_strncpy_fault
|
|
||||||
|
|
||||||
.safe_user_strncpy_qword_done:
|
|
||||||
testq %rcx, %rcx
|
|
||||||
jz safe_user_strncpy_fault
|
|
||||||
|
|
||||||
.safe_user_strncpy_byte_loop:
|
|
||||||
movb (%rsi), %al
|
|
||||||
movb %al, (%rdi)
|
|
||||||
testb %al, %al
|
|
||||||
jz .safe_user_strncpy_done
|
|
||||||
|
|
||||||
incq %rdi
|
|
||||||
incq %rsi
|
|
||||||
decq %rcx
|
|
||||||
jnz .safe_user_strncpy_byte_loop
|
|
||||||
|
|
||||||
safe_user_strncpy_fault:
|
|
||||||
xorq %rax, %rax
|
|
||||||
ret
|
|
||||||
|
|
||||||
.safe_user_strncpy_done:
|
|
||||||
movb $1, %al
|
|
||||||
ret
|
|
||||||
safe_user_strncpy_end:
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
.global asm_yield_trampoline
|
|
||||||
asm_yield_trampoline:
|
|
||||||
leaq 8(%rsp), %rcx
|
|
||||||
movq %rdi, %rsp
|
|
||||||
|
|
||||||
subq $8, %rsp
|
|
||||||
pushq -8(%rcx)
|
|
||||||
pushq %rcx
|
|
||||||
pushq %rax
|
|
||||||
pushq %rbx
|
|
||||||
pushq %rbp
|
|
||||||
pushq %r12
|
|
||||||
pushq %r13
|
|
||||||
pushq %r14
|
|
||||||
pushq %r15
|
|
||||||
|
|
||||||
movq %rsp, %rdi
|
|
||||||
call scheduler_on_yield_trampoline
|
|
||||||
|
|
||||||
popq %r15
|
|
||||||
popq %r14
|
|
||||||
popq %r13
|
|
||||||
popq %r12
|
|
||||||
popq %rbp
|
|
||||||
popq %rbx
|
|
||||||
popq %rax
|
|
||||||
movq 8(%rsp), %rcx
|
|
||||||
movq 0(%rsp), %rsp
|
|
||||||
jmp *%rcx
|
|
||||||
@@ -11,28 +11,26 @@
|
|||||||
|
|
||||||
.code32
|
.code32
|
||||||
|
|
||||||
// custom addresses, video mode info, page align modules
|
# multiboot2 header
|
||||||
.set multiboot_flags, (1 << 16) | (1 << 2) | (1 << 0)
|
|
||||||
|
|
||||||
.section .multiboot, "aw"
|
.section .multiboot, "aw"
|
||||||
|
.align 8
|
||||||
multiboot_start:
|
multiboot_start:
|
||||||
.long 0x1BADB002
|
.long 0x1BADB002
|
||||||
.long multiboot_flags
|
.long (1 << 2) # page align modules
|
||||||
.long -(0x1BADB002 + multiboot_flags)
|
.long -(0x1BADB002 + (1 << 2))
|
||||||
|
|
||||||
.long V2P(multiboot_start)
|
.long 0
|
||||||
.long V2P(g_kernel_start)
|
.long 0
|
||||||
.long V2P(g_kernel_bss_start)
|
.long 0
|
||||||
.long V2P(g_kernel_end)
|
.long 0
|
||||||
.long V2P(_start)
|
.long 0
|
||||||
|
|
||||||
.long 0
|
.long 0
|
||||||
.long FB_WIDTH
|
.long FB_WIDTH
|
||||||
.long FB_HEIGHT
|
.long FB_HEIGHT
|
||||||
.long FB_BPP
|
.long FB_BPP
|
||||||
multiboot_end:
|
multiboot_end:
|
||||||
|
.align 8
|
||||||
.section .multiboot2, "aw"
|
|
||||||
multiboot2_start:
|
multiboot2_start:
|
||||||
.long 0xE85250D6
|
.long 0xE85250D6
|
||||||
.long 0
|
.long 0
|
||||||
@@ -68,6 +66,7 @@ multiboot2_start:
|
|||||||
multiboot2_end:
|
multiboot2_end:
|
||||||
|
|
||||||
.section .bananboot, "aw"
|
.section .bananboot, "aw"
|
||||||
|
.align 8
|
||||||
bananboot_start:
|
bananboot_start:
|
||||||
.long 0xBABAB007
|
.long 0xBABAB007
|
||||||
.long -(0xBABAB007 + FB_WIDTH + FB_HEIGHT + FB_BPP)
|
.long -(0xBABAB007 + FB_WIDTH + FB_HEIGHT + FB_BPP)
|
||||||
@@ -97,25 +96,27 @@ bananboot_end:
|
|||||||
.align 4096
|
.align 4096
|
||||||
boot_pml4:
|
boot_pml4:
|
||||||
.quad V2P(boot_pdpt_lo) + (PG_READ_WRITE | PG_PRESENT)
|
.quad V2P(boot_pdpt_lo) + (PG_READ_WRITE | PG_PRESENT)
|
||||||
.skip 510 * 8
|
.rept 510
|
||||||
|
.quad 0
|
||||||
|
.endr
|
||||||
.quad V2P(boot_pdpt_hi) + (PG_READ_WRITE | PG_PRESENT)
|
.quad V2P(boot_pdpt_hi) + (PG_READ_WRITE | PG_PRESENT)
|
||||||
boot_pdpt_lo:
|
boot_pdpt_lo:
|
||||||
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
||||||
.skip 511 * 8
|
.rept 511
|
||||||
|
.quad 0
|
||||||
|
.endr
|
||||||
boot_pdpt_hi:
|
boot_pdpt_hi:
|
||||||
.skip 510 * 8
|
.rept 510
|
||||||
|
.quad 0
|
||||||
|
.endr
|
||||||
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
||||||
.skip 8
|
.quad 0
|
||||||
boot_pd:
|
boot_pd:
|
||||||
.set i, 0
|
.set i, 0
|
||||||
.rept 511
|
.rept 512
|
||||||
.quad i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
|
.quad i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
|
||||||
.set i, i + 0x200000
|
.set i, i + 0x200000
|
||||||
.endr
|
.endr
|
||||||
.quad V2P(g_boot_fast_page_pt) + (PG_READ_WRITE | PG_PRESENT)
|
|
||||||
.global g_boot_fast_page_pt
|
|
||||||
g_boot_fast_page_pt:
|
|
||||||
.skip 512 * 8
|
|
||||||
|
|
||||||
boot_gdt:
|
boot_gdt:
|
||||||
.quad 0x0000000000000000 # null descriptor
|
.quad 0x0000000000000000 # null descriptor
|
||||||
@@ -271,7 +272,7 @@ system_halt:
|
|||||||
jmp 1b
|
jmp 1b
|
||||||
|
|
||||||
|
|
||||||
#define AP_REL(vaddr) ((vaddr) - ap_trampoline + 0xF000)
|
#define AP_V2P(vaddr) ((vaddr) - ap_trampoline + 0xF000)
|
||||||
|
|
||||||
.section .ap_init, "ax"
|
.section .ap_init, "ax"
|
||||||
|
|
||||||
@@ -281,27 +282,21 @@ ap_trampoline:
|
|||||||
jmp 1f
|
jmp 1f
|
||||||
|
|
||||||
.align 8
|
.align 8
|
||||||
ap_stack_paddr:
|
ap_stack_ptr:
|
||||||
.skip 8
|
.skip 4
|
||||||
ap_stack_vaddr:
|
ap_stack_loaded:
|
||||||
.skip 8
|
.skip 1
|
||||||
ap_prepare_paging:
|
|
||||||
.skip 8
|
|
||||||
ap_page_table:
|
|
||||||
.skip 8
|
|
||||||
ap_ready:
|
|
||||||
.skip 8
|
|
||||||
|
|
||||||
1: cli; cld
|
1: cli; cld
|
||||||
ljmpl $0x00, $AP_REL(ap_cs_clear)
|
ljmpl $0x00, $AP_V2P(ap_cs_clear)
|
||||||
|
|
||||||
ap_cs_clear:
|
ap_cs_clear:
|
||||||
# load ap gdt and enter protected mode
|
# load ap gdt and enter protected mode
|
||||||
lgdt AP_REL(ap_gdtr)
|
lgdt AP_V2P(ap_gdtr)
|
||||||
movl %cr0, %eax
|
movl %cr0, %eax
|
||||||
orb $1, %al
|
orb $1, %al
|
||||||
movl %eax, %cr0
|
movl %eax, %cr0
|
||||||
ljmpl $0x08, $AP_REL(ap_protected_mode)
|
ljmpl $0x08, $AP_V2P(ap_protected_mode)
|
||||||
|
|
||||||
.code32
|
.code32
|
||||||
ap_protected_mode:
|
ap_protected_mode:
|
||||||
@@ -310,7 +305,8 @@ ap_protected_mode:
|
|||||||
movw %ax, %ss
|
movw %ax, %ss
|
||||||
movw %ax, %es
|
movw %ax, %es
|
||||||
|
|
||||||
movl AP_REL(ap_stack_paddr), %esp
|
movl AP_V2P(ap_stack_ptr), %esp
|
||||||
|
movb $1, AP_V2P(ap_stack_loaded)
|
||||||
|
|
||||||
leal V2P(enable_sse), %ecx; call *%ecx
|
leal V2P(enable_sse), %ecx; call *%ecx
|
||||||
leal V2P(enable_tsc), %ecx; call *%ecx
|
leal V2P(enable_tsc), %ecx; call *%ecx
|
||||||
@@ -318,34 +314,28 @@ ap_protected_mode:
|
|||||||
|
|
||||||
# load boot gdt and enter long mode
|
# load boot gdt and enter long mode
|
||||||
lgdt V2P(boot_gdtr)
|
lgdt V2P(boot_gdtr)
|
||||||
ljmpl $0x08, $AP_REL(ap_long_mode)
|
ljmpl $0x08, $AP_V2P(ap_long_mode)
|
||||||
|
|
||||||
.code64
|
.code64
|
||||||
ap_long_mode:
|
ap_long_mode:
|
||||||
movq $ap_higher_half, %rax
|
# move stack pointer to higher half
|
||||||
jmp *%rax
|
movl %esp, %esp
|
||||||
|
addq $KERNEL_OFFSET, %rsp
|
||||||
ap_higher_half:
|
|
||||||
movq AP_REL(ap_prepare_paging), %rax
|
|
||||||
call *%rax
|
|
||||||
|
|
||||||
# load AP's initial values
|
|
||||||
movq AP_REL(ap_stack_vaddr), %rsp
|
|
||||||
movq AP_REL(ap_page_table), %rax
|
|
||||||
movq $1, AP_REL(ap_ready)
|
|
||||||
movq %rax, %cr3
|
|
||||||
|
|
||||||
# clear rbp for stacktrace
|
# clear rbp for stacktrace
|
||||||
xorq %rbp, %rbp
|
xorq %rbp, %rbp
|
||||||
|
|
||||||
|
xorb %al, %al
|
||||||
1: pause
|
1: pause
|
||||||
cmpb $0, g_ap_startup_done
|
cmpb %al, g_ap_startup_done
|
||||||
je 1b
|
jz 1b
|
||||||
|
|
||||||
lock incb g_ap_running_count
|
lock incb g_ap_running_count
|
||||||
|
|
||||||
call ap_main
|
# jump to ap_main in higher half
|
||||||
jmp system_halt
|
movabsq $ap_main, %rcx
|
||||||
|
call *%rcx
|
||||||
|
jmp V2P(system_halt)
|
||||||
|
|
||||||
ap_gdt:
|
ap_gdt:
|
||||||
.quad 0x0000000000000000 # null descriptor
|
.quad 0x0000000000000000 # null descriptor
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
.macro intr_header, n
|
.macro swapgs_if_necessary, n
|
||||||
|
testb $3, \n(%rsp)
|
||||||
|
jz 1f; jnp 1f
|
||||||
|
swapgs
|
||||||
|
1:
|
||||||
|
.endm
|
||||||
|
|
||||||
|
.macro pushaq, n
|
||||||
|
swapgs_if_necessary \n
|
||||||
pushq %rax
|
pushq %rax
|
||||||
pushq %rcx
|
pushq %rcx
|
||||||
pushq %rdx
|
pushq %rdx
|
||||||
@@ -14,18 +22,10 @@
|
|||||||
pushq %r13
|
pushq %r13
|
||||||
pushq %r14
|
pushq %r14
|
||||||
pushq %r15
|
pushq %r15
|
||||||
testb $3, \n+15*8(%rsp)
|
|
||||||
jz 1f
|
|
||||||
swapgs
|
|
||||||
1: cld
|
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
.macro intr_footer, n
|
.macro popaq, n
|
||||||
testb $3, \n+15*8(%rsp)
|
popq %r15
|
||||||
jz 1f
|
|
||||||
call cpp_check_signal
|
|
||||||
swapgs
|
|
||||||
1: popq %r15
|
|
||||||
popq %r14
|
popq %r14
|
||||||
popq %r13
|
popq %r13
|
||||||
popq %r12
|
popq %r12
|
||||||
@@ -40,44 +40,61 @@
|
|||||||
popq %rdx
|
popq %rdx
|
||||||
popq %rcx
|
popq %rcx
|
||||||
popq %rax
|
popq %rax
|
||||||
|
swapgs_if_necessary \n
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
isr_stub:
|
isr_stub:
|
||||||
intr_header 24
|
pushaq 24
|
||||||
|
cld
|
||||||
|
movq %cr0, %rax; pushq %rax
|
||||||
|
movq %cr2, %rax; pushq %rax
|
||||||
|
movq %cr3, %rax; pushq %rax
|
||||||
|
movq %cr4, %rax; pushq %rax
|
||||||
|
|
||||||
movq 120(%rsp), %rdi // isr number
|
movq 152(%rsp), %rdi // isr number
|
||||||
movq 128(%rsp), %rsi // error code
|
movq 160(%rsp), %rsi // error code
|
||||||
leaq 136(%rsp), %rdx // interrupt stack ptr
|
leaq 168(%rsp), %rdx // interrupt stack ptr
|
||||||
movq %rsp, %rcx // register ptr
|
movq %rsp, %rcx // register ptr
|
||||||
call cpp_isr_handler
|
call cpp_isr_handler
|
||||||
|
addq $32, %rsp
|
||||||
|
|
||||||
intr_footer 24
|
popaq 24
|
||||||
addq $16, %rsp
|
addq $16, %rsp
|
||||||
iretq
|
iretq
|
||||||
|
|
||||||
irq_stub:
|
irq_stub:
|
||||||
intr_header 24
|
pushaq 24
|
||||||
xorq %rbp, %rbp
|
cld
|
||||||
movq 120(%rsp), %rdi # irq number
|
movq 120(%rsp), %rdi # irq number
|
||||||
call cpp_irq_handler
|
call cpp_irq_handler
|
||||||
intr_footer 24
|
popaq 24
|
||||||
addq $16, %rsp
|
addq $16, %rsp
|
||||||
iretq
|
iretq
|
||||||
|
|
||||||
|
.global asm_yield_handler
|
||||||
|
asm_yield_handler:
|
||||||
|
pushaq 8
|
||||||
|
cld
|
||||||
|
leaq 120(%rsp), %rdi # interrupt stack ptr
|
||||||
|
movq %rsp, %rsi # interrupt register ptr
|
||||||
|
call cpp_yield_handler
|
||||||
|
popaq 8
|
||||||
|
iretq
|
||||||
|
|
||||||
.global asm_ipi_handler
|
.global asm_ipi_handler
|
||||||
asm_ipi_handler:
|
asm_ipi_handler:
|
||||||
intr_header 8
|
pushaq 8
|
||||||
xorq %rbp, %rbp
|
cld
|
||||||
call cpp_ipi_handler
|
call cpp_ipi_handler
|
||||||
intr_footer 8
|
popaq 8
|
||||||
iretq
|
iretq
|
||||||
|
|
||||||
.global asm_timer_handler
|
.global asm_timer_handler
|
||||||
asm_timer_handler:
|
asm_timer_handler:
|
||||||
intr_header 8
|
pushaq 8
|
||||||
xorq %rbp, %rbp
|
cld
|
||||||
call cpp_timer_handler
|
call cpp_timer_handler
|
||||||
intr_footer 8
|
popaq 8
|
||||||
iretq
|
iretq
|
||||||
|
|
||||||
.macro isr n
|
.macro isr n
|
||||||
|
|||||||
@@ -11,21 +11,20 @@ SECTIONS
|
|||||||
{
|
{
|
||||||
g_kernel_execute_start = .;
|
g_kernel_execute_start = .;
|
||||||
*(.multiboot)
|
*(.multiboot)
|
||||||
*(.multiboot2)
|
|
||||||
*(.bananboot)
|
*(.bananboot)
|
||||||
*(.text.*)
|
*(.text.*)
|
||||||
}
|
}
|
||||||
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
|
||||||
{
|
|
||||||
g_ap_init_addr = .;
|
|
||||||
*(.ap_init)
|
|
||||||
g_kernel_execute_end = .;
|
|
||||||
}
|
|
||||||
.userspace ALIGN(4K) : AT(ADDR(.userspace) - KERNEL_OFFSET)
|
.userspace ALIGN(4K) : AT(ADDR(.userspace) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_userspace_start = .;
|
g_userspace_start = .;
|
||||||
*(.userspace)
|
*(.userspace)
|
||||||
g_userspace_end = .;
|
g_userspace_end = .;
|
||||||
|
g_kernel_execute_end = .;
|
||||||
|
}
|
||||||
|
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
||||||
|
{
|
||||||
|
g_ap_init_addr = .;
|
||||||
|
*(.ap_init)
|
||||||
}
|
}
|
||||||
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
@@ -41,21 +40,9 @@ SECTIONS
|
|||||||
{
|
{
|
||||||
g_kernel_writable_start = .;
|
g_kernel_writable_start = .;
|
||||||
*(.data)
|
*(.data)
|
||||||
|
|
||||||
. = ALIGN(8);
|
|
||||||
g_drv_builtin_begin = .;
|
|
||||||
KEEP(*(.banos-driver))
|
|
||||||
g_drv_builtin_end = .;
|
|
||||||
. = ALIGN(8);
|
|
||||||
g_banos_export = .;
|
|
||||||
KEEP(*(.banos-export))
|
|
||||||
g_banos_export_end = .;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_kernel_bss_start = .;
|
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
*(.bss)
|
*(.bss)
|
||||||
g_kernel_writable_end = .;
|
g_kernel_writable_end = .;
|
||||||
|
|||||||
@@ -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)))
|
|
||||||
@@ -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)
|
|
||||||
@@ -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
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
typedef unsigned long banos_revision_t;
|
|
||||||
@@ -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)
|
|
||||||
@@ -8,19 +8,19 @@ namespace Kernel::API
|
|||||||
enum SharedPageFeature : uint32_t
|
enum SharedPageFeature : uint32_t
|
||||||
{
|
{
|
||||||
SPF_GETTIME = 1 << 0,
|
SPF_GETTIME = 1 << 0,
|
||||||
SPF_RDTSCP = 1 << 1,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SharedPage
|
struct SharedPage
|
||||||
{
|
{
|
||||||
uint16_t gdt_cpu_offset;
|
uint8_t __sequence[0x100];
|
||||||
|
|
||||||
uint32_t features;
|
uint32_t features;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
uint64_t realtime_s;
|
uint8_t shift;
|
||||||
uint32_t realtime_ns;
|
uint64_t mult;
|
||||||
|
uint64_t realtime_seconds;
|
||||||
} gettime_shared;
|
} gettime_shared;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
@@ -28,8 +28,6 @@ namespace Kernel::API
|
|||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
uint32_t seq;
|
uint32_t seq;
|
||||||
uint32_t mult;
|
|
||||||
int8_t shift;
|
|
||||||
uint64_t last_ns;
|
uint64_t last_ns;
|
||||||
uint64_t last_tsc;
|
uint64_t last_tsc;
|
||||||
} gettime_local;
|
} gettime_local;
|
||||||
|
|||||||
@@ -1,34 +1,44 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/MacroUtils.h>
|
#include <kernel/Attributes.h>
|
||||||
|
#include <kernel/IDT.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
namespace Kernel
|
||||||
#define _kas_instruction "syscall"
|
{
|
||||||
#define _kas_result rax
|
|
||||||
#define _kas_arguments rdi, rsi, rdx, r10, r8, r9
|
ALWAYS_INLINE long syscall(int syscall, uintptr_t arg1 = 0, uintptr_t arg2 = 0, uintptr_t arg3 = 0, uintptr_t arg4 = 0, uintptr_t arg5 = 0)
|
||||||
#define _kas_globbers rcx, rdx, rdi, rsi, r8, r9, r10, r11
|
{
|
||||||
#elif defined(__i686__)
|
long ret;
|
||||||
#define _kas_instruction "int $0xF0"
|
#if ARCH(x86_64)
|
||||||
#define _kas_result eax
|
register uintptr_t r10 asm("r10") = arg3;
|
||||||
#define _kas_arguments eax, ebx, ecx, edx, esi, edi
|
register uintptr_t r8 asm( "r8") = arg4;
|
||||||
#define _kas_globbers
|
register uintptr_t r9 asm( "r9") = arg5;
|
||||||
|
asm volatile(
|
||||||
|
"syscall"
|
||||||
|
: "=a"(ret)
|
||||||
|
, "+D"(syscall)
|
||||||
|
, "+S"(arg1)
|
||||||
|
, "+d"(arg2)
|
||||||
|
, "+r"(r10)
|
||||||
|
, "+r"(r8)
|
||||||
|
, "+r"(r9)
|
||||||
|
:: "rcx", "r11", "memory");
|
||||||
|
#elif ARCH(i686)
|
||||||
|
asm volatile(
|
||||||
|
"int %[irq]"
|
||||||
|
: "=a"(ret)
|
||||||
|
: [irq]"i"(static_cast<int>(IRQ_SYSCALL)) // WTF GCC 15
|
||||||
|
, "a"(syscall)
|
||||||
|
, "b"(arg1)
|
||||||
|
, "c"(arg2)
|
||||||
|
, "d"(arg3)
|
||||||
|
, "S"(arg4)
|
||||||
|
, "D"(arg5)
|
||||||
|
: "memory");
|
||||||
#endif
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
#define _kas_argument_var(index, value) register long _kas_a##index asm(_ban_stringify(_ban_get(index, _kas_arguments))) = (long)(value);
|
}
|
||||||
#define _kas_dummy_var(index, value) register long _kas_d##index asm(#value);
|
|
||||||
#define _kas_input(index, _) "r"(_kas_a##index)
|
|
||||||
#define _kas_output(index, _) , "=r"(_kas_d##index)
|
|
||||||
#define _kas_globber(_, value) #value
|
|
||||||
|
|
||||||
#define _kas_syscall(...) ({ \
|
|
||||||
register long _kas_ret asm(_ban_stringify(_kas_result)); \
|
|
||||||
_ban_for_each(_kas_argument_var, __VA_ARGS__) \
|
|
||||||
_ban_for_each(_kas_dummy_var, _kas_globbers) \
|
|
||||||
asm volatile( \
|
|
||||||
_kas_instruction \
|
|
||||||
: "=r"(_kas_ret) _ban_for_each(_kas_output, _kas_globbers) \
|
|
||||||
: _ban_for_each_comma(_kas_input, __VA_ARGS__) \
|
|
||||||
: "cc", "memory"); \
|
|
||||||
(void)_kas_a0; /* require 1 argument */ \
|
|
||||||
_kas_ret; \
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<uint8_t> reserve_gsi(uint32_t gsi);
|
BAN::ErrorOr<uint8_t> reserve_gsi(uint32_t gsi);
|
||||||
|
|
||||||
void initialize_timer();
|
void initialize_timer();
|
||||||
void set_timer_dealine(uint64_t ns);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t read_from_local_apic(ptrdiff_t);
|
uint32_t read_from_local_apic(ptrdiff_t);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class AC97AudioController final : public AudioController, public Interruptable
|
class AC97AudioController : public AudioController, public Interruptable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<void> create(PCI::Device& pci_device);
|
static BAN::ErrorOr<void> create(PCI::Device& pci_device);
|
||||||
@@ -23,15 +23,11 @@ namespace Kernel
|
|||||||
uint32_t get_current_pin() const override { return 0; }
|
uint32_t get_current_pin() const override { return 0; }
|
||||||
BAN::ErrorOr<void> set_current_pin(uint32_t pin) override { if (pin != 0) return BAN::Error::from_errno(EINVAL); return {}; }
|
BAN::ErrorOr<void> set_current_pin(uint32_t pin) override { if (pin != 0) return BAN::Error::from_errno(EINVAL); return {}; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> set_volume_mdB(int32_t) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AC97AudioController(PCI::Device& pci_device)
|
AC97AudioController(PCI::Device& pci_device)
|
||||||
: m_pci_device(pci_device)
|
: m_pci_device(pci_device)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
uint32_t get_volume_data() const;
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> initialize();
|
BAN::ErrorOr<void> initialize();
|
||||||
BAN::ErrorOr<void> initialize_bld();
|
BAN::ErrorOr<void> initialize_bld();
|
||||||
BAN::ErrorOr<void> initialize_interrupts();
|
BAN::ErrorOr<void> initialize_interrupts();
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <kernel/Device/Device.h>
|
#include <kernel/Device/Device.h>
|
||||||
#include <kernel/Memory/ByteRingBuffer.h>
|
|
||||||
#include <kernel/PCI.h>
|
#include <kernel/PCI.h>
|
||||||
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -14,11 +11,11 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<void> create(PCI::Device& pci_device);
|
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; }
|
BAN::StringView name() const override { return m_name; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
AudioController();
|
AudioController();
|
||||||
BAN::ErrorOr<void> initialize();
|
|
||||||
|
|
||||||
virtual void handle_new_data() = 0;
|
virtual void handle_new_data() = 0;
|
||||||
|
|
||||||
@@ -29,27 +26,26 @@ namespace Kernel
|
|||||||
virtual uint32_t get_current_pin() const = 0;
|
virtual uint32_t get_current_pin() const = 0;
|
||||||
virtual BAN::ErrorOr<void> set_current_pin(uint32_t) = 0;
|
virtual BAN::ErrorOr<void> set_current_pin(uint32_t) = 0;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> set_volume_mdB(int32_t) = 0;
|
|
||||||
|
|
||||||
bool can_read_impl() const override { return false; }
|
bool can_read_impl() const override { return false; }
|
||||||
bool can_write_impl() const override { SpinLockGuard _(m_spinlock); return !m_sample_data->full(); }
|
bool can_write_impl() const override { SpinLockGuard _(m_spinlock); return m_sample_data_size < m_sample_data_capacity; }
|
||||||
bool has_error_impl() const override { return false; }
|
bool has_error_impl() const override { return false; }
|
||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
BAN::ErrorOr<long> ioctl_impl(int cmd, void* arg) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ThreadBlocker m_sample_data_blocker;
|
ThreadBlocker m_sample_data_blocker;
|
||||||
mutable SpinLock m_spinlock;
|
mutable SpinLock m_spinlock;
|
||||||
|
|
||||||
static constexpr size_t m_sample_data_capacity = 1 << 20;
|
static constexpr size_t m_sample_data_capacity = 1 << 20;
|
||||||
BAN::UniqPtr<ByteRingBuffer> m_sample_data;
|
uint8_t m_sample_data[m_sample_data_capacity];
|
||||||
|
size_t m_sample_data_head { 0 };
|
||||||
snd_volume_info m_volume_info {};
|
size_t m_sample_data_size { 0 };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
char m_name[10] {};
|
char m_name[10] {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace Kernel
|
|||||||
|
|
||||||
class HDAudioController;
|
class HDAudioController;
|
||||||
|
|
||||||
class HDAudioFunctionGroup final : public AudioController
|
class HDAudioFunctionGroup : public AudioController
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<HDAudioFunctionGroup>> create(BAN::RefPtr<HDAudioController>, uint8_t cid, HDAudio::AFGNode&&);
|
static BAN::ErrorOr<BAN::RefPtr<HDAudioFunctionGroup>> create(BAN::RefPtr<HDAudioController>, uint8_t cid, HDAudio::AFGNode&&);
|
||||||
@@ -24,8 +24,6 @@ namespace Kernel
|
|||||||
uint32_t get_current_pin() const override;
|
uint32_t get_current_pin() const override;
|
||||||
BAN::ErrorOr<void> set_current_pin(uint32_t) override;
|
BAN::ErrorOr<void> set_current_pin(uint32_t) override;
|
||||||
|
|
||||||
BAN::ErrorOr<void> set_volume_mdB(int32_t) override;
|
|
||||||
|
|
||||||
void handle_new_data() override;
|
void handle_new_data() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -43,17 +41,20 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> enable_output_path(uint8_t index);
|
BAN::ErrorOr<void> enable_output_path(uint8_t index);
|
||||||
BAN::ErrorOr<void> disable_output_path(uint8_t index);
|
BAN::ErrorOr<void> disable_output_path(uint8_t index);
|
||||||
|
|
||||||
BAN::ErrorOr<void> reset_stream();
|
void reset_stream();
|
||||||
|
|
||||||
BAN::ErrorOr<void> recurse_output_paths(const HDAudio::AFGWidget& widget, BAN::Vector<const HDAudio::AFGWidget*>& path);
|
BAN::ErrorOr<void> recurse_output_paths(const HDAudio::AFGWidget& widget, BAN::Vector<const HDAudio::AFGWidget*>& path);
|
||||||
|
|
||||||
uint16_t get_format_data() const;
|
uint16_t get_format_data() const;
|
||||||
|
uint16_t get_volume_data() const;
|
||||||
|
|
||||||
size_t bdl_offset() const;
|
size_t bdl_offset() const;
|
||||||
|
|
||||||
void queue_bdl_data();
|
void queue_bdl_data();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static constexpr size_t m_max_path_length = 16;
|
||||||
|
|
||||||
// use 6 512 sample BDL entries
|
// use 6 512 sample BDL entries
|
||||||
// each entry is ~10.7 ms at 48 kHz
|
// each entry is ~10.7 ms at 48 kHz
|
||||||
// -> total buffered audio is 64 ms
|
// -> total buffered audio is 64 ms
|
||||||
@@ -65,7 +66,6 @@ namespace Kernel
|
|||||||
const uint8_t m_cid;
|
const uint8_t m_cid;
|
||||||
|
|
||||||
BAN::Vector<BAN::Vector<const HDAudio::AFGWidget*>> m_output_paths;
|
BAN::Vector<BAN::Vector<const HDAudio::AFGWidget*>> m_output_paths;
|
||||||
BAN::Vector<const HDAudio::AFGWidget*> m_output_pins;
|
|
||||||
size_t m_output_path_index { SIZE_MAX };
|
size_t m_output_path_index { SIZE_MAX };
|
||||||
|
|
||||||
uint8_t m_stream_id { 0xFF };
|
uint8_t m_stream_id { 0xFF };
|
||||||
|
|||||||
@@ -50,21 +50,9 @@ namespace Kernel::HDAudio
|
|||||||
{
|
{
|
||||||
bool input;
|
bool input;
|
||||||
bool output;
|
bool output;
|
||||||
bool display; // HDMI or DP
|
|
||||||
uint32_t config;
|
|
||||||
} pin_complex;
|
} pin_complex;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Amplifier
|
|
||||||
{
|
|
||||||
uint8_t offset;
|
|
||||||
uint8_t num_steps;
|
|
||||||
uint8_t step_size;
|
|
||||||
bool mute;
|
|
||||||
};
|
|
||||||
|
|
||||||
BAN::Optional<Amplifier> output_amplifier;
|
|
||||||
|
|
||||||
BAN::Vector<uint16_t> connections;
|
BAN::Vector<uint16_t> connections;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace Kernel::HDAudio
|
|||||||
VMIN = 0x02,
|
VMIN = 0x02,
|
||||||
VMAJ = 0x03,
|
VMAJ = 0x03,
|
||||||
GCTL = 0x08,
|
GCTL = 0x08,
|
||||||
STATESTS = 0x0E,
|
|
||||||
|
|
||||||
INTCTL = 0x20,
|
INTCTL = 0x20,
|
||||||
INTSTS = 0x24,
|
INTSTS = 0x24,
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -44,7 +44,7 @@ namespace Kernel
|
|||||||
struct BootModule
|
struct BootModule
|
||||||
{
|
{
|
||||||
paddr_t start;
|
paddr_t start;
|
||||||
uint64_t size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BootInfo
|
struct BootInfo
|
||||||
|
|||||||
@@ -82,8 +82,5 @@ namespace CPUID
|
|||||||
bool has_pat();
|
bool has_pat();
|
||||||
bool has_1gib_pages();
|
bool has_1gib_pages();
|
||||||
bool has_invariant_tsc();
|
bool has_invariant_tsc();
|
||||||
bool has_rdtscp();
|
|
||||||
uint64_t get_tsc_frequency();
|
|
||||||
bool has_kvm_pvclock();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,8 +49,6 @@
|
|||||||
|
|
||||||
#define DEBUG_VTTY 1
|
#define DEBUG_VTTY 1
|
||||||
|
|
||||||
#define DEBUG_DEVFS 0
|
|
||||||
|
|
||||||
#define DEBUG_PCI 0
|
#define DEBUG_PCI 0
|
||||||
#define DEBUG_SCHEDULER 0
|
#define DEBUG_SCHEDULER 0
|
||||||
#define DEBUG_PS2 1
|
#define DEBUG_PS2 1
|
||||||
@@ -78,7 +76,6 @@
|
|||||||
namespace Debug
|
namespace Debug
|
||||||
{
|
{
|
||||||
void dump_stack_trace();
|
void dump_stack_trace();
|
||||||
void dump_stack_trace(uintptr_t ip, uintptr_t bp);
|
|
||||||
void dump_qr_code();
|
void dump_qr_code();
|
||||||
|
|
||||||
void putchar(char);
|
void putchar(char);
|
||||||
|
|||||||
@@ -8,14 +8,15 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<DebugDevice>> create(mode_t, uid_t, gid_t);
|
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; }
|
virtual BAN::StringView name() const override { return "debug"_sv; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
DebugDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
DebugDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
||||||
: CharacterDevice(mode, uid, gid)
|
: 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> read_impl(off_t, BAN::ByteSpan) override { return 0; }
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override;
|
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 can_write_impl() const override { return true; }
|
||||||
virtual bool has_error_impl() const override { return false; }
|
virtual bool has_error_impl() const override { return false; }
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,20 +12,24 @@ namespace Kernel
|
|||||||
virtual ~Device() = default;
|
virtual ~Device() = default;
|
||||||
virtual void update() {}
|
virtual void update() {}
|
||||||
|
|
||||||
|
virtual bool is_device() const override { return true; }
|
||||||
|
virtual bool is_partition() const { return false; }
|
||||||
|
virtual bool is_storage_device() const { return false; }
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::UniqPtr<MemoryRegion>> mmap_region(PageTable&, off_t offset, size_t len, AddressRange, MemoryRegion::Type, PageTable::flags_t, int status_flags)
|
virtual BAN::ErrorOr<BAN::UniqPtr<MemoryRegion>> mmap_region(PageTable&, off_t offset, size_t len, AddressRange, MemoryRegion::Type, PageTable::flags_t, int status_flags)
|
||||||
{
|
{
|
||||||
(void)offset; (void)len; (void)status_flags;
|
(void)offset; (void)len; (void)status_flags;
|
||||||
return BAN::Error::from_errno(ENOTSUP);
|
return BAN::Error::from_errno(ENOTSUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual dev_t rdev() const override = 0;
|
||||||
|
|
||||||
virtual BAN::StringView name() const = 0;
|
virtual BAN::StringView name() const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Device(mode_t, uid_t, gid_t);
|
Device(mode_t, uid_t, gid_t);
|
||||||
|
|
||||||
private:
|
virtual BAN::ErrorOr<void> fsync_impl() final override { return BAN::Error::from_errno(EINVAL); }
|
||||||
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
|
|
||||||
BAN::ErrorOr<void> sync_data() final override { return {}; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class BlockDevice : public Device, public BAN::Weakable<BlockDevice>
|
class BlockDevice : public Device, public BAN::Weakable<BlockDevice>
|
||||||
@@ -41,7 +45,7 @@ namespace Kernel
|
|||||||
BlockDevice(mode_t mode, uid_t uid, gid_t gid)
|
BlockDevice(mode_t mode, uid_t uid, gid_t gid)
|
||||||
: Device(mode, uid, 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)
|
CharacterDevice(mode_t mode, uid_t uid, gid_t gid)
|
||||||
: Device(mode, uid, gid)
|
: Device(mode, uid, gid)
|
||||||
{
|
{
|
||||||
m_mode |= Inode::Mode::IFCHR;
|
m_inode_info.mode |= Inode::Mode::IFCHR;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,14 +30,13 @@ 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 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(); }
|
virtual BAN::StringView name() const override { return m_name.sv(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl_impl(unsigned long request, void* arg) override;
|
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
virtual bool has_error_impl() const override { return false; }
|
virtual bool has_error_impl() const override { return false; }
|
||||||
@@ -49,6 +48,7 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const BAN::String m_name;
|
const BAN::String m_name;
|
||||||
|
const dev_t m_rdev;
|
||||||
|
|
||||||
vaddr_t m_video_memory_vaddr { 0 };
|
vaddr_t m_video_memory_vaddr { 0 };
|
||||||
const paddr_t m_video_memory_paddr;
|
const paddr_t m_video_memory_paddr;
|
||||||
|
|||||||
@@ -10,14 +10,15 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<NullDevice>> create(mode_t, uid_t, gid_t);
|
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; }
|
virtual BAN::StringView name() const override { return "null"_sv; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
NullDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
NullDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
||||||
: CharacterDevice(mode, uid, gid)
|
: 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> 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(); };
|
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_error_impl() const override { return false; }
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,15 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<RandomDevice>> create(mode_t, uid_t, gid_t);
|
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; }
|
virtual BAN::StringView name() const override { return "random"_sv; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
RandomDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
RandomDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
||||||
: CharacterDevice(mode, uid, gid)
|
: 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> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
|
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 can_write_impl() const override { return false; }
|
||||||
virtual bool has_error_impl() const override { return false; }
|
virtual bool has_error_impl() const override { return false; }
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,15 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ZeroDevice>> create(mode_t, uid_t, gid_t);
|
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; }
|
virtual BAN::StringView name() const override { return "zero"_sv; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ZeroDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
ZeroDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
||||||
: CharacterDevice(mode, uid, gid)
|
: 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> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
|
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 can_write_impl() const override { return false; }
|
||||||
virtual bool has_error_impl() const override { return false; }
|
virtual bool has_error_impl() const override { return false; }
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ namespace Kernel::ELF
|
|||||||
size_t size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool open_execfd;
|
||||||
vaddr_t entry_point;
|
vaddr_t entry_point;
|
||||||
BAN::Optional<vaddr_t> interp_base;
|
|
||||||
BAN::Optional<TLS> master_tls;
|
BAN::Optional<TLS> master_tls;
|
||||||
BAN::Vector<BAN::UniqPtr<MemoryRegion>> regions;
|
BAN::Vector<BAN::UniqPtr<MemoryRegion>> regions;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/Array.h>
|
||||||
|
#include <BAN/CircularQueue.h>
|
||||||
#include <BAN/HashMap.h>
|
#include <BAN/HashMap.h>
|
||||||
|
#include <BAN/HashSet.h>
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/Inode.h>
|
||||||
#include <kernel/Lock/Mutex.h>
|
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
@@ -21,7 +24,24 @@ namespace Kernel
|
|||||||
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
|
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
|
||||||
|
|
||||||
private:
|
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; }
|
const FileSystem* filesystem() const override { return nullptr; }
|
||||||
|
|
||||||
@@ -30,57 +50,60 @@ namespace Kernel
|
|||||||
bool has_error_impl() const override { return false; }
|
bool has_error_impl() const override { return false; }
|
||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
BAN::ErrorOr<void> fsync_impl() override { return {}; }
|
||||||
BAN::ErrorOr<void> sync_data() override { return {}; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct InodeRefPtrHash
|
||||||
|
{
|
||||||
|
BAN::hash_t operator()(const BAN::RefPtr<Inode>& inode)
|
||||||
|
{
|
||||||
|
return BAN::hash<const Inode*>()(inode.ptr());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct ListenEventList
|
struct ListenEventList
|
||||||
{
|
{
|
||||||
ListenEventList() = default;
|
BAN::Array<epoll_event, OPEN_MAX> events;
|
||||||
|
uint32_t bitmap[(OPEN_MAX + 31) / 32] {};
|
||||||
ListenEventList(const ListenEventList&) = delete;
|
|
||||||
ListenEventList& operator=(const ListenEventList&) = delete;
|
|
||||||
|
|
||||||
ListenEventList(ListenEventList&& other)
|
|
||||||
: events(BAN::move(other.events))
|
|
||||||
{}
|
|
||||||
ListenEventList& operator=(ListenEventList&& other)
|
|
||||||
{
|
|
||||||
events = BAN::move(other.events);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::HashMap<int, epoll_event> events;
|
|
||||||
|
|
||||||
bool has_fd(int fd) const
|
bool has_fd(int fd) const
|
||||||
{
|
{
|
||||||
return events.contains(fd);
|
// For some reason having (fd < 0 || ...) makes GCC 15.1.0
|
||||||
|
// think bitmap access can be out of bounds...
|
||||||
|
if (static_cast<size_t>(fd) >= events.size())
|
||||||
|
return false;
|
||||||
|
return bitmap[fd / 32] & (1u << (fd % 32));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const
|
||||||
{
|
{
|
||||||
return events.empty();
|
for (auto val : bitmap)
|
||||||
|
if (val != 0)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> add_fd(int fd, epoll_event event)
|
void add_fd(int fd, epoll_event event)
|
||||||
{
|
{
|
||||||
TRY(events.insert(fd, event));
|
ASSERT(!has_fd(fd));
|
||||||
return {};
|
bitmap[fd / 32] |= (1u << (fd % 32));
|
||||||
|
events[fd] = event;
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove_fd(int fd)
|
void remove_fd(int fd)
|
||||||
{
|
{
|
||||||
events.remove(fd);
|
ASSERT(has_fd(fd));
|
||||||
|
bitmap[fd / 32] &= ~(1u << (fd % 32));
|
||||||
|
events[fd] = {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_mutex;
|
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_thread_blocker;
|
||||||
SpinLock m_ready_lock;
|
SpinLock m_ready_lock;
|
||||||
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t> m_ready_events;
|
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t, InodeRefPtrHash> m_ready_events;
|
||||||
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t> m_processing_events;
|
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t, InodeRefPtrHash> m_processing_events;
|
||||||
BAN::HashMap<BAN::RefPtr<Inode>, ListenEventList> m_listening_events;
|
BAN::HashMap<BAN::RefPtr<Inode>, ListenEventList, InodeRefPtrHash> m_listening_events;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <kernel/FS/Inode.h>
|
|
||||||
#include <kernel/Lock/Mutex.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
|
||||||
{
|
|
||||||
|
|
||||||
class EventFD final : public Inode
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uint64_t initval, bool semaphore);
|
|
||||||
|
|
||||||
private:
|
|
||||||
EventFD(uint64_t initval, bool is_semaphore);
|
|
||||||
|
|
||||||
const FileSystem* filesystem() const override { return nullptr; }
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
|
||||||
BAN::ErrorOr<void> sync_data() override { return {}; }
|
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
|
||||||
BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
|
||||||
|
|
||||||
bool can_read_impl() const override { return m_value > 0; }
|
|
||||||
bool can_write_impl() const override { return m_value < UINT64_MAX - 1; }
|
|
||||||
bool has_error_impl() const override { return false; }
|
|
||||||
bool has_hungup_impl() const override { return false; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
const bool m_is_semaphore;
|
|
||||||
BAN::Atomic<uint64_t> m_value;
|
|
||||||
|
|
||||||
Mutex m_mutex;
|
|
||||||
ThreadBlocker m_thread_blocker;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -79,12 +79,6 @@ namespace Kernel::Ext2
|
|||||||
uint8_t __reserved[12];
|
uint8_t __reserved[12];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct InodeBlocks {
|
|
||||||
uint32_t block[15];
|
|
||||||
};
|
|
||||||
struct Osd2 {
|
|
||||||
uint32_t osd2[3];
|
|
||||||
};
|
|
||||||
struct Inode
|
struct Inode
|
||||||
{
|
{
|
||||||
uint16_t mode;
|
uint16_t mode;
|
||||||
@@ -99,12 +93,12 @@ namespace Kernel::Ext2
|
|||||||
uint32_t blocks;
|
uint32_t blocks;
|
||||||
uint32_t flags;
|
uint32_t flags;
|
||||||
uint32_t osd1;
|
uint32_t osd1;
|
||||||
InodeBlocks block;
|
uint32_t block[15];
|
||||||
uint32_t generation;
|
uint32_t generation;
|
||||||
uint32_t file_acl;
|
uint32_t file_acl;
|
||||||
uint32_t dir_acl;
|
uint32_t dir_acl;
|
||||||
uint32_t faddr;
|
uint32_t faddr;
|
||||||
Osd2 osd2;
|
uint32_t osd2[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LinkedDirectoryEntry
|
struct LinkedDirectoryEntry
|
||||||
|
|||||||
@@ -28,28 +28,36 @@ namespace Kernel
|
|||||||
BAN_NON_COPYABLE(BlockBufferWrapper);
|
BAN_NON_COPYABLE(BlockBufferWrapper);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BlockBufferWrapper(BAN::Span<uint8_t> buffer, void (*callback)(void*, const uint8_t*), void* argument)
|
BlockBufferWrapper(BAN::Span<uint8_t> buffer, bool* used, Mutex* mutex, ThreadBlocker* blocker)
|
||||||
: m_buffer(buffer)
|
: m_buffer(buffer)
|
||||||
, m_callback(callback)
|
, m_used(used)
|
||||||
, m_argument(argument)
|
, m_mutex(mutex)
|
||||||
{ }
|
, m_blocker(blocker)
|
||||||
|
{
|
||||||
|
ASSERT(m_used && *m_used);
|
||||||
|
}
|
||||||
BlockBufferWrapper(BlockBufferWrapper&& other) { *this = BAN::move(other); }
|
BlockBufferWrapper(BlockBufferWrapper&& other) { *this = BAN::move(other); }
|
||||||
~BlockBufferWrapper()
|
~BlockBufferWrapper()
|
||||||
{
|
{
|
||||||
if (m_callback == nullptr)
|
if (m_used == nullptr)
|
||||||
return;
|
return;
|
||||||
m_callback(m_argument, m_buffer.data());
|
m_mutex->lock();
|
||||||
|
*m_used = false;
|
||||||
|
m_blocker->unblock();
|
||||||
|
m_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockBufferWrapper& operator=(BlockBufferWrapper&& other)
|
BlockBufferWrapper& operator=(BlockBufferWrapper&& other)
|
||||||
{
|
{
|
||||||
this->m_buffer = other.m_buffer;
|
this->m_buffer = other.m_buffer;
|
||||||
this->m_callback = other.m_callback;
|
this->m_used = other.m_used;
|
||||||
this->m_argument = other.m_argument;
|
this->m_mutex = other.m_mutex;
|
||||||
|
this->m_blocker = other.m_blocker;
|
||||||
|
|
||||||
other.m_buffer = {};
|
other.m_buffer = {};
|
||||||
other.m_callback = nullptr;
|
other.m_used = nullptr;
|
||||||
other.m_argument = nullptr;
|
other.m_mutex = nullptr;
|
||||||
|
other.m_blocker = nullptr;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -67,8 +75,9 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Span<uint8_t> m_buffer;
|
BAN::Span<uint8_t> m_buffer;
|
||||||
void (*m_callback)(void*, const uint8_t*);
|
bool* m_used;
|
||||||
void* m_argument;
|
Mutex* m_mutex;
|
||||||
|
ThreadBlocker* m_blocker;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -99,8 +108,7 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
|
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
|
||||||
BAN::ErrorOr<void> release_block(uint32_t block);
|
BAN::ErrorOr<void> release_block(uint32_t block);
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> open_inode(ino_t);
|
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>>& inode_cache() { return m_inode_cache; }
|
||||||
void remove_from_cache(ino_t);
|
|
||||||
|
|
||||||
const Ext2::Superblock& superblock() const { return m_superblock; }
|
const Ext2::Superblock& superblock() const { return m_superblock; }
|
||||||
|
|
||||||
@@ -122,9 +130,6 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::ErrorOr<void> initialize(size_t block_size);
|
BAN::ErrorOr<void> initialize(size_t block_size);
|
||||||
|
|
||||||
private:
|
|
||||||
void destroy_callback(const uint8_t* buffer_ptr);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct BlockBuffer
|
struct BlockBuffer
|
||||||
{
|
{
|
||||||
@@ -132,20 +137,10 @@ namespace Kernel
|
|||||||
bool used { false };
|
bool used { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ThreadInfo
|
|
||||||
{
|
|
||||||
pid_t tid { 0 };
|
|
||||||
size_t buffers { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t max_threads = 8;
|
|
||||||
static constexpr size_t max_buffers_per_thread = 6;
|
|
||||||
|
|
||||||
Mutex m_buffer_mutex;
|
Mutex m_buffer_mutex;
|
||||||
ThreadBlocker m_buffer_blocker;
|
ThreadBlocker m_buffer_blocker;
|
||||||
BAN::Array<BlockBuffer, max_threads * max_buffers_per_thread> m_buffers;
|
BAN::Array<BlockBuffer, 16> m_buffers;
|
||||||
BAN::Array<ThreadInfo, max_threads> m_thread_infos;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -156,7 +151,6 @@ namespace Kernel
|
|||||||
BAN::RefPtr<Inode> m_root_inode;
|
BAN::RefPtr<Inode> m_root_inode;
|
||||||
BAN::Vector<uint32_t> m_superblock_backups;
|
BAN::Vector<uint32_t> m_superblock_backups;
|
||||||
|
|
||||||
Mutex m_inode_cache_lock;
|
|
||||||
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>> m_inode_cache;
|
BAN::HashMap<ino_t, BAN::RefPtr<Ext2Inode>> m_inode_cache;
|
||||||
|
|
||||||
BlockBufferManager m_buffer_manager;
|
BlockBufferManager m_buffer_manager;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <BAN/StringView.h>
|
#include <BAN/StringView.h>
|
||||||
#include <kernel/FS/Ext2/Definitions.h>
|
#include <kernel/FS/Ext2/Definitions.h>
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/Inode.h>
|
||||||
#include <kernel/Lock/RWLock.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
@@ -16,12 +15,23 @@ namespace Kernel
|
|||||||
public:
|
public:
|
||||||
~Ext2Inode();
|
~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;
|
virtual const FileSystem* filesystem() const override;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
|
|
||||||
virtual BAN::ErrorOr<void> sync_data() override;
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||||
@@ -36,6 +46,10 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
|
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
||||||
|
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) override;
|
||||||
|
virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) override;
|
||||||
|
virtual BAN::ErrorOr<void> fsync_impl() override;
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
@@ -43,96 +57,57 @@ namespace Kernel
|
|||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t block_group() const;
|
|
||||||
|
|
||||||
// Returns maximum number of data blocks in use
|
// Returns maximum number of data blocks in use
|
||||||
// NOTE: the inode might have more blocks than what this suggests if it has been shrinked
|
// NOTE: the inode might have more blocks than what this suggests if it has been shrinked
|
||||||
uint32_t max_used_data_block_count() const { return size() / blksize(); }
|
uint32_t max_used_data_block_count() const { return size() / blksize(); }
|
||||||
|
|
||||||
BAN::ErrorOr<void> sync_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<void> link_inode_to_directory(Ext2Inode&, BAN::StringView name);
|
||||||
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_no_lock(BAN::StringView);
|
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<void> cleanup_indirect_block(uint32_t block, uint32_t depth);
|
||||||
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<void> cleanup_default_links();
|
||||||
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_from_fs();
|
||||||
|
|
||||||
/* needs write end of the lock */
|
BAN::ErrorOr<void> sync();
|
||||||
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);
|
|
||||||
|
|
||||||
/* needs write end of the lock */
|
uint32_t block_group() const;
|
||||||
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();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino);
|
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino)
|
||||||
|
: m_fs(fs)
|
||||||
BAN::Optional<uint32_t> block_cache_find(uint32_t block, uint32_t index) const;
|
, m_inode(inode)
|
||||||
void block_cache_remove(uint32_t block, uint32_t index);
|
, m_ino(ino)
|
||||||
void block_cache_add(uint32_t block, uint32_t index, uint32_t target);
|
{}
|
||||||
|
static BAN::ErrorOr<BAN::RefPtr<Ext2Inode>> create(Ext2FS&, uint32_t);
|
||||||
BAN::RefPtr<Inode> dir_cache_find(BAN::StringView) const;
|
|
||||||
void dir_cache_remove(BAN::StringView);
|
|
||||||
void dir_cache_add(BAN::StringView, BAN::RefPtr<Inode>);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct ScopedSync
|
struct ScopedSync
|
||||||
{
|
{
|
||||||
ScopedSync(Ext2Inode& inode)
|
ScopedSync(Ext2Inode& inode)
|
||||||
: inode(inode)
|
: inode(inode)
|
||||||
|
, inode_info(inode.m_inode)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
~ScopedSync()
|
~ScopedSync()
|
||||||
{
|
{
|
||||||
// TODO: there was some memcmp smarty pants stuff here.
|
if (memcmp(&inode.m_inode, &inode_info, sizeof(Ext2::Inode)) == 0)
|
||||||
// How do we wanna approach it?
|
return;
|
||||||
if (auto ret = inode.sync_inode_no_lock(); ret.is_error())
|
if (auto ret = inode.sync(); ret.is_error())
|
||||||
dwarnln("failed to sync inode: {}", ret.error());
|
dwarnln("failed to sync inode: {}", ret.error());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ext2Inode& inode;
|
Ext2Inode& inode;
|
||||||
|
Ext2::Inode inode_info;
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ext2FS& m_fs;
|
Ext2FS& m_fs;
|
||||||
RWLock m_lock;
|
Ext2::Inode m_inode;
|
||||||
|
const uint32_t m_ino;
|
||||||
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;
|
|
||||||
|
|
||||||
friend class Ext2FS;
|
friend class Ext2FS;
|
||||||
friend class BAN::RefPtr<Ext2Inode>;
|
friend class BAN::RefPtr<Ext2Inode>;
|
||||||
|
|||||||
@@ -15,14 +15,25 @@ namespace Kernel
|
|||||||
class FATInode final : public Inode, public BAN::Weakable<FATInode>
|
class FATInode final : public Inode, public BAN::Weakable<FATInode>
|
||||||
{
|
{
|
||||||
public:
|
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;
|
virtual const FileSystem* filesystem() const override;
|
||||||
|
|
||||||
const FAT::DirectoryEntry& entry() const { return m_entry; }
|
const FAT::DirectoryEntry& entry() const { return m_entry; }
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
virtual BAN::ErrorOr<void> sync_inode(SyncType) override { return {}; }
|
|
||||||
virtual BAN::ErrorOr<void> sync_data() override { return {}; }
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||||
//virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
//virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||||
@@ -32,6 +43,9 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
//virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
//virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
//virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
//virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
|
//virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
||||||
|
//virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) override;
|
||||||
|
virtual BAN::ErrorOr<void> fsync_impl() override { return {}; }
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return true; }
|
virtual bool can_read_impl() const override { return true; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
@@ -39,8 +53,12 @@ namespace Kernel
|
|||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
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() {}
|
~FATInode() {}
|
||||||
|
|
||||||
BAN::ErrorOr<void> for_each_directory_entry(BAN::ConstByteSpan, BAN::Function<BAN::Iteration(const FAT::DirectoryEntry&)>);
|
BAN::ErrorOr<void> for_each_directory_entry(BAN::ConstByteSpan, BAN::Function<BAN::Iteration(const FAT::DirectoryEntry&)>);
|
||||||
@@ -49,6 +67,7 @@ namespace Kernel
|
|||||||
private:
|
private:
|
||||||
FATFS& m_fs;
|
FATFS& m_fs;
|
||||||
FAT::DirectoryEntry m_entry;
|
FAT::DirectoryEntry m_entry;
|
||||||
|
const ino_t m_ino;
|
||||||
uint32_t m_block_count;
|
uint32_t m_block_count;
|
||||||
|
|
||||||
friend class Ext2FS;
|
friend class Ext2FS;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include <kernel/Credentials.h>
|
#include <kernel/Credentials.h>
|
||||||
#include <kernel/Debug.h>
|
#include <kernel/Debug.h>
|
||||||
|
#include <kernel/Lock/Mutex.h>
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -62,24 +63,6 @@ namespace Kernel
|
|||||||
mode_t mode;
|
mode_t mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum InodeKind : uint8_t
|
|
||||||
{
|
|
||||||
DEVICE = 0x01,
|
|
||||||
EPOLL = 0x02,
|
|
||||||
PIPE = 0x04,
|
|
||||||
TTY = 0x08,
|
|
||||||
PARTITION = 0x10,
|
|
||||||
STORAGE = 0x20,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class SyncType
|
|
||||||
{
|
|
||||||
General,
|
|
||||||
Mode,
|
|
||||||
UidGid,
|
|
||||||
Times,
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~Inode() {}
|
virtual ~Inode() {}
|
||||||
|
|
||||||
@@ -87,26 +70,24 @@ namespace Kernel
|
|||||||
|
|
||||||
bool operator==(const Inode& other) const { return dev() == other.dev() && ino() == other.ino(); }
|
bool operator==(const Inode& other) const { return dev() == other.dev() && ino() == other.ino(); }
|
||||||
|
|
||||||
ino_t ino() const { return m_ino; }
|
virtual ino_t ino() const = 0;
|
||||||
Mode mode() const { return Mode(m_mode); }
|
virtual Mode mode() const = 0;
|
||||||
nlink_t nlink() const { return m_nlink; }
|
virtual nlink_t nlink() const = 0;
|
||||||
uid_t uid() const { return m_uid; }
|
virtual uid_t uid() const = 0;
|
||||||
gid_t gid() const { return m_gid; }
|
virtual gid_t gid() const = 0;
|
||||||
off_t size() const { return m_size; }
|
virtual off_t size() const = 0;
|
||||||
timespec atime() const { return m_atime; }
|
virtual timespec atime() const = 0;
|
||||||
timespec mtime() const { return m_mtime; }
|
virtual timespec mtime() const = 0;
|
||||||
timespec ctime() const { return m_ctime; }
|
virtual timespec ctime() const = 0;
|
||||||
blksize_t blksize() const { return m_blksize; }
|
virtual blksize_t blksize() const = 0;
|
||||||
blkcnt_t blocks() const { return m_blocks; }
|
virtual blkcnt_t blocks() const = 0;
|
||||||
dev_t dev() const { return m_dev; }
|
virtual dev_t dev() const = 0;
|
||||||
dev_t rdev() const { return m_rdev; }
|
virtual dev_t rdev() const = 0;
|
||||||
|
|
||||||
bool is_device() const { return m_kind & InodeKind::DEVICE; }
|
virtual bool is_device() const { return false; }
|
||||||
bool is_epoll() const { return m_kind & InodeKind::EPOLL; }
|
virtual bool is_epoll() const { return false; }
|
||||||
bool is_pipe() const { return m_kind & InodeKind::PIPE; }
|
virtual bool is_pipe() const { return false; }
|
||||||
bool is_tty() const { return m_kind & InodeKind::TTY; }
|
virtual bool is_tty() const { return false; }
|
||||||
bool is_partition() const { return m_kind & InodeKind::PARTITION; }
|
|
||||||
bool is_storage_device() const { return m_kind & InodeKind::STORAGE; }
|
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const = 0;
|
virtual const FileSystem* filesystem() const = 0;
|
||||||
|
|
||||||
@@ -132,8 +113,6 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<size_t> recvmsg(msghdr& message, int flags);
|
BAN::ErrorOr<size_t> recvmsg(msghdr& message, int flags);
|
||||||
BAN::ErrorOr<void> getsockname(sockaddr* address, socklen_t* address_len);
|
BAN::ErrorOr<void> getsockname(sockaddr* address, socklen_t* address_len);
|
||||||
BAN::ErrorOr<void> getpeername(sockaddr* address, socklen_t* address_len);
|
BAN::ErrorOr<void> getpeername(sockaddr* address, socklen_t* address_len);
|
||||||
BAN::ErrorOr<void> getsockopt(int level, int option, void* value, socklen_t* value_len);
|
|
||||||
BAN::ErrorOr<void> setsockopt(int level, int option, const void* value, socklen_t value_len);
|
|
||||||
|
|
||||||
// General API
|
// General API
|
||||||
BAN::ErrorOr<size_t> read(off_t, BAN::ByteSpan buffer);
|
BAN::ErrorOr<size_t> read(off_t, BAN::ByteSpan buffer);
|
||||||
@@ -145,12 +124,12 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<void> fsync();
|
BAN::ErrorOr<void> fsync();
|
||||||
|
|
||||||
// Select/Non blocking API
|
// Select/Non blocking API
|
||||||
bool can_read() const { return can_read_impl(); }
|
bool can_read() const;
|
||||||
bool can_write() const { return can_write_impl(); }
|
bool can_write() const;
|
||||||
bool has_error() const { return has_error_impl(); }
|
bool has_error() const;
|
||||||
bool has_hungup() const { return has_hungup_impl(); }
|
bool has_hungup() const;
|
||||||
|
|
||||||
BAN::ErrorOr<long> ioctl(unsigned long request, void* arg);
|
BAN::ErrorOr<long> ioctl(int request, void* arg);
|
||||||
|
|
||||||
BAN::ErrorOr<void> add_epoll(class Epoll*);
|
BAN::ErrorOr<void> add_epoll(class Epoll*);
|
||||||
void del_epoll(class Epoll*);
|
void del_epoll(class Epoll*);
|
||||||
@@ -159,9 +138,6 @@ namespace Kernel
|
|||||||
virtual void on_close(int status_flags) { (void)status_flags; }
|
virtual void on_close(int status_flags) { (void)status_flags; }
|
||||||
virtual void on_clone(int status_flags) { (void)status_flags; }
|
virtual void on_clone(int status_flags) { (void)status_flags; }
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> sync_inode(SyncType) = 0;
|
|
||||||
virtual BAN::ErrorOr<void> sync_data() = 0;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Directory API
|
// Directory API
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
@@ -185,13 +161,15 @@ namespace Kernel
|
|||||||
virtual BAN::ErrorOr<size_t> sendmsg_impl(const msghdr&, int) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<size_t> sendmsg_impl(const msghdr&, int) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<void> getsockname_impl(sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<void> getpeername_impl(sockaddr*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> getsockopt_impl(int, int, void*, socklen_t*) { return BAN::Error::from_errno(ENOTSUP); }
|
|
||||||
virtual BAN::ErrorOr<void> setsockopt_impl(int, int, const void*, socklen_t) { return BAN::Error::from_errno(ENOTSUP); }
|
|
||||||
|
|
||||||
// General API
|
// General API
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
virtual BAN::ErrorOr<void> utimens_impl(const timespec[2]) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
virtual BAN::ErrorOr<void> fsync_impl() = 0;
|
||||||
|
|
||||||
// Select/Non blocking API
|
// Select/Non blocking API
|
||||||
virtual bool can_read_impl() const = 0;
|
virtual bool can_read_impl() const = 0;
|
||||||
@@ -199,36 +177,15 @@ namespace Kernel
|
|||||||
virtual bool has_error_impl() const = 0;
|
virtual bool has_error_impl() const = 0;
|
||||||
virtual bool has_hungup_impl() const = 0;
|
virtual bool has_hungup_impl() const = 0;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<long> ioctl_impl(unsigned long, void*) { return BAN::Error::from_errno(ENOTSUP); }
|
virtual BAN::ErrorOr<long> ioctl_impl(int, void*) { return BAN::Error::from_errno(ENOTSUP); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// TODO: this is supposed to be const I guess?
|
mutable PriorityMutex m_mutex;
|
||||||
// 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;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpinLock m_shared_region_lock;
|
|
||||||
BAN::WeakPtr<SharedFileData> m_shared_region;
|
BAN::WeakPtr<SharedFileData> m_shared_region;
|
||||||
|
|
||||||
SpinLock m_epoll_lock;
|
SpinLock m_epoll_lock;
|
||||||
BAN::LinkedList<class Epoll*> m_epolls;
|
BAN::LinkedList<class Epoll*> m_epolls;
|
||||||
|
|
||||||
friend class Epoll;
|
friend class Epoll;
|
||||||
friend class FileBackedRegion;
|
friend class FileBackedRegion;
|
||||||
friend class OpenFileDescriptorSet;
|
friend class OpenFileDescriptorSet;
|
||||||
|
|||||||
@@ -1,53 +1,65 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/Array.h>
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/Inode.h>
|
||||||
#include <kernel/Lock/Mutex.h>
|
|
||||||
#include <kernel/Memory/ByteRingBuffer.h>
|
|
||||||
#include <kernel/ThreadBlocker.h>
|
#include <kernel/ThreadBlocker.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
class Pipe final : public Inode, public BAN::Weakable<Pipe>
|
class Pipe : public Inode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Inode>> open(BAN::RefPtr<Inode>, int status_flags);
|
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(const Credentials&);
|
||||||
static BAN::ErrorOr<BAN::RefPtr<Inode>> create(uid_t, gid_t);
|
|
||||||
~Pipe();
|
virtual bool is_pipe() const override { return true; }
|
||||||
|
|
||||||
void on_close(int status_flags) override;
|
void on_close(int status_flags) override;
|
||||||
void on_clone(int status_flags) override;
|
void on_clone(int status_flags) override;
|
||||||
|
|
||||||
|
virtual 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; }
|
virtual const FileSystem* filesystem() const override { return nullptr; }
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
|
|
||||||
virtual BAN::ErrorOr<void> sync_data() override;
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> 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_read_impl() const override { return m_buffer_size > 0; }
|
||||||
virtual bool can_write_impl() const override { return true; }
|
virtual bool can_write_impl() const override { return true; }
|
||||||
virtual bool has_error_impl() const override { return m_reading_count == 0; }
|
virtual bool has_error_impl() const override { return m_reading_count == 0; }
|
||||||
virtual bool has_hungup_impl() const override { return m_writing_count == 0; }
|
virtual bool has_hungup_impl() const override { return m_writing_count == 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pipe(const struct stat&);
|
Pipe(const Credentials&);
|
||||||
|
|
||||||
private:
|
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;
|
ThreadBlocker m_thread_blocker;
|
||||||
|
|
||||||
BAN::UniqPtr<ByteRingBuffer> m_buffer;
|
BAN::Array<uint8_t, PAGE_SIZE> m_buffer;
|
||||||
|
BAN::Atomic<size_t> m_buffer_size { 0 };
|
||||||
|
size_t m_buffer_tail { 0 };
|
||||||
|
|
||||||
BAN::Atomic<uint32_t> m_writing_count { 0 };
|
BAN::Atomic<uint32_t> m_writing_count { 1 };
|
||||||
BAN::Atomic<uint32_t> m_reading_count { 0 };
|
BAN::Atomic<uint32_t> m_reading_count { 1 };
|
||||||
|
|
||||||
BAN::RefPtr<Inode> m_named_inode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ namespace Kernel
|
|||||||
static void initialize();
|
static void initialize();
|
||||||
static ProcFileSystem& get();
|
static ProcFileSystem& get();
|
||||||
|
|
||||||
void post_scheduler_initialize();
|
|
||||||
|
|
||||||
BAN::ErrorOr<void> on_process_create(Process&);
|
BAN::ErrorOr<void> on_process_create(Process&);
|
||||||
void on_process_delete(Process&);
|
void on_process_delete(Process&);
|
||||||
|
|
||||||
|
|||||||
@@ -9,27 +9,34 @@ namespace Kernel
|
|||||||
|
|
||||||
class ProcPidInode final : public TmpDirectoryInode
|
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:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create_new(Process&, TmpFileSystem&, mode_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create_new(Process&, TmpFileSystem&, mode_t);
|
||||||
~ProcPidInode() = default;
|
~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();
|
void cleanup();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override { return BAN::Error::from_errno(EPERM); }
|
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override { return BAN::Error::from_errno(EPERM); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ProcPidInode(Process&, TmpFileSystem&, const TmpInodeInfo&);
|
ProcPidInode(Process&, TmpFileSystem&, const TmpInodeInfo&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Process& m_process;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProcROProcessInode final : public TmpInode
|
class ProcROProcessInode final : public TmpInode
|
||||||
{
|
{
|
||||||
//FIXME: dynamically update ruid/rgid
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcROProcessInode>> create_new(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem&, mode_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcROProcessInode>> create_new(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem&, mode_t);
|
||||||
~ProcROProcessInode() = default;
|
~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:
|
protected:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
|
|
||||||
@@ -52,11 +59,13 @@ namespace Kernel
|
|||||||
|
|
||||||
class ProcSymlinkProcessInode final : public TmpInode
|
class ProcSymlinkProcessInode final : public TmpInode
|
||||||
{
|
{
|
||||||
//FIXME: dynamically update ruid/rgid
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcSymlinkProcessInode>> create_new(Process& process, BAN::ErrorOr<BAN::String> (Process::*callback)() const, TmpFileSystem&, mode_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcSymlinkProcessInode>> create_new(Process& process, BAN::ErrorOr<BAN::String> (Process::*callback)() const, TmpFileSystem&, mode_t);
|
||||||
~ProcSymlinkProcessInode() = default;
|
~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:
|
protected:
|
||||||
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
|
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
|
||||||
|
|
||||||
@@ -76,7 +85,7 @@ namespace Kernel
|
|||||||
class ProcROInode final : public TmpInode
|
class ProcROInode final : public TmpInode
|
||||||
{
|
{
|
||||||
public:
|
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;
|
~ProcROInode() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -92,11 +101,10 @@ namespace Kernel
|
|||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
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:
|
private:
|
||||||
BAN::ErrorOr<size_t> (*m_callback)(off_t, BAN::ByteSpan, void*);
|
size_t (*m_callback)(off_t, BAN::ByteSpan);
|
||||||
void* m_argument;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProcSymlinkInode final : public TmpInode
|
class ProcSymlinkInode final : public TmpInode
|
||||||
@@ -124,10 +132,13 @@ namespace Kernel
|
|||||||
|
|
||||||
class ProcFDDirectoryInode final : public TmpInode
|
class ProcFDDirectoryInode final : public TmpInode
|
||||||
{
|
{
|
||||||
//FIXME: dynamically update ruid/rgid
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcFDDirectoryInode>> create_new(Process&, TmpFileSystem&, mode_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcFDDirectoryInode>> create_new(Process&, TmpFileSystem&, mode_t);
|
||||||
~ProcFDDirectoryInode() = default;
|
~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:
|
protected:
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||||
|
|||||||
@@ -30,19 +30,31 @@ namespace Kernel
|
|||||||
};
|
};
|
||||||
|
|
||||||
public:
|
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; }
|
const FileSystem* filesystem() const final override { return nullptr; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Socket(const Info& info)
|
Socket(const Info& info)
|
||||||
{
|
: m_info(info)
|
||||||
m_mode = info.mode;
|
{}
|
||||||
m_uid = info.uid;
|
|
||||||
m_gid = info.gid;
|
BAN::ErrorOr<void> fsync_impl() final override { return {}; }
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::ErrorOr<void> sync_inode(SyncType) final override { return {}; }
|
const Info m_info;
|
||||||
BAN::ErrorOr<void> sync_data() final override { return {}; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,18 @@
|
|||||||
namespace Kernel
|
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)
|
#if ARCH(x86_64)
|
||||||
// 2x direct blocks
|
// 2x direct blocks
|
||||||
// 1x singly indirect
|
// 1x singly indirect
|
||||||
@@ -31,23 +41,8 @@ namespace Kernel
|
|||||||
#else
|
#else
|
||||||
#error
|
#error
|
||||||
#endif
|
#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 =
|
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 +
|
||||||
(PAGE_SIZE / sizeof(paddr_t)) * (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;
|
(PAGE_SIZE / sizeof(paddr_t)) * (PAGE_SIZE / sizeof(paddr_t)) * (PAGE_SIZE / sizeof(paddr_t)) * PAGE_SIZE;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <BAN/Optional.h>
|
#include <BAN/Optional.h>
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/Inode.h>
|
||||||
#include <kernel/FS/TmpFS/Definitions.h>
|
#include <kernel/FS/TmpFS/Definitions.h>
|
||||||
#include <kernel/Lock/Mutex.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
@@ -24,37 +23,51 @@ namespace Kernel
|
|||||||
|
|
||||||
class TmpInode : public Inode
|
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:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
static BAN::ErrorOr<BAN::RefPtr<TmpInode>> create_from_existing(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
virtual ~TmpInode();
|
~TmpInode();
|
||||||
|
|
||||||
virtual const FileSystem* filesystem() const override;
|
virtual const FileSystem* filesystem() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
|
|
||||||
void write_inode_to_fs();
|
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
|
||||||
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() { return {}; };
|
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_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(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_with_allocation(size_t data_block_index);
|
||||||
BAN::ErrorOr<size_t> block_index_from_indirect_with_allocation_no_lock(size_t& block, size_t index, uint32_t depth);
|
BAN::ErrorOr<size_t> block_index_from_indirect_with_allocation(size_t& block, size_t index, uint32_t depth);
|
||||||
|
|
||||||
private:
|
|
||||||
BAN::ErrorOr<void> sync_inode(SyncType) override;
|
|
||||||
BAN::ErrorOr<void> sync_data() override;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TmpFileSystem& m_fs;
|
TmpFileSystem& m_fs;
|
||||||
TmpBlocks m_tmp_blocks;
|
TmpInodeInfo m_inode_info;
|
||||||
|
const ino_t m_ino;
|
||||||
// TODO: try to reduce locking or replace this with rwlock(?)
|
|
||||||
Mutex m_lock;
|
|
||||||
|
|
||||||
// has to be able to increase link count
|
// has to be able to increase link count
|
||||||
friend class TmpDirectoryInode;
|
friend class TmpDirectoryInode;
|
||||||
@@ -66,7 +79,7 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<TmpFileInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<TmpFileInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
||||||
~TmpFileInode();
|
~TmpFileInode();
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
@@ -82,36 +95,13 @@ namespace Kernel
|
|||||||
friend class TmpInode;
|
friend class TmpInode;
|
||||||
};
|
};
|
||||||
|
|
||||||
// NOTE: this is just a dummy, when opening a fifo a pipe is created
|
|
||||||
class TmpFIFOInode : public TmpInode
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static BAN::ErrorOr<BAN::RefPtr<TmpFIFOInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
|
||||||
~TmpFIFOInode();
|
|
||||||
|
|
||||||
private:
|
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
|
||||||
|
|
||||||
virtual bool can_read_impl() const override { return false; }
|
|
||||||
virtual bool can_write_impl() const override { return false; }
|
|
||||||
virtual bool has_error_impl() const override { return false; }
|
|
||||||
virtual bool has_hungup_impl() const override { return false; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
TmpFIFOInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
|
||||||
|
|
||||||
friend class TmpInode;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TmpSocketInode : public TmpInode
|
class TmpSocketInode : public TmpInode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<TmpSocketInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t);
|
||||||
~TmpSocketInode();
|
~TmpSocketInode();
|
||||||
|
|
||||||
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> read_impl(off_t, BAN::ByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(ENODEV); }
|
||||||
@@ -133,7 +123,7 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
|
static BAN::ErrorOr<BAN::RefPtr<TmpSymlinkInode>> create_new(TmpFileSystem&, mode_t, uid_t, gid_t, BAN::StringView target);
|
||||||
~TmpSymlinkInode();
|
~TmpSymlinkInode();
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
BAN::ErrorOr<BAN::String> link_target_impl() override;
|
BAN::ErrorOr<BAN::String> link_target_impl() override;
|
||||||
BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
|
BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
|
||||||
|
|
||||||
@@ -159,7 +149,7 @@ namespace Kernel
|
|||||||
protected:
|
protected:
|
||||||
TmpDirectoryInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
TmpDirectoryInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> prepare_unlink_no_lock() override;
|
virtual BAN::ErrorOr<void> prepare_unlink() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override final;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override final;
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <kernel/BootInfo.h>
|
#include <kernel/BootInfo.h>
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/FileSystem.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
BAN::ErrorOr<bool> unpack_boot_module_into_directory(BAN::RefPtr<Inode>, const BootModule&);
|
bool is_ustar_boot_module(const BootModule&);
|
||||||
|
BAN::ErrorOr<void> unpack_boot_module_into_filesystem(BAN::RefPtr<FileSystem>, const BootModule&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,9 +92,8 @@ namespace Kernel
|
|||||||
MountPoint* mount_from_root_inode(BAN::RefPtr<Inode>);
|
MountPoint* mount_from_root_inode(BAN::RefPtr<Inode>);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Mutex m_mutex;
|
||||||
BAN::RefPtr<FileSystem> m_root_fs;
|
BAN::RefPtr<FileSystem> m_root_fs;
|
||||||
|
|
||||||
Mutex m_mount_point_lock;
|
|
||||||
BAN::Vector<MountPoint> m_mount_points;
|
BAN::Vector<MountPoint> m_mount_points;
|
||||||
|
|
||||||
friend class BAN::RefPtr<VirtualFileSystem>;
|
friend class BAN::RefPtr<VirtualFileSystem>;
|
||||||
|
|||||||
@@ -133,12 +133,6 @@ namespace Kernel
|
|||||||
void set_gsbase(uintptr_t addr);
|
void set_gsbase(uintptr_t addr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static uint16_t cpu_index_offset() { return m_cpu_index_offset; }
|
|
||||||
void set_cpu_index(uint8_t index)
|
|
||||||
{
|
|
||||||
write_entry(m_cpu_index_offset, 0, index, 0xF2, 0x4);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GDT() = default;
|
GDT() = default;
|
||||||
|
|
||||||
@@ -157,13 +151,11 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
#if ARCH(x86_64)
|
#if ARCH(x86_64)
|
||||||
BAN::Array<SegmentDescriptor, 9> m_gdt; // null, kernel code, kernel data, user code (32 bit), user data, user code (64 bit), cpu-index, tss low, tss high
|
BAN::Array<SegmentDescriptor, 8> m_gdt; // null, kernel code, kernel data, user code (32 bit), user data, user code (64 bit), tss low, tss high
|
||||||
static constexpr uint16_t m_cpu_index_offset = 0x30;
|
static constexpr uint16_t m_tss_offset = 0x30;
|
||||||
static constexpr uint16_t m_tss_offset = 0x38;
|
|
||||||
#elif ARCH(i686)
|
#elif ARCH(i686)
|
||||||
BAN::Array<SegmentDescriptor, 10> m_gdt; // null, kernel code, kernel data, user code, user data, processor data, fsbase, gsbase, cpu-index, tss
|
BAN::Array<SegmentDescriptor, 9> m_gdt; // null, kernel code, kernel data, user code, user data, processor data, fsbase, gsbase, tss
|
||||||
static constexpr uint16_t m_cpu_index_offset = 0x40;
|
static constexpr uint16_t m_tss_offset = 0x40;
|
||||||
static constexpr uint16_t m_tss_offset = 0x48;
|
|
||||||
#endif
|
#endif
|
||||||
TaskStateSegment m_tss;
|
TaskStateSegment m_tss;
|
||||||
const GDTR m_gdtr {
|
const GDTR m_gdtr {
|
||||||
|
|||||||
@@ -20,10 +20,11 @@ namespace Kernel
|
|||||||
constexpr uint8_t IRQ_MSI_BASE = 0x80;
|
constexpr uint8_t IRQ_MSI_BASE = 0x80;
|
||||||
constexpr uint8_t IRQ_MSI_END = 0xF0;
|
constexpr uint8_t IRQ_MSI_END = 0xF0;
|
||||||
#if ARCH(i686)
|
#if ARCH(i686)
|
||||||
constexpr uint8_t IRQ_SYSCALL = 0xF0; // hard coded in kernel/API/Syscall.h
|
constexpr uint8_t IRQ_SYSCALL = 0xF0;
|
||||||
#endif
|
#endif
|
||||||
constexpr uint8_t IRQ_IPI = 0xF1;
|
constexpr uint8_t IRQ_YIELD = 0xF1;
|
||||||
constexpr uint8_t IRQ_TIMER = 0xF2;
|
constexpr uint8_t IRQ_IPI = 0xF2;
|
||||||
|
constexpr uint8_t IRQ_TIMER = 0xF3;
|
||||||
|
|
||||||
#if ARCH(x86_64)
|
#if ARCH(x86_64)
|
||||||
struct GateDescriptor
|
struct GateDescriptor
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ namespace Kernel
|
|||||||
InputDevice(Type type);
|
InputDevice(Type type);
|
||||||
|
|
||||||
BAN::StringView name() const final override { return m_name; }
|
BAN::StringView name() const final override { return m_name; }
|
||||||
|
dev_t rdev() const final override { return m_rdev; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void add_event(BAN::ConstByteSpan);
|
void add_event(BAN::ConstByteSpan);
|
||||||
|
|
||||||
@@ -36,7 +38,8 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<size_t> read_non_block(BAN::ByteSpan);
|
BAN::ErrorOr<size_t> read_non_block(BAN::ByteSpan);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::String m_name;
|
const dev_t m_rdev;
|
||||||
|
const BAN::String m_name;
|
||||||
|
|
||||||
const Type m_type;
|
const Type m_type;
|
||||||
|
|
||||||
@@ -61,7 +64,6 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid);
|
static BAN::ErrorOr<BAN::RefPtr<KeyboardDevice>> create(mode_t mode, uid_t uid, gid_t gid);
|
||||||
static BAN::ErrorOr<void> initialize_tty_thread();
|
|
||||||
|
|
||||||
void notify();
|
void notify();
|
||||||
|
|
||||||
@@ -75,8 +77,10 @@ namespace Kernel
|
|||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
BAN::StringView name() const final override { return m_name; }
|
BAN::StringView name() const final override { return m_name; }
|
||||||
|
dev_t rdev() const final override { return m_rdev; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
const BAN::StringView m_name;
|
const BAN::StringView m_name;
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_thread_blocker;
|
||||||
|
|
||||||
@@ -100,7 +104,10 @@ namespace Kernel
|
|||||||
bool has_hungup_impl() const override { return false; }
|
bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
BAN::StringView name() const final override { return m_name; }
|
BAN::StringView name() const final override { return m_name; }
|
||||||
|
dev_t rdev() const final override { return m_rdev; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const dev_t m_rdev;
|
||||||
const BAN::StringView m_name;
|
const BAN::StringView m_name;
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_thread_blocker;
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ namespace Kernel
|
|||||||
uintptr_t r10;
|
uintptr_t r10;
|
||||||
uintptr_t r9;
|
uintptr_t r9;
|
||||||
uintptr_t r8;
|
uintptr_t r8;
|
||||||
|
|
||||||
uintptr_t rdi;
|
uintptr_t rdi;
|
||||||
uintptr_t rsi;
|
uintptr_t rsi;
|
||||||
uintptr_t rbp;
|
uintptr_t rbp;
|
||||||
@@ -35,18 +36,6 @@ namespace Kernel
|
|||||||
uintptr_t rcx;
|
uintptr_t rcx;
|
||||||
uintptr_t rax;
|
uintptr_t rax;
|
||||||
};
|
};
|
||||||
struct YieldRegisters
|
|
||||||
{
|
|
||||||
uintptr_t r15;
|
|
||||||
uintptr_t r14;
|
|
||||||
uintptr_t r13;
|
|
||||||
uintptr_t r12;
|
|
||||||
uintptr_t rbp;
|
|
||||||
uintptr_t rbx;
|
|
||||||
uintptr_t ret;
|
|
||||||
uintptr_t sp;
|
|
||||||
uintptr_t ip;
|
|
||||||
};
|
|
||||||
#elif ARCH(i686)
|
#elif ARCH(i686)
|
||||||
struct InterruptRegisters
|
struct InterruptRegisters
|
||||||
{
|
{
|
||||||
@@ -59,16 +48,6 @@ namespace Kernel
|
|||||||
uintptr_t ecx;
|
uintptr_t ecx;
|
||||||
uintptr_t eax;
|
uintptr_t eax;
|
||||||
};
|
};
|
||||||
struct YieldRegisters
|
|
||||||
{
|
|
||||||
uintptr_t ebp;
|
|
||||||
uintptr_t edi;
|
|
||||||
uintptr_t esi;
|
|
||||||
uintptr_t ebx;
|
|
||||||
uintptr_t ret;
|
|
||||||
uintptr_t sp;
|
|
||||||
uintptr_t ip;
|
|
||||||
};
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename Lock> requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
|
template<typename Lock>
|
||||||
class LockGuard
|
class LockGuard
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(LockGuard);
|
BAN_NON_COPYABLE(LockGuard);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Kernel
|
|||||||
virtual uint32_t lock_depth() const = 0;
|
virtual uint32_t lock_depth() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mutex final : public BaseMutex
|
class Mutex : public BaseMutex
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(Mutex);
|
BAN_NON_COPYABLE(Mutex);
|
||||||
BAN_NON_MOVABLE(Mutex);
|
BAN_NON_MOVABLE(Mutex);
|
||||||
@@ -40,7 +40,6 @@ namespace Kernel
|
|||||||
pid_t expected = -1;
|
pid_t expected = -1;
|
||||||
while (!m_locker.compare_exchange(expected, tid))
|
while (!m_locker.compare_exchange(expected, tid))
|
||||||
{
|
{
|
||||||
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
|
|
||||||
Processor::yield();
|
Processor::yield();
|
||||||
expected = -1;
|
expected = -1;
|
||||||
}
|
}
|
||||||
@@ -85,14 +84,13 @@ namespace Kernel
|
|||||||
pid_t locker() const override { return m_locker; }
|
pid_t locker() const override { return m_locker; }
|
||||||
bool is_locked() const override { return m_locker != -1; }
|
bool is_locked() const override { return m_locker != -1; }
|
||||||
uint32_t lock_depth() const override { return m_lock_depth; }
|
uint32_t lock_depth() const override { return m_lock_depth; }
|
||||||
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Atomic<pid_t> m_locker { -1 };
|
BAN::Atomic<pid_t> m_locker { -1 };
|
||||||
uint32_t m_lock_depth { 0 };
|
uint32_t m_lock_depth { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
class PriorityMutex final : public BaseMutex
|
class PriorityMutex : public BaseMutex
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(PriorityMutex);
|
BAN_NON_COPYABLE(PriorityMutex);
|
||||||
BAN_NON_MOVABLE(PriorityMutex);
|
BAN_NON_MOVABLE(PriorityMutex);
|
||||||
@@ -115,7 +113,6 @@ namespace Kernel
|
|||||||
pid_t expected = -1;
|
pid_t expected = -1;
|
||||||
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
|
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
|
||||||
{
|
{
|
||||||
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
|
|
||||||
Processor::yield();
|
Processor::yield();
|
||||||
expected = -1;
|
expected = -1;
|
||||||
}
|
}
|
||||||
@@ -167,7 +164,6 @@ namespace Kernel
|
|||||||
pid_t locker() const override { return m_locker; }
|
pid_t locker() const override { return m_locker; }
|
||||||
bool is_locked() const override { return m_locker != -1; }
|
bool is_locked() const override { return m_locker != -1; }
|
||||||
uint32_t lock_depth() const override { return m_lock_depth; }
|
uint32_t lock_depth() const override { return m_lock_depth; }
|
||||||
bool is_locked_by_current_thread() const { return m_locker == Thread::current_tid(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Atomic<pid_t> m_locker { -1 };
|
BAN::Atomic<pid_t> m_locker { -1 };
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <kernel/Lock/SpinLock.h>
|
#include <kernel/Lock/Mutex.h>
|
||||||
#include <kernel/Lock/SpinLockAsMutex.h>
|
#include <kernel/Lock/LockGuard.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
@@ -15,60 +15,43 @@ namespace Kernel
|
|||||||
|
|
||||||
void rd_lock()
|
void rd_lock()
|
||||||
{
|
{
|
||||||
SpinLockGuard _(m_lock);
|
LockGuard _(m_mutex);
|
||||||
while (m_writers_waiting > 0 || m_writer != -1)
|
while (m_writers_waiting > 0 || m_writer_active)
|
||||||
{
|
m_thread_blocker.block_indefinite(&m_mutex);
|
||||||
SpinLockGuardAsMutex smutex(_);
|
|
||||||
m_thread_blocker.block_indefinite(&smutex);
|
|
||||||
}
|
|
||||||
m_readers_active++;
|
m_readers_active++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rd_unlock()
|
void rd_unlock()
|
||||||
{
|
{
|
||||||
SpinLockGuard _(m_lock);
|
LockGuard _(m_mutex);
|
||||||
if (--m_readers_active == 0)
|
if (--m_readers_active == 0)
|
||||||
m_thread_blocker.unblock();
|
m_thread_blocker.unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wr_lock()
|
void wr_lock()
|
||||||
{
|
{
|
||||||
if (m_writer == Thread::current_tid())
|
LockGuard _(m_mutex);
|
||||||
{
|
|
||||||
m_writer_depth++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SpinLockGuard _(m_lock);
|
|
||||||
|
|
||||||
m_writers_waiting++;
|
m_writers_waiting++;
|
||||||
while (m_readers_active > 0 || m_writer != -1)
|
while (m_readers_active > 0 || m_writer_active)
|
||||||
{
|
m_thread_blocker.block_indefinite(&m_mutex);
|
||||||
SpinLockGuardAsMutex smutex(_);
|
|
||||||
m_thread_blocker.block_indefinite(&smutex);
|
|
||||||
}
|
|
||||||
m_writers_waiting--;
|
m_writers_waiting--;
|
||||||
|
m_writer_active = true;
|
||||||
m_writer = Thread::current_tid();
|
|
||||||
m_writer_depth = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void wr_unlock()
|
void wr_unlock()
|
||||||
{
|
{
|
||||||
if (--m_writer_depth != 0)
|
LockGuard _(m_mutex);
|
||||||
return;
|
m_writer_active = false;
|
||||||
SpinLockGuard _(m_lock);
|
|
||||||
m_writer = -1;
|
|
||||||
m_thread_blocker.unblock();
|
m_thread_blocker.unblock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpinLock m_lock;
|
Mutex m_mutex;
|
||||||
ThreadBlocker m_thread_blocker;
|
ThreadBlocker m_thread_blocker;
|
||||||
uint32_t m_readers_active { 0 };
|
uint32_t m_readers_active { 0 };
|
||||||
uint32_t m_writers_waiting { 0 };
|
uint32_t m_writers_waiting { 0 };
|
||||||
pid_t m_writer { -1 };
|
bool m_writer_active { false };
|
||||||
uint32_t m_writer_depth { 0 };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class RWLockRDGuard
|
class RWLockRDGuard
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace Kernel
|
|||||||
uint32_t lock_depth() const override { return m_lock_depth; }
|
uint32_t lock_depth() const override { return m_lock_depth; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Lock& m_lock;
|
SpinLock& m_lock;
|
||||||
uint32_t m_lock_depth { 0 };
|
uint32_t m_lock_depth { 0 };
|
||||||
InterruptState m_state;
|
InterruptState m_state;
|
||||||
const pid_t m_locker;
|
const pid_t m_locker;
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <BAN/ByteSpan.h>
|
|
||||||
#include <BAN/UniqPtr.h>
|
|
||||||
#include <BAN/Vector.h>
|
|
||||||
|
|
||||||
#include <kernel/Memory/Types.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
|
||||||
{
|
|
||||||
|
|
||||||
class ByteRingBuffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<ByteRingBuffer>> create(size_t size);
|
|
||||||
~ByteRingBuffer();
|
|
||||||
|
|
||||||
void push(BAN::ConstByteSpan data)
|
|
||||||
{
|
|
||||||
ASSERT(data.size() + m_size <= m_capacity);
|
|
||||||
uint8_t* buffer_head = reinterpret_cast<uint8_t*>(m_vaddr) + (m_tail + m_size) % m_capacity;
|
|
||||||
memcpy(buffer_head, data.data(), data.size());
|
|
||||||
m_size += data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
void pop(size_t size)
|
|
||||||
{
|
|
||||||
ASSERT(size <= m_size);
|
|
||||||
m_tail = (m_tail + size) % m_capacity;
|
|
||||||
m_size -= size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void pop_back(size_t size)
|
|
||||||
{
|
|
||||||
ASSERT(size <= m_size);
|
|
||||||
m_size -= size;
|
|
||||||
}
|
|
||||||
|
|
||||||
BAN::ConstByteSpan get_data() const
|
|
||||||
{
|
|
||||||
const uint8_t* base = reinterpret_cast<const uint8_t*>(m_vaddr);
|
|
||||||
return { base + m_tail, m_size };
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t front() const
|
|
||||||
{
|
|
||||||
ASSERT(!empty());
|
|
||||||
return reinterpret_cast<const uint8_t*>(m_vaddr)[m_tail];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t back() const
|
|
||||||
{
|
|
||||||
ASSERT(!empty());
|
|
||||||
return reinterpret_cast<const uint8_t*>(m_vaddr)[m_tail + m_size - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const { return m_size == 0; }
|
|
||||||
bool full() const { return m_size == m_capacity; }
|
|
||||||
size_t free() const { return m_capacity - m_size; }
|
|
||||||
size_t size() const { return m_size; }
|
|
||||||
size_t capacity() const { return m_capacity; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
ByteRingBuffer(size_t capacity)
|
|
||||||
: m_capacity(capacity)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
private:
|
|
||||||
size_t m_size { 0 };
|
|
||||||
size_t m_tail { 0 };
|
|
||||||
const size_t m_capacity;
|
|
||||||
|
|
||||||
vaddr_t m_vaddr { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@ namespace Kernel
|
|||||||
class DMARegion
|
class DMARegion
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<DMARegion>> create(size_t size, PageTable::MemoryType type = PageTable::MemoryType::Uncached);
|
static BAN::ErrorOr<BAN::UniqPtr<DMARegion>> create(size_t size);
|
||||||
~DMARegion();
|
~DMARegion();
|
||||||
|
|
||||||
size_t size() const { return m_size; }
|
size_t size() const { return m_size; }
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <kernel/FS/Inode.h>
|
#include <kernel/FS/Inode.h>
|
||||||
#include <kernel/Lock/RWLock.h>
|
|
||||||
#include <kernel/Memory/MemoryRegion.h>
|
#include <kernel/Memory/MemoryRegion.h>
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
@@ -11,15 +10,15 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
~SharedFileData();
|
~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
|
// FIXME: this should probably be ordered tree like map
|
||||||
// for fast lookup and less memory usage
|
// for fast lookup and less memory usage
|
||||||
BAN::Vector<paddr_t> pages;
|
BAN::Vector<paddr_t> pages;
|
||||||
BAN::Vector<uint32_t> writers;
|
|
||||||
BAN::RefPtr<Inode> inode;
|
BAN::RefPtr<Inode> inode;
|
||||||
|
uint8_t page_buffer[PAGE_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
class FileBackedRegion final : public MemoryRegion
|
class FileBackedRegion final : public MemoryRegion
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ namespace Kernel
|
|||||||
static void initialize();
|
static void initialize();
|
||||||
static Heap& get();
|
static Heap& get();
|
||||||
|
|
||||||
void release_boot_modules();
|
|
||||||
|
|
||||||
paddr_t take_free_page();
|
paddr_t take_free_page();
|
||||||
void release_page(paddr_t);
|
void release_page(paddr_t);
|
||||||
|
|
||||||
|
|||||||
@@ -28,20 +28,6 @@ namespace Kernel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
MemoryBackedRegion(PageTable&, size_t size, Type, PageTable::flags_t, int status_flags);
|
MemoryBackedRegion(PageTable&, size_t size, Type, PageTable::flags_t, int status_flags);
|
||||||
|
|
||||||
private:
|
|
||||||
struct PhysicalPage
|
|
||||||
{
|
|
||||||
PhysicalPage(paddr_t paddr)
|
|
||||||
: paddr(paddr)
|
|
||||||
{ }
|
|
||||||
~PhysicalPage();
|
|
||||||
|
|
||||||
BAN::Atomic<uint32_t> ref_count { 1 };
|
|
||||||
const paddr_t paddr;
|
|
||||||
};
|
|
||||||
BAN::Vector<PhysicalPage*> m_physical_pages;
|
|
||||||
Mutex m_mutex;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,12 @@
|
|||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct AddressRange
|
||||||
|
{
|
||||||
|
vaddr_t start;
|
||||||
|
vaddr_t end;
|
||||||
|
};
|
||||||
|
|
||||||
class MemoryRegion
|
class MemoryRegion
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(MemoryRegion);
|
BAN_NON_COPYABLE(MemoryRegion);
|
||||||
|
|||||||
@@ -14,12 +14,6 @@ namespace Kernel
|
|||||||
requires BAN::is_same_v<decltype(func()), void>;
|
requires BAN::is_same_v<decltype(func()), void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
concept with_per_cpu_fast_page_callback = requires(F func, void* addr)
|
|
||||||
{
|
|
||||||
requires BAN::is_same_v<decltype(func(addr)), void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
concept with_fast_page_callback_error = requires(F func)
|
concept with_fast_page_callback_error = requires(F func)
|
||||||
{
|
{
|
||||||
@@ -51,27 +45,14 @@ namespace Kernel
|
|||||||
WriteThrough,
|
WriteThrough,
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr bool full_tlb_flush_threshold = 32;
|
|
||||||
|
|
||||||
static constexpr size_t reserved_fast_pages = 0x10;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void initialize_fast_page();
|
static void initialize_pre_heap();
|
||||||
static void initialize_and_load();
|
static void initialize_post_heap();
|
||||||
|
|
||||||
static void enable_cpu_features();
|
|
||||||
|
|
||||||
static PageTable& kernel();
|
static PageTable& kernel();
|
||||||
static PageTable& current() { return *reinterpret_cast<PageTable*>(Processor::get_current_page_table()); }
|
static PageTable& current() { return *reinterpret_cast<PageTable*>(Processor::get_current_page_table()); }
|
||||||
|
|
||||||
static constexpr vaddr_t fast_page()
|
static constexpr vaddr_t fast_page() { return KERNEL_OFFSET; }
|
||||||
{
|
|
||||||
#if ARCH(x86_64)
|
|
||||||
return 0xffffffffbfe00000;
|
|
||||||
#elif ARCH(i686)
|
|
||||||
return 0xffe00000;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<with_fast_page_callback F>
|
template<with_fast_page_callback F>
|
||||||
static void with_fast_page(paddr_t paddr, F callback)
|
static void with_fast_page(paddr_t paddr, F callback)
|
||||||
@@ -82,18 +63,6 @@ namespace Kernel
|
|||||||
unmap_fast_page();
|
unmap_fast_page();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<with_per_cpu_fast_page_callback F>
|
|
||||||
static void with_per_cpu_fast_page(paddr_t paddr, F callback)
|
|
||||||
{
|
|
||||||
const auto state = Processor::get_interrupt_state();
|
|
||||||
Processor::set_interrupt_state(InterruptState::Disabled);
|
|
||||||
const size_t index = Processor::current_index() + reserved_fast_pages;
|
|
||||||
void* addr = map_fast_page(index, paddr);
|
|
||||||
callback(addr);
|
|
||||||
unmap_fast_page(index);
|
|
||||||
Processor::set_interrupt_state(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<with_fast_page_callback_error F>
|
template<with_fast_page_callback_error F>
|
||||||
static BAN::ErrorOr<void> with_fast_page(paddr_t paddr, F callback)
|
static BAN::ErrorOr<void> with_fast_page(paddr_t paddr, F callback)
|
||||||
{
|
{
|
||||||
@@ -131,50 +100,44 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<PageTable*> create_userspace();
|
static BAN::ErrorOr<PageTable*> create_userspace();
|
||||||
~PageTable();
|
~PageTable();
|
||||||
|
|
||||||
void unmap_page(vaddr_t, bool invalidate = true);
|
void unmap_page(vaddr_t, bool send_smp_message = true);
|
||||||
void unmap_range(vaddr_t, size_t bytes);
|
void unmap_range(vaddr_t, size_t bytes);
|
||||||
|
|
||||||
void map_page_at(paddr_t, vaddr_t, flags_t, MemoryType = MemoryType::Normal, bool invalidate = true);
|
void map_page_at(paddr_t, vaddr_t, flags_t, MemoryType = MemoryType::Normal, bool send_smp_message = true);
|
||||||
void map_range_at(paddr_t, vaddr_t, size_t bytes, flags_t, MemoryType = MemoryType::Normal);
|
void map_range_at(paddr_t, vaddr_t, size_t bytes, flags_t, MemoryType = MemoryType::Normal);
|
||||||
|
|
||||||
void remove_writable_from_range(vaddr_t, size_t);
|
|
||||||
|
|
||||||
paddr_t physical_address_of(vaddr_t) const;
|
paddr_t physical_address_of(vaddr_t) const;
|
||||||
flags_t get_page_flags(vaddr_t) const;
|
flags_t get_page_flags(vaddr_t) const;
|
||||||
|
|
||||||
bool is_page_free(vaddr_t) const;
|
bool is_page_free(vaddr_t) const;
|
||||||
bool is_range_free(vaddr_t, size_t bytes) const;
|
bool is_range_free(vaddr_t, size_t bytes) const;
|
||||||
|
|
||||||
void reserve_page(vaddr_t);
|
bool reserve_page(vaddr_t, bool only_free = true, bool send_smp_message = true);
|
||||||
void reserve_range(vaddr_t, size_t bytes);
|
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_page(vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
||||||
vaddr_t reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
vaddr_t reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address = UINTPTR_MAX);
|
||||||
|
|
||||||
void load();
|
void load();
|
||||||
|
void initial_load();
|
||||||
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(); }
|
InterruptState lock() const { return m_lock.lock(); }
|
||||||
void unlock(InterruptState state) const { m_lock.unlock(state); }
|
void unlock(InterruptState state) const { m_lock.unlock(state); }
|
||||||
|
|
||||||
paddr_t paddr() const { return m_highest_paging_struct; }
|
|
||||||
|
|
||||||
void debug_dump();
|
void debug_dump();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PageTable() = default;
|
PageTable() = default;
|
||||||
uint64_t get_page_data(vaddr_t) const;
|
uint64_t get_page_data(vaddr_t) const;
|
||||||
|
void initialize_kernel();
|
||||||
void map_kernel_memory();
|
void map_kernel_memory();
|
||||||
|
void prepare_fast_page();
|
||||||
|
|
||||||
|
static void invalidate(vaddr_t, bool send_smp_message);
|
||||||
|
|
||||||
static void map_fast_page(paddr_t);
|
static void map_fast_page(paddr_t);
|
||||||
static void unmap_fast_page();
|
static void unmap_fast_page();
|
||||||
|
|
||||||
static void* map_fast_page(size_t index, paddr_t);
|
|
||||||
static void unmap_fast_page(size_t index);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
paddr_t m_highest_paging_struct { 0 };
|
paddr_t m_highest_paging_struct { 0 };
|
||||||
mutable RecursiveSpinLock m_lock;
|
mutable RecursiveSpinLock m_lock;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Kernel
|
|||||||
class PhysicalRange
|
class PhysicalRange
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PhysicalRange(paddr_t, uint64_t);
|
PhysicalRange(paddr_t, size_t);
|
||||||
|
|
||||||
paddr_t reserve_page();
|
paddr_t reserve_page();
|
||||||
void release_page(paddr_t);
|
void release_page(paddr_t);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#if ARCH(x86_64)
|
#if ARCH(x86_64)
|
||||||
#define KERNEL_OFFSET 0xFFFFFFFF80000000
|
#define KERNEL_OFFSET 0xFFFFFFFF80000000
|
||||||
#define USERSPACE_END 0x800000000000
|
#define USERSPACE_END 0xFFFF800000000000
|
||||||
#elif ARCH(i686)
|
#elif ARCH(i686)
|
||||||
#define KERNEL_OFFSET 0xC0000000
|
#define KERNEL_OFFSET 0xC0000000
|
||||||
#define USERSPACE_END 0xC0000000
|
#define USERSPACE_END 0xC0000000
|
||||||
@@ -23,10 +23,4 @@ namespace Kernel
|
|||||||
using vaddr_t = uintptr_t;
|
using vaddr_t = uintptr_t;
|
||||||
using paddr_t = uint64_t;
|
using paddr_t = uint64_t;
|
||||||
|
|
||||||
struct AddressRange
|
|
||||||
{
|
|
||||||
vaddr_t start;
|
|
||||||
vaddr_t end;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,27 +14,44 @@ namespace Kernel
|
|||||||
BAN_NON_MOVABLE(VirtualRange);
|
BAN_NON_MOVABLE(VirtualRange);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr_range(PageTable&, AddressRange address_range, size_t, PageTable::flags_t flags, bool add_guard_pages);
|
// Create virtual range to fixed virtual address
|
||||||
|
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr(PageTable&, vaddr_t, size_t, PageTable::flags_t flags, bool preallocate_pages, bool add_guard_pages);
|
||||||
|
// Create virtual range to virtual address range
|
||||||
|
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr_range(PageTable&, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t, PageTable::flags_t flags, bool preallocate_pages, bool add_guard_pages);
|
||||||
~VirtualRange();
|
~VirtualRange();
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> clone(PageTable&);
|
||||||
|
|
||||||
vaddr_t vaddr() const { return m_vaddr + (m_has_guard_pages ? PAGE_SIZE : 0); }
|
vaddr_t vaddr() const { return m_vaddr + (m_has_guard_pages ? PAGE_SIZE : 0); }
|
||||||
size_t size() const { return m_size - (m_has_guard_pages ? 2 * PAGE_SIZE : 0); }
|
size_t size() const { return m_size - (m_has_guard_pages ? 2 * PAGE_SIZE : 0); }
|
||||||
PageTable::flags_t flags() const { return m_flags; }
|
PageTable::flags_t flags() const { return m_flags; }
|
||||||
|
|
||||||
paddr_t paddr_of(vaddr_t vaddr) const { return m_page_table.physical_address_of(vaddr & PAGE_ADDR_MASK); }
|
paddr_t paddr_of(vaddr_t vaddr) const
|
||||||
|
{
|
||||||
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
||||||
|
const size_t index = (vaddr - this->vaddr()) / PAGE_SIZE;
|
||||||
|
ASSERT(index < m_paddrs.size());
|
||||||
|
const paddr_t paddr = m_paddrs[index];
|
||||||
|
ASSERT(paddr);
|
||||||
|
return paddr;
|
||||||
|
}
|
||||||
|
|
||||||
bool contains(vaddr_t address) const { return vaddr() <= address && address < vaddr() + size(); }
|
bool contains(vaddr_t address) const { return vaddr() <= address && address < vaddr() + size(); }
|
||||||
|
|
||||||
|
BAN::ErrorOr<bool> allocate_page_for_demand_paging(vaddr_t address);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VirtualRange(PageTable&, bool has_guard_pages, vaddr_t, size_t, PageTable::flags_t);
|
VirtualRange(PageTable&, bool preallocated, bool has_guard_pages, vaddr_t, size_t, PageTable::flags_t);
|
||||||
BAN::ErrorOr<void> initialize();
|
BAN::ErrorOr<void> initialize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PageTable& m_page_table;
|
PageTable& m_page_table;
|
||||||
|
const bool m_preallocated;
|
||||||
const bool m_has_guard_pages;
|
const bool m_has_guard_pages;
|
||||||
const vaddr_t m_vaddr;
|
const vaddr_t m_vaddr;
|
||||||
const size_t m_size;
|
const size_t m_size;
|
||||||
const PageTable::flags_t m_flags;
|
const PageTable::flags_t m_flags;
|
||||||
|
BAN::Vector<paddr_t> m_paddrs;
|
||||||
SpinLock m_lock;
|
SpinLock m_lock;
|
||||||
|
|
||||||
friend class BAN::UniqPtr<VirtualRange>;
|
friend class BAN::UniqPtr<VirtualRange>;
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/Optional.h>
|
||||||
|
#include <kernel/Memory/Types.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
void kmalloc_initialize();
|
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*);
|
void kfree(void*);
|
||||||
|
|
||||||
|
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t);
|
||||||
|
BAN::Optional<Kernel::vaddr_t> kmalloc_vaddr_of(Kernel::paddr_t);
|
||||||
|
|||||||
@@ -31,18 +31,35 @@ namespace Kernel
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<ARPTable>> create();
|
static BAN::ErrorOr<BAN::UniqPtr<ARPTable>> create();
|
||||||
|
~ARPTable();
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::MACAddress> get_mac_from_ipv4(NetworkInterface&, BAN::IPv4Address);
|
BAN::ErrorOr<BAN::MACAddress> get_mac_from_ipv4(NetworkInterface&, BAN::IPv4Address);
|
||||||
|
|
||||||
BAN::ErrorOr<void> handle_arp_packet(NetworkInterface&, BAN::ConstByteSpan);
|
void add_arp_packet(NetworkInterface&, BAN::ConstByteSpan);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ARPTable() = default;
|
ARPTable();
|
||||||
|
|
||||||
|
void packet_handle_task();
|
||||||
|
BAN::ErrorOr<void> handle_arp_packet(NetworkInterface&, const ARPPacket&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SpinLock m_arp_table_lock;
|
struct PendingArpPacket
|
||||||
|
{
|
||||||
|
NetworkInterface& interface;
|
||||||
|
ARPPacket packet;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
SpinLock m_table_lock;
|
||||||
|
SpinLock m_pending_lock;
|
||||||
|
|
||||||
BAN::HashMap<BAN::IPv4Address, BAN::MACAddress> m_arp_table;
|
BAN::HashMap<BAN::IPv4Address, BAN::MACAddress> m_arp_table;
|
||||||
|
|
||||||
|
Thread* m_thread { nullptr };
|
||||||
|
BAN::CircularQueue<PendingArpPacket, 128> m_pending_packets;
|
||||||
|
ThreadBlocker m_pending_thread_blocker;
|
||||||
|
|
||||||
friend class BAN::UniqPtr<ARPTable>;
|
friend class BAN::UniqPtr<ARPTable>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ namespace Kernel
|
|||||||
REG_TDLEN = 0x3808,
|
REG_TDLEN = 0x3808,
|
||||||
REG_TDH = 0x3810,
|
REG_TDH = 0x3810,
|
||||||
REG_TDT = 0x3818,
|
REG_TDT = 0x3818,
|
||||||
REG_RXCSUM = 0x5000,
|
|
||||||
REG_MTA = 0x5200,
|
REG_MTA = 0x5200,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -168,15 +167,6 @@ namespace Kernel
|
|||||||
RCTL_BSIZE_16384 = (0b01 << 16) | RCTL_BSEX,
|
RCTL_BSIZE_16384 = (0b01 << 16) | RCTL_BSEX,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum E1000_RXCSUM : uint32_t
|
|
||||||
{
|
|
||||||
RXSUM_IPOFLD = 1 << 8,
|
|
||||||
RXSUM_TUOFLD = 1 << 9,
|
|
||||||
RXSUM_CRCOFLD = 1 << 11,
|
|
||||||
RXSUM_IPPCE = 1 << 12,
|
|
||||||
RXSUM_PCSD = 1 << 13,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum E1000_CMD : uint8_t
|
enum E1000_CMD : uint8_t
|
||||||
{
|
{
|
||||||
CMD_EOP = 1 << 0,
|
CMD_EOP = 1 << 0,
|
||||||
@@ -188,26 +178,6 @@ namespace Kernel
|
|||||||
CMD_IDE = 1 << 7,
|
CMD_IDE = 1 << 7,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum E1000_RX_STS : uint8_t
|
|
||||||
{
|
|
||||||
RX_STS_IPCS = 1 << 6,
|
|
||||||
RX_STS_TCPCS = 1 << 5,
|
|
||||||
RX_STS_UDPCS = 1 << 5,
|
|
||||||
RX_STS_EOP = 1 << 1,
|
|
||||||
RX_STS_DD = 1 << 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum E1000_RX_ERR : uint8_t
|
|
||||||
{
|
|
||||||
RX_ERR_RXE = 1 << 7,
|
|
||||||
RX_ERR_IPE = 1 << 6,
|
|
||||||
RX_ERR_TCPE = 1 << 5,
|
|
||||||
RX_ERR_CXE = 1 << 4,
|
|
||||||
RX_ERR_SEQ = 1 << 2,
|
|
||||||
RX_ERR_SE = 1 << 1,
|
|
||||||
RX_ERR_CE = 1 << 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct e1000_rx_desc
|
struct e1000_rx_desc
|
||||||
{
|
{
|
||||||
uint64_t addr;
|
uint64_t addr;
|
||||||
|
|||||||
@@ -23,14 +23,14 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<E1000>> create(PCI::Device&);
|
static BAN::ErrorOr<BAN::RefPtr<E1000>> create(PCI::Device&);
|
||||||
~E1000();
|
~E1000();
|
||||||
|
|
||||||
BAN::MACAddress get_mac_address() const override { return m_mac_address; }
|
virtual BAN::MACAddress get_mac_address() const override { return m_mac_address; }
|
||||||
|
|
||||||
bool link_up() override { return m_link_up; }
|
virtual bool link_up() override { return m_link_up; }
|
||||||
int link_speed() override;
|
virtual int link_speed() override;
|
||||||
|
|
||||||
size_t payload_mtu() const override { return E1000_RX_BUFFER_SIZE - sizeof(EthernetHeader); }
|
virtual size_t payload_mtu() const override { return E1000_RX_BUFFER_SIZE - sizeof(EthernetHeader); }
|
||||||
|
|
||||||
void handle_irq() final override;
|
virtual void handle_irq() final override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
E1000(PCI::Device& pci_device)
|
E1000(PCI::Device& pci_device)
|
||||||
@@ -45,12 +45,12 @@ namespace Kernel
|
|||||||
uint32_t read32(uint16_t reg);
|
uint32_t read32(uint16_t reg);
|
||||||
void write32(uint16_t reg, uint32_t value);
|
void write32(uint16_t reg, uint32_t value);
|
||||||
|
|
||||||
BAN::ErrorOr<void> send_raw_bytes(BAN::Span<const BAN::ConstByteSpan> buffers) override;
|
virtual BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::ConstByteSpan) override;
|
||||||
|
|
||||||
bool can_read_impl() const override { return false; }
|
virtual bool can_read_impl() const override { return false; }
|
||||||
bool can_write_impl() const override { return false; }
|
virtual bool can_write_impl() const override { return false; }
|
||||||
bool has_error_impl() const override { return false; }
|
virtual bool has_error_impl() const override { return false; }
|
||||||
bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::ErrorOr<void> read_mac_address();
|
BAN::ErrorOr<void> read_mac_address();
|
||||||
@@ -61,7 +61,7 @@ namespace Kernel
|
|||||||
void enable_link();
|
void enable_link();
|
||||||
BAN::ErrorOr<void> enable_interrupt();
|
BAN::ErrorOr<void> enable_interrupt();
|
||||||
|
|
||||||
void receive_thread();
|
void handle_receive();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PCI::Device& m_pci_device;
|
PCI::Device& m_pci_device;
|
||||||
@@ -73,15 +73,7 @@ namespace Kernel
|
|||||||
BAN::UniqPtr<DMARegion> m_tx_buffer_region;
|
BAN::UniqPtr<DMARegion> m_tx_buffer_region;
|
||||||
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_rx_descriptor_region;
|
||||||
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
BAN::UniqPtr<DMARegion> m_tx_descriptor_region;
|
||||||
|
SpinLock m_lock;
|
||||||
BAN::Atomic<uint32_t> m_tx_head { 0 };
|
|
||||||
BAN::Atomic<uint32_t> m_tx_commit { 0 };
|
|
||||||
|
|
||||||
SpinLock m_rx_lock;
|
|
||||||
ThreadBlocker m_rx_blocker;
|
|
||||||
|
|
||||||
bool m_thread_should_die { false };
|
|
||||||
BAN::Atomic<bool> m_thread_is_dead { true };
|
|
||||||
|
|
||||||
BAN::MACAddress m_mac_address {};
|
BAN::MACAddress m_mac_address {};
|
||||||
bool m_link_up { false };
|
bool m_link_up { false };
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<BAN::RefPtr<E1000E>> create(PCI::Device&);
|
static BAN::ErrorOr<BAN::RefPtr<E1000E>> create(PCI::Device&);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void detect_eeprom() override;
|
virtual void detect_eeprom() override;
|
||||||
uint32_t eeprom_read(uint8_t addr) override;
|
virtual uint32_t eeprom_read(uint8_t addr) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
E1000E(PCI::Device& pci_device)
|
E1000E(PCI::Device& pci_device)
|
||||||
|
|||||||
@@ -38,31 +38,52 @@ namespace Kernel
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<IPv4Layer>> create();
|
static BAN::ErrorOr<BAN::UniqPtr<IPv4Layer>> create();
|
||||||
|
~IPv4Layer();
|
||||||
|
|
||||||
ARPTable& arp_table() { return *m_arp_table; }
|
ARPTable& arp_table() { return *m_arp_table; }
|
||||||
|
|
||||||
BAN::ErrorOr<void> handle_ipv4_packet(NetworkInterface&, BAN::ConstByteSpan, uint32_t validated_cksums);
|
void add_ipv4_packet(NetworkInterface&, BAN::ConstByteSpan);
|
||||||
|
|
||||||
virtual void unbind_socket(uint16_t port) override;
|
virtual void unbind_socket(uint16_t port) override;
|
||||||
virtual BAN::ErrorOr<void> bind_socket_with_target(BAN::RefPtr<NetworkSocket>, const sockaddr* target_address, socklen_t target_address_len) override;
|
virtual BAN::ErrorOr<void> bind_socket_with_target(BAN::RefPtr<NetworkSocket>, const sockaddr* target_address, socklen_t target_address_len) override;
|
||||||
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) override;
|
virtual BAN::ErrorOr<void> bind_socket_to_address(BAN::RefPtr<NetworkSocket>, const sockaddr* address, socklen_t address_len) override;
|
||||||
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) override;
|
virtual BAN::ErrorOr<void> get_socket_address(BAN::RefPtr<NetworkSocket>, sockaddr* address, socklen_t* address_len) override;
|
||||||
|
|
||||||
virtual BAN::ErrorOr<void> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;
|
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;
|
||||||
|
|
||||||
virtual Socket::Domain domain() const override { return Socket::Domain::INET ;}
|
virtual Socket::Domain domain() const override { return Socket::Domain::INET ;}
|
||||||
virtual size_t header_size() const override { return sizeof(IPv4Header); }
|
virtual size_t header_size() const override { return sizeof(IPv4Header); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IPv4Layer() = default;
|
IPv4Layer();
|
||||||
|
|
||||||
|
void add_ipv4_header(BAN::ByteSpan packet, BAN::IPv4Address src_ipv4, BAN::IPv4Address dst_ipv4, uint8_t protocol) const;
|
||||||
|
|
||||||
BAN::ErrorOr<in_port_t> find_free_port();
|
BAN::ErrorOr<in_port_t> find_free_port();
|
||||||
|
|
||||||
private:
|
void packet_handle_task();
|
||||||
BAN::UniqPtr<ARPTable> m_arp_table;
|
BAN::ErrorOr<void> handle_ipv4_packet(NetworkInterface&, BAN::ByteSpan);
|
||||||
|
|
||||||
RecursiveSpinLock m_bound_socket_lock;
|
private:
|
||||||
BAN::HashMap<int, BAN::WeakPtr<NetworkSocket>> m_bound_sockets;
|
struct PendingIPv4Packet
|
||||||
|
{
|
||||||
|
NetworkInterface& interface;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
RecursiveSpinLock m_bound_socket_lock;
|
||||||
|
|
||||||
|
BAN::UniqPtr<ARPTable> m_arp_table;
|
||||||
|
Thread* m_thread { nullptr };
|
||||||
|
|
||||||
|
static constexpr size_t pending_packet_buffer_size = 128 * PAGE_SIZE;
|
||||||
|
BAN::UniqPtr<VirtualRange> m_pending_packet_buffer;
|
||||||
|
BAN::CircularQueue<PendingIPv4Packet, 128> m_pending_packets;
|
||||||
|
ThreadBlocker m_pending_thread_blocker;
|
||||||
|
SpinLock m_pending_lock;
|
||||||
|
size_t m_pending_total_size { 0 };
|
||||||
|
|
||||||
|
BAN::HashMap<int, BAN::WeakPtr<NetworkSocket>> m_bound_sockets;
|
||||||
|
|
||||||
friend class BAN::UniqPtr<IPv4Layer>;
|
friend class BAN::UniqPtr<IPv4Layer>;
|
||||||
};
|
};
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user