1
0
Fork 0
mirror of https://github.com/tailix/libclayer.git synced 2024-11-20 11:06:24 -05:00

Remove PFA

This commit is contained in:
Alex Kotov 2022-12-25 13:43:24 +04:00
parent b461b8919f
commit 9b7c930205
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
13 changed files with 0 additions and 433 deletions

View file

@ -68,11 +68,3 @@ if ASM_X86_64
libkernaux_la_SOURCES += src/asm/x86_64.S libkernaux_la_SOURCES += src/asm/x86_64.S
endif endif
endif endif
####################
# Default packages #
####################
if WITH_PFA
libkernaux_la_SOURCES += src/pfa.c
endif

View file

@ -43,8 +43,6 @@ zero). Work-in-progress APIs can change at any time.
* [Runtime environment](/include/kernaux/runtime.h) (*non-breaking since* **0.7.0**) * [Runtime environment](/include/kernaux/runtime.h) (*non-breaking since* **0.7.0**)
* [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**) * [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**)
* Stack trace *(planned)* * Stack trace *(planned)*
* Algorithms
* [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*)
* libc replacement (*work in progress*) * libc replacement (*work in progress*)
* [ctype.h](/libc/include/ctype.h) * [ctype.h](/libc/include/ctype.h)
* [errno.h](/libc/include/errno.h) * [errno.h](/libc/include/errno.h)

View file

@ -66,7 +66,6 @@ AC_ARG_WITH( [arch-i386], AS_HELP_STRING([--without-arch-i386], [wit
AC_ARG_WITH( [arch-riscv64], AS_HELP_STRING([--without-arch-riscv64], [without architecture riscv64])) AC_ARG_WITH( [arch-riscv64], AS_HELP_STRING([--without-arch-riscv64], [without architecture riscv64]))
AC_ARG_WITH( [arch-x86-64], AS_HELP_STRING([--without-arch-x86-64], [without architecture x86-64])) AC_ARG_WITH( [arch-x86-64], AS_HELP_STRING([--without-arch-x86-64], [without architecture x86-64]))
AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without kernel assembler helpers])) AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without kernel assembler helpers]))
AC_ARG_WITH( [pfa], AS_HELP_STRING([--without-pfa], [without Page Frame Allocator]))
dnl Packages (disabled by default) dnl Packages (disabled by default)
AC_ARG_WITH( [libc], AS_HELP_STRING([--with-libc], [with libc replacement])) AC_ARG_WITH( [libc], AS_HELP_STRING([--with-libc], [with libc replacement]))
@ -98,7 +97,6 @@ if test -z "$with_arch_i386"; then with_arch_i386=no; fi
if test -z "$with_arch_riscv64"; then with_arch_riscv64=no; fi if test -z "$with_arch_riscv64"; then with_arch_riscv64=no; fi
if test -z "$with_arch_x86_64"; then with_arch_x86_64=no; fi if test -z "$with_arch_x86_64"; then with_arch_x86_64=no; fi
if test -z "$with_asm"; then with_asm=no; fi if test -z "$with_asm"; then with_asm=no; fi
if test -z "$with_pfa"; then with_pfa=no; fi
]) ])
AS_IF([test "$with_all" = no], do_without_all) AS_IF([test "$with_all" = no], do_without_all)
@ -131,7 +129,6 @@ AS_IF([test "$with_arch_i386" = no ], [with_arch_i386=no], [wit
AS_IF([test "$with_arch_riscv64" = no ], [with_arch_riscv64=no], [with_arch_riscv64=yes]) AS_IF([test "$with_arch_riscv64" = no ], [with_arch_riscv64=no], [with_arch_riscv64=yes])
AS_IF([test "$with_arch_x86_64" = no ], [with_arch_x86_64=no], [with_arch_x86_64=yes]) AS_IF([test "$with_arch_x86_64" = no ], [with_arch_x86_64=no], [with_arch_x86_64=yes])
AS_IF([test "$with_asm" = no ], [with_asm=no], [with_asm=yes]) AS_IF([test "$with_asm" = no ], [with_asm=no], [with_asm=yes])
AS_IF([test "$with_pfa" = no ], [with_pfa=no], [with_pfa=yes])
dnl Packages (disabled by default) dnl Packages (disabled by default)
AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc=no]) AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc=no])
@ -178,7 +175,6 @@ AM_CONDITIONAL([WITH_ARCH_I386], [test "$with_arch_i386" = yes])
AM_CONDITIONAL([WITH_ARCH_RISCV64], [test "$with_arch_riscv64" = yes]) AM_CONDITIONAL([WITH_ARCH_RISCV64], [test "$with_arch_riscv64" = yes])
AM_CONDITIONAL([WITH_ARCH_X86_64], [test "$with_arch_x86_64" = yes]) AM_CONDITIONAL([WITH_ARCH_X86_64], [test "$with_arch_x86_64" = yes])
AM_CONDITIONAL([WITH_ASM], [test "$with_asm" = yes]) AM_CONDITIONAL([WITH_ASM], [test "$with_asm" = yes])
AM_CONDITIONAL([WITH_PFA], [test "$with_pfa" = yes])
dnl Packages (disabled by default) dnl Packages (disabled by default)
AM_CONDITIONAL([WITH_LIBC], [test "$with_libc" = yes]) AM_CONDITIONAL([WITH_LIBC], [test "$with_libc" = yes])
@ -217,7 +213,6 @@ AS_IF([test "$with_arch_i386" = yes], [AC_DEFINE([WITH_ARCH_I386],
AS_IF([test "$with_arch_riscv64" = yes], [AC_DEFINE([WITH_ARCH_RISCV64], [1], [with architecture riscv64])]) AS_IF([test "$with_arch_riscv64" = yes], [AC_DEFINE([WITH_ARCH_RISCV64], [1], [with architecture riscv64])])
AS_IF([test "$with_arch_x86_64" = yes], [AC_DEFINE([WITH_ARCH_X86_64], [1], [with architecture x86_64])]) AS_IF([test "$with_arch_x86_64" = yes], [AC_DEFINE([WITH_ARCH_X86_64], [1], [with architecture x86_64])])
AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1], [with kernel assembler helpers])]) AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1], [with kernel assembler helpers])])
AS_IF([test "$with_pfa" = yes], [AC_DEFINE([WITH_PFA], [1], [with Page Frame Allocator])])
dnl Packages (disabled by default) dnl Packages (disabled by default)
AS_IF([test "$with_libc" = yes], [AC_DEFINE([WITH_LIBC], [1], [with libc replacement])]) AS_IF([test "$with_libc" = yes], [AC_DEFINE([WITH_LIBC], [1], [with libc replacement])])
@ -239,7 +234,6 @@ dnl Packages (enabled by default)
AS_IF([test "$with_arch_i386" = no], [AC_SUBST([comment_line_arch_i386], [//])]) AS_IF([test "$with_arch_i386" = no], [AC_SUBST([comment_line_arch_i386], [//])])
AS_IF([test "$with_arch_riscv64" = no], [AC_SUBST([comment_line_arch_riscv64], [//])]) AS_IF([test "$with_arch_riscv64" = no], [AC_SUBST([comment_line_arch_riscv64], [//])])
AS_IF([test "$with_arch_x86_64" = no], [AC_SUBST([comment_line_arch_x86_64], [//])]) AS_IF([test "$with_arch_x86_64" = no], [AC_SUBST([comment_line_arch_x86_64], [//])])
AS_IF([test "$with_pfa" = no], [AC_SUBST([comment_line_pfa], [//])])

View file

@ -12,6 +12,5 @@ nobase_include_HEADERS = \
kernaux/macro.h \ kernaux/macro.h \
kernaux/macro/packing_end.run \ kernaux/macro/packing_end.run \
kernaux/macro/packing_start.run \ kernaux/macro/packing_start.run \
kernaux/pfa.h \
kernaux/runtime.h \ kernaux/runtime.h \
kernaux/version.h kernaux/version.h

View file

@ -7,6 +7,5 @@
#include <kernaux/arch/riscv64.h> #include <kernaux/arch/riscv64.h>
#include <kernaux/arch/x86_64.h> #include <kernaux/arch/x86_64.h>
#include <kernaux/macro.h> #include <kernaux/macro.h>
#include <kernaux/pfa.h>
#include <kernaux/runtime.h> #include <kernaux/runtime.h>
#include <kernaux/version.h> #include <kernaux/version.h>

View file

@ -1,36 +0,0 @@
#ifndef KERNAUX_INCLUDED_PFA
#define KERNAUX_INCLUDED_PFA
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define KERNAUX_PFA_PAGE_SIZE (4 * 1024)
#define KERNAUX_PFA_PAGES_COUNT_MAX (1024 * 1024)
#define KERNAUX_PFA_FLAGS_SIZE (KERNAUX_PFA_PAGES_COUNT_MAX / 8)
typedef struct KernAux_PFA *KernAux_PFA;
struct KernAux_PFA {
uint8_t flags[KERNAUX_PFA_FLAGS_SIZE];
};
void KernAux_PFA_initialize(KernAux_PFA pfa);
bool KernAux_PFA_is_available(KernAux_PFA pfa, size_t page_addr);
void KernAux_PFA_mark_available(KernAux_PFA pfa, size_t start, size_t end);
void KernAux_PFA_mark_unavailable(KernAux_PFA pfa, size_t start, size_t end);
size_t KernAux_PFA_alloc_pages(KernAux_PFA pfa, size_t mem_size);
void KernAux_PFA_free_pages(KernAux_PFA pfa, size_t page_addr, size_t mem_size);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,6 +1,4 @@
#ifndef KERNAUX_INCLUDED_VERSION #ifndef KERNAUX_INCLUDED_VERSION
#define KERNAUX_INCLUDED_VERSION #define KERNAUX_INCLUDED_VERSION
@comment_line_pfa@#define KERNAUX_VERSION_WITH_PFA
#endif #endif

View file

@ -11,7 +11,6 @@ include/kernaux/asm/x86_64.h
include/kernaux/macro.h include/kernaux/macro.h
include/kernaux/macro/packing_end.run include/kernaux/macro/packing_end.run
include/kernaux/macro/packing_start.run include/kernaux/macro/packing_start.run
include/kernaux/pfa.h
include/kernaux/runtime.h include/kernaux/runtime.h
include/kernaux/version.h include/kernaux/version.h
lib/libkernaux.a lib/libkernaux.a

163
src/pfa.c
View file

@ -1,163 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "assert.h"
#include <kernaux/macro.h>
#include <kernaux/pfa.h>
#include <string.h>
#define PAGE_INDEX(page_addr) ((page_addr) / KERNAUX_PFA_PAGE_SIZE)
#define FLAG_INDEX_FROM_INDEX(page_index) ((page_index) / 8)
#define FLAG_MASK_FROM_INDEX(page_index) KERNAUX_BITS((page_index) % 8)
#define FLAG_INDEX_FROM_ADDR(page_addr) \
(FLAG_INDEX_FROM_INDEX(PAGE_INDEX(page_addr)))
#define FLAG_MASK_FROM_ADDR(page_addr) \
(FLAG_MASK_FROM_INDEX(PAGE_INDEX(page_addr)))
#define GET_FLAG_FROM_INDEX(pfa, page_index) \
(!!((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] & \
FLAG_MASK_FROM_INDEX(page_index)))
#define GET_FLAG_FROM_ADDR(pfa, page_addr) \
(!!((pfa)->flags[FLAG_INDEX_FROM_ADDR(page_addr)] & \
FLAG_MASK_FROM_ADDR(page_addr)))
#define FLAG_TRUE_FROM_INDEX(pfa, page_index) \
((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] |= \
FLAG_MASK_FROM_INDEX(page_index))
#define FLAG_FALSE_FROM_INDEX(pfa, page_index) \
((pfa)->flags[FLAG_INDEX_FROM_INDEX(page_index)] &= \
~FLAG_MASK_FROM_INDEX(page_index))
static void KernAux_PFA_mark(
KernAux_PFA pfa,
bool status,
size_t start,
size_t end
);
void KernAux_PFA_initialize(const KernAux_PFA pfa)
{
KERNAUX_NOTNULL(pfa);
// cppcheck-suppress ctunullpointer
memset(pfa->flags, 0, sizeof(pfa->flags));
}
bool KernAux_PFA_is_available(const KernAux_PFA pfa, const size_t page_addr)
{
KERNAUX_NOTNULL(pfa);
KERNAUX_ASSERT(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
// cppcheck-suppress ctunullpointer
return GET_FLAG_FROM_ADDR(pfa, page_addr);
}
void KernAux_PFA_mark_available(
const KernAux_PFA pfa,
const size_t start,
const size_t end
) {
KernAux_PFA_mark(pfa, true, start, end);
}
void KernAux_PFA_mark_unavailable(
const KernAux_PFA pfa,
const size_t start,
const size_t end
) {
KernAux_PFA_mark(pfa, false, start, end);
}
void KernAux_PFA_mark(
const KernAux_PFA pfa,
const bool status,
size_t start,
size_t end
) {
KERNAUX_NOTNULL(pfa);
KERNAUX_ASSERT(start < end);
const size_t start_rem = start % KERNAUX_PFA_PAGE_SIZE;
const size_t end_rem = (end + 1) % KERNAUX_PFA_PAGE_SIZE;
if (start_rem != 0) {
start = start - start_rem + KERNAUX_PFA_PAGE_SIZE;
}
if (end_rem != 0) {
end = end - end_rem;
}
const size_t start_index = start / KERNAUX_PFA_PAGE_SIZE;
const size_t end_index = end / KERNAUX_PFA_PAGE_SIZE;
for (size_t index = start_index; index <= end_index; ++index) {
if (status) {
FLAG_TRUE_FROM_INDEX(pfa, index);
} else {
FLAG_FALSE_FROM_INDEX(pfa, index);
}
}
}
size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, size_t mem_size)
{
KERNAUX_NOTNULL(pfa);
const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE;
if (mem_rem != 0) {
mem_size = mem_size - mem_rem + KERNAUX_PFA_PAGE_SIZE;
}
const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE;
// We start from 1 because 0 indicates failure.
// It is not very useful to alloc page at address 0;
for (size_t index = 1, start = 0;
index < KERNAUX_PFA_PAGES_COUNT_MAX;
++index)
{
if (!GET_FLAG_FROM_INDEX(pfa, index)) {
start = 0;
continue;
}
if (start == 0) start = index;
if (index - start + 1 == pages_count) {
for (; index >= start; --index) {
FLAG_FALSE_FROM_INDEX(pfa, index);
}
return start * KERNAUX_PFA_PAGE_SIZE;
}
}
return 0;
}
void KernAux_PFA_free_pages(
const KernAux_PFA pfa,
const size_t page_addr,
size_t mem_size
) {
KERNAUX_NOTNULL(pfa);
KERNAUX_ASSERT(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE;
if (mem_rem != 0) {
mem_size = mem_size - mem_rem + KERNAUX_PFA_PAGE_SIZE;
}
const size_t start_index = page_addr / KERNAUX_PFA_PAGE_SIZE;
const size_t pages_count = mem_size / KERNAUX_PFA_PAGE_SIZE;
for (size_t index = 0; index < pages_count; ++index) {
FLAG_TRUE_FROM_INDEX(pfa, start_index + index);
}
}

2
tests/.gitignore vendored
View file

@ -1,3 +1 @@
/test_arch_i386 /test_arch_i386
/test_pfa
/test_pfa_assert

View file

@ -15,29 +15,3 @@ test_arch_i386_SOURCES = \
main.c \ main.c \
test_arch_i386.c test_arch_i386.c
endif endif
############
# test_pfa #
############
if WITH_PFA
TESTS += test_pfa
test_pfa_LDADD = $(top_builddir)/libkernaux.la
test_pfa_SOURCES = \
main.c \
test_pfa.c
endif
###################
# test_pfa_assert #
###################
if ENABLE_ASSERT
if WITH_PFA
TESTS += test_pfa_assert
test_pfa_assert_LDADD = $(top_builddir)/libkernaux.la
test_pfa_assert_SOURCES = \
main.c \
test_pfa_assert.c
endif
endif

View file

@ -1,86 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/pfa.h>
#include <assert.h>
void test_main()
{
struct KernAux_PFA pfa;
KernAux_PFA_initialize(&pfa);
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
KernAux_PFA_mark_available(&pfa, 0, 654335);
KernAux_PFA_mark_available(&pfa, 1048576, 134086655);
KernAux_PFA_mark_unavailable(&pfa, 4194304, 6291455); // [4 MB, 6 MB)
for (size_t index = 0; index < 159; ++index) {
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 159; index < 256; ++index) {
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 256; index < 1024; ++index) { // [1 MB, 4 MB)
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 1024; index < 1536; ++index) { // [4 MB, 6 MB)
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 1536; index < 32736; ++index) { // [6 MB, ~127 MB)
assert(KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
for (size_t index = 32736; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
assert(!KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE));
}
{
const size_t page_addr = KernAux_PFA_alloc_pages(&pfa, 1);
assert(page_addr != 0);
assert(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
assert(!KernAux_PFA_is_available(&pfa, page_addr));
KernAux_PFA_free_pages(&pfa, page_addr, 1);
assert(KernAux_PFA_is_available(&pfa, page_addr));
}
{
const size_t page_addr =
KernAux_PFA_alloc_pages(&pfa, 10 * KERNAUX_PFA_PAGE_SIZE);
assert(page_addr != 0);
assert(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
for (size_t index = 0, addr = page_addr; index < 10; ++index) {
assert(!KernAux_PFA_is_available(&pfa, addr));
addr += KERNAUX_PFA_PAGE_SIZE;
}
KernAux_PFA_free_pages(&pfa, page_addr, 10 * KERNAUX_PFA_PAGE_SIZE);
for (size_t index = 0, addr = page_addr; index < 10; ++index) {
assert(KernAux_PFA_is_available(&pfa, addr));
addr += KERNAUX_PFA_PAGE_SIZE;
}
}
for (size_t index = 0; index < KERNAUX_PFA_PAGES_COUNT_MAX; ++index) {
if (KernAux_PFA_is_available(&pfa, index * KERNAUX_PFA_PAGE_SIZE)) {
assert(KernAux_PFA_alloc_pages(&pfa, 1) != 0);
}
}
assert(KernAux_PFA_alloc_pages(&pfa, 1) == 0);
}

View file

@ -1,99 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/macro.h>
#include <kernaux/pfa.h>
#include <kernaux/runtime.h>
#include <assert.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdlib.h>
static unsigned int count = 0;
static jmp_buf jmpbuf;
static void assert_cb(
const char *const file KERNAUX_UNUSED,
const int line KERNAUX_UNUSED,
const char *const str KERNAUX_UNUSED
) {
++count;
longjmp(jmpbuf, 1);
}
void test_main()
{
if (setjmp(jmpbuf) != 0) abort();
kernaux_assert_cb = assert_cb;
struct KernAux_PFA pfa;
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_initialize(&pfa);
} else {
assert(count == 0);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_initialize(NULL);
} else {
assert(count == 1);
}
if (setjmp(jmpbuf) == 0) {
assert(!KernAux_PFA_is_available(NULL, KERNAUX_PFA_PAGE_SIZE));
} else {
assert(count == 2);
}
if (setjmp(jmpbuf) == 0) {
assert(!KernAux_PFA_is_available(&pfa, 123));
} else {
assert(count == 3);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_mark_available(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
} else {
assert(count == 4);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_mark_available(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
} else {
assert(count == 5);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_mark_unavailable(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
} else {
assert(count == 6);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_mark_unavailable(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
} else {
assert(count == 7);
}
if (setjmp(jmpbuf) == 0) {
assert(KernAux_PFA_alloc_pages(NULL, KERNAUX_PFA_PAGE_SIZE) == 0);
} else {
assert(count == 8);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_free_pages(NULL, KERNAUX_PFA_PAGE_SIZE, KERNAUX_PFA_PAGE_SIZE);
} else {
assert(count == 9);
}
if (setjmp(jmpbuf) == 0) {
KernAux_PFA_free_pages(&pfa, 123, KERNAUX_PFA_PAGE_SIZE);
} else {
assert(count == 10);
}
}