Skip to content

Commit

Permalink
1. kpatch is deprecated, instead is supercmd 2. hook imporvoed 3. add…
Browse files Browse the repository at this point in the history
… thread local interface for module 4. doc (#91)


Co-authored-by: bmax <[email protected]>
  • Loading branch information
bmax121 and bmax authored May 16, 2024
1 parent cbd6d6d commit 221a3d5
Show file tree
Hide file tree
Showing 73 changed files with 2,905 additions and 1,045 deletions.
29 changes: 8 additions & 21 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ jobs:
- name: Build kpimg
run: |
export TARGET_COMPILE=`pwd`/arm-gnu-toolchain-12.2.rel1-x86_64-aarch64-none-elf/bin/aarch64-none-elf-
cd kernel
make
mv kpimg kpimg-linux
mv kpimg.elf kpimg.elf-linux
make clean
export ANDROID=1
make
mv kpimg kpimg-android
mv kpimg.elf kpimg.elf-android
unset ANDROID
make
mv kpimg kpimg-linux
mv kpimg.elf kpimg.elf-linux
make clean
cd ..
cd kpms
Expand Down Expand Up @@ -125,7 +127,7 @@ jobs:
replacesArtifacts: true
omitBodyDuringUpdate: true

Build-android-kpatch-kptools:
Build-android-kptools:
runs-on: ubuntu-latest
permissions:
contents: write
Expand Down Expand Up @@ -158,21 +160,6 @@ jobs:
cd kernel
make hdr
- name: Build kpatch-android
run: |
cd user
export ANDROID=1
mkdir -p build/android && cd build/android
echo ${{ steps.setup-ndk.outputs.ndk-path }}
cmake \
-DCMAKE_TOOLCHAIN_FILE=${{ steps.setup-ndk.outputs.ndk-path }}/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_PLATFORM=android-33 \
-DANDROID_ABI=arm64-v8a ../..
cmake --build .
unset ANDROID
mv kpatch kpatch-android
- name: Build kptools-android
run: |
cd tools
Expand Down
28 changes: 8 additions & 20 deletions .github/workflows/build_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ jobs:
run: |
export TARGET_COMPILE=`pwd`/arm-gnu-toolchain-12.2.rel1-x86_64-aarch64-none-elf/bin/aarch64-none-elf-
cd kernel
make
mv kpimg kpimg-linux
mv kpimg.elf kpimg.elf-linux
make clean
export ANDROID=1
make
mv kpimg kpimg-android
mv kpimg.elf kpimg.elf-android
unset ANDROID
make
mv kpimg kpimg-linux
mv kpimg.elf kpimg.elf-linux
make clean
cd ..
cd kpms
Expand Down Expand Up @@ -134,7 +137,7 @@ jobs:
replacesArtifacts: true
prerelease: true

Build-android-kpatch-kptools:
Build-android-kptools:
runs-on: ubuntu-latest
permissions:
contents: write
Expand Down Expand Up @@ -167,21 +170,6 @@ jobs:
cd kernel
make hdr
- name: Build kpatch-android
run: |
cd user
export ANDROID=1
mkdir -p build/android && cd build/android
echo ${{ steps.setup-ndk.outputs.ndk-path }}
cmake \
-DCMAKE_TOOLCHAIN_FILE=${{ steps.setup-ndk.outputs.ndk-path }}/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_PLATFORM=android-33 \
-DANDROID_ABI=arm64-v8a ../..
cmake --build .
unset ANDROID
mv kpatch kpatch-android
- name: Build kptools-android
run: |
cd tools
Expand Down
1 change: 1 addition & 0 deletions doc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api
18 changes: 18 additions & 0 deletions doxyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PROJECT_NAME = "KernelPatch Document"
OUTPUT_DIRECTORY = ./doc/api

INPUT = \
./user/supercall.h \
./kernel/include/hook.h \
./kernel/patch/include/accctl.h \
./kernel/patch/include/taskext.h \
./kernel/patch/include/uapi/scdefs.h \

FILE_PATTERNS = *.h *.md

RECURSIVE = YES
GENERATE_LATEX = NO
SOURCE_BROWSER = YES
EXTRACT_ALL = YES
EXTRACT_PRIVATE = YES
EXTRACT_STATIC = YES
11 changes: 9 additions & 2 deletions kernel/base/fphook.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ uint64_t __attribute__((section(".fp.transit0.text"))) __attribute__((__noinline
uint32_t *vptr = (uint32_t *)this_va;
while (*--vptr != ARM64_NOP) {
};
vptr--;
fp_hook_chain_t *hook_chain = local_container_of((uint64_t)vptr, fp_hook_chain_t, transit);
hook_fargs0_t fargs;
fargs.skip_origin = 0;
Expand Down Expand Up @@ -52,6 +53,7 @@ _fp_transit4(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3)
uint32_t *vptr = (uint32_t *)this_va;
while (*--vptr != ARM64_NOP) {
};
vptr--;
fp_hook_chain_t *hook_chain = local_container_of((uint64_t)vptr, fp_hook_chain_t, transit);
hook_fargs4_t fargs;
fargs.skip_origin = 0;
Expand Down Expand Up @@ -91,6 +93,7 @@ _fp_transit8(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_
uint32_t *vptr = (uint32_t *)this_va;
while (*--vptr != ARM64_NOP) {
};
vptr--;
fp_hook_chain_t *hook_chain = local_container_of((uint64_t)vptr, fp_hook_chain_t, transit);
hook_fargs8_t fargs;
fargs.skip_origin = 0;
Expand Down Expand Up @@ -136,6 +139,7 @@ _fp_transit12(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64
uint32_t *vptr = (uint32_t *)this_va;
while (*--vptr != ARM64_NOP) {
};
vptr--;
fp_hook_chain_t *hook_chain = local_container_of((uint64_t)vptr, fp_hook_chain_t, transit);
hook_fargs12_t fargs;
fargs.skip_origin = 0;
Expand Down Expand Up @@ -205,9 +209,10 @@ static hook_err_t hook_chain_prepare(uint32_t *transit, int32_t argno)
// todo: assert
if (transit_num >= TRANSIT_INST_NUM) return -HOOK_TRANSIT_NO_MEM;

transit[0] = ARM64_NOP;
transit[0] = ARM64_BTI_JC;
transit[1] = ARM64_NOP;
for (int i = 0; i < transit_num; i++) {
transit[i + 1] = ((uint32_t *)transit_start)[i];
transit[i + 2] = ((uint32_t *)transit_start)[i];
}
return HOOK_NO_ERR;
}
Expand Down Expand Up @@ -258,6 +263,8 @@ hook_err_t fp_hook_wrap(uintptr_t fp_addr, int32_t argno, void *before, void *af
}

for (int i = 0; i < FP_HOOK_CHAIN_NUM; i++) {
if ((before && chain->befores[i] == before) || (after && chain->afters[i] == after)) return -HOOK_DUPLICATED;

// todo: atomic or lock
if (chain->states[i] == CHAIN_ITEM_STATE_EMPTY) {
chain->states[i] = CHAIN_ITEM_STATE_BUSY;
Expand Down
Loading

0 comments on commit 221a3d5

Please sign in to comment.