├── .qemu_config ├── CODEOWNERS ├── include ├── stddef.h ├── stdint.h ├── stdbool.h ├── stdarg.h ├── limits.h ├── drivers │ ├── power_button.h │ ├── logo.h │ ├── fb.h │ ├── vga.h │ ├── pit.h │ ├── pic.h │ ├── hpet.h │ └── keyboard.h ├── test.h ├── arch │ └── x86 │ │ ├── real_mode.h │ │ ├── cpuid.h │ │ ├── traps.h │ │ ├── tss.h │ │ └── pci_cfg.h ├── smp │ ├── smp.h │ └── mptables.h ├── time.h ├── ktf.h ├── symbols.h ├── spinlock.h ├── semaphore.h ├── setup.h ├── extables.h ├── mm │ ├── vmm.h │ └── slab.h ├── console.h ├── usermode.h ├── mtrr.h ├── cmdline.h ├── bitmap.h ├── cpu.h ├── pci.h ├── list.h └── sched.h ├── .lgtm.yml ├── NOTICE ├── third-party ├── libpfm │ └── libpfm-4.13.0.tar.gz └── acpica │ ├── acpica-unix-20230628.tar.gz │ └── acpica_ktf.patch ├── .config ├── tools ├── xen_config │ └── config ├── asm-offsets │ └── asm-offsets.sh ├── docker │ ├── Dockerfile │ └── OnelineScanDockerfile ├── ci │ ├── get-python.sh │ ├── build-docker.sh │ └── launch-test.sh └── libpfm │ └── patch_headers.py ├── CODE_OF_CONDUCT.md ├── .github ├── workflows │ ├── coverity.yml │ ├── c-cpp-gcc.yml │ ├── c-cpp-clang.yml │ ├── clang-format.yml │ ├── auto_assign.yml │ ├── docker-ci.yml │ └── codeql-analysis.yml └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── grub ├── grub-prod.cfg └── grub-test.cfg ├── .gitignore ├── LICENSE ├── arch └── x86 │ ├── pci_cfg.c │ ├── extables.c │ ├── pat.c │ ├── real_mode_func.c │ ├── mtrr.c │ ├── boot │ ├── boot.S │ ├── head.S │ └── pagetables.S │ ├── asm-offsets.c │ └── cpuid.c ├── drivers ├── power_button.c ├── pit.c ├── pic.c ├── vga.c ├── hpet.c └── acpi │ └── acpica │ └── acktf.h ├── common ├── bitmap.c ├── percpu.c ├── kernel.c ├── symbols.c ├── cpu.c └── console.c ├── lib ├── time.c ├── semaphore.c └── lib.c ├── smp ├── bootstrap.S └── smp.c ├── tests └── test.c ├── toolkit └── cache │ └── lib.c ├── THIRD-PARTY-LICENSES ├── CONTRIBUTING.md ├── .clang-format └── mm └── vmm.c /.qemu_config: -------------------------------------------------------------------------------- 1 | QEMU_RAM=128 2 | QEMU_CPUS=2 3 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @KernelTestFramework/ktf 2 | 3 | -------------------------------------------------------------------------------- /include/stddef.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define NULL ((void *) 0) -------------------------------------------------------------------------------- /include/stdint.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include -------------------------------------------------------------------------------- /.lgtm.yml: -------------------------------------------------------------------------------- 1 | queries: 2 | - exclude: cpp/fixme-comment 3 | - exclude: cpp/short-global-name 4 | -------------------------------------------------------------------------------- /include/stdbool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define bool _Bool 4 | #define true 1 5 | #define false 0 -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | KTF - Kernel Test Framework 2 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | -------------------------------------------------------------------------------- /third-party/libpfm/libpfm-4.13.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelTestFramework/ktf/HEAD/third-party/libpfm/libpfm-4.13.0.tar.gz -------------------------------------------------------------------------------- /third-party/acpica/acpica-unix-20230628.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelTestFramework/ktf/HEAD/third-party/acpica/acpica-unix-20230628.tar.gz -------------------------------------------------------------------------------- /.config: -------------------------------------------------------------------------------- 1 | # Size (in MB) of boot-time virtual address space coverage 2 | CONFIG_EARLY_VIRT_MEM=8 3 | CONFIG_LIBPFM=n 4 | CONFIG_ACPICA=y 5 | CONFIG_DEBUG=n 6 | -------------------------------------------------------------------------------- /include/stdarg.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef __builtin_va_list va_list; 4 | 5 | #define va_start __builtin_va_start 6 | #define va_end __builtin_va_end 7 | #define va_arg __builtin_va_arg -------------------------------------------------------------------------------- /tools/xen_config/config: -------------------------------------------------------------------------------- 1 | name="kernel64" 2 | builder="hvm" 3 | memory=1024 4 | 5 | serial= [ 'file:/tmp/kernel.log', 'pty' ] 6 | 7 | disk = [ '/home/user/ktf.iso,,hdc,cdrom' ] 8 | 9 | on_reboot = "destroy" 10 | 11 | vcpus=1 12 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /tools/asm-offsets/asm-offsets.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | INPUT=$1 4 | OUTPUT=$2 5 | 6 | cat < "$OUTPUT" 7 | /* This file has been automatically generated from $INPUT. */ 8 | 9 | #ifndef KTF_ASM_OFFSETS_H 10 | #define KTF_ASM_OFFSETS_H 11 | 12 | $(awk -F '@@' '/\.ascii.*@@.*@@/ {print $2}' "$INPUT" | tr -d '$') 13 | 14 | #endif /* KTF_ASM_OFFSETS_H */ 15 | EOT 16 | -------------------------------------------------------------------------------- /.github/workflows/coverity.yml: -------------------------------------------------------------------------------- 1 | name: Coverity Scan 2 | 3 | on: 4 | push: 5 | branches: [mainline] 6 | 7 | jobs: 8 | coverity: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v3 12 | - uses: vapier/coverity-scan-action@v1 13 | with: 14 | email: ${{ secrets.COV_EMAIL }} 15 | token: ${{ secrets.COV_TOKEN }} 16 | -------------------------------------------------------------------------------- /grub/grub-prod.cfg: -------------------------------------------------------------------------------- 1 | set timeout=0 2 | set default=0 3 | 4 | insmod serial 5 | insmod multiboot2 6 | insmod all_video 7 | 8 | serial --speed=115200 --word=8 --parity=no --stop=1 9 | terminal_input --append serial 10 | terminal_output --append serial 11 | 12 | menuentry "kernel64" { 13 | multiboot2 /boot/kernel64.bin.xz poweroff=1 com1=0x3f8,115200,8,n,1 14 | boot 15 | } 16 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp-gcc.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI (GCC) 2 | 3 | on: 4 | push: 5 | branches: [ mainline ] 6 | pull_request: 7 | branches: [ mainline ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: make clean 17 | run: make clean 18 | - name: make CC=gcc all 19 | run: make CC=gcc all 20 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp-clang.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI (CLang) 2 | 3 | on: 4 | push: 5 | branches: [ mainline ] 6 | pull_request: 7 | branches: [ mainline ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: make clean 17 | run: make clean 18 | - name: make CC=clang all 19 | run: make CC=clang all 20 | -------------------------------------------------------------------------------- /tools/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:latest 2 | 3 | # build dependencies 4 | RUN apt-get update -y 5 | RUN apt-get install -y build-essential clang make xorriso mtools qemu-utils qemu-system-x86 patch 6 | # grub is a bit special in containers 7 | RUN DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install grub2 grub-efi-amd64-bin kmod python3 8 | 9 | CMD ["/bin/bash"] 10 | -------------------------------------------------------------------------------- /.github/workflows/clang-format.yml: -------------------------------------------------------------------------------- 1 | name: test-clang-format 2 | 3 | on: 4 | push: 5 | branches: [ mainline ] 6 | pull_request: 7 | branches: [ mainline ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: DoozyX/clang-format-lint-action@v0.8 16 | with: 17 | source: '.' 18 | clangFormatVersion: 10 19 | extensions: 'h,c' 20 | -------------------------------------------------------------------------------- /include/limits.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define ULONG_MAX __UINT64_MAX__ 4 | #define LONG_MAX __INT64_MAX__ 5 | #define LONG_MIN (-LONG_MAX - 1L) 6 | 7 | /* Minimum and maximum values a `signed int' can hold. */ 8 | #undef INT_MAX 9 | #define INT_MAX __INT_MAX__ 10 | #undef INT_MIN 11 | #define INT_MIN (-INT_MAX - 1) 12 | 13 | /* Maximum value an `unsigned int' can hold. (Minimum is 0). */ 14 | #undef UINT_MAX 15 | #define UINT_MAX (INT_MAX * 2U + 1U) -------------------------------------------------------------------------------- /tools/ci/get-python.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Not all systems ship a 'python' wrapper, try version specific ones if we 4 | # fail to find the version-agnostic one. 5 | # 6 | # Copyright (c) 2023 Open Source Security, Inc. 7 | 8 | try_python() { 9 | if $1 -c 'exit' 2>/dev/null; then 10 | echo "$1" 11 | exit 0 12 | fi 13 | } 14 | 15 | try_python python 16 | try_python python3 17 | try_python python2 18 | 19 | echo "error: python interpreter not found!" >&2 20 | exit 1 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.d 2 | *.o 3 | *.bin 4 | *.lds 5 | cscope.* 6 | *.iso 7 | *.img 8 | *.debug 9 | Makeconf.local 10 | .DS_Store 11 | *.swp 12 | *.xz 13 | asm-offsets.s 14 | asm-offsets.h 15 | grub/boot/grub 16 | drivers/acpi/acpica/source/ 17 | third-party/libpfm/COPYING 18 | third-party/libpfm/Makefile 19 | third-party/libpfm/README 20 | third-party/libpfm/config.mk 21 | third-party/libpfm/include/ 22 | third-party/libpfm/lib/ 23 | third-party/libpfm/libpfm.a 24 | third-party/libpfm/rules.mk 25 | -------------------------------------------------------------------------------- /grub/grub-test.cfg: -------------------------------------------------------------------------------- 1 | set timeout=0 2 | set default=0 3 | 4 | insmod serial 5 | insmod multiboot2 6 | insmod all_video 7 | 8 | serial --speed=115200 --word=8 --parity=no --stop=1 9 | terminal_input --append serial 10 | terminal_output --append serial 11 | 12 | menuentry "kernel64" { 13 | multiboot2 /boot/kernel64.bin.xz poweroff=1 com1=0x3f8,115200,8,n,1 pit=1 hpet=1 apic_timer=1 integer=42 boolean=1 string=foo badstring=toolong booleantwo tests=unit_tests 14 | boot 15 | } 16 | -------------------------------------------------------------------------------- /.github/workflows/auto_assign.yml: -------------------------------------------------------------------------------- 1 | name: Auto Assign 2 | 3 | on: 4 | issues: 5 | types: 6 | - opened 7 | pull_request: 8 | types: 9 | - opened 10 | 11 | jobs: 12 | add-to-project: 13 | name: Add issue/PR to project 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/add-to-project@v0.5.0 17 | with: 18 | project-url: https://github.com/orgs/KernelTestFramework/projects/2 19 | github-token: ${{ secrets.ADD_TO_PROJECT_PAT }} 20 | -------------------------------------------------------------------------------- /tools/ci/build-docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | # 5 | # This script builds the project's ktf.iso file in the docker environment 6 | set -e 7 | 8 | COMPILER=$1 9 | 10 | # Execute relative to this script 11 | SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )" 12 | declare -r SCRIPTDIR 13 | 14 | # Go to root directory of repository 15 | cd "$SCRIPTDIR"/../.. 16 | 17 | # Build project in docker 18 | make clean V=1 19 | make docker:ktf.iso V=1 CC=$COMPILER 20 | -------------------------------------------------------------------------------- /include/drivers/power_button.h: -------------------------------------------------------------------------------- 1 | #ifndef KTF_POWER_BUTTON_H 2 | #define KTF_POWER_BUTTON_H 3 | 4 | typedef void (*pb_handler_t)(void *); 5 | 6 | /** 7 | * Set a handler for the power button. Useful to control experiments. 8 | * 9 | * Note: This function MUST be called from the bsp to ensure consistency! 10 | */ 11 | void pb_set_handler(pb_handler_t handler, void *context); 12 | 13 | /** 14 | * Initialize power button handling. Requires ACPICA library. 15 | */ 16 | extern bool init_power_button(); 17 | 18 | #endif /* KTF_POWER_BUTTON_H */ 19 | -------------------------------------------------------------------------------- /tools/docker/OnelineScanDockerfile: -------------------------------------------------------------------------------- 1 | FROM one-line-scan:latest 2 | 3 | ARG USER=onelinescan 4 | ARG USER_ID=1000 5 | ARG GROUP_ID=1000 6 | 7 | RUN DEBIAN_FRONTEND=noninteractive apt-get -y \ 8 | -o Dpkg::Options::="--force-confdef" \ 9 | -o Dpkg::Options::="--force-confold" \ 10 | install grub2 python gcc make xorriso qemu-utils 11 | 12 | # Create proper users so that our build artifacts 13 | # can be shared with the outside user 14 | # https://vsupalov.com/docker-shared-permissions/ 15 | RUN addgroup --gid $GROUP_ID $USER 16 | RUN useradd --no-log-init --uid $USER_ID --gid $GROUP_ID $USER 17 | USER $USER 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Run KTF with '...' 16 | 2. See error 17 | 18 | **Expected behavior** 19 | A clear and concise description of what you expected to happen. 20 | 21 | **Logs** 22 | If applicable, add console logs to help explain your problem. 23 | 24 | **Additional context** 25 | Add any other context about the problem here. 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "[Feature]" 5 | labels: feature 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/docker-ci.yml: -------------------------------------------------------------------------------- 1 | name: Docker Run Test 2 | 3 | on: 4 | push: 5 | branches: [ mainline ] 6 | pull_request: 7 | branches: [ mainline ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Install Required Packages 14 | env: 15 | DEBIAN_FRONTEND: noninteractive 16 | run: | 17 | sudo apt-get update 18 | sudo apt-get install -y build-essential clang qemu-system qemu-user 19 | - uses: actions/checkout@v2 20 | - name: Build the Docker image (GCC) 21 | run: ./tools/ci/build-docker.sh gcc 22 | - name: Run the Docker image (GCC) 23 | run: ./tools/ci/launch-test.sh gcc 24 | - name: Build the Docker image (CLang) 25 | run: ./tools/ci/build-docker.sh clang 26 | - name: Run the Docker image (CLang) 27 | run: ./tools/ci/launch-test.sh clang 28 | -------------------------------------------------------------------------------- /third-party/acpica/acpica_ktf.patch: -------------------------------------------------------------------------------- 1 | --- drivers/acpi/acpica/source/include/platform/acenv.h 2021-07-30 17:20:33.000000000 +0200 2 | @@ -305,7 +305,10 @@ 3 | 4 | #endif 5 | 6 | -#if defined(_LINUX) || defined(__linux__) 7 | +#if defined(__KTF__) 8 | +#include "acktf.h" 9 | + 10 | +#elif defined(_LINUX) || defined(__linux__) 11 | #include "aclinux.h" 12 | 13 | #elif defined(_APPLE) || defined(__APPLE__) 14 | --- drivers/acpi/acpica/source/components/namespace/nsaccess.c 2023-06-28 23:38:40.000000000 +0200 15 | @@ -613,6 +613,8 @@ 16 | 17 | if (SearchParentFlag == ACPI_NS_NO_UPSEARCH) 18 | { 19 | + /* avoid compiler error when not debugging */ 20 | + (void)NumCarats; 21 | ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, 22 | "Search scope is [%4.4s], path has %u carat(s)\n", 23 | AcpiUtGetNodeName (ThisNode), NumCarats)); -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ mainline ] 6 | pull_request: 7 | # The branches below must be a subset of the branches above 8 | branches: [ mainline ] 9 | schedule: 10 | - cron: '20 7 * * 4' 11 | 12 | jobs: 13 | analyze: 14 | name: Analyze 15 | runs-on: ubuntu-latest 16 | permissions: 17 | actions: read 18 | contents: read 19 | security-events: write 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | language: [ 'cpp', 'python' ] 25 | 26 | steps: 27 | - name: Checkout repository 28 | uses: actions/checkout@v2 29 | 30 | # Initializes the CodeQL tools for scanning. 31 | - name: Initialize CodeQL 32 | uses: github/codeql-action/init@v1 33 | with: 34 | languages: ${{ matrix.language }} 35 | 36 | - name: Autobuild 37 | uses: github/codeql-action/autobuild@v1 38 | 39 | - name: Perform CodeQL Analysis 40 | uses: github/codeql-action/analyze@v1 41 | -------------------------------------------------------------------------------- /tools/ci/launch-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | # 5 | # This script builds the project's ktf.iso file in the docker environment and 6 | # next launches a test and checks for a successful timeout. 7 | set -e 8 | 9 | COMPILER=$1 10 | 11 | # Execute relative to this script 12 | SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )" 13 | declare -r SCRIPTDIR 14 | 15 | # Go to root directory of repository 16 | cd "$SCRIPTDIR"/../.. 17 | 18 | # Build project in docker 19 | echo "Building project from scratch" 20 | make clean V=1 21 | make UNITTEST=1 docker:ktf.iso V=1 CC=$COMPILER 22 | 23 | # Use QEMU to launch the guest 24 | echo "Launching KTF" 25 | declare -i STATUS=0 26 | timeout 10 make UNITTEST=1 docker:boot V=1 CC=$COMPILER || STATUS=$? 27 | 28 | # Check if the expected exit code happened (124 for timeout) 29 | if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 124 ] 30 | then 31 | echo "error: received unexpected exit code $STATUS" 32 | exit $STATUS 33 | elif [ "$STATUS" -eq 124 ] 34 | then 35 | echo "info: terminated with expected timeout signal" 36 | fi 37 | 38 | exit 0 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright © 2020 Amazon.com, Inc. or its affiliates. 4 | Copyright © 2014,2015 Citrix Systems Ltd. 5 | All Rights Reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /arch/x86/pci_cfg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | 27 | /* Lock to synchronize PCI config data and address IO ports */ 28 | spinlock_t pci_cfg_lock = SPINLOCK_INIT; 29 | -------------------------------------------------------------------------------- /include/test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_TEST_H 26 | #define KTF_TEST_H 27 | 28 | #define MAX_OPT_TESTS_LEN 128 29 | 30 | typedef int(test_fn)(void *arg); 31 | 32 | #endif /* KTF_TEST_H */ 33 | -------------------------------------------------------------------------------- /include/drivers/logo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_DRV_FB_LOGO_H 26 | #define KTF_DRV_FB_LOGO_H 27 | 28 | #define LOGO_WIDTH 174 29 | #define LOGO_HEIGHT 66 30 | 31 | extern unsigned char logo[]; 32 | 33 | #endif /* KTF_DRV_FB_LOGO_H */ -------------------------------------------------------------------------------- /include/arch/x86/real_mode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_REAL_MODE_H 26 | #define KTF_REAL_MODE_H 27 | 28 | #ifndef __ASSEMBLY__ 29 | 30 | extern void long_to_real(void); 31 | extern void init_real_mode(void); 32 | 33 | #endif /* __ASSEMBLY__ */ 34 | 35 | #endif /* KTF_REAL_MODE_H */ 36 | -------------------------------------------------------------------------------- /include/arch/x86/cpuid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_CPUID_H 26 | #define KTF_CPUID_H 27 | 28 | #include 29 | 30 | /* CPU vendor detection */ 31 | #define CPUID_EXT_INFO_LEAF 0x80000000U 32 | #define CPUID_BRAND_INFO_MIN 0x80000002U 33 | #define CPUID_BRAND_INFO_MAX 0x80000004U 34 | 35 | extern uint64_t get_cpu_freq(const char *cpu_str); 36 | extern bool cpu_vendor_string(char *cpu_str); 37 | 38 | #endif /* KTF_CPUID_H */ -------------------------------------------------------------------------------- /arch/x86/extables.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Amazon.com, Inc. or its affiliates. 3 | * Copyright (c) 2021 Open Source Security, Inc. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | 29 | void init_extables(void) { 30 | for (extable_entry_t *cur = __start_extables; cur < __stop_extables; ++cur) { 31 | if (!cur->fixup && !cur->cb) 32 | warning("extable entry #%lu for addr 0x%lx lacks fixup and callback!", 33 | cur - __start_extables, cur->fault_addr); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /include/smp/smp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SMP_H 26 | #define KTF_SMP_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | /* External declarations */ 33 | 34 | extern void init_smp(void); 35 | extern unsigned get_nr_cpus(void); 36 | 37 | /* Static declarations */ 38 | 39 | static inline unsigned int smp_processor_id(void) { 40 | return (unsigned int) rdmsr(MSR_TSC_AUX); 41 | } 42 | 43 | #endif /* KTF_SMP_H */ 44 | -------------------------------------------------------------------------------- /include/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_TIME_H 26 | #define KTF_TIME_H 27 | 28 | typedef uint64_t time_t; 29 | 30 | extern int msleep(time_t ms); 31 | extern int msleep_local(time_t ms); 32 | extern time_t get_timer_ticks(void); 33 | extern time_t get_local_ticks(void); 34 | 35 | /* Static declarations */ 36 | 37 | static inline int sleep(time_t s) { 38 | return msleep(s * 1000); 39 | } 40 | 41 | static inline int sleep_local(time_t s) { 42 | return msleep_local(s * 1000); 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/ktf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_KTF_H 26 | #define KTF_KTF_H 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | #ifndef __ASSEMBLY__ 38 | 39 | typedef uint16_t io_port_t; 40 | 41 | extern bool opt_debug; 42 | 43 | extern void reboot(void); 44 | extern void kernel_main(void) __noreturn; 45 | extern unsigned long test_main(void *unused); 46 | 47 | #endif /* __ASSEMBLY__ */ 48 | 49 | #endif /* KTF_KTF_H */ 50 | -------------------------------------------------------------------------------- /include/symbols.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SYMBOLS_H 26 | #define KTF_SYMBOLS_H 27 | 28 | extern unsigned int symbol_count __attribute__((weak)); 29 | extern void *symbol_addresses[] __attribute__((weak)); 30 | extern unsigned int symbol_sizes[] __attribute__((weak)); 31 | extern const char *symbol_names_ptr[] __attribute__((weak)); 32 | 33 | /* External declarations */ 34 | 35 | extern const char *symbol_name(const void *addr); 36 | extern void *symbol_address(const char *name); 37 | 38 | extern void print_symbol(const void *addr); 39 | 40 | #endif /* KTF_SYMBOLS_H */ -------------------------------------------------------------------------------- /tools/libpfm/patch_headers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | # 5 | # In order to generate the patch .h & .c files in libpfm had to be patched 6 | # (almost 200-300 of them). So I wrote a python script (see tools/libpfm/patch_headers.py) 7 | # to do that. libpfm uses standard C headers while we compile KTF without standard C headers 8 | # So those includes need to be patched out. We also have to include ktf.h in C and h files. 9 | # This script does that as well. 10 | import sys 11 | import fnmatch 12 | import os 13 | 14 | std_includes = [ 15 | "", 16 | "", 17 | "", 18 | "", 19 | "", 20 | "", 21 | "", 22 | "", 23 | "", 24 | "", 25 | ] 26 | 27 | ktf_patch_disclaimer = "/* Auto generated disclaimer by KTF project to patch headers */\n" 28 | ktf_include = "#include \n" 29 | ktf_header_insert = ktf_patch_disclaimer + ktf_include 30 | 31 | c_and_h_files = [] 32 | 33 | for root, dirnames, filenames in os.walk(os.curdir): 34 | for filename in fnmatch.filter(filenames, '*.c'): 35 | c_and_h_files.append(os.path.join(root, filename)) 36 | for filename in fnmatch.filter(filenames, '*.h'): 37 | c_and_h_files.append(os.path.join(root, filename)) 38 | 39 | for source_file in c_and_h_files: 40 | f = open(source_file) 41 | lines = f.readlines() 42 | 43 | lines = [ 44 | "" if any(include in line for include in std_includes) else line 45 | for line in lines 46 | ] 47 | 48 | for index, line in enumerate(lines): 49 | if line.startswith('#include'): 50 | if ktf_include not in line: 51 | lines.insert(index, ktf_header_insert) 52 | break 53 | 54 | with open(source_file, "w") as f: 55 | f.writelines(lines) 56 | -------------------------------------------------------------------------------- /include/drivers/fb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_DRV_FB_H 26 | #define KTF_DRV_FB_H 27 | 28 | #include 29 | #include 30 | 31 | #define FB_WHITE 0xFFFFFFFF 32 | 33 | extern void init_framebuffer(const struct multiboot2_tag_framebuffer *fb); 34 | extern void put_char(char c, uint32_t x, uint32_t y, uint32_t color); 35 | extern void draw_line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t color); 36 | extern void draw_logo(void); 37 | extern void fb_write(void *fb_addr, const char *buf, size_t len, uint32_t color); 38 | extern bool setup_framebuffer(void); 39 | extern void set_fb_scroll(bool state); 40 | 41 | #endif /* KTF_DRV_FB_H */ 42 | -------------------------------------------------------------------------------- /arch/x86/pat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | 28 | #define PAT_FIELD_BIT_POS(field) (8 * field) 29 | 30 | void pat_set_type(pat_field_t field, pat_memory_type_t type) { 31 | /* TODO: cpuid: check this feature is implemented */ 32 | unsigned long value = rdmsr(MSR_PAT); 33 | value = value & ~(0x7 << PAT_FIELD_BIT_POS(field)); 34 | value = value | (type << PAT_FIELD_BIT_POS(field)); 35 | wrmsr(MSR_PAT, value); 36 | } 37 | 38 | pat_memory_type_t pat_get_type(pat_field_t field) { 39 | /* TODO: cpuid: check this feature is implemented */ 40 | unsigned long value = rdmsr(MSR_PAT); 41 | value = value >> PAT_FIELD_BIT_POS(field); 42 | return value & 0x7; 43 | } -------------------------------------------------------------------------------- /include/spinlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SPINLOCK_H 26 | #define KTF_SPINLOCK_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #define LOCK_BIT 0U 33 | 34 | typedef volatile unsigned int spinlock_t; 35 | 36 | #define SPINLOCK_INIT (0U) 37 | 38 | /* Static declarations */ 39 | 40 | static inline void spin_lock(spinlock_t *lock) { 41 | ASSERT(lock); 42 | while (atomic_test_and_set_bit(LOCK_BIT, lock)) 43 | cpu_relax(); 44 | } 45 | 46 | static inline void spin_unlock(spinlock_t *lock) { 47 | ASSERT(lock); 48 | atomic_test_and_reset_bit(LOCK_BIT, lock); 49 | } 50 | 51 | /* External declarations */ 52 | 53 | #endif /* KTF_SPINLOCK_H */ 54 | -------------------------------------------------------------------------------- /drivers/power_button.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void default_handler(void *); 5 | 6 | /** power button press handler */ 7 | static pb_handler_t pb_handler = default_handler; 8 | /** context passed to the power button press handler */ 9 | static void *pb_context = NULL; 10 | 11 | #ifdef KTF_ACPICA 12 | static UINT32 button_handler(void *Context) { 13 | if (ACPI_FAILURE(AcpiClearEvent(ACPI_EVENT_POWER_BUTTON))) 14 | panic("PWRB: Failed to clear power button event"); 15 | 16 | if (pb_handler) 17 | pb_handler(pb_context); 18 | 19 | return ACPI_INTERRUPT_HANDLED; 20 | } 21 | #endif 22 | 23 | static void default_handler(void *notused) { 24 | #ifdef KTF_ACPICA 25 | acpi_power_off(); 26 | #endif 27 | } 28 | 29 | void pb_set_handler(pb_handler_t handler, void *context) { 30 | // check that we are on the bsp. Assumes that there is no task migration 31 | ASSERT(is_cpu_bsp(get_this_cpu())); 32 | 33 | unsigned long flags = interrupts_disable_save(); 34 | pb_handler = handler; 35 | pb_context = context; 36 | interrupts_restore(flags); 37 | } 38 | 39 | bool init_power_button() { 40 | #ifdef KTF_ACPICA 41 | ACPI_TABLE_FADT *fadt = acpi_find_table(ACPI_SIG_FADT); 42 | 43 | if (!(fadt->Flags & ACPI_FADT_POWER_BUTTON)) { 44 | printk("PWRB: Configuring ACPI 'fixed' power button handling\n"); 45 | 46 | if (ACPI_FAILURE(AcpiClearEvent(ACPI_EVENT_POWER_BUTTON))) { 47 | warning("PWRB: Failed to clear power button event"); 48 | return false; 49 | } 50 | 51 | if (ACPI_FAILURE(AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 52 | button_handler, NULL))) { 53 | warning("PWRB: Failed to install power button handler"); 54 | return false; 55 | } 56 | } 57 | else { 58 | warning("PWRB: Non-fixed power button not implemented\n"); 59 | return false; 60 | } 61 | 62 | return true; 63 | #else 64 | warning("PWRB: Power button without ACPICA not implemented\n"); 65 | 66 | return false; 67 | #endif 68 | } 69 | -------------------------------------------------------------------------------- /drivers/pit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | void init_pit(const cpu_t *cpu) { 34 | printk("Initializing PIT\n"); 35 | outb(PIT_COMMAND_PORT, PIT_CHANNEL_0 | PIT_ACCESS_MODE_LH | PIT_OP_MODE_RATE); 36 | outb(PIT_DATA_PORT_CH0, PIT_FREQUENCY & 0xFF); /* send low byte */ 37 | outb(PIT_DATA_PORT_CH0, (PIT_FREQUENCY & 0xFF00) >> 8); /* send high byte */ 38 | configure_isa_irq(PIT_IRQ, PIT_IRQ_OFFSET, IOAPIC_DEST_MODE_PHYSICAL, cpu->id); 39 | } 40 | 41 | void pit_disable(void) { 42 | pic_disable_irq(PIC1_DEVICE_SEL, PIT_IRQ); 43 | } 44 | -------------------------------------------------------------------------------- /arch/x86/real_mode_func.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | extern void _long_to_real(const void *gdt_ptr, const void *idt_ptr); 34 | 35 | static spinlock_t lock = SPINLOCK_INIT; 36 | 37 | /* FIXME: add real mode calling functionality */ 38 | void long_to_real(void) { 39 | spin_lock(&lock); 40 | percpu_t *percpu = get_percpu_page(smp_processor_id()); 41 | 42 | dprintk("%s: Before call\n", __func__); 43 | _long_to_real(&percpu->gdt_ptr, &percpu->idt_ptr); 44 | dprintk("%s: After call\n", __func__); 45 | spin_unlock(&lock); 46 | } 47 | 48 | void init_real_mode(void) { 49 | init_rmode_traps(); 50 | } 51 | -------------------------------------------------------------------------------- /common/bitmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | 28 | bitmap_t *bitmap_alloc(unsigned int nbits) { 29 | bitmap_t *map; 30 | 31 | if (0 == nbits || UINT_MAX == nbits) 32 | return NULL; 33 | 34 | map = kzalloc(sizeof(*map)); 35 | if (NULL == map) 36 | return NULL; 37 | 38 | map->word = kzalloc(BITS_TO_LONGS(nbits) * __SIZEOF_LONG__); 39 | if (NULL == map->word) { 40 | kfree(map); 41 | return NULL; 42 | } 43 | 44 | map->nbits = nbits; 45 | 46 | return map; 47 | } 48 | 49 | void bitmap_free(bitmap_t *map) { 50 | if (NULL == map) 51 | return; 52 | 53 | if (map->word) { 54 | kfree(map->word); 55 | map->word = NULL; 56 | } 57 | 58 | kfree(map); 59 | } 60 | -------------------------------------------------------------------------------- /include/semaphore.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SEMAPHORE_H 26 | #define KTF_SEMAPHORE_H 27 | 28 | #include 29 | #include 30 | 31 | struct sem { 32 | atomic_t v; 33 | }; 34 | typedef struct sem sem_t; 35 | 36 | #define MAX_SEMAPHORE_VALUE (_U32(-1) / 2) 37 | #define SEM_INIT(value) \ 38 | { .v = {(value)}, } 39 | 40 | extern int32_t sem_value(const sem_t *sem); 41 | 42 | extern void sem_init(sem_t *sem, uint32_t value); 43 | extern bool sem_trywait(sem_t *sem); 44 | extern void sem_wait(sem_t *sem); 45 | extern void sem_post(sem_t *sem); 46 | 47 | extern bool sem_trywait_units(sem_t *sem, int32_t units); 48 | extern void sem_wait_units(sem_t *sem, int32_t units); 49 | extern void sem_post_units(sem_t *sem, int32_t units); 50 | 51 | #endif /* KTF_SEMAPHORE_H */ 52 | -------------------------------------------------------------------------------- /arch/x86/mtrr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | 29 | mtrr_cap_t mtrr_read_cap(void) { 30 | mtrr_cap_t c; 31 | c.reg = rdmsr(MSR_MTRR_CAP); 32 | return c; 33 | } 34 | 35 | mtrr_def_type_t mtrr_read_def_type(void) { 36 | mtrr_def_type_t t; 37 | t.reg = rdmsr(MSR_MTRR_DEF_TYPE); 38 | return t; 39 | } 40 | 41 | bool mtrr_set_phys_base(mtrr_base_t base, uint8_t reg) { 42 | mtrr_cap_t cap = mtrr_read_cap(); 43 | if (reg < cap.vcnt) { 44 | wrmsr(MSR_MTRR_PHYS_BASE(reg), base.reg); 45 | return true; 46 | } 47 | return false; 48 | } 49 | 50 | bool mtrr_set_phys_mask(mtrr_mask_t mask, uint8_t reg) { 51 | mtrr_cap_t cap = mtrr_read_cap(); 52 | if (reg < cap.vcnt) { 53 | wrmsr(MSR_MTRR_PHYS_MASK(reg), mask.reg); 54 | return true; 55 | } 56 | return false; 57 | } 58 | -------------------------------------------------------------------------------- /include/setup.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SETUP_H 26 | #define KTF_SETUP_H 27 | 28 | #ifndef __ASSEMBLY__ 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | struct boot_flags { 36 | uint64_t virt : 1, legacy_devs : 1, i8042 : 1, vga : 1, msi : 1, aspm : 1, rtc : 1, 37 | nosmp : 1, timer_global : 1, rsvd : 55; 38 | }; 39 | typedef struct boot_flags boot_flags_t; 40 | 41 | extern char cpu_identifier[49]; 42 | extern unsigned long cpu_frequency; 43 | extern boot_flags_t boot_flags; 44 | 45 | /* Static declarations */ 46 | 47 | /* External declarations */ 48 | 49 | extern unsigned get_bsp_cpu_id(void); 50 | extern void set_bsp_cpu_id(unsigned cpu_id); 51 | 52 | extern void zap_boot_mappings(void); 53 | 54 | extern void init_timers(cpu_t *cpu); 55 | #endif /* __ASSEMBLY__ */ 56 | 57 | #endif /* KTF_SETUP_H */ 58 | -------------------------------------------------------------------------------- /include/arch/x86/traps.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * Copyright © 2014,2015 Citrix Systems Ltd. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef KTF_TRAPS_H 27 | #define KTF_TRAPS_H 28 | 29 | #include 30 | 31 | #define EXC_BASE 0 32 | #define IRQ_BASE 32 33 | 34 | #define SYSCALL_INT 0x80 35 | 36 | #define MAX_INT 256 37 | 38 | #ifndef __ASSEMBLY__ 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #define SERIAL_COM1_IRQ COM1_IRQ_VECTOR 45 | #define SERIAL_COM2_IRQ COM2_IRQ_VECTOR 46 | #define TIMER_IRQ PIT_IRQ_VECTOR 47 | #define KB_PORT1_IRQ KEYBOARD_PORT1_IRQ_VECTOR 48 | #define KB_PORT2_IRQ KEYBOARD_PORT2_IRQ_VECTOR 49 | #define APIC_TIMER_IRQ APIC_TIMER_IRQ_VECTOR 50 | 51 | #define APIC_SPI_VECTOR 0xFF 52 | 53 | extern void init_traps(const cpu_t *cpu); 54 | extern void init_boot_traps(void); 55 | extern void init_rmode_traps(void); 56 | 57 | extern void print_callstack(const void *sp, const void *ip); 58 | extern void do_exception(struct cpu_regs *regs); 59 | 60 | #endif /* __ASSEMBLY__ */ 61 | 62 | #endif /* KTF_TRAPS_H */ 63 | -------------------------------------------------------------------------------- /include/drivers/vga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_DRV_VGA_H 26 | #define KTF_DRV_VGA_H 27 | 28 | #include 29 | 30 | #define VGA_COLOR(bg, fg) (((bg) << 4) | (fg)) 31 | 32 | enum vga_color { 33 | VGA_BLACK = 0x0, 34 | VGA_BLUE = 0x1, 35 | VGA_GREEN = 0x2, 36 | VGA_CYAN = 0x3, 37 | VGA_RED = 0x4, 38 | VGA_MAGNETA = 0x5, 39 | VGA_BROWN = 0x6, 40 | VGA_LIGHT_GRAY = 0x7, 41 | VGA_GRAY = 0x8, 42 | VGA_LIGHT_BLUE = 0x9, 43 | VGA_LIGHT_GREEN = 0xa, 44 | VGA_LIGHT_CYAN = 0xb, 45 | VGA_LIGHT_RED = 0xc, 46 | VGA_LIGHT_MAGNETA = 0xd, 47 | VGA_YELLOW = 0xe, 48 | VGA_WHITE = 0xf, 49 | }; 50 | typedef enum vga_color vga_color_t; 51 | 52 | #define VGA_START_ADDR 0xB8000 53 | #define VGA_END_ADDR 0xBFFFF 54 | #define VGA_ROWS 25 55 | #define VGA_COLS 80 56 | #define VGA_SCREENS 10 57 | 58 | extern void vga_scroll_up(void); 59 | extern void vga_scroll_down(void); 60 | 61 | extern void vga_write(void *vga_memory, const char *buf, size_t len, vga_color_t color); 62 | 63 | extern void map_vga_area(void); 64 | #endif /* KTF_DRV_VGA_H */ 65 | -------------------------------------------------------------------------------- /arch/x86/boot/boot.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * Copyright © 2022 Open Source Security, Inc. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include 28 | 29 | SECTION(.multiboot, "a", 8) 30 | #define MULTIBOOT2_HEADER_FLAGS (MULTIBOOT2_ARCHITECTURE_I386) 31 | #define MULTIBOOT2_HEADER_SIZE (multiboot_header_end - multiboot_header) 32 | #define MULTIBOOT2_CHECKSUM -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_FLAGS + MULTIBOOT2_HEADER_SIZE) 33 | 34 | #define MULTIBOOT2_FB_WIDTH 1024 35 | #define MULTIBOOT2_FB_HEIGHT 768 36 | #define MULTIBOOT2_FB_BPP 32 37 | 38 | .code32 39 | multiboot_header: 40 | .long MULTIBOOT2_HEADER_MAGIC 41 | .long MULTIBOOT2_HEADER_FLAGS 42 | .long MULTIBOOT2_HEADER_SIZE 43 | .long MULTIBOOT2_CHECKSUM 44 | framebuffer_tag_start: 45 | .short MULTIBOOT2_HEADER_TAG_FRAMEBUFFER 46 | .short MULTIBOOT2_HEADER_TAG_OPTIONAL 47 | .long framebuffer_tag_end - framebuffer_tag_start 48 | .long MULTIBOOT2_FB_WIDTH 49 | .long MULTIBOOT2_FB_HEIGHT 50 | .long MULTIBOOT2_FB_BPP 51 | framebuffer_tag_end: 52 | .quad MULTIBOOT2_HEADER_TAG_END 53 | multiboot_header_end: 54 | -------------------------------------------------------------------------------- /include/extables.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_EXTABLES_H 26 | #define KTF_EXTABLES_H 27 | 28 | #include 29 | 30 | typedef void (*extable_entry_callback_t)(cpu_regs_t *regs); 31 | 32 | struct extable_entry { 33 | x86_reg_t fault_addr; 34 | x86_reg_t fixup; 35 | extable_entry_callback_t cb; 36 | } __packed; 37 | typedef struct extable_entry extable_entry_t; 38 | 39 | #if defined(__x86_64__) 40 | #define ASM_EXTABLE_HANDLER(fault_address, fixup, handler) \ 41 | PUSHSECTION(.extables) \ 42 | ".quad " STR(fault_address) ", " STR(fixup) ", " STR(handler) ";\n" POPSECTION 43 | #else 44 | #define ASM_EXTABLE_HANDLER(fault_address, fixup, handler) \ 45 | PUSHSECTION(.extables) \ 46 | ".long " STR(fault_address) ", " STR(fixup) ", " STR(handler) ";\n" POPSECTION 47 | #endif 48 | 49 | #define ASM_EXTABLE(fault_address, fixup) ASM_EXTABLE_HANDLER(fault_address, fixup, 0) 50 | 51 | extern void init_extables(void); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /common/percpu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | static list_head_t percpu_frames; 35 | 36 | void init_percpu(void) { 37 | printk("Initialize Per CPU structures\n"); 38 | 39 | list_init(&percpu_frames); 40 | } 41 | 42 | percpu_t *get_percpu_page(unsigned int cpu) { 43 | percpu_t *percpu; 44 | 45 | list_for_each_entry (percpu, &percpu_frames, list) { 46 | if (percpu->apic_id == cpu) 47 | return percpu; 48 | } 49 | 50 | /* Per CPU page must be identity mapped, 51 | * because GDT descriptor has 32-bit base. 52 | */ 53 | percpu = get_free_page(GFP_IDENT | GFP_KERNEL_MAP | GFP_USER); 54 | BUG_ON(!percpu); 55 | memset(percpu, 0, PAGE_SIZE); 56 | 57 | percpu->apic_id = cpu; 58 | 59 | list_add(&percpu->list, &percpu_frames); 60 | return percpu; 61 | } 62 | 63 | void for_each_percpu(void (*func)(percpu_t *percpu)) { 64 | percpu_t *percpu; 65 | 66 | list_for_each_entry (percpu, &percpu_frames, list) 67 | func(percpu); 68 | } 69 | -------------------------------------------------------------------------------- /include/mm/vmm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_VMM_H 26 | #define KTF_VMM_H 27 | 28 | #include 29 | 30 | /* clang-format off */ 31 | enum gfp_flags { 32 | GFP_NONE = 0x00000000, 33 | GFP_IDENT = 0x00000001, 34 | GFP_USER = 0x00000002, 35 | GFP_KERNEL = 0x00000004, 36 | GFP_KERNEL_MAP = 0x00000008, 37 | }; 38 | /* clang-format on */ 39 | typedef enum gfp_flags gfp_flags_t; 40 | 41 | /* External definitions */ 42 | 43 | extern void *get_free_pages(unsigned int order, gfp_flags_t flags); 44 | extern void put_pages(void *page); 45 | 46 | /* Static definitions */ 47 | 48 | static inline void *get_free_page(gfp_flags_t flags) { 49 | return get_free_pages(PAGE_ORDER_4K, flags); 50 | } 51 | 52 | static inline void *get_free_pages_top(unsigned int order, gfp_flags_t flags) { 53 | return get_free_pages(order, flags) + (PAGE_SIZE << order); 54 | } 55 | 56 | static inline void *get_free_page_top(gfp_flags_t flags) { 57 | return get_free_page(flags) + PAGE_SIZE; 58 | } 59 | 60 | static inline void put_page(void *page) { 61 | put_pages(page); 62 | } 63 | 64 | static inline void put_page_top(void *page) { 65 | put_pages(page - PAGE_SIZE); 66 | } 67 | 68 | #endif /* KTF_VMM_H */ 69 | -------------------------------------------------------------------------------- /lib/time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | extern boot_flags_t boot_flags; 32 | 33 | static __aligned(16) volatile time_t ticks = 0; 34 | 35 | void timer_interrupt_handler(void) { 36 | asm volatile("lock incq %[ticks]" : [ ticks ] "=m"(ACCESS_ONCE(ticks))); 37 | apic_EOI(); 38 | } 39 | 40 | void apic_timer_interrupt_handler(void) { 41 | asm volatile("lock incq %%gs:%[ticks]" 42 | : [ ticks ] "=m"(ACCESS_ONCE(PERCPU_VAR(apic_ticks)))); 43 | apic_EOI(); 44 | } 45 | 46 | int msleep(time_t ms) { 47 | time_t end; 48 | 49 | if (!boot_flags.timer_global) 50 | return -ENODEV; 51 | 52 | end = ACCESS_ONCE(ticks) + ms; 53 | while (ACCESS_ONCE(ticks) < end) 54 | cpu_relax(); 55 | 56 | return 0; 57 | } 58 | 59 | int msleep_local(time_t ms) { 60 | time_t end; 61 | 62 | if (!PERCPU_GET(apic_timer_enabled)) 63 | return -ENODEV; 64 | 65 | end = PERCPU_GET(apic_ticks) + ms; 66 | while (PERCPU_GET(apic_ticks) < end) 67 | cpu_relax(); 68 | 69 | return 0; 70 | } 71 | 72 | time_t get_timer_ticks(void) { 73 | return ACCESS_ONCE(ticks); 74 | } 75 | 76 | time_t get_local_ticks(void) { 77 | return PERCPU_GET(apic_ticks); 78 | } 79 | -------------------------------------------------------------------------------- /include/console.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_CONSOLE_H 26 | #define KTF_CONSOLE_H 27 | 28 | #include 29 | 30 | typedef void (*console_callback_t)(void *arg, const char *buf, size_t len); 31 | 32 | struct console_callback_entry { 33 | console_callback_t cb; 34 | void *arg; 35 | }; 36 | typedef struct console_callback_entry console_callback_entry_t; 37 | 38 | extern void vprintk(const char *fmt, va_list args); 39 | extern void printk(const char *fmt, ...); 40 | 41 | #define dprintk(fmt, ...) \ 42 | do { \ 43 | if (opt_debug) \ 44 | printk("%s (%s.%d): " fmt, __FILE__, __func__, __LINE__, ##__VA_ARGS__); \ 45 | } while (0) 46 | 47 | extern void serial_console_write(void *arg, const char *buf, size_t len); 48 | extern void qemu_console_write(void *arg, const char *buf, size_t len); 49 | extern void vga_console_write(void *arg, const char *buf, size_t len); 50 | extern void fb_console_write(void *arg, const char *buf, size_t len); 51 | 52 | extern void register_console_callback(console_callback_t func, void *arg); 53 | 54 | extern void panic(const char *fmt, ...) __noreturn; 55 | extern void warning(const char *fmt, ...); 56 | 57 | #endif /* KTF_CONSOLE_H */ 58 | -------------------------------------------------------------------------------- /include/arch/x86/tss.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * Copyright © 2014,2015 Citrix Systems Ltd. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef TSS_TSS_H 27 | #define TSS_TSS_H 28 | 29 | #ifndef __ASSEMBLY__ 30 | 31 | #if defined(__i386__) 32 | struct __packed x86_tss32 { 33 | uint16_t link, rsvd0; 34 | uint32_t esp0; 35 | uint16_t ss0, rsvd1; 36 | uint32_t esp1; 37 | uint16_t ss1, rsvd2; 38 | uint32_t esp2; 39 | uint16_t ss2, rsvd3; 40 | uint32_t cr3; 41 | uint32_t _ASM_IP; 42 | uint32_t _ASM_FLAGS; 43 | uint32_t _ASM_AX; 44 | uint32_t _ASM_CX; 45 | uint32_t _ASM_DX; 46 | uint32_t _ASM_BX; 47 | uint32_t _ASM_SP; 48 | uint32_t _ASM_BP; 49 | uint32_t _ASM_SI; 50 | uint32_t _ASM_DI; 51 | uint16_t es, rsvd4; 52 | uint16_t cs, rsvd5; 53 | uint16_t ss, rsvd6; 54 | uint16_t ds, rsvd7; 55 | uint16_t fs, rsvd8; 56 | uint16_t gs, rsvd9; 57 | uint16_t ldtr, rsvd10; 58 | uint16_t rsvd11, iopb; 59 | }; 60 | 61 | typedef struct x86_tss32 x86_tss_t; 62 | 63 | #elif defined(__x86_64__) 64 | 65 | struct __packed x86_tss64 { 66 | uint32_t rsvd0; 67 | uint64_t rsp0; 68 | uint64_t rsp1; 69 | uint64_t rsp2; 70 | uint64_t rsvd1; 71 | uint64_t ist[7]; 72 | uint64_t rsvd2; 73 | uint16_t rsvd3, iopb; 74 | }; 75 | 76 | typedef struct x86_tss64 x86_tss_t; 77 | 78 | #endif /* __i386__ */ 79 | 80 | #endif /* __ASSEMBLY__ */ 81 | 82 | #endif /* TSS_TSS_H */ 83 | -------------------------------------------------------------------------------- /smp/bootstrap.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | 29 | .code16 30 | SECTION(.text.init, "ax", 0x1000) 31 | GLOBAL(ap_start) 32 | mov %cs, %ax 33 | mov %ax, %ds 34 | 35 | lgdt boot_gdt_ptr - ap_start 36 | 37 | xor %ax, %ax 38 | inc %ax 39 | lmsw %ax 40 | 41 | ljmpl $__KERN_CS32, $.Lprot_mode 42 | 43 | .code32 44 | .Lprot_mode: 45 | mov $__KERN_DS32, %eax 46 | mov %eax, %ds 47 | mov %eax, %es 48 | mov %eax, %gs 49 | mov %eax, %fs 50 | mov %eax, %ss 51 | mov $_boot_stack_top, %esp 52 | mov %esp, %ebp 53 | 54 | mov %cr4, %eax 55 | or $(X86_CR4_PAE | X86_CR4_PSE), %eax 56 | mov %eax, %cr4 57 | 58 | mov (ap_cr3), %eax 59 | mov %eax, %cr3 60 | 61 | /* Enable long mode */ 62 | movl $MSR_EFER, %ecx 63 | rdmsr 64 | or $EFER_LME, %eax 65 | wrmsr 66 | 67 | /* Activate long mode: enable paging */ 68 | mov %cr0, %eax 69 | or $(X86_CR0_PG | X86_CR0_WP), %eax 70 | mov %eax, %cr0 71 | 72 | /* clear prefetch and jump to 64bit code */ 73 | ljmp $__KERN_CS64, $.Llong_mode 74 | 75 | .code64 76 | .Llong_mode: 77 | xor %rax, %rax 78 | mov %ax, %ds 79 | mov %ax, %es 80 | mov %ax, %fs 81 | mov %ax, %gs 82 | mov %ax, %ss 83 | 84 | push $X86_EFLAGS_MBS 85 | POPF 86 | 87 | cld 88 | jmp ap_startup 89 | 90 | ud2 91 | -------------------------------------------------------------------------------- /common/kernel.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #ifdef KTF_PMU 36 | #include 37 | #endif 38 | 39 | void reboot(void) { 40 | warning("Rebooting..."); 41 | io_delay(); 42 | 43 | #ifdef KTF_ACPICA 44 | acpi_reboot(); 45 | #endif 46 | keyboard_reboot(); 47 | hard_reboot(); 48 | } 49 | 50 | static void __noreturn echo_loop(void) { 51 | time_t reboot_timeout = opt_reboot_timeout * 1000; /* ms */ 52 | time_t start_time = get_timer_ticks(); 53 | 54 | while (1) { 55 | io_delay(); 56 | keyboard_process_keys(); 57 | 58 | if (reboot_timeout > 0) { 59 | if (get_timer_ticks() - start_time >= reboot_timeout) 60 | reboot(); 61 | } 62 | } 63 | } 64 | 65 | void kernel_main(void) { 66 | printk("\nKTF - Kernel Test Framework!\n"); 67 | 68 | zap_boot_mappings(); 69 | if (opt_debug) { 70 | display_memory_map(); 71 | display_multiboot_mmap(); 72 | } 73 | 74 | execute_tasks(); 75 | 76 | test_main(NULL); 77 | printk("All tasks done.\n"); 78 | 79 | execute_tasks(); 80 | 81 | #ifdef KTF_PMU 82 | pfm_terminate(); 83 | #endif 84 | 85 | #ifdef KTF_ACPICA 86 | if (opt_poweroff) 87 | acpi_power_off(); 88 | #endif 89 | echo_loop(); 90 | } 91 | -------------------------------------------------------------------------------- /tests/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | static const char opt_test_delims[] = ","; 33 | #include 34 | 35 | enum get_next_test_result { 36 | TESTS_DONE, 37 | TESTS_FOUND, 38 | TESTS_ERROR, 39 | }; 40 | typedef enum get_next_test_result get_next_test_result_t; 41 | 42 | static char opt_tests[MAX_OPT_TESTS_LEN]; 43 | string_cmd("tests", opt_tests); 44 | 45 | static get_next_test_result_t get_next_test(test_fn **out_test_fn, char **out_name) { 46 | static char *opt = opt_tests; 47 | 48 | *out_name = strtok(opt, opt_test_delims); 49 | 50 | opt = NULL; 51 | if (*out_name) { 52 | *out_test_fn = symbol_address(*out_name); 53 | if (!*out_test_fn) { 54 | warning("Symbol for test %s not found", *out_name); 55 | return TESTS_ERROR; 56 | } 57 | return TESTS_FOUND; 58 | } 59 | return TESTS_DONE; 60 | } 61 | 62 | unsigned long test_main(void *unused) { 63 | char *name; 64 | test_fn *fn = NULL; 65 | unsigned n = 0; 66 | 67 | printk("\nRunning tests\n"); 68 | 69 | while (get_next_test(&fn, &name) == TESTS_FOUND) { 70 | int rc; 71 | 72 | printk("Running test: %s\n", name); 73 | rc = fn(NULL); 74 | execute_tasks(); 75 | 76 | printk("Test %s returned: 0x%x\n", name, rc); 77 | n++; 78 | } 79 | 80 | printk("Tests completed: %u\n", n); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /lib/semaphore.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | 29 | void sem_init(sem_t *sem, uint32_t value) { 30 | /* Do not allow more than 2^31-1 threads to access this semaphore */ 31 | BUG_ON(value > MAX_SEMAPHORE_VALUE); 32 | 33 | atomic_set(&(sem->v), value); 34 | } 35 | 36 | int32_t sem_value(const sem_t *sem) { 37 | return atomic_read(&sem->v); 38 | } 39 | 40 | bool sem_trywait(sem_t *sem) { 41 | int64_t val; 42 | 43 | if (sem_value(sem) > 0) { 44 | val = atomic_dec_return(&(sem->v)); 45 | if (val >= 0) { 46 | return true; 47 | } 48 | else { 49 | atomic_inc(&(sem->v)); 50 | } 51 | } 52 | 53 | return false; 54 | } 55 | 56 | bool sem_trywait_units(sem_t *sem, int32_t units) { 57 | int64_t val; 58 | 59 | if (sem_value(sem) >= units) { 60 | val = atomic_sub_return(&(sem->v), units); 61 | if (val >= 0) { 62 | return true; 63 | } 64 | else { 65 | atomic_add_return(&(sem->v), units); 66 | } 67 | } 68 | 69 | return false; 70 | } 71 | 72 | void sem_wait(sem_t *sem) { 73 | while (!sem_trywait(sem)) { 74 | cpu_relax(); 75 | } 76 | } 77 | 78 | void sem_wait_units(sem_t *sem, int32_t units) { 79 | while (!sem_trywait_units(sem, units)) { 80 | cpu_relax(); 81 | } 82 | } 83 | 84 | void sem_post(sem_t *sem) { 85 | atomic_inc(&(sem->v)); 86 | } 87 | 88 | void sem_post_units(sem_t *sem, int32_t units) { 89 | atomic_add_return(&(sem->v), units); 90 | } 91 | -------------------------------------------------------------------------------- /toolkit/cache/lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | /* Calculate average baseline for the specified cache line timing access. 31 | * Calculate first intermediate average without flushing and then second 32 | * intermediate average based on samples collected right after flushing 33 | * the cache line. 34 | * Return average of both averages. 35 | */ 36 | static uint64_t test_timings(const cache_line_t *cl) { 37 | int i, t[SAMPLES_COUNT], avg[2]; 38 | 39 | for (i = 0; i < SAMPLES_COUNT; i++) { 40 | *(volatile uint8_t *) &cl->m8[0]; 41 | t[i] = cache_read_access_time(cl); 42 | } 43 | 44 | avg[0] = 0; 45 | for (i = 0; i < SAMPLES_COUNT; i++) 46 | avg[0] += t[i]; 47 | 48 | for (i = 0; i < SAMPLES_COUNT; i++) { 49 | clflush(&cl->m8[0]); 50 | t[i] = cache_read_access_time(cl); 51 | } 52 | 53 | avg[1] = 0; 54 | for (i = 0; i < SAMPLES_COUNT; i++) 55 | avg[1] += t[i]; 56 | 57 | return (avg[0] + avg[1]) / (SAMPLES_COUNT * ARRAY_SIZE(avg)); 58 | } 59 | 60 | /* 61 | * Get cache access time baseline for specified memory address. 62 | * The function returns a number of cycles it takes to read given 63 | * memory on average. 64 | */ 65 | uint64_t cache_channel_baseline(const cache_line_t *cl, unsigned delay) { 66 | uint64_t baseline = ~0UL; 67 | 68 | for (int i = 0; i < SAMPLES_COUNT; i++) { 69 | baseline = min(baseline, test_timings(cl)); 70 | wait_cycles(delay); 71 | } 72 | 73 | return baseline; 74 | } 75 | -------------------------------------------------------------------------------- /include/drivers/pit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_PIT_H 26 | #define KTF_PIT_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #define PIT_IRQ 0x00 /* IRQ line */ 33 | #define PIT_IRQ_OFFSET (PIC_IRQ0_OFFSET + PIT_IRQ) /* IRQ relative to PIC master */ 34 | #define PIT_IRQ_VECTOR PIT_IRQ_OFFSET 35 | 36 | #define PIT_OUT_FREQUENCY 1193182 /* oscillator output frequency */ 37 | #define PIT_RELOAD 1000 /* 1ms */ 38 | #define PIT_FREQUENCY (PIT_OUT_FREQUENCY / PIT_RELOAD) 39 | #define PIT_DATA_PORT_CH0 0x40 40 | #define PIT_COMMAND_PORT 0x43 41 | 42 | #define PIT_CHANNEL_0 0 43 | #define PIT_ACCESS_MODE_LOW (1 << 4) 44 | #define PIT_ACCESS_MODE_HIGH (1 << 5) 45 | #define PIT_ACCESS_MODE_LH (PIT_ACCESS_MODE_LOW | PIT_ACCESS_MODE_HIGH) 46 | 47 | enum pit_operational_mode { 48 | PIT_OP_MODE_COUNT = 0x00 << 1, /* interrupt on terminal count */ 49 | PIT_OP_MODE_ONE_SHOT = 0x01 << 1, /* hardware re-triggerable one-shot */ 50 | PIT_OP_MODE_RATE = 0x02 << 1, /* rate generator */ 51 | PIT_OP_MODE_WAVE = 0x03 << 1, /* square wave generator */ 52 | PIT_OP_MODE_SW_STROBE = 0x04 << 1, /* software triggered strobe */ 53 | PIT_OP_MODE_HW_STROBE = 0x05 << 1, /* hardware triggered strobe */ 54 | PIT_OP_MODE_RATE_6 = 0x06 << 1, /* rate generator */ 55 | PIT_OP_MODE_WAVE_7 = 0x07 << 1 /* square wave generator */ 56 | }; 57 | typedef enum pit_operational_mode pit_operational_mode_t; 58 | 59 | #define PIT_BCD_MODE 1 60 | 61 | extern void init_pit(const cpu_t *cpu); 62 | extern void pit_disable(void); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /drivers/pic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | 29 | static inline void pic_outb(io_port_t port, unsigned char value) { 30 | outb(port, value); 31 | io_delay(); 32 | } 33 | 34 | void init_pic(void) { 35 | /* Cascade mode initialization sequence */ 36 | pic_outb(PIC1_PORT_CMD, PIC_ICW1_INIT | PIC_ICW1_ICW4); 37 | pic_outb(PIC2_PORT_CMD, PIC_ICW1_INIT | PIC_ICW1_ICW4); 38 | 39 | /* Remap PICs interrupt vectors */ 40 | pic_outb(PIC1_PORT_DATA, PIC_IRQ0_OFFSET); 41 | pic_outb(PIC2_PORT_DATA, PIC_IRQ8_OFFSET); 42 | 43 | /* Set PIC1 and PIC2 cascade IRQ */ 44 | outb(PIC1_PORT_DATA, PIC_CASCADE_PIC1_IRQ); 45 | outb(PIC2_PORT_DATA, PIC_CASCADE_PIC2_IRQ); 46 | 47 | /* PIC mode: 80x86 */ 48 | outb(PIC1_PORT_DATA, PIC_ICW4_8086); 49 | outb(PIC2_PORT_DATA, PIC_ICW4_8086); 50 | 51 | /* Mask the 8259A PICs by setting all IMR bits */ 52 | outb(PIC1_PORT_DATA, 0xFF); 53 | outb(PIC2_PORT_DATA, 0xFF); 54 | } 55 | 56 | static void pic_toggle_irq(pic_device_sel_t pic, uint8_t irq, bool enable) { 57 | BUG_ON((pic != PIC1_DEVICE_SEL && pic != PIC2_DEVICE_SEL) || irq >= PIC_IRQ_MAX); 58 | 59 | uint8_t port = (pic == PIC1_DEVICE_SEL ? PIC1_PORT_DATA : PIC2_PORT_DATA); 60 | uint8_t mask = 1 << irq; 61 | uint8_t unmasked_irqs = inb(port); 62 | 63 | outb(port, enable ? (~mask & unmasked_irqs) : (mask | unmasked_irqs)); 64 | } 65 | 66 | void pic_enable_irq(pic_device_sel_t pic, uint8_t irq) { 67 | pic_toggle_irq(pic, irq, true); 68 | } 69 | 70 | void pic_disable_irq(pic_device_sel_t pic, uint8_t irq) { 71 | pic_toggle_irq(pic, irq, false); 72 | } 73 | -------------------------------------------------------------------------------- /include/usermode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_USERMODE_H 26 | #define KTF_USERMODE_H 27 | 28 | #define SYSCALL_EXIT 0 29 | #define SYSCALL_PRINTF 1 30 | #define SYSCALL_MMAP 2 31 | #define SYSCALL_MUNMAP 3 32 | 33 | #define USERMODE_FLAGS_MASK \ 34 | (X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | X86_EFLAGS_SF | \ 35 | X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF | X86_EFLAGS_ID | \ 36 | X86_EFLAGS_NT | X86_EFLAGS_RF | X86_EFLAGS_AC | X86_EFLAGS_IOPL) 37 | 38 | #ifndef __ASSEMBLY__ 39 | #include 40 | #include 41 | 42 | enum syscall_mode { 43 | SYSCALL_MODE_SYSCALL, // use SYSCALL 44 | SYSCALL_MODE_SYSENTER, // use SYSENTER 45 | SYSCALL_MODE_INT80, // use INT 0x80 46 | }; 47 | typedef enum syscall_mode syscall_mode_t; 48 | 49 | /* Static declarations */ 50 | 51 | static inline bool enter_from_usermode(uint16_t cs) { 52 | return (cs & 0x3) == 0x3; 53 | } 54 | 55 | /* External declarations */ 56 | 57 | extern unsigned long enter_usermode(task_func_t fn, void *fn_arg, void *user_stack); 58 | extern void syscall_handler_entry(void); 59 | extern void sysenter_handler_entry(void); 60 | extern void int80_handler_entry(void); 61 | 62 | extern void init_usermode(percpu_t *percpu); 63 | 64 | extern void __user_text exit(unsigned long exit_code); 65 | extern void __user_text printf(const char *fmt, ...); 66 | extern void *__user_text mmap(void *va, unsigned long order); 67 | extern int __user_text munmap(void *va); 68 | extern bool __user_text syscall_mode(syscall_mode_t); 69 | 70 | #endif /* __ASSEMBLY__ */ 71 | 72 | #endif /* KTF_USERMODE_H */ 73 | -------------------------------------------------------------------------------- /include/drivers/pic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_DRV_PIC_H 26 | #define KTF_DRV_PIC_H 27 | 28 | #include 29 | 30 | #define PIC1_PORT_CMD 0x20 31 | #define PIC2_PORT_CMD 0xa0 32 | #define PIC1_PORT_DATA (PIC1_PORT_CMD + 1) 33 | #define PIC2_PORT_DATA (PIC2_PORT_CMD + 1) 34 | 35 | #define PIC_EOI 0x20 /* End-of-interrupt command code */ 36 | 37 | #define PIC_ICW1_ICW4 0x01 38 | #define PIC_ICW1_SINGLE 0x02 /* Single (cascade) mode */ 39 | #define PIC_ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */ 40 | #define PIC_ICW1_LEVEL 0x08 /* Level triggered (edge) mode */ 41 | #define PIC_ICW1_INIT 0x10 42 | 43 | #define PIC_ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */ 44 | #define PIC_ICW4_AUTO 0x02 /* Auto (normal) EOI */ 45 | #define PIC_ICW4_BUF_PIC2 0x08 /* Buffered mode PIC2 */ 46 | #define PIC_ICW4_BUF_PIC1 0x0C /* Buffered mode PIC1 */ 47 | #define PIC_ICW4_SFNM 0x10 /* Special fully nested mode */ 48 | 49 | #define PIC_CASCADE_PIC2_IRQ 0x02 50 | #define PIC_CASCADE_PIC1_IRQ 0x04 51 | 52 | #define PIC_IRQ0_OFFSET 0x28 53 | #define PIC_IRQ8_OFFSET (PIC_IRQ0_OFFSET + 8) 54 | 55 | #define PIC_IRQ_MAX 0x08 /* One beyond the max IRQ number */ 56 | #define PIC_IRQ_END_OFFSET \ 57 | (PIC_IRQ8_OFFSET + PIC_IRQ_MAX) /* One beyond the max IRQ offset */ 58 | 59 | enum pic_device_sel { PIC1_DEVICE_SEL = 1, PIC2_DEVICE_SEL }; 60 | typedef enum pic_device_sel pic_device_sel_t; 61 | 62 | /* External declarations */ 63 | 64 | extern void init_pic(void); 65 | extern void pic_enable_irq(pic_device_sel_t pic, uint8_t irq); 66 | extern void pic_disable_irq(pic_device_sel_t pic, uint8_t irq); 67 | 68 | #endif /* KTF_DRV_PIC_H */ 69 | -------------------------------------------------------------------------------- /lib/lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | void __noreturn halt(void) { 31 | interrupts_disable(); 32 | 33 | while (1) { 34 | hlt(); 35 | pause(); 36 | } 37 | } 38 | 39 | /* Trigger Triple Fault */ 40 | void __noreturn hard_reboot(void) { 41 | idt_ptr_t idt_ptr = {0}; 42 | 43 | interrupts_disable(); 44 | lidt(&idt_ptr); 45 | int3(); 46 | 47 | halt(); 48 | } 49 | 50 | static uint64_t seed; 51 | 52 | void srand(unsigned s) { 53 | seed = s - 1; 54 | } 55 | 56 | int rand(void) { 57 | seed = 6364136223846793005ULL * seed + 1; 58 | return seed >> 33; 59 | } 60 | 61 | /* 62 | * read the msr_idx msr into value; the result is valid iff the returned value is true 63 | */ 64 | bool rdmsr_safe(uint32_t msr_idx, uint64_t *value) { 65 | volatile bool success = false; 66 | uint32_t low, high; 67 | 68 | asm volatile("1: rdmsr; movb $1, %[success];" 69 | "2:" ASM_EXTABLE(1b, 2b) 70 | : "=a"(low), "=d"(high), [ success ] "=m"(success) 71 | : "c"(msr_idx) 72 | : "memory"); 73 | *value = (((uint64_t) high) << 32) | low; 74 | return success; 75 | } 76 | 77 | /* 78 | * write to the msr_idx msr; the result is valid iff the returned value is true 79 | */ 80 | bool wrmsr_safe(uint32_t msr_idx, uint64_t value) { 81 | volatile bool success = false; 82 | 83 | asm volatile("1: wrmsr; movq $1, %[success];" 84 | "2:" ASM_EXTABLE(1b, 2b) 85 | : [ success ] "=m"(success) 86 | : "c"(msr_idx), "a"((uint32_t) value), "d"((uint32_t)(value >> 32)) 87 | : "memory"); 88 | return success; 89 | } 90 | -------------------------------------------------------------------------------- /THIRD-PARTY-LICENSES: -------------------------------------------------------------------------------- 1 | The KTF (Kernel Test Framework) includes the following third-party software/licensing: 2 | 3 | ** libpfm4; version 11.1 -- https://sourceforge.net/p/perfmon2/libpfm4 4 | Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is furnished to do 11 | so,subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 20 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 21 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | ---------------- 24 | 25 | ** ACPI Component Architecture (ACPICA) -- https://acpica.org/ 26 | 1. Copyright Notice 27 | 28 | Some or all of this work - Copyright (c) 1999 - 2021, Intel Corp. 29 | All rights reserved. 30 | 31 | 2. License 32 | 33 | Redistribution and use in source and binary forms, with or without 34 | modification, are permitted provided that the following conditions 35 | are met: 36 | 1. Redistributions of source code must retain the above copyright 37 | notice, this list of conditions, and the following disclaimer, 38 | without modification. 39 | 2. Redistributions in binary form must reproduce at minimum a disclaimer 40 | substantially similar to the "NO WARRANTY" disclaimer below 41 | ("Disclaimer") and any redistribution must be conditioned upon 42 | including a substantially similar Disclaimer requirement for further 43 | binary redistribution. 44 | 3. Neither the names of the above-listed copyright holders nor the names 45 | of any contributors may be used to endorse or promote products derived 46 | from this software without specific prior written permission. 47 | 48 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 52 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 53 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 54 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 58 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 | -------------------------------------------------------------------------------- /include/mtrr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_MTRR_H 26 | #define KTF_MTRR_H 27 | 28 | #include 29 | 30 | enum mtrr_memory_type { 31 | MTRR_UC = 0, /* Uncacheable */ 32 | MTRR_WC = 1, /* Write Combining */ 33 | MTRR_WT = 4, /* Write Through */ 34 | MTRR_WP, /* Write Protected */ 35 | MTRR_WB, /* Write Back */ 36 | }; 37 | typedef enum mtrr_memory_type mtrr_memory_type_t; 38 | 39 | union mtrr_cap { 40 | struct { 41 | /* clang-format off */ 42 | uint64_t vcnt : 8, 43 | fix : 1, 44 | _rsvd : 1, 45 | wc : 1, 46 | _rsvd2 : 53; 47 | /* clang-format on */ 48 | } __packed; 49 | uint64_t reg; 50 | }; 51 | typedef union mtrr_cap mtrr_cap_t; 52 | 53 | union mtrr_def_type { 54 | struct { 55 | /* clang-format off */ 56 | uint64_t type : 8, 57 | _rsvd : 2, 58 | fe : 1, 59 | e : 1, 60 | _rsvd2 : 52; 61 | /* clang-format on */ 62 | } __packed; 63 | uint64_t reg; 64 | }; 65 | typedef union mtrr_def_type mtrr_def_type_t; 66 | 67 | union mtrr_base { 68 | struct { 69 | /* clang-format off */ 70 | uint64_t type : 8, 71 | _rsvd : 4, 72 | phys_base : 40, 73 | _rsvd2 : 12; 74 | /* clang-format on */ 75 | } __packed; 76 | uint64_t reg; 77 | }; 78 | typedef union mtrr_base mtrr_base_t; 79 | 80 | union mtrr_mask { 81 | struct { 82 | /* clang-format off */ 83 | uint64_t _rsvd : 10, 84 | valid : 1, 85 | phys_base : 40, 86 | _rsvd2 : 13; 87 | /* clang-format on */ 88 | } __packed; 89 | uint64_t reg; 90 | }; 91 | typedef union mtrr_mask mtrr_mask_t; 92 | 93 | extern mtrr_cap_t mtrr_read_cap(void); 94 | extern mtrr_def_type_t mtrr_read_def_type(void); 95 | extern bool mtrr_set_phys_base(mtrr_base_t base, uint8_t reg); 96 | extern bool mtrr_set_phys_mask(mtrr_mask_t mask, uint8_t reg); 97 | 98 | #endif /* KTF_MTRR_H */ 99 | -------------------------------------------------------------------------------- /drivers/vga.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #define MAX_ROWS VGA_ROWS 31 | #define MAX_COLS (2 * VGA_COLS) 32 | 33 | static unsigned scroll_screen = 0; 34 | 35 | static uint8_t vga_buffer[VGA_SCREENS][MAX_ROWS][MAX_COLS]; 36 | 37 | static inline void write_vga_buffer(int cur_screen) { 38 | void *vga_memory = paddr_to_virt_kern(VGA_START_ADDR); 39 | 40 | memcpy(vga_memory, vga_buffer[cur_screen], sizeof(vga_buffer[cur_screen])); 41 | } 42 | 43 | void vga_scroll_down(void) { 44 | if (scroll_screen < (VGA_SCREENS - 1)) 45 | write_vga_buffer(++scroll_screen); 46 | } 47 | 48 | void vga_scroll_up(void) { 49 | if (scroll_screen > 0) 50 | write_vga_buffer(--scroll_screen); 51 | } 52 | 53 | void vga_write(void *vga_memory, const char *buf, size_t len, vga_color_t color) { 54 | static int screen = 0, row = 0, col = 0; 55 | 56 | for (unsigned int i = 0; i < len; i++) { 57 | char c = buf[i]; 58 | 59 | /* Newline on LF or when columns limit is hit */ 60 | if ((col > 0 && (col % (MAX_COLS - 2)) == 0) || c == '\n') { 61 | col = 0; 62 | row++; 63 | } 64 | 65 | /* Go to the next screen when hit end of VGA area */ 66 | if (row == (MAX_ROWS - 1)) { 67 | screen = (screen + 1) % VGA_SCREENS; 68 | memset(vga_buffer[screen], 0x00, sizeof(vga_buffer[screen])); 69 | row = col = 0; 70 | } 71 | 72 | if (c == '\n') 73 | continue; 74 | 75 | vga_buffer[screen][row][col++] = buf[i]; 76 | vga_buffer[screen][row][col++] = color; 77 | } 78 | 79 | scroll_screen = screen; 80 | memcpy(vga_memory, vga_buffer[screen], sizeof(vga_buffer[screen])); 81 | } 82 | 83 | void map_vga_area(void) { 84 | vmap_range(VGA_START_ADDR, VGA_END_ADDR - VGA_START_ADDR, L1_PROT_NOCACHE_GLOB, 85 | VMAP_IDENT | VMAP_KERNEL); 86 | } 87 | -------------------------------------------------------------------------------- /common/symbols.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | static inline bool is_symbol_address(const void *addr, unsigned index) { 32 | return addr >= symbol_addresses[index] && 33 | addr < symbol_addresses[index] + symbol_sizes[index]; 34 | } 35 | 36 | /* This function uses binary search and requires symbol_addresses[] to be sorted */ 37 | static long symbol_index_by_address(const void *addr) { 38 | unsigned left, right; 39 | 40 | if (!in_text_section(addr) && !in_user_text_section(addr)) 41 | return -1; 42 | 43 | left = 0; 44 | right = symbol_count - 1; 45 | 46 | while (left != right) { 47 | unsigned mid = (left + right) / 2; 48 | 49 | if (is_symbol_address(addr, mid)) 50 | return mid; 51 | else if (addr < symbol_addresses[mid]) 52 | right = mid; 53 | else 54 | left = mid; 55 | } 56 | 57 | if (is_symbol_address(addr, left)) 58 | return left; 59 | 60 | return -1; 61 | } 62 | 63 | static long symbol_index_by_name(const char *name) { 64 | /* FIXME: this probably should use better search algorithm */ 65 | for (unsigned int i = 0; i < symbol_count; i++) { 66 | if (!strcmp(symbol_names_ptr[i], name)) 67 | return i; 68 | } 69 | 70 | return -1; 71 | } 72 | 73 | const char *symbol_name(const void *addr) { 74 | long index = symbol_index_by_address(addr); 75 | 76 | return index < 0 ? NULL : symbol_names_ptr[index]; 77 | } 78 | 79 | void *symbol_address(const char *name) { 80 | long index = symbol_index_by_name(name); 81 | 82 | return index < 0 ? NULL : symbol_addresses[index]; 83 | } 84 | 85 | void print_symbol(const void *addr) { 86 | long index = symbol_index_by_address(addr); 87 | 88 | if (index < 0) 89 | return; 90 | 91 | printk("0x%016lx: %s + <0x%lx> [0x%x]\n", _ul(addr), symbol_names_ptr[index], 92 | _ul(addr - symbol_addresses[index]), symbol_sizes[index]); 93 | } 94 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /include/mm/slab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_ALLOC_SLAB_H 26 | #define KTF_ALLOC_SLAB_H 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | enum slab_alloc_order { 34 | SLAB_ORDER_16, 35 | SLAB_ORDER_32, 36 | SLAB_ORDER_64, 37 | SLAB_ORDER_128, 38 | SLAB_ORDER_256, 39 | SLAB_ORDER_512, 40 | SLAB_ORDER_1024, 41 | SLAB_ORDER_2048, 42 | SLAB_ORDER_MAX, 43 | }; 44 | typedef enum slab_alloc_order slab_alloc_order_t; 45 | 46 | enum slab_size { 47 | SLAB_SIZE_16 = 16, 48 | SLAB_SIZE_MIN = SLAB_SIZE_16, 49 | SLAB_SIZE_32 = SLAB_SIZE_16 << 1, 50 | SLAB_SIZE_64 = SLAB_SIZE_32 << 1, 51 | SLAB_SIZE_128 = SLAB_SIZE_64 << 1, 52 | SLAB_SIZE_256 = SLAB_SIZE_128 << 1, 53 | SLAB_SIZE_512 = SLAB_SIZE_256 << 1, 54 | SLAB_SIZE_1024 = SLAB_SIZE_512 << 1, 55 | SLAB_SIZE_2048 = SLAB_SIZE_1024 << 1, 56 | SLAB_SIZE_MAX = SLAB_SIZE_2048, 57 | }; 58 | typedef enum slab_size slab_size_t; 59 | 60 | #define SLAB_SIZE_FULL_MASK ((SLAB_SIZE_MAX << 1) - 1) 61 | 62 | #define MAX_SLAB_ALLOC_COUNT (PAGE_SIZE / SLAB_SIZE_MIN) 63 | 64 | #define META_SLAB_PAGE_ENTRY(meta_slab) ((meta_slab_t *) (_ul(meta_slab) & PAGE_MASK)) 65 | 66 | /* 67 | * SLAB sizes >= 4K should directly allocate pages 68 | */ 69 | 70 | struct slab { 71 | list_head_t list; 72 | }; 73 | 74 | typedef struct slab slab_t; 75 | 76 | struct meta_slab { 77 | list_head_t list; 78 | list_head_t slab_head; 79 | void *slab_base; 80 | unsigned int slab_len; 81 | /* 82 | * Don't need more than 12 bits. Currently max slab size is 2048 bytes = 2^11 83 | */ 84 | unsigned int slab_size : 12; 85 | /* 86 | * slab_allocs is tracking number of allocations currently in this slab. 87 | * At max this can go 4096/16 = 256 slabs. Thus 10 bits are enough 88 | */ 89 | unsigned int slab_allocs : 10; 90 | unsigned int reserved : 10; 91 | }; 92 | 93 | typedef struct meta_slab meta_slab_t; 94 | 95 | /* External declarations */ 96 | 97 | extern int init_slab(void); 98 | extern void *kmalloc(size_t size); 99 | extern void *kzalloc(size_t size); 100 | extern void kfree(void *ptr); 101 | 102 | #endif /* KTF_ALLOC_SLAB_H */ 103 | -------------------------------------------------------------------------------- /include/cmdline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_CMDLINE_H 26 | #define KTF_CMDLINE_H 27 | 28 | #ifndef __ASSEMBLY__ 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #define PARAM_MAX_LENGTH 32 36 | 37 | struct __packed ktf_param { 38 | char name[PARAM_MAX_LENGTH]; 39 | enum { STRING, ULONG, BOOL } type; 40 | void *var; 41 | unsigned int varlen; 42 | }; 43 | 44 | #define __ktfparam static __cmdline __used __aligned(1) struct ktf_param 45 | 46 | /* compile time check for param name size */ 47 | #define __param_size_check(_name, _sizename) \ 48 | char __unused_##_name[(sizeof(_sizename) >= PARAM_MAX_LENGTH) ? -1 : 0] 49 | 50 | #define cmd_param(_name, _var, _type) \ 51 | __param_size_check(_var, _name); \ 52 | __ktfparam __cmd_##_var = {_name, _type, &(_var), sizeof(_var)}; 53 | 54 | #define bool_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, BOOL) 55 | #define ulong_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, ULONG) 56 | #define string_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, STRING) 57 | 58 | #endif /* __ASSEMBLY__ */ 59 | 60 | /* External declarations */ 61 | 62 | extern bool opt_debug; 63 | extern bool opt_keyboard; 64 | extern bool opt_pit; 65 | extern bool opt_apic_timer; 66 | extern bool opt_hpet; 67 | extern bool opt_fpu; 68 | extern bool opt_qemu_console; 69 | extern bool opt_poweroff; 70 | extern bool opt_power_button; 71 | extern bool opt_fb_scroll; 72 | extern unsigned long opt_reboot_timeout; 73 | extern bool opt_tlb_global; 74 | 75 | extern const char *kernel_cmdline; 76 | 77 | extern void cmdline_parse(const char *cmdline); 78 | 79 | extern bool parse_com_port(com_idx_t com, uart_config_t *cfg); 80 | 81 | /* Static declarations */ 82 | 83 | static inline int parse_bool(const char *s) { 84 | if (!strcmp("no", s) || !strcmp("off", s) || !strcmp("false", s) || 85 | !strcmp("disable", s) || !strcmp("0", s)) 86 | return 0; 87 | 88 | if (!strcmp("yes", s) || !strcmp("on", s) || !strcmp("true", s) || 89 | !strcmp("enable", s) || !strcmp("1", s)) 90 | return 1; 91 | 92 | return -1; 93 | } 94 | 95 | #endif /* KTF_CMDLINE_H */ 96 | -------------------------------------------------------------------------------- /include/bitmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_BITMAP_H 26 | #define KTF_BITMAP_H 27 | 28 | #include 29 | #include 30 | 31 | #define BITS_TO_LONGS(nbits) div_round_up(nbits, BITS_PER_LONG) 32 | 33 | typedef struct { 34 | unsigned long *word; 35 | unsigned int nbits; 36 | } bitmap_t; 37 | 38 | bitmap_t *bitmap_alloc(unsigned int nbits); 39 | void bitmap_free(bitmap_t *map); 40 | 41 | static inline unsigned int bitmap_find_first_set(const bitmap_t *map) { 42 | if (NULL == map || NULL == map->word) 43 | return UINT_MAX; 44 | 45 | for (unsigned int i = 0; i < BITS_TO_LONGS(map->nbits); i++) { 46 | unsigned int ret = __builtin_ffsl(map->word[i]); 47 | if (0 == ret) 48 | continue; 49 | 50 | return i * BITS_PER_LONG + ret - 1; 51 | } 52 | 53 | return UINT_MAX; 54 | } 55 | 56 | static inline unsigned int bitmap_find_first_clear(const bitmap_t *map) { 57 | if (NULL == map || NULL == map->word) 58 | return UINT_MAX; 59 | 60 | for (unsigned int i = 0; i < BITS_TO_LONGS(map->nbits); i++) { 61 | unsigned int ret = __builtin_ffsl(~map->word[i]); 62 | if (0 == ret) 63 | continue; 64 | 65 | return i * BITS_PER_LONG + ret - 1; 66 | } 67 | 68 | return UINT_MAX; 69 | } 70 | 71 | static inline bool bitmap_test_bit(const bitmap_t *map, unsigned int bit) { 72 | unsigned int i; 73 | 74 | if (NULL == map || NULL == map->word || bit >= map->nbits) 75 | return false; 76 | 77 | i = bit / BITS_PER_LONG; 78 | 79 | return (map->word[i] & (1UL << (bit % BITS_PER_LONG))) != 0; 80 | } 81 | 82 | static inline void bitmap_set_bit(bitmap_t *map, unsigned int bit) { 83 | unsigned int i; 84 | 85 | if (NULL == map || NULL == map->word || bit >= map->nbits) 86 | return; 87 | 88 | i = bit / BITS_PER_LONG; 89 | 90 | map->word[i] |= (1UL << (bit % BITS_PER_LONG)); 91 | } 92 | 93 | static inline void bitmap_clear_bit(bitmap_t *map, unsigned int bit) { 94 | unsigned int i; 95 | 96 | if (NULL == map || NULL == map->word || bit >= map->nbits) 97 | return; 98 | 99 | i = bit / BITS_PER_LONG; 100 | 101 | map->word[i] &= ~(1UL << (bit % BITS_PER_LONG)); 102 | } 103 | 104 | #endif /* KTF_BITMAP_H */ 105 | -------------------------------------------------------------------------------- /arch/x86/asm-offsets.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2023 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define EMIT_DEFINE(s, v) \ 32 | asm volatile(".ascii \"@@#define " #s " %0 /* " #v " */@@\"\n" ::"i"(v)) 33 | #define OFFSETOF(__symbol, __type, __member) \ 34 | EMIT_DEFINE(__symbol, offsetof(__type, __member)); 35 | 36 | void __asm_offset_header(void) { 37 | OFFSETOF(usermode_private, percpu_t, usermode_private); 38 | 39 | OFFSETOF(cpu_exc_vector, cpu_exc_t, vector); 40 | OFFSETOF(cpu_exc_error_code, cpu_exc_t, error_code); 41 | OFFSETOF(cpu_exc_ip, cpu_exc_t, _ASM_IP); 42 | OFFSETOF(cpu_exc_cs, cpu_exc_t, cs); 43 | OFFSETOF(cpu_exc_flags, cpu_exc_t, _ASM_FLAGS); 44 | OFFSETOF(cpu_exc_sp, cpu_exc_t, _ASM_SP); 45 | OFFSETOF(cpu_exc_ss, cpu_exc_t, ss); 46 | 47 | OFFSETOF(cpu_regs_ss, cpu_regs_t, exc.ss); 48 | OFFSETOF(cpu_regs_sp, cpu_regs_t, exc._ASM_SP); 49 | OFFSETOF(cpu_regs_flags, cpu_regs_t, exc._ASM_FLAGS); 50 | OFFSETOF(cpu_regs_cs, cpu_regs_t, exc.cs); 51 | OFFSETOF(cpu_regs_ip, cpu_regs_t, exc._ASM_IP); 52 | OFFSETOF(cpu_regs_error_code, cpu_regs_t, exc.error_code); 53 | OFFSETOF(cpu_regs_vector, cpu_regs_t, exc.vector); 54 | 55 | OFFSETOF(cpu_regs_ax, cpu_regs_t, _ASM_AX); 56 | OFFSETOF(cpu_regs_bx, cpu_regs_t, _ASM_BX); 57 | OFFSETOF(cpu_regs_cx, cpu_regs_t, _ASM_CX); 58 | OFFSETOF(cpu_regs_dx, cpu_regs_t, _ASM_DX); 59 | OFFSETOF(cpu_regs_si, cpu_regs_t, _ASM_SI); 60 | OFFSETOF(cpu_regs_di, cpu_regs_t, _ASM_DI); 61 | OFFSETOF(cpu_regs_bp, cpu_regs_t, _ASM_BP); 62 | #if defined(__x86_64__) 63 | OFFSETOF(cpu_regs_r8, cpu_regs_t, r8); 64 | OFFSETOF(cpu_regs_r9, cpu_regs_t, r9); 65 | OFFSETOF(cpu_regs_r10, cpu_regs_t, r10); 66 | OFFSETOF(cpu_regs_r11, cpu_regs_t, r11); 67 | OFFSETOF(cpu_regs_r12, cpu_regs_t, r12); 68 | OFFSETOF(cpu_regs_r13, cpu_regs_t, r13); 69 | OFFSETOF(cpu_regs_r14, cpu_regs_t, r14); 70 | OFFSETOF(cpu_regs_r15, cpu_regs_t, r15); 71 | #endif 72 | 73 | EMIT_DEFINE(serial_com1_irq, SERIAL_COM1_IRQ); 74 | EMIT_DEFINE(serial_com2_irq, SERIAL_COM2_IRQ); 75 | EMIT_DEFINE(timer_irq, TIMER_IRQ); 76 | EMIT_DEFINE(kb_port1_irq, KB_PORT1_IRQ); 77 | EMIT_DEFINE(kb_port2_irq, KB_PORT2_IRQ); 78 | EMIT_DEFINE(apic_timer_irq, APIC_TIMER_IRQ); 79 | #ifdef KTF_ACPICA 80 | EMIT_DEFINE(acpi_sci_irq, ACPI_SCI_IRQ); 81 | #endif 82 | } 83 | -------------------------------------------------------------------------------- /arch/x86/boot/head.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #define BOOT_STACK_SIZE 0x1000 31 | 32 | .code32 33 | SECTION(.text.init, "ax", 16) 34 | GLOBAL(_start) 35 | /* Save multiboot bootloader magic */ 36 | mov %eax, %edi 37 | mov %ebx, %esi 38 | 39 | mov %cs, %ax 40 | mov %ax, %ds 41 | 42 | lgdt boot_gdt_ptr 43 | 44 | mov $X86_CR0_PE, %eax 45 | mov %eax, %cr0 46 | 47 | ljmp $__KERN_CS32, $.Lprot_mode 48 | 49 | .Lprot_mode: 50 | mov $__KERN_DS32, %ax 51 | mov %ax, %ds 52 | mov %ax, %es 53 | mov %ax, %gs 54 | mov %ax, %fs 55 | mov %ax, %ss 56 | mov $_boot_stack_top, %esp 57 | mov %esp, %ebp 58 | 59 | mov $(X86_CR4_PAE | X86_CR4_PSE), %eax 60 | mov %eax, %cr4 61 | 62 | mov $l4_pt_entries, %eax 63 | mov %eax, %cr3 64 | 65 | #if defined (KTF_UNIT_TEST) 66 | SAVE_ALL_REGS32 67 | call prot_to_real 68 | RESTORE_ALL_REGS32 69 | #endif 70 | 71 | /* Enable long mode */ 72 | movl $MSR_EFER, %ecx 73 | rdmsr 74 | or $EFER_LME, %eax 75 | wrmsr 76 | 77 | /* Activate long mode: enable paging */ 78 | mov %cr0, %eax 79 | or $(X86_CR0_PG | X86_CR0_WP), %eax 80 | mov %eax, %cr0 81 | 82 | /* clear prefetch and jump to 64bit code */ 83 | ljmp $__KERN_CS64, $.Llong_mode 84 | 85 | ENTRY(prot_to_real) 86 | /* FIXME: Add spinlock */ 87 | 88 | lea (boot_gdt_ptr), %eax 89 | push %eax 90 | lea (boot_idt_ptr), %eax 91 | push %eax 92 | 93 | call _prot_to_real 94 | 95 | add $8, %esp 96 | ret 97 | END_FUNC(prot_to_real) 98 | 99 | .code64 100 | .Llong_mode: 101 | xor %rax, %rax 102 | mov %ax, %ds 103 | mov %ax, %es 104 | mov %ax, %fs 105 | mov %ax, %gs 106 | mov %ax, %ss 107 | 108 | push $X86_EFLAGS_MBS 109 | POPF 110 | 111 | cld 112 | jmp kernel_start 113 | 114 | ud2 115 | 116 | SECTION(.bss.init, "aw", PAGE_SIZE) 117 | _boot_stack: 118 | .skip BOOT_STACK_SIZE 119 | GLOBAL(_boot_stack_top) 120 | 121 | .align PAGE_SIZE 122 | _boot_stack_ist: 123 | .skip BOOT_STACK_SIZE 124 | GLOBAL(_boot_stack_ist_top) 125 | 126 | .align PAGE_SIZE 127 | _boot_stack_df: 128 | .skip BOOT_STACK_SIZE 129 | GLOBAL(_boot_stack_df_top) 130 | 131 | #define XEN_ELFNOTE_PHYS32_ENTRY 18 132 | ELF_NOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long, _start) 133 | -------------------------------------------------------------------------------- /drivers/hpet.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | bool init_hpet(const cpu_t *cpu) { 32 | #ifndef KTF_ACPICA 33 | acpi_hpet_t *hpet; 34 | #else 35 | ACPI_TABLE_HPET *hpet; 36 | #endif 37 | mfn_t hpet_base_mfn; 38 | volatile acpi_hpet_timer_t *config; 39 | volatile acpi_hpet_general_t *general; 40 | volatile uint64_t *main_counter; 41 | uint64_t address; 42 | 43 | printk("Initializing HPET\n"); 44 | 45 | #ifndef KTF_ACPICA 46 | hpet = (acpi_hpet_t *) acpi_find_table(HPET_SIGNATURE); 47 | #else 48 | hpet = (ACPI_TABLE_HPET *) acpi_find_table(ACPI_SIG_HPET); 49 | #endif 50 | 51 | if (!hpet) { 52 | warning("HPET not initialized"); 53 | return false; 54 | } 55 | 56 | #ifndef KTF_ACPICA 57 | address = hpet->address.address; 58 | #else 59 | address = hpet->Address.Address; 60 | #endif 61 | 62 | hpet_base_mfn = paddr_to_mfn(address); 63 | vmap_kern_4k(_ptr(address), hpet_base_mfn, L1_PROT_NOCACHE_GLOB); 64 | config = (acpi_hpet_timer_t *) (address + HPET_OFFSET_TIMER_0_CONFIG_CAP_REG); 65 | general = (acpi_hpet_general_t *) (address + HPET_OFFSET_GENERAL_CAP_REG); 66 | main_counter = (uint64_t *) (address + HPET_OFFSET_GENERAL_MAIN_COUNTER_REG); 67 | 68 | general->enabled = 0; 69 | 70 | if (config->cfg_int_type != HPET_CONFIG_INT_TYPE_TRIGGER_MODE) { 71 | return false; 72 | } 73 | 74 | if (!config->cap_int_periodic) { 75 | return false; 76 | } 77 | 78 | if (general->num_timers_cap == 0) { 79 | return false; 80 | } 81 | 82 | /* Disable all timers */ 83 | for (int i = 0; i < general->num_timers_cap; ++i) { 84 | (config + i)->cfg_int_enabled = 0; 85 | } 86 | 87 | *main_counter = 0; 88 | 89 | /* 1 fs = 10^15s */ 90 | uint64_t freq_hz = 1000000000000000UL / general->counter_clock_period; 91 | uint64_t ticks = freq_hz / 1000; /* Interrupt every 1ms */ 92 | 93 | config->cfg_int_enabled = 1; 94 | config->cfg_int_route = 0; 95 | config->cfg_fsb_enable = 0; 96 | config->cfg_type = 1; 97 | config->cfg_value_set = 1; 98 | config->comparator = ticks; 99 | 100 | general->leg_repl_cfg = 0; /* Disable legacy route */ 101 | general->enabled = 1; 102 | 103 | configure_isa_irq(HPET_IRQ, HPET_IRQ_OFFSET, IOAPIC_DEST_MODE_PHYSICAL, cpu->id); 104 | dprintk("Initialized HPET\n"); 105 | return true; 106 | } 107 | -------------------------------------------------------------------------------- /smp/smp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include 40 | #include 41 | 42 | extern void ap_start(void); 43 | 44 | static unsigned nr_cpus; 45 | 46 | static __data_init unsigned ap_cpuid; 47 | static __data_init bool ap_callin; 48 | static __data_init void *ap_new_sp; 49 | cr3_t __data_init ap_cr3; 50 | 51 | void __noreturn ap_startup(void) { 52 | WRITE_SP(ap_new_sp); 53 | setup_tlb_global(); 54 | 55 | cpu_t *cpu = get_cpu(ap_cpuid); 56 | 57 | init_traps(cpu); 58 | init_apic(ap_cpuid, apic_get_mode()); 59 | 60 | /* Initialize timers and enable interrupts */ 61 | init_timers(cpu); 62 | interrupts_enable(); 63 | 64 | if (opt_fpu) 65 | enable_fpu(); 66 | 67 | /* Release BSP after full AP initialization */ 68 | ap_callin = true; 69 | smp_wmb(); 70 | 71 | while (true) 72 | run_tasks(cpu); 73 | 74 | UNREACHABLE(); 75 | } 76 | 77 | static __text_init void boot_cpu(cpu_t *cpu) { 78 | percpu_t *percpu = cpu->percpu; 79 | apic_icr_t icr; 80 | 81 | if (is_cpu_bsp(cpu)) 82 | return; 83 | 84 | ap_new_sp = get_free_pages_top(PAGE_ORDER_2M, GFP_KERNEL_MAP); 85 | BUG_ON(!ap_new_sp); 86 | ap_cpuid = cpu->id; 87 | ap_callin = false; 88 | smp_wmb(); 89 | 90 | dprintk("Starting AP: %u\n", cpu->id); 91 | 92 | memset(&icr, 0, sizeof(icr)); 93 | apic_icr_set_dest(&icr, percpu->apic_id); 94 | 95 | /* Wake up the secondary processor: INIT-SIPI-SIPI... */ 96 | icr.deliv_mode = APIC_DELIV_MODE_INIT; 97 | apic_wait_ready(); 98 | apic_icr_write(&icr); 99 | 100 | icr.deliv_mode = APIC_DELIV_MODE_SIPI; 101 | icr.vector = GET_SIPI_VECTOR(ap_start); 102 | apic_wait_ready(); 103 | apic_icr_write(&icr); 104 | 105 | apic_wait_ready(); 106 | apic_icr_write(&icr); 107 | 108 | apic_wait_ready(); 109 | 110 | /* Wait for AP initialization */ 111 | while (!ap_callin) 112 | cpu_relax(); 113 | 114 | dprintk("AP: %u Done \n", cpu->id); 115 | } 116 | 117 | void __text_init init_smp(void) { 118 | nr_cpus = get_nr_cpus(); 119 | 120 | printk("Initializing SMP support (CPUs: %u)\n", nr_cpus); 121 | ap_cr3 = cr3; 122 | 123 | for_each_cpu(boot_cpu); 124 | } 125 | -------------------------------------------------------------------------------- /include/cpu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_CPU_H 26 | #define KTF_CPU_H 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #define CPU_UNBLOCKED (1 << 0) 36 | #define CPU_FINISHED (1 << 1) 37 | 38 | struct cpu_flags { 39 | uint64_t bsp : 1, enabled : 1, rsvd : 62; 40 | }; 41 | typedef struct cpu_flags cpu_flags_t; 42 | 43 | struct cpu { 44 | list_head_t list; 45 | percpu_t *percpu; 46 | spinlock_t lock; 47 | list_head_t task_queue; 48 | atomic_t run_state; 49 | 50 | unsigned int id; 51 | cpu_flags_t flags; 52 | }; 53 | typedef struct cpu cpu_t; 54 | 55 | /* External declarations */ 56 | 57 | extern cpu_t *init_cpus(void); 58 | extern cpu_t *add_cpu(unsigned int id, bool bsp, bool enabled); 59 | extern cpu_t *get_cpu(unsigned int id); 60 | extern cpu_t *get_bsp_cpu(void); 61 | extern unsigned int get_nr_cpus(void); 62 | extern void for_each_cpu(void (*func)(cpu_t *cpu)); 63 | extern void unblock_all_cpus(void); 64 | extern void block_all_cpus(void); 65 | extern void finish_all_cpus(void); 66 | extern void wait_for_all_cpus(void); 67 | 68 | /* Static declarations */ 69 | 70 | static inline cpu_t *get_this_cpu() { 71 | return PERCPU_GET(cpu); 72 | } 73 | 74 | static inline bool is_cpu_bsp(cpu_t *cpu) { 75 | return cpu->flags.bsp; 76 | } 77 | 78 | static inline bool is_cpu_enabled(cpu_t *cpu) { 79 | return cpu->flags.enabled; 80 | } 81 | 82 | static inline void init_cpu_runstate(cpu_t *cpu) { 83 | atomic_set(&cpu->run_state, 0); 84 | } 85 | 86 | static inline bool is_cpu_finished(cpu_t *cpu) { 87 | return atomic_test_bit(CPU_FINISHED, &cpu->run_state); 88 | } 89 | 90 | static inline void set_cpu_finished(cpu_t *cpu) { 91 | atomic_test_and_set_bit(CPU_FINISHED, &cpu->run_state); 92 | } 93 | 94 | static inline void set_cpu_unfinished(cpu_t *cpu) { 95 | atomic_test_and_reset_bit(CPU_FINISHED, &cpu->run_state); 96 | } 97 | 98 | static inline bool is_cpu_unblocked(cpu_t *cpu) { 99 | return atomic_test_bit(CPU_UNBLOCKED, &cpu->run_state); 100 | } 101 | 102 | static inline void set_cpu_unblocked(cpu_t *cpu) { 103 | atomic_test_and_set_bit(CPU_UNBLOCKED, &cpu->run_state); 104 | } 105 | 106 | static inline void set_cpu_blocked(cpu_t *cpu) { 107 | atomic_test_and_reset_bit(CPU_UNBLOCKED, &cpu->run_state); 108 | } 109 | 110 | static inline void wait_cpu_unblocked(cpu_t *cpu) { 111 | while (!is_cpu_unblocked(cpu)) 112 | cpu_relax(); 113 | } 114 | 115 | #endif /* KTF_CPU_H */ 116 | -------------------------------------------------------------------------------- /arch/x86/boot/pagetables.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * Copyright © 2014,2015 Citrix Systems Ltd. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include 28 | 29 | #define EARLY_PT_NUM (EARLY_VIRT_MEM / 2) 30 | #define PT_NAME(x) l1_pt_entries ## x 31 | 32 | #if EARLY_PT_NUM > L2_PT_ENTRIES 33 | #error "Unable to cover more than 1GB of early address space" 34 | #endif 35 | 36 | #define FIRST_PT_IDX 0 37 | 38 | /* Each single l1_pt_entry covers a 4K of virtual addr range */ 39 | .macro alloc_l1_pt idx 40 | GLOBAL(PT_NAME(\idx)) 41 | .rept L1_PT_ENTRIES 42 | .long PT_PADDR(PT_NAME(0), L1_PT_SHIFT) + L1_PROT, 0 43 | .endr 44 | END_OBJECT(PT_NAME(\idx)) 45 | .endm 46 | 47 | .macro l2_pt_entry idx 48 | .long PT_NAME(\idx) + L2_PROT, 0 49 | .endm 50 | 51 | .altmacro 52 | 53 | /* Initial identity map page tables */ 54 | SECTION(.data.init, "aw", PAGE_SIZE) 55 | 56 | /* Allocate L1 page tables */ 57 | .set i, FIRST_PT_IDX 58 | .rept EARLY_PT_NUM 59 | alloc_l1_pt %i 60 | .set i, i + 1 61 | .endr 62 | 63 | /* 64 | * Each single l2_pt_entry points to l1_pt_entry table and 65 | * thus a single l2_pt_entry covers a 2M of virtual addr range 66 | */ 67 | GLOBAL(l2_pt_entries) 68 | /* Assign L1 page tables to their L2 table */ 69 | .set i, FIRST_PT_IDX 70 | .rept EARLY_PT_NUM 71 | l2_pt_entry %i 72 | .set i, i + 1 73 | .endr 74 | .fill (L2_PT_ENTRIES - EARLY_PT_NUM), PTE_SIZE, 0 /* EARLY_PT_NUM entries used, rests all are zeroed */ 75 | END_OBJECT(l2_pt_entries) 76 | 77 | /* 78 | * Each single l3_pt_entry points to a l2_pt_entry table and 79 | * thus a single l3_pt_entry covers a 1G of virtual addr range 80 | */ 81 | #if defined(__i386__) 82 | .align PAGE_SIZE 83 | #endif 84 | GLOBAL(l3_pt_entries) 85 | /* Should cover identity and user addresses */ 86 | .long l2_pt_entries + L3_PROT, 0 87 | 88 | .fill (L3_PT_ENTRIES - 3), PTE_SIZE, 0 89 | 90 | /* 91 | * Kernel range starts at 0xffffffff80000000. 92 | * That makes l3 index = 0x1fe. 93 | */ 94 | .long l2_pt_entries + L3_PROT, 0 95 | 96 | /* 97 | * Don't expect to spill over 0x1fe at boot time. So 1ff is zeroed 98 | */ 99 | .quad 0 100 | END_OBJECT(l3_pt_entries) 101 | 102 | #if defined(__x86_64__) 103 | .align PAGE_SIZE 104 | GLOBAL(l4_pt_entries) 105 | .long l3_pt_entries + L4_PROT, 0 106 | 107 | .fill (L4_PT_ENTRIES - 2), PTE_SIZE, 0 108 | 109 | /* 0xffff... */ 110 | .long l3_pt_entries + L4_PROT, 0 111 | END_OBJECT(l4_pt_entries) 112 | #endif 113 | 114 | .noaltmacro 115 | -------------------------------------------------------------------------------- /drivers/acpi/acpica/acktf.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * Name: acktf.h - OS specific defines, etc. for KTF 4 | * 5 | *****************************************************************************/ 6 | 7 | /****************************************************************************** 8 | * 9 | * 1. Copyright Notice 10 | * 11 | * Some or all of this work - Copyright (c) 1999 - 2021, Intel Corp. 12 | * All rights reserved. 13 | * 14 | * 2. License 15 | * 16 | * Redistribution and use in source and binary forms, with or without 17 | * modification, are permitted provided that the following conditions 18 | * are met: 19 | * 1. Redistributions of source code must retain the above copyright 20 | * notice, this list of conditions, and the following disclaimer, 21 | * without modification. 22 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer 23 | * substantially similar to the "NO WARRANTY" disclaimer below 24 | * ("Disclaimer") and any redistribution must be conditioned upon 25 | * including a substantially similar Disclaimer requirement for further 26 | * binary redistribution. 27 | * 3. Neither the names of the above-listed copyright holders nor the names 28 | * of any contributors may be used to endorse or promote products derived 29 | * from this software without specific prior written permission. 30 | * 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 36 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 37 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 | * 43 | *****************************************************************************/ 44 | 45 | #ifdef KTF_ACPICA 46 | 47 | #ifndef __ACKTF_H__ 48 | #define __ACKTF_H__ 49 | 50 | /* Common (in-kernel/user-space) ACPICA configuration */ 51 | 52 | #define ACPI_USE_SYSTEM_CLIBRARY 53 | #define ACPI_USE_DO_WHILE_0 54 | #define ACPI_USE_LOCAL_CACHE 55 | 56 | /* Kernel specific ACPICA configuration */ 57 | 58 | #ifdef KTF_DEBUG 59 | #undef ACPI_NO_ERROR_MESSAGES 60 | #define ACPI_DEBUG_OUTPUT 61 | #endif 62 | 63 | #include 64 | #include 65 | 66 | #include "acenv.h" 67 | 68 | #define ACPI_INIT_FUNCTION __text_init 69 | #define ACPI_CACHE_T ACPI_MEMORY_LIST 70 | 71 | #define ACPI_UINTPTR_T uintptr_t 72 | #define ACPI_TO_INTEGER(p) ((uintptr_t)(p)) 73 | #define ACPI_OFFSET(d, f) offsetof(d, f) 74 | 75 | /* Host-dependent types and defines for in-kernel ACPICA */ 76 | 77 | #define ACPI_EXPORT_SYMBOL(symbol) 78 | 79 | #define ACPI_SPINLOCK spinlock_t * 80 | #define ACPI_CPU_FLAGS unsigned long 81 | 82 | #define ACPI_MSG_ERROR "ACPI Error: " 83 | #define ACPI_MSG_EXCEPTION "ACPI Exception: " 84 | #define ACPI_MSG_WARNING "ACPI Warning: " 85 | #define ACPI_MSG_INFO "ACPI: " 86 | 87 | #define ACPI_MSG_BIOS_ERROR "ACPI BIOS Error (bug): " 88 | #define ACPI_MSG_BIOS_WARNING "ACPI BIOS Warning (bug): " 89 | 90 | #ifndef __init 91 | #define __init 92 | #endif 93 | #ifndef __iomem 94 | #define __iomem 95 | #endif 96 | 97 | #if defined(__x86_64__) 98 | #define ACPI_MACHINE_WIDTH 64 99 | #define COMPILER_DEPENDENT_INT64 long 100 | #define COMPILER_DEPENDENT_UINT64 unsigned long 101 | #else 102 | #define ACPI_MACHINE_WIDTH 32 103 | #define COMPILER_DEPENDENT_INT64 long long 104 | #define COMPILER_DEPENDENT_UINT64 unsigned long long 105 | #endif 106 | 107 | #endif /* __ACKTF_H__ */ 108 | #endif /* KTF_ACPICA */ 109 | -------------------------------------------------------------------------------- /common/cpu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Open Source Security, Inc. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | static list_head_t cpus; 36 | static unsigned int nr_cpus = 0; 37 | 38 | static cpu_t bsp = {0}; 39 | 40 | static void init_cpu(cpu_t *cpu, unsigned int id, bool is_bsp, bool enabled) { 41 | memset(cpu, 0, sizeof(*cpu)); 42 | cpu->id = id; 43 | cpu->flags.bsp = is_bsp; 44 | cpu->flags.enabled = enabled; 45 | 46 | init_cpu_runstate(cpu); 47 | if (is_bsp) 48 | set_cpu_unblocked(cpu); 49 | 50 | cpu->percpu = get_percpu_page(id); 51 | BUG_ON(!cpu->percpu); 52 | cpu->percpu->cpu = cpu; 53 | 54 | cpu->lock = SPINLOCK_INIT; 55 | list_init(&cpu->task_queue); 56 | } 57 | 58 | cpu_t *init_cpus(void) { 59 | printk("Initialize CPU structures\n"); 60 | 61 | list_init(&cpus); 62 | 63 | init_cpu(&bsp, 0, true, true); 64 | list_add(&bsp.list, &cpus); 65 | nr_cpus = 1; 66 | 67 | return &bsp; 68 | } 69 | 70 | cpu_t *add_cpu(unsigned int id, bool is_bsp, bool enabled) { 71 | cpu_t *cpu = kzalloc(sizeof(*cpu)); 72 | 73 | if (!cpu) 74 | return NULL; 75 | 76 | init_cpu(cpu, id, is_bsp, enabled); 77 | 78 | list_add(&cpu->list, &cpus); 79 | nr_cpus++; 80 | 81 | return cpu; 82 | } 83 | 84 | cpu_t *get_cpu(unsigned int id) { 85 | cpu_t *cpu; 86 | 87 | list_for_each_entry (cpu, &cpus, list) { 88 | if (cpu->id == id) 89 | return cpu; 90 | } 91 | 92 | return NULL; 93 | } 94 | 95 | unsigned int get_nr_cpus(void) { 96 | return nr_cpus; 97 | } 98 | 99 | cpu_t *get_bsp_cpu(void) { 100 | return &bsp; 101 | } 102 | 103 | void for_each_cpu(void (*func)(cpu_t *cpu)) { 104 | cpu_t *cpu; 105 | 106 | list_for_each_entry (cpu, &cpus, list) 107 | func(cpu); 108 | } 109 | 110 | void unblock_all_cpus(void) { 111 | cpu_t *cpu; 112 | 113 | list_for_each_entry (cpu, &cpus, list) 114 | set_cpu_unblocked(cpu); 115 | } 116 | 117 | void block_all_cpus(void) { 118 | cpu_t *cpu; 119 | 120 | list_for_each_entry (cpu, &cpus, list) 121 | set_cpu_blocked(cpu); 122 | } 123 | 124 | void finish_all_cpus(void) { 125 | cpu_t *cpu; 126 | 127 | list_for_each_entry (cpu, &cpus, list) 128 | set_cpu_finished(cpu); 129 | } 130 | 131 | void wait_for_all_cpus(void) { 132 | cpu_t *cpu; 133 | 134 | list_for_each_entry (cpu, &cpus, list) { 135 | if (is_cpu_bsp(cpu)) 136 | continue; 137 | 138 | while (!is_cpu_finished(cpu) || !list_is_empty(&cpu->task_queue)) 139 | cpu_relax(); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /include/pci.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2022 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_PCI_H 26 | #define KTF_PCI_H 27 | 28 | #include 29 | 30 | #define PCI_NR_BUS 256 31 | #define PCI_NR_DEV 32 32 | #define PCI_NR_FUNC 8 33 | 34 | #define PCI_VENDOR_MASK 0x0000FFFF 35 | #define PCI_VENDOR_INVALID 0xFFFF 36 | 37 | #define PCI_DEVICE_MASK 0xFFFF0000 38 | #define PCI_DEVICE_SHIFT 16 39 | 40 | #define PCI_COMMAND_MASK 0x0000FFFF 41 | 42 | #define PCI_STATUS_MASK 0xFFFF0000 43 | #define PCI_STATUS_SHIFT 16 44 | #define PCI_STATUS_CAP_LIST (1U << 4) 45 | 46 | #define PCI_CLASS_MASK 0xFF000000 47 | #define PCI_CLASS_SHIFT 24 48 | #define PCI_CLASS_BRIDGE 0x06 49 | 50 | #define PCI_SUBCLASS_MASK 0x00FF0000 51 | #define PCI_SUBCLASS_SHIFT 16 52 | #define PCI_SUBCLASS_HOST_BRIDGE 0x0 53 | 54 | #define PCI_HDR_MASK 0x00FF0000 55 | #define PCI_HDR_SHIFT 16 56 | #define PCI_HDR_TYPE 0x7F 57 | #define PCI_HDR_TYPE_NORMAL 0x00 58 | #define PCI_HDR_TYPE_PCI_BRIDGE 0x01 59 | #define PCI_HDR_MULTIFUNC 0x80 60 | 61 | #define PCI_SEC_BUS_MASK 0x0000FF00 62 | #define PCI_SEC_BUS_SHIFT 8 63 | #define PCI_SUB_BUS_MASK 0x00FF0000 64 | #define PCI_SUB_BUS_SHIFT 16 65 | 66 | #define PCI_REG_VENDOR 0x0 67 | #define PCI_REG_COMMAND 0x1 68 | #define PCI_REG_CLASS 0x2 69 | #define PCI_REG_HDR 0x3 70 | #define PCI_REG_BUS 0x6 71 | #define PCI_REG_CAP_PTR 0xD 72 | 73 | #define PCI_VENDOR(cfg_reg0) (cfg_reg0 & PCI_VENDOR_MASK) 74 | #define PCI_DEVICE(cfg_reg0) ((cfg_reg0 & PCI_DEVICE_MASK) >> PCI_DEVICE_SHIFT) 75 | #define PCI_COMMAND(cfg_reg1) (cfg_reg1 & PCI_COMMAND_MASK) 76 | #define PCI_STATUS(cfg_reg1) ((cfg_reg1 & PCI_STATUS_MASK) >> PCI_STATUS_SHIFT) 77 | #define PCI_CLASS(cfg_reg2) ((cfg_reg2 & PCI_CLASS_MASK) >> PCI_CLASS_SHIFT) 78 | #define PCI_SUBCLASS(cfg_reg2) ((cfg_reg2 & PCI_SUBCLASS_MASK) >> PCI_SUBCLASS_SHIFT) 79 | #define PCI_HDR(cfg_reg3) ((cfg_reg3 & PCI_HDR_MASK) >> PCI_HDR_SHIFT) 80 | #define PCI_SEC_BUS(cfg_reg6) ((cfg_reg6 & PCI_SEC_BUS_MASK) >> PCI_SEC_BUS_SHIFT) 81 | #define PCI_SUB_BUS(cfg_reg6) ((cfg_reg6 & PCI_SUB_BUS_MASK) >> PCI_SUB_BUS_SHIFT) 82 | 83 | #define PCI_DEV_EXISTS(cfg_reg0) (PCI_VENDOR(cfg_reg0) != PCI_VENDOR_INVALID) 84 | 85 | struct pcidev { 86 | list_head_t list; 87 | uint32_t segment : 16; 88 | uint32_t bus : 8; 89 | uint32_t dev : 5; 90 | uint32_t func : 3; 91 | uint16_t vendor_id; 92 | uint16_t device_id; 93 | uint16_t command; 94 | uint16_t status; 95 | uint8_t subclass; 96 | uint8_t class; 97 | uint8_t hdr; 98 | uint8_t cap_ptr; 99 | struct pcidev *bridge; 100 | char bdf_str[10]; 101 | }; 102 | typedef struct pcidev pcidev_t; 103 | 104 | extern void init_pci(void); 105 | 106 | #endif /* KTF_PCI_H */ 107 | -------------------------------------------------------------------------------- /include/drivers/hpet.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef KTF_HPET_H 28 | #define KTF_HPET_H 29 | 30 | #include 31 | 32 | #ifndef KTF_ACPICA 33 | #include 34 | #else 35 | #include 36 | #endif 37 | #include 38 | 39 | #define HPET_IRQ PIT_IRQ 40 | #define HPET_IRQ_OFFSET PIT_IRQ_OFFSET 41 | #define HPET_IRQ_VECTOR HPET_IRQ_OFFSET 42 | 43 | #ifndef KTF_ACPICA 44 | #define HPET_SIGNATURE (('H') | ('P' << 8) | ('E' << 16) | ('T' << 24)) 45 | 46 | struct acpi_hpet { 47 | acpi_table_hdr_t header; 48 | uint8_t hardware_rev_id; 49 | uint8_t comparator_count : 5; 50 | uint8_t counter_size : 1; 51 | uint8_t reserved : 1; 52 | uint8_t legacy_replacement : 1; 53 | uint16_t pci_vendor_id; 54 | acpi_gas_t address; 55 | uint8_t hpet_number; 56 | uint16_t minimum_tick; 57 | uint8_t page_protection; 58 | } __packed; 59 | typedef struct acpi_hpet acpi_hpet_t; 60 | #endif 61 | 62 | struct acpi_hpet_timer { 63 | uint64_t resv0 : 1; 64 | uint64_t cfg_int_type : 1; 65 | uint64_t cfg_int_enabled : 1; 66 | uint64_t cfg_type : 1; 67 | uint64_t cap_int_periodic : 1; 68 | uint64_t cap_size : 1; 69 | uint64_t cfg_value_set : 1; 70 | uint64_t resv1 : 1; 71 | uint64_t cfg_32bit_mode : 1; 72 | uint64_t cfg_int_route : 5; 73 | uint64_t cfg_fsb_enable : 1; 74 | uint64_t cap_fsb_int_delivery : 1; 75 | uint64_t resv2 : 8; 76 | uint64_t cap_int_route : 32; 77 | uint64_t comparator; 78 | uint64_t fsb; 79 | uint64_t resv3; 80 | }; 81 | typedef struct acpi_hpet_timer acpi_hpet_timer_t; 82 | 83 | struct acpi_hpet_general { 84 | uint64_t rev_id : 8; 85 | uint64_t num_timers_cap : 5; 86 | uint64_t count_size_cap : 1; 87 | uint64_t resv0 : 1; 88 | uint64_t leg_repl_cap : 1; 89 | uint64_t vendor_id : 16; 90 | uint64_t counter_clock_period : 32; 91 | uint64_t _pad; 92 | uint64_t enabled : 1; 93 | uint64_t leg_repl_cfg : 1; 94 | uint64_t resv1 : 62; 95 | uint64_t _pad2; 96 | uint64_t timer_n_int_status : 32; /* n^th bit for the n^th timer */ 97 | uint32_t resv2; 98 | }; 99 | typedef struct acpi_hpet_general acpi_hpet_general_t; 100 | 101 | enum { 102 | HPET_OFFSET_GENERAL_CAP_REG = 0x000, 103 | HPET_OFFSET_GENERAL_CONFIG_REG = 0x010, 104 | HPET_OFFSET_GENERAL_INT_STATUS_REG = 0x020, 105 | HPET_OFFSET_GENERAL_MAIN_COUNTER_REG = 0x0f0, 106 | HPET_OFFSET_TIMER_0_CONFIG_CAP_REG = 0x100, 107 | HPET_OFFSET_TIMER_0_COMPARATOR_REG = 0x108, 108 | HPET_OFFSET_TIMER_0_FSB_ROUTE_REG = 0x110, 109 | }; 110 | 111 | #define HPET_CONFIG_INT_TYPE_TRIGGER_MODE 0 112 | 113 | /* External Declarations */ 114 | 115 | extern bool init_hpet(const cpu_t *cpu); 116 | 117 | #endif /* KTF_HPET_H */ 118 | -------------------------------------------------------------------------------- /include/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_LIST_H 26 | #define KTF_LIST_H 27 | 28 | #include 29 | #include 30 | 31 | struct list_head { 32 | struct list_head *next; 33 | struct list_head *prev; 34 | }; 35 | typedef struct list_head list_head_t; 36 | 37 | #define LIST_INIT(head) \ 38 | { .next = &(head), .prev = &(head) } 39 | 40 | /* Static declarations */ 41 | 42 | static inline void list_init(list_head_t *head) { 43 | head->next = head; 44 | head->prev = head; 45 | } 46 | 47 | static inline void list_insert(list_head_t *new, list_head_t *prev, list_head_t *next) { 48 | new->prev = prev; 49 | new->next = next; 50 | 51 | next->prev = new; 52 | prev->next = new; 53 | } 54 | 55 | static inline void list_add(list_head_t *new, list_head_t *top) { 56 | list_insert(new, top, top->next); 57 | } 58 | 59 | static inline void list_add_tail(list_head_t *new, list_head_t *top) { 60 | list_insert(new, top->prev, top); 61 | } 62 | 63 | static inline void list_unlink(list_head_t *entry) { 64 | entry->next->prev = entry->prev; 65 | entry->prev->next = entry->next; 66 | entry->next = NULL; 67 | entry->prev = NULL; 68 | } 69 | 70 | static inline bool list_is_empty(list_head_t *list) { 71 | return list->next == list; 72 | } 73 | 74 | #define list_entry(elem, type, member) container_of(elem, type, member) 75 | 76 | #define list_next_entry(elem, member) \ 77 | list_entry((elem)->member.next, typeof(*(elem)), member) 78 | #define list_prev_entry(elem, member) \ 79 | list_entry((elem)->member.prev, typeof(*(elem)), member) 80 | 81 | #define list_first_entry(head, type, member) list_entry((head)->next, type, member) 82 | #define list_last_entry(head, type, member) list_entry((head)->prev, type, member) 83 | 84 | #define list_for_each(ptr, head) for (ptr = (head)->next; ptr != (head); ptr = ptr->next) 85 | 86 | #define list_for_each_safe(ptr, bak, head) \ 87 | for (ptr = (head)->next, bak = ptr->next; ptr != (head); ptr = bak, bak = ptr->next) 88 | 89 | #define list_for_each_entry(elem, head, member) \ 90 | for (elem = list_first_entry(head, typeof(*elem), member); &elem->member != (head); \ 91 | elem = list_next_entry(elem, member)) 92 | 93 | #define list_for_each_entry_safe(elem, bak, head, member) \ 94 | for (elem = list_first_entry(head, typeof(*elem), member), \ 95 | bak = list_next_entry(elem, member); \ 96 | &elem->member != (head); elem = bak, bak = list_next_entry(bak, member)) 97 | 98 | #endif /* KTF_LIST_H */ 99 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | AccessModifierOffset: -2 4 | AlignAfterOpenBracket: Align 5 | AlignConsecutiveMacros: true 6 | AlignConsecutiveAssignments: false 7 | AlignConsecutiveDeclarations: false 8 | AlignEscapedNewlines: Right 9 | AlignOperands: true 10 | AlignTrailingComments: true 11 | AllowAllArgumentsOnNextLine: true 12 | AllowAllConstructorInitializersOnNextLine: true 13 | AllowAllParametersOfDeclarationOnNextLine: true 14 | AllowShortBlocksOnASingleLine: Never 15 | AllowShortCaseLabelsOnASingleLine: false 16 | AllowShortFunctionsOnASingleLine: false 17 | AllowShortLambdasOnASingleLine: All 18 | AllowShortIfStatementsOnASingleLine: Never 19 | AllowShortLoopsOnASingleLine: false 20 | AlwaysBreakAfterDefinitionReturnType: None 21 | AlwaysBreakAfterReturnType: None 22 | AlwaysBreakBeforeMultilineStrings: false 23 | AlwaysBreakTemplateDeclarations: MultiLine 24 | BinPackArguments: true 25 | BinPackParameters: true 26 | BraceWrapping: 27 | AfterCaseLabel: false 28 | AfterClass: false 29 | AfterControlStatement: false 30 | AfterEnum: false 31 | AfterFunction: false 32 | AfterNamespace: false 33 | AfterObjCDeclaration: false 34 | AfterStruct: false 35 | AfterUnion: false 36 | AfterExternBlock: false 37 | BeforeCatch: false 38 | BeforeElse: true 39 | IndentBraces: false 40 | SplitEmptyFunction: true 41 | SplitEmptyRecord: true 42 | SplitEmptyNamespace: true 43 | BreakBeforeBinaryOperators: None 44 | BreakBeforeBraces: Custom 45 | BreakBeforeInheritanceComma: false 46 | BreakInheritanceList: BeforeColon 47 | BreakBeforeTernaryOperators: true 48 | BreakConstructorInitializersBeforeComma: false 49 | BreakConstructorInitializers: BeforeColon 50 | BreakAfterJavaFieldAnnotations: false 51 | BreakStringLiterals: true 52 | ColumnLimit: 90 53 | CommentPragmas: '^ IWYU pragma:' 54 | CompactNamespaces: false 55 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 56 | ConstructorInitializerIndentWidth: 4 57 | ContinuationIndentWidth: 4 58 | Cpp11BracedListStyle: true 59 | DeriveLineEnding: true 60 | DerivePointerAlignment: false 61 | DisableFormat: false 62 | ExperimentalAutoDetectBinPacking: false 63 | FixNamespaceComments: true 64 | ForEachMacros: 65 | - 'for_each_memory_range' 66 | - 'for_each_order' 67 | - 'list_for_each' 68 | - 'list_for_each_entry' 69 | - 'list_for_each_entry_safe' 70 | - 'list_for_each_safe' 71 | IncludeBlocks: Preserve 72 | IncludeCategories: 73 | - Regex: '.*' 74 | Priority: 1 75 | SortPriority: 0 76 | IncludeIsMainRegex: '(Test)?$' 77 | IncludeIsMainSourceRegex: '' 78 | IndentCaseLabels: false 79 | IndentGotoLabels: true 80 | IndentPPDirectives: None 81 | IndentWidth: 4 82 | IndentWrappedFunctionNames: false 83 | JavaScriptQuotes: Leave 84 | JavaScriptWrapImports: true 85 | KeepEmptyLinesAtTheStartOfBlocks: true 86 | MacroBlockBegin: '' 87 | MacroBlockEnd: '' 88 | MaxEmptyLinesToKeep: 1 89 | NamespaceIndentation: None 90 | ObjCBinPackProtocolList: Auto 91 | ObjCBlockIndentWidth: 2 92 | ObjCSpaceAfterProperty: false 93 | ObjCSpaceBeforeProtocolList: true 94 | PenaltyBreakAssignment: 2 95 | PenaltyBreakBeforeFirstCallParameter: 19 96 | PenaltyBreakComment: 300 97 | PenaltyBreakFirstLessLess: 120 98 | PenaltyBreakString: 1000 99 | PenaltyBreakTemplateDeclaration: 10 100 | PenaltyExcessCharacter: 1000000 101 | PenaltyReturnTypeOnItsOwnLine: 60 102 | PointerAlignment: Right 103 | ReflowComments: true 104 | SortIncludes: true 105 | SortUsingDeclarations: true 106 | SpaceAfterCStyleCast: true 107 | SpaceAfterLogicalNot: false 108 | SpaceAfterTemplateKeyword: true 109 | SpaceBeforeAssignmentOperators: true 110 | SpaceBeforeCpp11BracedList: false 111 | SpaceBeforeCtorInitializerColon: true 112 | SpaceBeforeInheritanceColon: true 113 | SpaceBeforeParens: ControlStatements 114 | SpaceBeforeRangeBasedForLoopColon: true 115 | SpaceInEmptyBlock: false 116 | SpaceInEmptyParentheses: false 117 | SpacesBeforeTrailingComments: 1 118 | SpacesInAngles: false 119 | SpacesInConditionalStatement: false 120 | SpacesInContainerLiterals: true 121 | SpacesInCStyleCastParentheses: false 122 | SpacesInParentheses: false 123 | SpacesInSquareBrackets: false 124 | SpaceBeforeSquareBrackets: false 125 | Standard: Latest 126 | StatementMacros: 127 | - Q_UNUSED 128 | - QT_REQUIRE_VERSION 129 | TabWidth: 4 130 | UseCRLF: false 131 | UseTab: Never 132 | ... 133 | 134 | -------------------------------------------------------------------------------- /include/sched.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_SCHED_H 26 | #define KTF_SCHED_H 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | typedef unsigned long (*task_func_t)(void *arg); 35 | 36 | enum task_state { 37 | TASK_STATE_NEW, 38 | TASK_STATE_READY, 39 | TASK_STATE_SCHEDULED, 40 | TASK_STATE_RUNNING, 41 | TASK_STATE_DONE, 42 | }; 43 | typedef enum task_state task_state_t; 44 | 45 | enum task_group { 46 | TASK_GROUP_ALL = 0, 47 | TASK_GROUP_ACPI, 48 | TASK_GROUP_TEST, 49 | }; 50 | typedef enum task_group task_group_t; 51 | 52 | enum task_type { 53 | TASK_TYPE_KERNEL = 0, 54 | TASK_TYPE_USER, 55 | TASK_TYPE_INTERRUPT, 56 | TASK_TYPE_ACPI_SERVICE, 57 | }; 58 | typedef enum task_type task_type_t; 59 | 60 | typedef unsigned int tid_t; 61 | 62 | typedef enum task_repeat { 63 | TASK_REPEAT_LOOP = 0, 64 | TASK_REPEAT_ONCE = 1, 65 | } task_repeat_t; 66 | 67 | struct task { 68 | list_head_t list; 69 | 70 | tid_t id; 71 | task_type_t type; 72 | task_group_t gid; 73 | task_state_t state; 74 | task_repeat_t repeat; 75 | atomic64_t exec_count; 76 | 77 | cpu_t *cpu; 78 | void *stack; 79 | 80 | const char *name; 81 | task_func_t func; 82 | void *arg; 83 | 84 | unsigned long result; 85 | }; 86 | typedef struct task task_t; 87 | 88 | /* External declarations */ 89 | 90 | extern void init_tasks(void); 91 | extern task_t *get_task_by_name(cpu_t *cpu, const char *name); 92 | extern task_t *new_task(const char *name, task_func_t func, void *arg, task_type_t type); 93 | extern int schedule_task(task_t *task, cpu_t *cpu); 94 | extern void run_tasks(cpu_t *cpu); 95 | extern void wait_for_task_group(const cpu_t *cpu, task_group_t group); 96 | 97 | /* Static declarations */ 98 | 99 | static inline void set_task_group(task_t *task, task_group_t gid) { 100 | task->gid = gid; 101 | } 102 | 103 | static inline void wait_for_cpu_tasks(cpu_t *cpu) { 104 | wait_for_task_group(cpu, TASK_GROUP_ALL); 105 | } 106 | 107 | static inline task_t *new_kernel_task(const char *name, task_func_t func, void *arg) { 108 | return new_task(name, func, arg, TASK_TYPE_KERNEL); 109 | } 110 | 111 | static inline task_t *new_user_task(const char *name, task_func_t func, void *arg) { 112 | return new_task(name, func, arg, TASK_TYPE_USER); 113 | } 114 | 115 | static inline void execute_tasks(void) { 116 | unblock_all_cpus(); 117 | run_tasks(get_bsp_cpu()); 118 | wait_for_all_cpus(); 119 | } 120 | 121 | static inline void set_task_repeat(task_t *task, task_repeat_t value) { 122 | ASSERT(task); 123 | task->repeat = value; 124 | } 125 | 126 | static inline void set_task_loop(task_t *task) { 127 | set_task_repeat(task, TASK_REPEAT_LOOP); 128 | } 129 | 130 | static inline void set_task_once(task_t *task) { 131 | set_task_repeat(task, TASK_REPEAT_ONCE); 132 | } 133 | 134 | #endif /* KTF_SCHED_H */ 135 | -------------------------------------------------------------------------------- /arch/x86/cpuid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | uint64_t get_cpu_freq(const char *cpu_str) { 31 | size_t len = strlen(cpu_str); 32 | uint64_t frequency = 0; 33 | char buf[16]; 34 | char buf2[16]; 35 | char *reverse = &buf[0]; 36 | char *freq = &buf2[0]; 37 | 38 | /* we need to reverse the vendor string for parsing the freq */ 39 | while (len--) { 40 | if (isspace(cpu_str[len])) { 41 | *reverse = '\0'; 42 | break; 43 | } 44 | *reverse++ = cpu_str[len]; 45 | } 46 | 47 | if (strstr(buf, "zHM")) { 48 | len = strlen(buf); 49 | if (len >= 1) { 50 | for (int i = (int) len - 1; i >= 0; i--) 51 | if (isdigit(buf[i])) 52 | *freq++ = buf[i]; 53 | 54 | frequency = strtoul(buf2, NULL, 0) * MHZ(1); 55 | } 56 | } 57 | else if (strstr(buf, "zHG")) { 58 | uint64_t multiplier = GHZ(1); 59 | 60 | /* Convert a floating number to 61 | * n * GHz + m * MHz 62 | */ 63 | len = strlen(buf); 64 | if (len >= 1) { 65 | for (int i = (int) len - 1; i >= 0; i--) { 66 | if (isdigit(buf[i])) 67 | *freq++ = buf[i]; 68 | if (ispunct(buf[i])) { 69 | *freq = '\0'; 70 | frequency = strtoul(buf2, NULL, 0) * multiplier; 71 | memset(buf2, 0, sizeof(buf2)); 72 | freq = &buf2[0]; 73 | multiplier = MHZ(1); 74 | } 75 | } 76 | *freq = '\0'; 77 | 78 | /* we have to check if we hit a float and calculate 79 | * the length of the MHz portion e.g. 2.80GHz or 2.8GHz 80 | */ 81 | if (multiplier == MHZ(1)) { 82 | frequency += 83 | strtoul(buf2, NULL, 0) * (1000 / ipow(10, strlen(buf2))) * multiplier; 84 | } 85 | else { 86 | frequency = strtoul(buf2, NULL, 0) * multiplier; 87 | } 88 | } 89 | } 90 | 91 | return frequency; 92 | } 93 | 94 | bool cpu_vendor_string(char *cpu_str) { 95 | uint32_t leaf = CPUID_EXT_INFO_LEAF; 96 | uint32_t ebx = 0, ecx = 0, edx = 0; 97 | uint32_t eax = cpuid_eax(leaf); 98 | 99 | if (!(eax & leaf) || (eax < CPUID_BRAND_INFO_MAX)) { 100 | dprintk("Extended Function CPUID Information not supported\n"); 101 | return false; 102 | } 103 | 104 | for (leaf = CPUID_BRAND_INFO_MIN; leaf <= CPUID_BRAND_INFO_MAX; 105 | leaf++, cpu_str += 16) { 106 | cpuid(leaf, &eax, &ebx, &ecx, &edx); 107 | memcpy(cpu_str, &eax, sizeof(eax)); 108 | memcpy(cpu_str + 4, &ebx, sizeof(ebx)); 109 | memcpy(cpu_str + 8, &ecx, sizeof(ecx)); 110 | memcpy(cpu_str + 12, &edx, sizeof(edx)); 111 | } 112 | 113 | return true; 114 | } 115 | -------------------------------------------------------------------------------- /include/arch/x86/pci_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2021 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_PCI_CFG_H 26 | #define KTF_PCI_CFG_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #define PCI_IO_PORT_ADDRESS 0xcf8 33 | #define PCI_IO_PORT_DATA 0xcfc 34 | 35 | extern spinlock_t pci_cfg_lock; 36 | 37 | union pci_cfg_addr { 38 | struct { 39 | uint32_t zero : 2, reg : 6, fn : 3, dev : 5, bus : 8, rsvd : 7, ecsm : 1; 40 | } __packed; 41 | uint32_t val; 42 | }; 43 | typedef union pci_cfg_addr pci_cfg_addr_t; 44 | 45 | union pci_cfg_value { 46 | uint8_t byte[4]; 47 | uint16_t word[2]; 48 | uint32_t dword; 49 | }; 50 | typedef union pci_cfg_value pci_cfg_value_t; 51 | 52 | /* Static declarations */ 53 | 54 | static inline void pci_cfg_set_addr(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { 55 | pci_cfg_addr_t addr; 56 | 57 | addr.val = 0; 58 | addr.reg = reg; 59 | addr.fn = func; 60 | addr.dev = dev; 61 | addr.bus = bus; 62 | addr.ecsm = 1; 63 | 64 | outd(PCI_IO_PORT_ADDRESS, addr.val); 65 | } 66 | 67 | static inline uint8_t pci_cfg_read8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { 68 | uint8_t ret; 69 | 70 | spin_lock(&pci_cfg_lock); 71 | 72 | pci_cfg_set_addr(bus, dev, func, reg); 73 | ret = inb(PCI_IO_PORT_DATA); 74 | 75 | spin_unlock(&pci_cfg_lock); 76 | 77 | return ret; 78 | } 79 | 80 | static inline void pci_cfg_write8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, 81 | uint8_t value) { 82 | spin_lock(&pci_cfg_lock); 83 | 84 | pci_cfg_set_addr(bus, dev, func, reg); 85 | outb(PCI_IO_PORT_DATA, value); 86 | 87 | spin_unlock(&pci_cfg_lock); 88 | } 89 | 90 | static inline uint16_t pci_cfg_read16(uint8_t bus, uint8_t dev, uint8_t func, 91 | uint8_t reg) { 92 | uint16_t ret; 93 | 94 | spin_lock(&pci_cfg_lock); 95 | 96 | pci_cfg_set_addr(bus, dev, func, reg); 97 | ret = inw(PCI_IO_PORT_DATA); 98 | 99 | spin_unlock(&pci_cfg_lock); 100 | 101 | return ret; 102 | } 103 | 104 | static inline void pci_cfg_write16(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, 105 | uint16_t value) { 106 | spin_lock(&pci_cfg_lock); 107 | 108 | pci_cfg_set_addr(bus, dev, func, reg); 109 | outw(PCI_IO_PORT_DATA, value); 110 | 111 | spin_unlock(&pci_cfg_lock); 112 | } 113 | 114 | static inline uint32_t pci_cfg_read(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { 115 | uint32_t ret; 116 | 117 | spin_lock(&pci_cfg_lock); 118 | 119 | pci_cfg_set_addr(bus, dev, func, reg); 120 | ret = ind(PCI_IO_PORT_DATA); 121 | 122 | spin_unlock(&pci_cfg_lock); 123 | 124 | return ret; 125 | } 126 | 127 | static inline void pci_cfg_write(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, 128 | uint32_t value) { 129 | spin_lock(&pci_cfg_lock); 130 | 131 | pci_cfg_set_addr(bus, dev, func, reg); 132 | outd(PCI_IO_PORT_DATA, value); 133 | 134 | spin_unlock(&pci_cfg_lock); 135 | } 136 | 137 | /* External declarations */ 138 | 139 | #endif /* KTF_PCI_CFG_H */ 140 | -------------------------------------------------------------------------------- /mm/vmm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | /* Used by higher level mmap_range() functions - must be taken before vmap_lock */ 34 | static spinlock_t mmap_lock = SPINLOCK_INIT; 35 | 36 | static inline vmap_flags_t gfp_to_vmap_flags(gfp_flags_t gfp_flags) { 37 | vmap_flags_t vmap_flags = VMAP_NONE; 38 | 39 | if (gfp_flags == GFP_USER) 40 | return VMAP_KERNEL_USER | VMAP_USER; 41 | 42 | if (gfp_flags & GFP_IDENT) { 43 | vmap_flags |= VMAP_IDENT; 44 | if (gfp_flags & GFP_USER) 45 | vmap_flags |= VMAP_USER_IDENT; 46 | } 47 | 48 | if (gfp_flags & GFP_KERNEL) { 49 | vmap_flags |= VMAP_KERNEL; 50 | if (gfp_flags & GFP_USER) 51 | vmap_flags |= VMAP_USER_KERNEL; 52 | } 53 | 54 | if (gfp_flags & GFP_KERNEL_MAP) { 55 | vmap_flags |= VMAP_KERNEL_MAP; 56 | if (gfp_flags & GFP_USER) 57 | vmap_flags |= VMAP_USER_KERNEL_MAP; 58 | } 59 | 60 | return vmap_flags; 61 | } 62 | 63 | static inline void *gfp_mfn_to_virt(gfp_flags_t gfp_flags, mfn_t mfn) { 64 | /* Return virtual address if a single area is specified ... */ 65 | switch (gfp_flags) { 66 | case GFP_IDENT: 67 | return mfn_to_virt(mfn); 68 | case GFP_KERNEL_MAP: 69 | return mfn_to_virt_map(mfn); 70 | case GFP_USER: 71 | return mfn_to_virt_user(mfn); 72 | case GFP_KERNEL: 73 | return mfn_to_virt_kern(mfn); 74 | default: 75 | /* Otherwise, return kernel addresses if specified before identity 76 | * mapping or user. The below order reflects most common uses. 77 | */ 78 | if (gfp_flags & GFP_KERNEL_MAP) 79 | return mfn_to_virt_map(mfn); 80 | else if (gfp_flags & GFP_KERNEL) 81 | return mfn_to_virt_kern(mfn); 82 | else if (gfp_flags & GFP_IDENT) 83 | return mfn_to_virt(mfn); 84 | else if (gfp_flags & GFP_USER) 85 | return mfn_to_virt_user(mfn); 86 | } 87 | 88 | return NULL; 89 | } 90 | 91 | void *get_free_pages(unsigned int order, gfp_flags_t gfp_flags) { 92 | void *va = NULL; 93 | frame_t *frame; 94 | mfn_t mfn; 95 | size_t size; 96 | unsigned long pt_flags; 97 | vmap_flags_t vmap_flags; 98 | 99 | ASSERT(gfp_flags != GFP_NONE); 100 | 101 | if (!boot_flags.virt) 102 | panic("Unable to use %s() before final page tables are set", __func__); 103 | 104 | frame = get_free_frames(order); 105 | if (!frame) 106 | return va; 107 | mfn = frame->mfn; 108 | 109 | size = ORDER_TO_SIZE(order); 110 | pt_flags = order_to_flags(order); 111 | vmap_flags = gfp_to_vmap_flags(gfp_flags); 112 | 113 | spin_lock(&mmap_lock); 114 | if (vmap_range(mfn_to_paddr(mfn), size, pt_flags, vmap_flags) == 0) 115 | va = gfp_mfn_to_virt(gfp_flags, mfn); 116 | spin_unlock(&mmap_lock); 117 | 118 | return va; 119 | } 120 | 121 | void put_pages(void *page) { 122 | unsigned int order; 123 | mfn_t mfn; 124 | 125 | spin_lock(&mmap_lock); 126 | BUG_ON(vunmap_kern(page, &mfn, &order)); 127 | spin_unlock(&mmap_lock); 128 | put_free_frames(mfn, order); 129 | } -------------------------------------------------------------------------------- /include/drivers/keyboard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_KEYBOARD_H 26 | #define KTF_KEYBOARD_H 27 | 28 | #include 29 | #include 30 | 31 | #define KEYBOARD_PORT_CMD 0x64 /* keyboard command port */ 32 | #define KEYBOARD_PORT_DATA 0x60 /* keyboard data port */ 33 | 34 | #define KEYBOARD_PORT1_IRQ 0x01 35 | #define KEYBOARD_PORT2_IRQ 0x04 /* Second port */ 36 | /* keyboard first channel irq offset */ 37 | #define KEYBOARD_PORT1_IRQ_OFFSET (PIC_IRQ0_OFFSET + KEYBOARD_PORT1_IRQ) 38 | #define KEYBOARD_PORT1_IRQ_VECTOR KEYBOARD_PORT1_IRQ_OFFSET 39 | #define KEYBOARD_PORT2_IRQ_OFFSET (PIC_IRQ0_OFFSET + KEYBOARD_PORT2_IRQ) 40 | #define KEYBOARD_PORT2_IRQ_VECTOR KEYBOARD_PORT2_IRQ_OFFSET 41 | 42 | /* bit set when the keyboard output buffer is full */ 43 | #define KEYBOARD_STATUS_OUT_FULL 0x01 44 | /* bit set when the keyboard input buffer is full */ 45 | #define KEYBOARD_STATUS_IN_FULL 0x02 46 | 47 | #define KEYBOARD_RESET_CMD 0xFE 48 | 49 | typedef enum { 50 | KEYBOARD_CMD_WRITE_CONFIGURATION = 0x60, /* Write configuration byte */ 51 | KEYBOARD_CMD_READ_CONFIGURATION = 0x20, /* Read configuration byte */ 52 | KEYBOARD_CMD_SELF_TEST = 0xAA, 53 | KEYBOARD_CMD_TEST_PORT_1 = 0xAB, 54 | KEYBOARD_CMD_TEST_PORT_2 = 0xA9, 55 | KEYBOARD_CMD_DISABLE_PORT_1 = 0xAD, 56 | KEYBOARD_CMD_DISABLE_PORT_2 = 0xA7, 57 | KEYBOARD_CMD_ENABLE_PORT_1 = 0xAE, 58 | KEYBOARD_CMD_ENABLE_PORT_2 = 0xA8, 59 | } keyboard_cmd_t; 60 | 61 | #define KEYBOARD_RES_SELF_TEST 0x55 62 | 63 | union keyboard_controller_config { 64 | struct { 65 | /* clang-format off */ 66 | uint8_t port1_int : 1, 67 | port2_int : 1, 68 | sys_flag : 1, 69 | zero0 : 1, 70 | clock1 : 1, 71 | clock2 : 1, 72 | translation : 1, 73 | zero1 : 1; 74 | /* clang-format on */ 75 | } __packed; 76 | uint8_t config; 77 | }; 78 | typedef union keyboard_controller_config keyboard_controller_config_t; 79 | 80 | #define KEY_BUF 80 /* size of internal input buffer */ 81 | 82 | #define SCAN_RELEASE_MASK 0x80 /* bit set on key release */ 83 | 84 | #define KEY_NULL 0x00 /* no key */ 85 | 86 | enum scan_code { 87 | SCAN_NULL = 0x00, 88 | SCAN_ESC = 0x01, 89 | SCAN_ENTER = 0x1c, 90 | SCAN_CTRL = 0x1d, 91 | SCAN_LSHIFT = 0x2a, 92 | SCAN_RSHIFT = 0x36, 93 | SCAN_ALT = 0x38, 94 | SCAN_CAPS = 0x3a, 95 | SCAN_F1 = 0x3b, 96 | SCAN_F2 = 0x3c, 97 | SCAN_F3 = 0x3d, 98 | SCAN_F4 = 0x3e, 99 | SCAN_F5 = 0x3f, 100 | SCAN_F6 = 0x40, 101 | SCAN_F7 = 0x41, 102 | SCAN_F8 = 0x42, 103 | SCAN_F9 = 0x43, 104 | SCAN_F10 = 0x44, 105 | SCAN_F11 = 0x55, 106 | SCAN_F12 = 0x58, 107 | SCAN_NUMLOCK = 0x45, 108 | SCAN_SCROLLLOCK = 0x46, 109 | SCAN_HOME = 0x47, 110 | SCAN_UP = 0x48, 111 | SCAN_PAGEUP = 0x49, 112 | SCAN_LEFT = 0x4b, 113 | SCAN_KEYPAD5 = 0x4c, 114 | SCAN_RIGHT = 0x4d, 115 | SCAN_END = 0x4f, 116 | SCAN_DOWN = 0x50, 117 | SCAN_PAGEDOWN = 0x51, 118 | SCAN_INS = 0x52, 119 | SCAN_DEL = 0x53 120 | }; 121 | typedef enum scan_code scan_code_t; 122 | 123 | /* External Declarations */ 124 | 125 | extern void keyboard_reboot(void); 126 | extern void init_keyboard(const cpu_t *cpu); 127 | extern void keyboard_interrupt_handler(void); 128 | extern unsigned int keyboard_process_keys(void); 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /include/smp/mptables.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * All Rights Reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | #ifndef KTF_MPTABLES_H 26 | #define KTF_MPTABLES_H 27 | 28 | #include 29 | #include 30 | 31 | #define MPF_SIGNATURE (('_' << 24) | ('P' << 16) | ('M' << 8) | ('_')) 32 | 33 | struct mpf { 34 | uint32_t signature; 35 | uint32_t mpc_base; 36 | uint8_t length; 37 | uint8_t spec_rev; 38 | uint8_t checksum; 39 | uint8_t mpc_type; 40 | uint8_t rsvd0 : 6, imcrp : 1; 41 | uint8_t rsvd1[3]; 42 | } __packed; 43 | typedef struct mpf mpf_t; 44 | 45 | #define MPC_SIGNATURE (('P' << 24) | ('M' << 16) | ('C' << 8) | ('P')) 46 | 47 | enum mpc_entry_type { 48 | MPC_PROCESSOR_ENTRY, 49 | MPC_BUS_ENTRY, 50 | MPC_IOAPIC_ENTRY, 51 | MPC_IO_INT_ENTRY, 52 | MPC_LOCAL_INT_ENTRY, 53 | }; 54 | typedef enum mpc_entry_type mpc_entry_type_t; 55 | 56 | struct mpc_hdr { 57 | uint32_t signature; 58 | uint16_t length; 59 | uint8_t spec_rev; 60 | uint8_t checksum; 61 | const char oem_id[8]; 62 | const char product_id[12]; 63 | uint32_t oem_tlb_ptr; 64 | uint16_t oem_tlb_size; 65 | uint16_t entry_count; 66 | uint32_t lapic_base; 67 | uint16_t ext_length; 68 | uint8_t ext_checksum; 69 | uint8_t rsvd; 70 | } __packed; 71 | typedef struct mpc_hdr mpc_hdr_t; 72 | 73 | struct mpc_processor_entry { 74 | uint8_t type; 75 | uint8_t lapic_id; 76 | uint8_t lapic_version; 77 | struct { 78 | uint8_t en : 1, bsp : 1, rsvd0 : 6; 79 | }; 80 | struct { 81 | uint32_t stepping : 4, model : 4, family : 4, rsvd1 : 20; 82 | }; 83 | uint32_t feature_flags; 84 | uint32_t rsvd2; 85 | uint32_t rsvd3; 86 | } __packed; 87 | typedef struct mpc_processor_entry mpc_processor_entry_t; 88 | 89 | struct mpc_bus_entry { 90 | uint8_t type; 91 | uint8_t id; 92 | uint8_t type_str[6]; 93 | } __packed; 94 | typedef struct mpc_bus_entry mpc_bus_entry_t; 95 | 96 | struct mpc_ioapic_entry { 97 | uint8_t type; 98 | uint8_t id; 99 | uint8_t version; 100 | struct { 101 | uint8_t en : 1, rsvd : 7; 102 | }; 103 | uint32_t base_addr; 104 | } __packed; 105 | typedef struct mpc_ioapic_entry mpc_ioapic_entry_t; 106 | 107 | #define MPC_IOINT_INT 0 108 | #define MPC_IOINT_NMI 1 109 | #define MPC_IOINT_SMI 2 110 | #define MPC_IOINT_EXTINT 3 111 | 112 | #define MPC_IOINT_POLARITY_BS 0x00 113 | #define MPC_IOINT_POLARITY_AH 0x01 114 | #define MPC_IOINT_POLARITY_RSVD 0x10 115 | #define MPC_IOINT_POLARITY_AL 0x11 116 | 117 | #define MPC_IOINT_TRIGGER_BS 0x00 118 | #define MPC_IOINT_TRIGGER_ET 0x01 119 | #define MPC_IOINT_TRIGGER_RSVD 0x10 120 | #define MPC_IOINT_TRIGGER_LT 0x11 121 | 122 | struct mpc_ioint_entry { 123 | uint8_t type; 124 | uint8_t int_type; 125 | uint16_t po : 2, el : 2, rsvd : 12; 126 | uint8_t src_bus_id; 127 | uint8_t src_bus_irq; 128 | uint8_t dst_ioapic_id; 129 | uint8_t dst_ioapic_intin; 130 | } __packed; 131 | typedef struct mpc_ioint_entry mpc_ioint_entry_t; 132 | 133 | struct mpc_lint_entry { 134 | uint8_t type; 135 | uint8_t int_type; 136 | uint16_t po : 2, el : 2, rsvd : 12; 137 | uint8_t src_bus_id; 138 | uint8_t src_bus_irq; 139 | uint8_t dst_lapic_id; 140 | uint8_t dst_lapic_lintin; 141 | } __packed; 142 | typedef struct mpc_lint_entry mpc_lint_entry_t; 143 | 144 | /* External declarations */ 145 | 146 | extern unsigned mptables_get_nr_cpus(void); 147 | extern int init_mptables(void); 148 | 149 | #endif /* KTF_MPTABLES_H */ 150 | -------------------------------------------------------------------------------- /common/console.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2020 Amazon.com, Inc. or its affiliates. 3 | * Copyright © 2014,2015 Citrix Systems Ltd. 4 | * All Rights Reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #define VPRINTK_BUF_SIZE 1024 40 | 41 | static console_callback_entry_t console_callbacks[2]; 42 | static unsigned int num_console_callbacks; 43 | 44 | void vprintk(const char *fmt, va_list args) { 45 | static char buf[VPRINTK_BUF_SIZE]; 46 | static spinlock_t lock = SPINLOCK_INIT; 47 | unsigned int i; 48 | int rc; 49 | 50 | spin_lock(&lock); 51 | 52 | rc = vsnprintf(buf, sizeof(buf), fmt, args); 53 | 54 | if (rc > (int) sizeof(buf)) 55 | panic("vprintk() buffer overflow"); 56 | 57 | for (i = 0; i < num_console_callbacks; i++) { 58 | void *arg = console_callbacks[i].arg; 59 | 60 | console_callbacks[i].cb(arg, buf, rc); 61 | } 62 | 63 | spin_unlock(&lock); 64 | } 65 | 66 | void printk(const char *fmt, ...) { 67 | va_list args; 68 | 69 | va_start(args, fmt); 70 | vprintk(fmt, args); 71 | va_end(args); 72 | } 73 | 74 | #ifdef KTF_ACPICA 75 | void AcpiOsVprintf(const char *Format, va_list args) { 76 | vprintk(Format, args); 77 | } 78 | 79 | void AcpiOsPrintf(const char *Format, ...) { 80 | va_list args; 81 | 82 | va_start(args, Format); 83 | vprintk(Format, args); 84 | va_end(args); 85 | } 86 | #endif 87 | 88 | void serial_console_write(void *arg, const char *buf, size_t len) { 89 | io_port_t port = (io_port_t) _ul(arg); 90 | 91 | serial_write(port, buf, len); 92 | } 93 | 94 | void qemu_console_write(void *arg, const char *buf, size_t len) { 95 | io_port_t port = (io_port_t) _ul(arg); 96 | 97 | puts(port, buf, len); 98 | } 99 | 100 | void vga_console_write(void *vga_memory, const char *buf, size_t len) { 101 | vga_write(vga_memory, buf, len, VGA_WHITE); 102 | } 103 | 104 | void fb_console_write(void *fb_memory, const char *buf, size_t len) { 105 | fb_write(fb_memory, buf, len, FB_WHITE); 106 | } 107 | 108 | static inline bool is_console_callback_registered(console_callback_t cb, void *arg) { 109 | for (unsigned int i = 0; i < num_console_callbacks; i++) { 110 | if (console_callbacks[i].cb == cb && console_callbacks[i].arg == arg) 111 | return true; 112 | } 113 | 114 | return false; 115 | } 116 | 117 | void register_console_callback(console_callback_t cb, void *arg) { 118 | if (is_console_callback_registered(cb, arg)) 119 | return; 120 | 121 | console_callbacks[num_console_callbacks].cb = cb; 122 | console_callbacks[num_console_callbacks++].arg = arg; 123 | } 124 | 125 | static void oops_print(const char *fmt, va_list args, const char *type) { 126 | static const char stars[] = "***********************************"; 127 | size_t slen = sizeof(stars) - 1; 128 | size_t tlen = strlen(type); 129 | int s1_len, s2_len; 130 | 131 | if (tlen > slen - 4) 132 | tlen = slen - 4; 133 | 134 | s1_len = (slen - tlen - 2) / 2; 135 | s2_len = slen - tlen - 2 - s1_len; 136 | 137 | printk("%.*s %s %.*s\n", s1_len, stars, type, s2_len, stars); 138 | printk("CPU[%u]: ", smp_processor_id()); 139 | vprintk(fmt, args); 140 | printk("\n%s\n", stars); 141 | } 142 | 143 | void panic(const char *fmt, ...) { 144 | va_list args; 145 | 146 | va_start(args, fmt); 147 | oops_print(fmt, args, "PANIC"); 148 | va_end(args); 149 | 150 | while (1) 151 | halt(); 152 | } 153 | 154 | void warning(const char *fmt, ...) { 155 | va_list args; 156 | 157 | va_start(args, fmt); 158 | oops_print(fmt, args, "WARNING"); 159 | va_end(args); 160 | } 161 | --------------------------------------------------------------------------------