diff --git a/libraries/Makefile b/libraries/Makefile
index 05253b1de..9ed0e10e4 100644
--- a/libraries/Makefile
+++ b/libraries/Makefile
@@ -1,4 +1,4 @@
-ATMOSPHERE_LIBRARIES := libstratosphere
+ATMOSPHERE_LIBRARIES := libmesosphere libstratosphere
TOPTARGETS := all clean
diff --git a/libraries/config/common.mk b/libraries/config/common.mk
index 91505afdc..4247dcf6c 100644
--- a/libraries/config/common.mk
+++ b/libraries/config/common.mk
@@ -63,12 +63,12 @@ export SOURCES ?= $(shell find source -type d \
-not \( -path source/arch -prune \) \
-not \( -path source/board -prune \) \)
-ifneq ($(strip $(wildcard source/$(ATMOSPHERE_ARCH_DIR)./.*)),)
+ifneq ($(strip $(wildcard source/$(ATMOSPHERE_ARCH_DIR)/.*)),)
SOURCES += $(shell find source/$(ATMOSPHERE_ARCH_DIR) -type d)
endif
-ifneq ($(strip $(wildcard source/$(ATMOSPHERE_BOARD_DIR)./.*)),)
+ifneq ($(strip $(wildcard source/$(ATMOSPHERE_BOARD_DIR)/.*)),)
SOURCES += $(shell find source/$(ATMOSPHERE_BOARD_DIR) -type d)
endif
-ifneq ($(strip $(wildcard source/$(ATMOSPHERE_OS_DIR)./.*)),)
+ifneq ($(strip $(wildcard source/$(ATMOSPHERE_OS_DIR)/.*)),)
SOURCES += $(shell find source/$(ATMOSPHERE_OS_DIR) -type d)
endif
diff --git a/libraries/libmesosphere/Makefile b/libraries/libmesosphere/Makefile
new file mode 100644
index 000000000..2562c0037
--- /dev/null
+++ b/libraries/libmesosphere/Makefile
@@ -0,0 +1,129 @@
+#---------------------------------------------------------------------------------
+# pull in common atmosphere configuration
+#---------------------------------------------------------------------------------
+include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../config/common.mk
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+DEFINES := $(ATMOSPHERE_DEFINES) -DATMOSPHERE_IS_MESOSPHERE
+SETTINGS := $(ATMOSPHERE_SETTINGS) -O2 -mgeneral-regs-only -Werror
+CFLAGS := $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE)
+CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -flto
+ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS)
+
+LIBS :=
+
+#---------------------------------------------------------------------------------
+# list of directories containing libraries, this must be the top level containing
+# include and lib
+#---------------------------------------------------------------------------------
+LIBDIRS := $(ATMOSPHERE_LIBRARIES_DIR)/libvapours
+
+#---------------------------------------------------------------------------------
+# no real need to edit anything past this point unless you need to add additional
+# rules for different file extensions
+#---------------------------------------------------------------------------------
+ifneq ($(BUILD),$(notdir $(CURDIR)))
+#---------------------------------------------------------------------------------
+
+export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
+ $(foreach dir,$(DATA),$(CURDIR)/$(dir))
+
+CFILES := $(foreach dir,$(SOURCES),$(filter-out $(notdir $(wildcard $(dir)/*.arch.*.c)) $(notdir $(wildcard $(dir)/*.board.*.c)) $(notdir $(wildcard $(dir)/*.os.*.c)), \
+ $(notdir $(wildcard $(dir)/*.c))))
+CFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.arch.$(ATMOSPHERE_ARCH_NAME).c)))
+CFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.board.$(ATMOSPHERE_BOARD_NAME).c)))
+CFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.os.$(ATMOSPHERE_OS_NAME).c)))
+
+CPPFILES := $(foreach dir,$(SOURCES),$(filter-out $(notdir $(wildcard $(dir)/*.arch.*.cpp)) $(notdir $(wildcard $(dir)/*.board.*.cpp)) $(notdir $(wildcard $(dir)/*.os.*.cpp)), \
+ $(notdir $(wildcard $(dir)/*.cpp))))
+CPPFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.arch.$(ATMOSPHERE_ARCH_NAME).cpp)))
+CPPFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.board.$(ATMOSPHERE_BOARD_NAME).cpp)))
+CPPFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.os.$(ATMOSPHERE_OS_NAME).cpp)))
+
+SFILES := $(foreach dir,$(SOURCES),$(filter-out $(notdir $(wildcard $(dir)/*.arch.*.s)) $(notdir $(wildcard $(dir)/*.board.*.s)) $(notdir $(wildcard $(dir)/*.os.*.s)), \
+ $(notdir $(wildcard $(dir)/*.s))))
+SFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.arch.$(ATMOSPHERE_ARCH_NAME).s)))
+SFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.board.$(ATMOSPHERE_BOARD_NAME).s)))
+SFILES += $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.os.$(ATMOSPHERE_OS_NAME).s)))
+
+#---------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+#---------------------------------------------------------------------------------
+ export LD := $(CC)
+#---------------------------------------------------------------------------------
+else
+#---------------------------------------------------------------------------------
+ export LD := $(CXX)
+#---------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------
+
+export OFILES_BIN := $(addsuffix .o,$(BINFILES))
+export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
+export OFILES := $(OFILES_BIN) $(OFILES_SRC)
+export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
+
+export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I.
+
+.PHONY: clean all
+
+#---------------------------------------------------------------------------------
+all: lib/$(TARGET).a
+
+lib:
+ @[ -d $@ ] || mkdir -p $@
+
+release:
+ @[ -d $@ ] || mkdir -p $@
+
+lib/$(TARGET).a : lib release $(SOURCES) $(INCLUDES)
+ @$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \
+ BUILD_CFLAGS="-DNDEBUG=1 -O2" \
+ DEPSDIR=$(CURDIR)/release \
+ --no-print-directory -C release \
+ -f $(CURDIR)/Makefile
+
+dist-bin: all
+ @tar --exclude=*~ -cjf $(TARGET).tar.bz2 include lib
+
+dist-src:
+ @tar --exclude=*~ -cjf $(TARGET)-src.tar.bz2 include source Makefile
+
+dist: dist-src dist-bin
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr release lib *.bz2
+
+#---------------------------------------------------------------------------------
+else
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+$(OUTPUT) : $(OFILES)
+
+$(OFILES_SRC) : $(HFILES)
+
+#---------------------------------------------------------------------------------
+%_bin.h %.bin.o : %.bin
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ @$(bin2o)
+
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------------
+
diff --git a/libraries/libmesosphere/README.md b/libraries/libmesosphere/README.md
new file mode 100644
index 000000000..88da81b25
--- /dev/null
+++ b/libraries/libmesosphere/README.md
@@ -0,0 +1,28 @@
+![License](https://img.shields.io/badge/License-GPLv2-blue.svg)
+
+libmesosphere is a work-in-progress C++ library implementing functionality for the Horizon Kernel.
+
+Licensing
+=====
+
+This software is licensed under the terms of the GPLv2, with exemptions for specific projects noted below.
+
+You can find a copy of the license in the [LICENSE file](LICENSE).
+
+Exemptions:
+* The [yuzu emulator project](https://github.com/yuzu-emu/yuzu) is exempt from GPLv2 licensing and may (at its option) instead license any source code authored for the libmesosphere project as GPLv2 or later.
+
+Credits
+=====
+
+libmesosphere is currently being developed and maintained by __SciresM__.
+
+In addition to those credited in [Atmosphère's credits](https://github.com/Atmosphere-NX/Atmosphere/blob/master/README.md#Credits), we would like to thank for contributing to libmesosphere in some significant way:
+
+* @[devkitPro](https://github.com/devkitPro)
+* @[yellows8](https://github.com/yellows8)
+* @[qlutoo](https://github.com/plutooo)
+* @[hedgeberg](https://github.com/hedgeberg)
+* @[Nintendo](https://github.com/Nintendo)
+* @[NVidia](https://github.com/NVidia)
+* @[Kaphotics](https://github.com/kwsch)
diff --git a/libraries/libmesosphere/include/mesosphere.hpp b/libraries/libmesosphere/include/mesosphere.hpp
new file mode 100644
index 000000000..77e798d98
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere.hpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+
+/* All kernel code should have access to libvapours. */
+#include
+
+/* First, pull in core macros (panic, etc). */
+#include "mesosphere/kern_panic.hpp"
+
+/* Primitive types. */
+#include "mesosphere/kern_k_typed_address.hpp"
+
+/* Core functionality. */
+#include "mesosphere/kern_select_k_system_control.hpp"
diff --git a/libraries/libmesosphere/include/mesosphere/board/nintendo/switch/kern_k_system_control.hpp b/libraries/libmesosphere/include/mesosphere/board/nintendo/switch/kern_k_system_control.hpp
new file mode 100644
index 000000000..d28a1230b
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere/board/nintendo/switch/kern_k_system_control.hpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+#include
+
+namespace ams::kern {
+
+ class KSystemControl {
+ public:
+
+ /* Panic. */
+ static NORETURN void StopSystem();
+ };
+
+}
\ No newline at end of file
diff --git a/libraries/libmesosphere/include/mesosphere/kern_common.hpp b/libraries/libmesosphere/include/mesosphere/kern_common.hpp
new file mode 100644
index 000000000..e69de29bb
diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_typed_address.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_typed_address.hpp
new file mode 100644
index 000000000..1fc030423
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere/kern_k_typed_address.hpp
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+#include
+
+namespace ams::kern {
+
+#ifndef MESOSPHERE_DISABLE_TYPED_ADDRESSES
+
+ template
+ class KTypedAddress {
+ private:
+ uintptr_t address;
+ public:
+ /* Constructors. */
+ constexpr ALWAYS_INLINE KTypedAddress() : address(0) { /* ... */ }
+ constexpr ALWAYS_INLINE KTypedAddress(uintptr_t a) : address(a) { /* ... */ }
+ template
+ constexpr ALWAYS_INLINE explicit KTypedAddress(U *ptr) : address(reinterpret_cast(ptr)) { /* ... */ }
+
+ /* Assignment operator. */
+ constexpr ALWAYS_INLINE KTypedAddress operator=(KTypedAddress rhs) {
+ this->address = rhs.address;
+ return *this;
+ }
+
+ /* Arithmetic operators. */
+ template
+ constexpr ALWAYS_INLINE KTypedAddress operator+(I rhs) const {
+ static_assert(std::is_integral::value);
+ return this->address + rhs;
+ }
+
+ template
+ constexpr ALWAYS_INLINE KTypedAddress operator-(I rhs) const {
+ static_assert(std::is_integral::value);
+ return this->address - rhs;
+ }
+
+ template
+ constexpr ALWAYS_INLINE KTypedAddress operator+=(I rhs) {
+ static_assert(std::is_integral::value);
+ this->address += rhs;
+ return *this;
+ }
+
+ template
+ constexpr ALWAYS_INLINE KTypedAddress operator-=(I rhs) {
+ static_assert(std::is_integral::value);
+ this->address -= rhs;
+ return *this;
+ }
+
+ /* Logical operators. */
+ constexpr ALWAYS_INLINE uintptr_t operator&(uintptr_t mask) const {
+ return this->address & mask;
+ }
+
+ constexpr ALWAYS_INLINE uintptr_t operator|(uintptr_t mask) const {
+ return this->address | mask;
+ }
+
+ constexpr ALWAYS_INLINE uintptr_t operator<<(int shift) const {
+ return this->address << shift;
+ }
+
+ constexpr ALWAYS_INLINE uintptr_t operator>>(int shift) const {
+ return this->address >> shift;
+ }
+
+ /* Comparison operators. */
+ constexpr ALWAYS_INLINE bool operator==(KTypedAddress rhs) const {
+ return this->address == rhs.address;
+ }
+
+ constexpr ALWAYS_INLINE bool operator!=(KTypedAddress rhs) const {
+ return this->address != rhs.address;
+ }
+
+ constexpr ALWAYS_INLINE bool operator<(KTypedAddress rhs) const {
+ return this->address < rhs.address;
+ }
+
+ constexpr ALWAYS_INLINE bool operator<=(KTypedAddress rhs) const {
+ return this->address <= rhs.address;
+ }
+
+ constexpr ALWAYS_INLINE bool operator>(KTypedAddress rhs) const {
+ return this->address > rhs.address;
+ }
+
+ constexpr ALWAYS_INLINE bool operator>=(KTypedAddress rhs) const {
+ return this->address >= rhs.address;
+ }
+
+ /* For convenience, also define comparison operators versus uintptr_t. */
+ constexpr ALWAYS_INLINE bool operator==(uintptr_t rhs) const {
+ return this->address == rhs;
+ }
+
+ constexpr ALWAYS_INLINE bool operator!=(uintptr_t rhs) const {
+ return this->address != rhs;
+ }
+
+ /* TODO: <, <=, >, >= against uintptr_t? would need to be declared outside of class. Maybe worth it. */
+
+ /* Allow getting the address explicitly, for use in accessors. */
+ constexpr ALWAYS_INLINE uintptr_t GetValue() const {
+ return this->address;
+ }
+
+ };
+
+ struct KPhysicalAddressTag{};
+ struct KVirtualAddressTag{};
+ struct KProcessAddressTag{};
+
+ using KPhysicalAddress = KTypedAddress;
+ using KVirtualAddress = KTypedAddress;
+ using KProcessAddress = KTypedAddress;
+
+ /* Define accessors. */
+ template
+ constexpr ALWAYS_INLINE uintptr_t GetInteger(KTypedAddress address) {
+ return address.GetValue();
+ }
+
+ template
+ constexpr ALWAYS_INLINE T *GetPointer(KTypedAddress address) {
+ return CONST_FOLD(reinterpret_cast(address.GetValue()));
+ }
+
+ template
+ constexpr ALWAYS_INLINE void *GetVoidPointer(KTypedAddress address) {
+ return CONST_FOLD(reinterpret_cast(address.GetValue()));
+ }
+
+#else
+
+ /* Plausibly, we may not want compiler overhead from using strongly typed addresses. */
+ /* In this case, we should just use uintptr_t. */
+ using KPhysicalAddress = uintptr_t;
+ using KVirtualAddress = uintptr_t;
+ using KProcessAddress = uintptr_t;
+
+ /* Define accessors. */
+ constexpr ALWAYS_INLINE uintptr_t GetInteger(uintptr_t address) {
+ return address;
+ }
+
+ template
+ constexpr ALWAYS_INLINE T *GetPointer(uintptr_t address) {
+ return CONST_FOLD(reinterpret_cast(address));
+ }
+
+ template
+ constexpr ALWAYS_INLINE void *GetVoidPointer(uintptr_t address) {
+ return CONST_FOLD(reinterpret_cast(address));
+ }
+
+#endif
+
+ template
+ constexpr inline T Null = [] {
+ if constexpr (std::is_same::value) {
+ return 0;
+ } else {
+ static_assert(std::is_same::value ||
+ std::is_same::value ||
+ std::is_same::value);
+ return T(0);
+ }
+ }();
+
+ /* Basic type validations. */
+ static_assert(sizeof(KPhysicalAddress) == sizeof(uintptr_t));
+ static_assert(sizeof(KVirtualAddress) == sizeof(uintptr_t));
+ static_assert(sizeof(KProcessAddress) == sizeof(uintptr_t));
+
+ static_assert(std::is_trivially_destructible::value);
+ static_assert(std::is_trivially_destructible::value);
+ static_assert(std::is_trivially_destructible::value);
+
+ static_assert(Null == 0);
+ static_assert(Null == Null);
+ static_assert(Null == Null);
+ static_assert(Null == Null);
+
+ /* Arithmetic validations. */
+ static_assert(KPhysicalAddress(10) + 5 == KPhysicalAddress(15));
+ static_assert(KPhysicalAddress(10) - 5 == KPhysicalAddress(5));
+ static_assert([]{ KPhysicalAddress v(10); v += 5; return v; }() == KPhysicalAddress(15));
+ static_assert([]{ KPhysicalAddress v(10); v -= 5; return v; }() == KPhysicalAddress(5));
+
+ /* Logical validations. */
+ static_assert((KPhysicalAddress(0b11111111) >> 1) == 0b01111111);
+ static_assert((KPhysicalAddress(0b10101010) >> 1) == 0b01010101);
+ static_assert((KPhysicalAddress(0b11111111) << 1) == 0b111111110);
+ static_assert((KPhysicalAddress(0b01010101) << 1) == 0b10101010);
+ static_assert((KPhysicalAddress(0b11111111) & 0b01010101) == 0b01010101);
+ static_assert((KPhysicalAddress(0b11111111) & 0b10101010) == 0b10101010);
+ static_assert((KPhysicalAddress(0b01010101) & 0b10101010) == 0b00000000);
+ static_assert((KPhysicalAddress(0b00000000) | 0b01010101) == 0b01010101);
+ static_assert((KPhysicalAddress(0b11111111) | 0b01010101) == 0b11111111);
+ static_assert((KPhysicalAddress(0b10101010) | 0b01010101) == 0b11111111);
+
+ /* Comparisons. */
+ static_assert(KPhysicalAddress(0) == KPhysicalAddress(0));
+ static_assert(KPhysicalAddress(0) != KPhysicalAddress(1));
+ static_assert(KPhysicalAddress(0) < KPhysicalAddress(1));
+ static_assert(KPhysicalAddress(0) <= KPhysicalAddress(1));
+ static_assert(KPhysicalAddress(1) > KPhysicalAddress(0));
+ static_assert(KPhysicalAddress(1) >= KPhysicalAddress(0));
+
+ static_assert(!(KPhysicalAddress(0) == KPhysicalAddress(1)));
+ static_assert(!(KPhysicalAddress(0) != KPhysicalAddress(0)));
+ static_assert(!(KPhysicalAddress(1) < KPhysicalAddress(0)));
+ static_assert(!(KPhysicalAddress(1) <= KPhysicalAddress(0)));
+ static_assert(!(KPhysicalAddress(0) > KPhysicalAddress(1)));
+ static_assert(!(KPhysicalAddress(0) >= KPhysicalAddress(1)));
+
+ /* Accessors. */
+ static_assert(15 == GetInteger(KPhysicalAddress(15)));
+ static_assert(0 == GetInteger(Null));
+ /* TODO: reinterpret_cast<> not valid in a constant expression, can't test get pointers. */
+
+}
diff --git a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp
new file mode 100644
index 000000000..49a6e7b34
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+#include
+
+namespace ams::kern {
+
+ NORETURN void Panic(const char *file, int line, const char *format, ...);
+ NORETURN void Panic();
+
+}
+
+#ifdef MESOSPHERE_ENABLE_DEBUG_PRINT
+#define MESOSPHERE_PANIC(...) ams::kern::Panic(__FILE__, __LINE__, __VA_ARGS__)
+#else
+#define MESOSPHERE_PANIC(...) ams::kern::Panic()
+#endif
+
+#ifdef MESOSPHERE_ENABLE_ASSERTIONS
+#define MESOSPHERE_ASSERT_IMPL(expr, ...) \
+ ({ \
+ if (AMS_UNLIKELY(!expr)) { \
+ MESOSPHERE_PANIC(__VA_ARGS__); \
+ } \
+ })
+#else
+#define MESOSPHERE_ASSERT_IMPL(expr, ...) do { } while (0)
+#endif
+
+#define MESOSPHERE_ASSERT(expr) MESOSPHERE_ASSERT_IMPL(expr, "Assertion failed: %s", #expr)
+#define MESOSPHERE_R_ASSERT(expr) MESOSPHERE_ASSERT_IMPL(R_SUCCEEDED(expr), "Result assertion failed: %s", #expr)
+
+#define MESOSPHERE_ABORT() MESOSPHERE_PANIC("Abort()");
+
+#define MESOSPHERE_ABORT_UNLESS(expr) \
+ ({ \
+ if (AMS_UNLIKELY(!expr)) { \
+ MESOSPHERE_PANIC("Abort(): %s", #expr); \
+ } \
+ })
diff --git a/libraries/libmesosphere/include/mesosphere/kern_select_k_system_control.hpp b/libraries/libmesosphere/include/mesosphere/kern_select_k_system_control.hpp
new file mode 100644
index 000000000..56ddc9f6c
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere/kern_select_k_system_control.hpp
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+
+#ifdef ATMOSPHERE_BOARD_NINTENDO_SWITCH
+ #include "board/nintendo/switch/kern_k_system_control.hpp"
+#else
+ #error "Unknown board for KSystemControl"
+#endif
\ No newline at end of file
diff --git a/libraries/libmesosphere/source/board/nintendo/switch/kern_k_system_control.cpp b/libraries/libmesosphere/source/board/nintendo/switch/kern_k_system_control.cpp
new file mode 100644
index 000000000..0bc829002
--- /dev/null
+++ b/libraries/libmesosphere/source/board/nintendo/switch/kern_k_system_control.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#include
+#include "kern_secure_monitor.hpp"
+
+namespace ams::kern {
+
+ void KSystemControl::StopSystem() {
+ /* TODO: smc::Panic(0xF00); */
+ while (true) { /* ... */ }
+ }
+
+}
\ No newline at end of file
diff --git a/libraries/libmesosphere/source/board/nintendo/switch/kern_secure_monitor.hpp b/libraries/libmesosphere/source/board/nintendo/switch/kern_secure_monitor.hpp
new file mode 100644
index 000000000..4c5e144f3
--- /dev/null
+++ b/libraries/libmesosphere/source/board/nintendo/switch/kern_secure_monitor.hpp
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+#include
+
+namespace ams::kern::smc {
+
+ /* TODO: Secure Monitor API. */
+
+}
\ No newline at end of file
diff --git a/libraries/libmesosphere/source/kern_panic.cpp b/libraries/libmesosphere/source/kern_panic.cpp
new file mode 100644
index 000000000..0ae7c63f1
--- /dev/null
+++ b/libraries/libmesosphere/source/kern_panic.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#include
+
+namespace ams::result::impl {
+
+ NORETURN void OnResultAssertion(Result result) {
+ MESOSPHERE_PANIC("OnResultAssertion(2%03d-%04d)", result.GetModule(), result.GetDescription());
+ }
+
+}
+
+namespace ams::kern {
+
+ namespace {
+
+ NORETURN void StopSystem() {
+ KSystemControl::StopSystem();
+ }
+
+ }
+
+ NORETURN WEAK_SYMBOL void Panic(const char *file, int line, const char *format, ...) {
+ /* TODO: Implement printing, log this information. */
+ StopSystem();
+ }
+
+ NORETURN WEAK_SYMBOL void Panic() {
+ StopSystem();
+ }
+
+}
diff --git a/libraries/libstratosphere/source/ams/ams_environment.cpp b/libraries/libstratosphere/source/ams/ams_environment.cpp
index 16d1cf30c..af1c412cd 100644
--- a/libraries/libstratosphere/source/ams/ams_environment.cpp
+++ b/libraries/libstratosphere/source/ams/ams_environment.cpp
@@ -36,7 +36,7 @@ namespace ams {
extern ncm::ProgramId CurrentProgramId;
- void WEAK ExceptionHandler(FatalErrorContext *ctx) {
+ void WEAK_SYMBOL ExceptionHandler(FatalErrorContext *ctx) {
R_ASSERT(amsBpcInitialize());
R_ASSERT(amsBpcRebootToFatalError(ctx));
while (1) { /* ... */ }
diff --git a/libraries/libstratosphere/source/pm/pm_boot_mode_api.cpp b/libraries/libstratosphere/source/pm/pm_boot_mode_api.cpp
index 48a7b2e86..ebb929745 100644
--- a/libraries/libstratosphere/source/pm/pm_boot_mode_api.cpp
+++ b/libraries/libstratosphere/source/pm/pm_boot_mode_api.cpp
@@ -19,13 +19,13 @@ namespace ams::pm::bm {
/* Boot Mode API. */
/* Both functions should be weakly linked, so that they can be overridden by ams::boot2 as needed. */
- BootMode WEAK GetBootMode() {
+ BootMode WEAK_SYMBOL GetBootMode() {
PmBootMode boot_mode = PmBootMode_Normal;
R_ASSERT(pmbmGetBootMode(&boot_mode));
return static_cast(boot_mode);
}
- void WEAK SetMaintenanceBoot() {
+ void WEAK_SYMBOL SetMaintenanceBoot() {
R_ASSERT(pmbmSetMaintenanceBoot());
}
diff --git a/libraries/libstratosphere/source/pm/pm_info_api.cpp b/libraries/libstratosphere/source/pm/pm_info_api.cpp
index e0028004e..6c5e300c6 100644
--- a/libraries/libstratosphere/source/pm/pm_info_api.cpp
+++ b/libraries/libstratosphere/source/pm/pm_info_api.cpp
@@ -49,7 +49,7 @@ namespace ams::pm::info {
return pminfoAtmosphereGetProcessInfo(reinterpret_cast(out_loc), reinterpret_cast(out_status), static_cast(process_id));
}
- Result WEAK HasLaunchedProgram(bool *out, ncm::ProgramId program_id) {
+ Result WEAK_SYMBOL HasLaunchedProgram(bool *out, ncm::ProgramId program_id) {
std::scoped_lock lk(g_info_lock);
if (g_cached_launched_programs.find(static_cast(program_id)) != g_cached_launched_programs.end()) {
diff --git a/libraries/libstratosphere/source/pm/pm_shell_api.cpp b/libraries/libstratosphere/source/pm/pm_shell_api.cpp
index 628f3df33..3b259174b 100644
--- a/libraries/libstratosphere/source/pm/pm_shell_api.cpp
+++ b/libraries/libstratosphere/source/pm/pm_shell_api.cpp
@@ -18,7 +18,7 @@
namespace ams::pm::shell {
/* Shell API. */
- Result WEAK LaunchProgram(os::ProcessId *out_process_id, const ncm::ProgramLocation &loc, u32 launch_flags) {
+ Result WEAK_SYMBOL LaunchProgram(os::ProcessId *out_process_id, const ncm::ProgramLocation &loc, u32 launch_flags) {
static_assert(sizeof(ncm::ProgramLocation) == sizeof(NcmProgramLocation));
static_assert(alignof(ncm::ProgramLocation) == alignof(NcmProgramLocation));
return pmshellLaunchProgram(launch_flags, reinterpret_cast(&loc), reinterpret_cast(out_process_id));
diff --git a/libraries/libstratosphere/source/result/result_on_assertion.cpp b/libraries/libstratosphere/source/result/result_on_assertion.cpp
index 5b3da6a6f..799f21954 100644
--- a/libraries/libstratosphere/source/result/result_on_assertion.cpp
+++ b/libraries/libstratosphere/source/result/result_on_assertion.cpp
@@ -23,7 +23,7 @@ namespace ams::result {
namespace ams::result::impl {
- NORETURN WEAK void OnResultAssertion(Result result) {
+ NORETURN WEAK_SYMBOL void OnResultAssertion(Result result) {
/* Assert that we should call fatal on result assertion. */
/* If we shouldn't fatal, this will std::abort(); */
/* If we should, we'll continue onwards. */
diff --git a/libraries/libstratosphere/source/settings/settings_fwdbg_api.cpp b/libraries/libstratosphere/source/settings/settings_fwdbg_api.cpp
index c63b087f1..62d08aba1 100644
--- a/libraries/libstratosphere/source/settings/settings_fwdbg_api.cpp
+++ b/libraries/libstratosphere/source/settings/settings_fwdbg_api.cpp
@@ -20,13 +20,13 @@ namespace ams::settings::fwdbg {
/* TODO: Implement when libnx wrapper is added. */
bool IsDebugModeEnabled();
- size_t WEAK GetSettingsItemValueSize(const char *name, const char *key) {
+ size_t WEAK_SYMBOL GetSettingsItemValueSize(const char *name, const char *key) {
u64 size = 0;
R_ASSERT(setsysGetSettingsItemValueSize(name, key, &size));
return size;
}
- size_t WEAK GetSettingsItemValue(void *dst, size_t dst_size, const char *name, const char *key) {
+ size_t WEAK_SYMBOL GetSettingsItemValue(void *dst, size_t dst_size, const char *name, const char *key) {
u64 size = 0;
R_ASSERT(setsysGetSettingsItemValue(name, key, dst, dst_size, &size));
return size;
diff --git a/libraries/libvapours/include/vapours/defines.hpp b/libraries/libvapours/include/vapours/defines.hpp
index 81e986431..eda392cbd 100644
--- a/libraries/libvapours/include/vapours/defines.hpp
+++ b/libraries/libvapours/include/vapours/defines.hpp
@@ -33,8 +33,10 @@
#define ALIGNED(algn) __attribute__((aligned(algn)))
#define NORETURN __attribute__((noreturn))
-#define WEAK __attribute__((weak))
+#define WEAK_SYMBOL __attribute__((weak))
+#define ALWAYS_INLINE inline __attribute__((always_inline))
+#define CONST_FOLD(x) (__builtin_constant_p(x) ? (x) : (x))
#define CONCATENATE_IMPL(S1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2)
@@ -44,3 +46,16 @@
#else
#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __LINE__)
#endif
+
+#define AMS_PREDICT(expr, value, _probability) __builtin_expect_with_probability(expr, value, ({ \
+ constexpr double probability = _probability; \
+ static_assert(0.0 <= probability); \
+ static_assert(probability <= 1.0); \
+ probability; \
+ }))
+
+#define AMS_PREDICT_TRUE(expr, probability) AMS_PREDICT(!!expr, 1, probability)
+#define AMS_PREDICT_FALSE(expr, probability) AMS_PREDICT(!!expr, 0, probability)
+
+#define AMS_LIKELY(expr) AMS_PREDICT_TRUE(expr, 1.0)
+#define AMS_UNLIKELY(expr) AMS_PREDICT_FALSE(expr, 1.0)
diff --git a/libraries/libvapours/include/vapours/includes.hpp b/libraries/libvapours/include/vapours/includes.hpp
index 811034f4f..e59a734b9 100644
--- a/libraries/libvapours/include/vapours/includes.hpp
+++ b/libraries/libvapours/include/vapours/includes.hpp
@@ -54,11 +54,20 @@
#ifdef ATMOSPHERE_BOARD_NINTENDO_SWITCH
+#ifdef ATMOSPHERE_IS_STRATOSPHERE
+
/* Libnx. */
#include
#else
+/* Non-EL0 code can't include libnx. */
+#include "types.hpp"
+
+#endif
+
+#else
+
#error "Unsupported board"
#endif /* ATMOSPHERE_BOARD_NINTENDO_SWITCH */
diff --git a/libraries/libvapours/include/vapours/types.hpp b/libraries/libvapours/include/vapours/types.hpp
new file mode 100644
index 000000000..894ec8896
--- /dev/null
+++ b/libraries/libvapours/include/vapours/types.hpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2018-2019 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#pragma once
+#include
+#include
+#include
+#include
+
+/* NOTE: This file serves as a substitute for libnx . */
+
+typedef uint8_t u8; ///< 8-bit unsigned integer.
+typedef uint16_t u16; ///< 16-bit unsigned integer.
+typedef uint32_t u32; ///< 32-bit unsigned integer.
+typedef uint64_t u64; ///< 64-bit unsigned integer.
+typedef __uint128_t u128; ///< 128-bit unsigned integer.
+
+typedef int8_t s8; ///< 8-bit signed integer.
+typedef int16_t s16; ///< 16-bit signed integer.
+typedef int32_t s32; ///< 32-bit signed integer.
+typedef int64_t s64; ///< 64-bit signed integer.
+typedef __int128_t s128; ///< 128-bit unsigned integer.
+
+typedef volatile u8 vu8; ///< 8-bit volatile unsigned integer.
+typedef volatile u16 vu16; ///< 16-bit volatile unsigned integer.
+typedef volatile u32 vu32; ///< 32-bit volatile unsigned integer.
+typedef volatile u64 vu64; ///< 64-bit volatile unsigned integer.
+typedef volatile u128 vu128; ///< 128-bit volatile unsigned integer.
+
+typedef volatile s8 vs8; ///< 8-bit volatile signed integer.
+typedef volatile s16 vs16; ///< 16-bit volatile signed integer.
+typedef volatile s32 vs32; ///< 32-bit volatile signed integer.
+typedef volatile s64 vs64; ///< 64-bit volatile signed integer.
+typedef volatile s128 vs128; ///< 128-bit volatile signed integer.
+
+typedef u32 Result; ///< Function error code result type.
+
+/// Creates a bitmask from a bit number.
+#ifndef BIT
+#define BIT(n) (1U<<(n))
+#endif
+
+/// Marks a function as not returning, for the purposes of compiler optimization.
+#ifndef NORETURN
+#define NORETURN __attribute__((noreturn))
+#endif
+
+/// This will get un-defined by
+#define R_SUCCEEDED(res) (res == 0)
+#define R_FAILED(res) (res != 0)
+
+
+/// Flags a function as (always) inline.
+#define NX_INLINE __attribute__((always_inline)) static inline
+
+/// Flags a function as constexpr in C++14 and above; or as (always) inline otherwise.
+#if __cplusplus >= 201402L
+#define NX_CONSTEXPR NX_INLINE constexpr
+#else
+#define NX_CONSTEXPR NX_INLINE
+#endif
\ No newline at end of file