Skip to content

Commit

Permalink
meta: add clang-format and reformat managarm sysdeps
Browse files Browse the repository at this point in the history
  • Loading branch information
64 committed Nov 30, 2024
1 parent 8a073a1 commit c2e79bc
Show file tree
Hide file tree
Showing 31 changed files with 3,297 additions and 3,059 deletions.
36 changes: 36 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
BasedOnStyle: LLVM
Language: Cpp

ColumnLimit: 100
IndentWidth: 4
TabWidth: 4
UseTab: ForIndentation

ContinuationIndentWidth: 4
ConstructorInitializerIndentWidth: 0
IndentCaseLabels: true
# Do not indent "public", "private", etc.
AccessModifierOffset: -4

AlignAfterOpenBracket: BlockIndent

# Put template<> on its own line.
BreakTemplateDeclarations: Yes
# Put long return types on their own line.
BreakAfterReturnType: Automatic
# Put binary operators at the start of the line.
BreakBeforeBinaryOperators: NonAssignment

# Put all initializers onto their own lines.
PackConstructorInitializers: CurrentLine

# Put all function arguments on their own lines.
BinPackArguments: false
BinPackParameters: false

# Format braces as {1, 2, 3}, not as { 1, 2, 3 }.
Cpp11BracedListStyle: true

# Force \n at EOF.
InsertNewlineAtEOF: true
21 changes: 21 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: pre-commit

on: [merge_group, pull_request]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Check .clang-format is up to date
- uses: actions/checkout@v4
with:
repository: 'managarm/common-config'
path: common-config
- run: 'cmp .clang-format common-config/.clang-format'
name: 'Check .clang-format is up to date'

# Check formatting
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fail_fast: false
repos:
- repo: https://github.com/pocc/pre-commit-hooks
rev: 336fdd7c3cab698ead0b1c95157b9e74d3906b62
hooks:
- id: clang-format
additional_dependencies: [clang-format==19.1.3]
args: ["-i"]
exclude: >
(?x)^(
options/ansi/musl-generic-math/.*|
options/ansi/generic/complex/.*|
options/posix/musl-generic-regex/.*
)
# Only enable on Managarm sysdeps for now.
files:
sysdeps/managarm/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ The type of library to be built (static, shared, or both) is controlled by meson

We also support building with `-Db_sanitize=undefined` to use UBSan inside mlibc. Note that this does not enable UBSan for external applications which link against `libc.so`, but it can be useful during development to detect internal bugs (e.g when adding new sysdeps).

## Running pre-commit hooks

To format your code before submitting a PR, you should install [`pre-commit`](https://pre-commit.com/). Then do `pre-commit install` to install the git hook which runs each time you commit. Alternatively, you can do `pre-commit run -a` to manually format all files.

## Running Tests

The `mlibc` test suite can be run under a Linux host. To do this, first install a set of kernel headers (as described [here](https://docs.kernel.org/kbuild/headers_install.html)), then run from the project root:
Expand Down
2 changes: 2 additions & 0 deletions sysdeps/aero/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/astral/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/dripos/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/ironclad/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/keyronex/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/lemon/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/lyre/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
39 changes: 24 additions & 15 deletions sysdeps/managarm/aarch64/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <mlibc/thread-entry.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <bits/ensure.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <mlibc/thread-entry.hpp>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>

extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) {
// Wait until our parent sets up the TID.
while(!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
while (!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);

if(mlibc::sys_tcb_set(tcb))
if (mlibc::sys_tcb_set(tcb))
__ensure(!"sys_tcb_set() failed");

tcb->invokeThreadFunc(entry, user_arg);
Expand All @@ -29,28 +29,37 @@ namespace mlibc {

static constexpr size_t default_stacksize = 0x200000;

int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) {
int sys_prepare_stack(
void **stack,
void *entry,
void *user_arg,
void *tcb,
size_t *stack_size,
size_t *guard_size,
void **stack_base
) {
if (!*stack_size)
*stack_size = default_stacksize;
*guard_size = 0;

if (*stack) {
*stack_base = *stack;
} else {
*stack_base = mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(*stack_base == MAP_FAILED) {
*stack_base =
mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (*stack_base == MAP_FAILED) {
return errno;
}
}

uintptr_t *sp = reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(*stack_base) + *stack_size);

uintptr_t *sp =
reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(*stack_base) + *stack_size);

*--sp = reinterpret_cast<uintptr_t>(tcb);
*--sp = reinterpret_cast<uintptr_t>(user_arg);
*--sp = reinterpret_cast<uintptr_t>(entry);
*stack = reinterpret_cast<void*>(sp);
*stack = reinterpret_cast<void *>(sp);
return 0;
}

} //namespace mlibc
} // namespace mlibc
Loading

0 comments on commit c2e79bc

Please sign in to comment.