summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitattributes4
-rw-r--r--.travis.yml15
-rwxr-xr-x.travis/common/post-upload.sh3
-rwxr-xr-x.travis/linux-mingw/build.sh3
-rwxr-xr-x.travis/linux-mingw/deps.sh3
-rwxr-xr-x.travis/linux-mingw/docker.sh59
-rw-r--r--.travis/linux-mingw/scan_dll.py106
-rwxr-xr-x.travis/linux-mingw/upload.sh13
-rwxr-xr-x.travis/linux/docker.sh4
-rwxr-xr-x.travis/macos/build.sh6
-rw-r--r--CMakeLists.txt27
-rw-r--r--CMakeModules/MinGWCross.cmake54
m---------externals/dynarmic0
-rw-r--r--src/audio_core/audio_renderer.cpp4
-rw-r--r--src/audio_core/audio_renderer.h1
-rw-r--r--src/audio_core/stream.cpp5
-rw-r--r--src/audio_core/stream.h15
-rw-r--r--src/audio_core/time_stretch.cpp2
-rw-r--r--src/common/common_paths.h2
-rw-r--r--src/common/file_util.cpp2
-rw-r--r--src/common/file_util.h2
-rw-r--r--src/common/logging/backend.cpp1
-rw-r--r--src/common/logging/log.h1
-rw-r--r--src/common/ring_buffer.h14
-rw-r--r--src/common/thread.h8
-rw-r--r--src/core/CMakeLists.txt5
-rw-r--r--src/core/arm/arm_interface.h15
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.cpp28
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.h4
-rw-r--r--src/core/core.cpp4
-rw-r--r--src/core/core_cpu.cpp8
-rw-r--r--src/core/core_cpu.h2
-rw-r--r--src/core/file_sys/bis_factory.cpp12
-rw-r--r--src/core/file_sys/bis_factory.h5
-rw-r--r--src/core/file_sys/content_archive.cpp2
-rw-r--r--src/core/file_sys/content_archive.h2
-rw-r--r--src/core/file_sys/control_metadata.cpp26
-rw-r--r--src/core/file_sys/control_metadata.h9
-rw-r--r--src/core/file_sys/fsmitm_romfsbuild.cpp366
-rw-r--r--src/core/file_sys/fsmitm_romfsbuild.h70
-rw-r--r--src/core/file_sys/nca_metadata.cpp2
-rw-r--r--src/core/file_sys/nca_metadata.h1
-rw-r--r--src/core/file_sys/partition_filesystem.cpp2
-rw-r--r--src/core/file_sys/partition_filesystem.h2
-rw-r--r--src/core/file_sys/patch_manager.cpp51
-rw-r--r--src/core/file_sys/patch_manager.h3
-rw-r--r--src/core/file_sys/program_metadata.cpp8
-rw-r--r--src/core/file_sys/program_metadata.h9
-rw-r--r--src/core/file_sys/registered_cache.cpp9
-rw-r--r--src/core/file_sys/registered_cache.h2
-rw-r--r--src/core/file_sys/romfs.cpp21
-rw-r--r--src/core/file_sys/romfs.h15
-rw-r--r--src/core/file_sys/romfs_factory.cpp4
-rw-r--r--src/core/file_sys/romfs_factory.h1
-rw-r--r--src/core/file_sys/savedata_factory.cpp15
-rw-r--r--src/core/file_sys/savedata_factory.h1
-rw-r--r--src/core/file_sys/submission_package.h2
-rw-r--r--src/core/file_sys/vfs.cpp45
-rw-r--r--src/core/file_sys/vfs.h17
-rw-r--r--src/core/file_sys/vfs_concat.cpp79
-rw-r--r--src/core/file_sys/vfs_concat.h20
-rw-r--r--src/core/file_sys/vfs_layered.cpp132
-rw-r--r--src/core/file_sys/vfs_layered.h50
-rw-r--r--src/core/file_sys/vfs_offset.cpp2
-rw-r--r--src/core/file_sys/vfs_offset.h1
-rw-r--r--src/core/file_sys/vfs_real.cpp17
-rw-r--r--src/core/file_sys/vfs_real.h1
-rw-r--r--src/core/file_sys/vfs_static.h79
-rw-r--r--src/core/file_sys/vfs_vector.cpp56
-rw-r--r--src/core/file_sys/vfs_vector.h26
-rw-r--r--src/core/file_sys/xts_archive.cpp13
-rw-r--r--src/core/file_sys/xts_archive.h5
-rw-r--r--src/core/gdbstub/gdbstub.cpp19
-rw-r--r--src/core/hle/ipc_helpers.h7
-rw-r--r--src/core/hle/kernel/errors.h2
-rw-r--r--src/core/hle/kernel/mutex.cpp1
-rw-r--r--src/core/hle/kernel/object.h3
-rw-r--r--src/core/hle/kernel/process.cpp108
-rw-r--r--src/core/hle/kernel/process.h160
-rw-r--r--src/core/hle/kernel/scheduler.cpp16
-rw-r--r--src/core/hle/kernel/scheduler.h4
-rw-r--r--src/core/hle/kernel/shared_memory.cpp16
-rw-r--r--src/core/hle/kernel/svc.cpp141
-rw-r--r--src/core/hle/kernel/svc_wrap.h5
-rw-r--r--src/core/hle/kernel/thread.cpp65
-rw-r--r--src/core/hle/kernel/thread.h15
-rw-r--r--src/core/hle/kernel/vm_manager.cpp180
-rw-r--r--src/core/hle/kernel/vm_manager.h100
-rw-r--r--src/core/hle/kernel/wait_object.h2
-rw-r--r--src/core/hle/service/acc/acc.cpp9
-rw-r--r--src/core/hle/service/am/am.cpp8
-rw-r--r--src/core/hle/service/audio/audout_u.cpp2
-rw-r--r--src/core/hle/service/audio/audren_u.cpp16
-rw-r--r--src/core/hle/service/fatal/fatal.cpp141
-rw-r--r--src/core/hle/service/fatal/fatal.h1
-rw-r--r--src/core/hle/service/fatal/fatal_u.cpp2
-rw-r--r--src/core/hle/service/filesystem/filesystem.cpp15
-rw-r--r--src/core/hle/service/filesystem/filesystem.h2
-rw-r--r--src/core/hle/service/hid/hid.cpp8
-rw-r--r--src/core/hle/service/hid/irs.cpp158
-rw-r--r--src/core/hle/service/hid/irs.h27
-rw-r--r--src/core/hle/service/nfp/nfp.cpp1
-rw-r--r--src/core/hle/service/nifm/nifm.cpp19
-rw-r--r--src/core/hle/service/nim/nim.cpp103
-rw-r--r--src/core/hle/service/ns/pl_u.cpp6
-rw-r--r--src/core/hle/service/sm/controller.cpp3
-rw-r--r--src/core/hle/service/sm/sm.cpp5
-rw-r--r--src/core/hle/service/ssl/ssl.cpp8
-rw-r--r--src/core/hle/service/vi/vi.cpp50
-rw-r--r--src/core/loader/deconstructed_rom_directory.cpp21
-rw-r--r--src/core/loader/deconstructed_rom_directory.h3
-rw-r--r--src/core/loader/elf.cpp38
-rw-r--r--src/core/loader/elf.h5
-rw-r--r--src/core/loader/loader.h3
-rw-r--r--src/core/loader/nax.cpp28
-rw-r--r--src/core/loader/nax.h6
-rw-r--r--src/core/loader/nca.cpp2
-rw-r--r--src/core/loader/nca.h3
-rw-r--r--src/core/loader/nro.cpp14
-rw-r--r--src/core/loader/nro.h3
-rw-r--r--src/core/loader/nso.cpp31
-rw-r--r--src/core/loader/nso.h4
-rw-r--r--src/core/loader/nsp.cpp4
-rw-r--r--src/core/loader/nsp.h2
-rw-r--r--src/core/loader/xci.cpp4
-rw-r--r--src/core/loader/xci.h2
-rw-r--r--src/core/memory.cpp42
-rw-r--r--src/core/memory.h63
-rw-r--r--src/tests/core/arm/arm_test_common.cpp9
-rw-r--r--src/video_core/engines/maxwell_3d.h20
-rw-r--r--src/video_core/engines/maxwell_compute.cpp19
-rw-r--r--src/video_core/engines/maxwell_compute.h36
-rw-r--r--src/video_core/engines/shader_bytecode.h27
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp46
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h11
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp436
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.h64
-rw-r--r--src/video_core/renderer_opengl/gl_shader_cache.cpp2
-rw-r--r--src/video_core/renderer_opengl/gl_shader_decompiler.cpp8
-rw-r--r--src/video_core/renderer_opengl/gl_shader_manager.h3
-rw-r--r--src/video_core/renderer_opengl/gl_state.cpp32
-rw-r--r--src/video_core/renderer_opengl/gl_state.h9
-rw-r--r--src/video_core/renderer_opengl/gl_stream_buffer.cpp2
-rw-r--r--src/video_core/textures/decoders.cpp117
-rw-r--r--src/video_core/textures/texture.h2
-rw-r--r--src/video_core/utils.h22
-rw-r--r--src/yuzu/debugger/wait_tree.cpp1
-rw-r--r--src/yuzu/game_list.cpp33
-rw-r--r--src/yuzu/game_list.h39
-rw-r--r--src/yuzu/game_list_p.h42
-rw-r--r--src/yuzu/main.cpp167
-rw-r--r--src/yuzu/main.h2
-rw-r--r--src/yuzu_cmd/config.cpp2
-rw-r--r--src/yuzu_cmd/yuzu.cpp4
154 files changed, 3632 insertions, 832 deletions
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 000000000..ab861a396
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,4 @@
1dist/languages/* linguist-vendored
2dist/qt_themes/* linguist-vendored
3externals/* linguist-vendored
4*.h linguist-language=cpp
diff --git a/.travis.yml b/.travis.yml
index dee34a8e3..b0fbe3c5f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -24,11 +24,24 @@ matrix:
24 - os: osx 24 - os: osx
25 env: NAME="macos build" 25 env: NAME="macos build"
26 sudo: false 26 sudo: false
27 osx_image: xcode9.3 27 osx_image: xcode10
28 install: "./.travis/macos/deps.sh" 28 install: "./.travis/macos/deps.sh"
29 script: "./.travis/macos/build.sh" 29 script: "./.travis/macos/build.sh"
30 after_success: "./.travis/macos/upload.sh" 30 after_success: "./.travis/macos/upload.sh"
31 cache: ccache 31 cache: ccache
32 - os: linux
33 env: NAME="MinGW build"
34 sudo: required
35 dist: trusty
36 services: docker
37 addons:
38 apt:
39 packages:
40 - p7zip-full
41 install: "./.travis/linux-mingw/deps.sh"
42 script: "./.travis/linux-mingw/build.sh"
43 after_success: "./.travis/linux-mingw/upload.sh"
44 cache: ccache
32 45
33deploy: 46deploy:
34 provider: releases 47 provider: releases
diff --git a/.travis/common/post-upload.sh b/.travis/common/post-upload.sh
index 90deaaec8..28735a9cf 100755
--- a/.travis/common/post-upload.sh
+++ b/.travis/common/post-upload.sh
@@ -11,6 +11,9 @@ if [ -z $TRAVIS_TAG ]; then
11 RELEASE_NAME=head 11 RELEASE_NAME=head
12else 12else
13 RELEASE_NAME=$(echo $TRAVIS_TAG | cut -d- -f1) 13 RELEASE_NAME=$(echo $TRAVIS_TAG | cut -d- -f1)
14 if [ "$NAME" = "MinGW build" ]; then
15 RELEASE_NAME="${RELEASE_NAME}-mingw"
16 fi
14fi 17fi
15 18
16mv "$REV_NAME" $RELEASE_NAME 19mv "$REV_NAME" $RELEASE_NAME
diff --git a/.travis/linux-mingw/build.sh b/.travis/linux-mingw/build.sh
new file mode 100755
index 000000000..be03cc0f3
--- /dev/null
+++ b/.travis/linux-mingw/build.sh
@@ -0,0 +1,3 @@
1#!/bin/bash -ex
2mkdir "$HOME/.ccache" || true
3docker run --env-file .travis/common/travis-ci.env -v $(pwd):/yuzu -v "$HOME/.ccache":/root/.ccache ubuntu:18.04 /bin/bash -ex /yuzu/.travis/linux-mingw/docker.sh
diff --git a/.travis/linux-mingw/deps.sh b/.travis/linux-mingw/deps.sh
new file mode 100755
index 000000000..540bb934a
--- /dev/null
+++ b/.travis/linux-mingw/deps.sh
@@ -0,0 +1,3 @@
1#!/bin/sh -ex
2
3docker pull ubuntu:18.04
diff --git a/.travis/linux-mingw/docker.sh b/.travis/linux-mingw/docker.sh
new file mode 100755
index 000000000..d15c3f6e8
--- /dev/null
+++ b/.travis/linux-mingw/docker.sh
@@ -0,0 +1,59 @@
1#!/bin/bash -ex
2
3cd /yuzu
4MINGW_PACKAGES="sdl2-mingw-w64 qt5base-mingw-w64 qt5tools-mingw-w64 libsamplerate-mingw-w64 qt5multimedia-mingw-w64"
5apt-get update
6apt-get install -y gpg wget git python3-pip python ccache g++-mingw-w64-x86-64 gcc-mingw-w64-x86-64 mingw-w64-tools cmake
7echo 'deb http://ppa.launchpad.net/tobydox/mingw-w64/ubuntu bionic main ' > /etc/apt/sources.list.d/extras.list
8apt-key adv --keyserver keyserver.ubuntu.com --recv '72931B477E22FEFD47F8DECE02FE5F12ADDE29B2'
9apt-get update
10apt-get install -y ${MINGW_PACKAGES}
11
12# fix a problem in current MinGW headers
13wget -q https://raw.githubusercontent.com/Alexpux/mingw-w64/d0d7f784833bbb0b2d279310ddc6afb52fe47a46/mingw-w64-headers/crt/errno.h -O /usr/x86_64-w64-mingw32/include/errno.h
14# override Travis CI unreasonable ccache size
15echo 'max_size = 3.0G' > "$HOME/.ccache/ccache.conf"
16
17# Dirty hack to trick unicorn makefile into believing we are in a MINGW system
18mv /bin/uname /bin/uname1 && echo -e '#!/bin/sh\necho MINGW64' >> /bin/uname
19chmod +x /bin/uname
20
21# Dirty hack to trick unicorn makefile into believing we have cmd
22echo '' >> /bin/cmd
23chmod +x /bin/cmd
24
25mkdir build && cd build
26cmake .. -DCMAKE_TOOLCHAIN_FILE="$(pwd)/../CMakeModules/MinGWCross.cmake" -DUSE_CCACHE=ON -DYUZU_USE_BUNDLED_UNICORN=ON -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DCMAKE_BUILD_TYPE=Release
27make -j4
28
29# Clean up the dirty hacks
30rm /bin/uname && mv /bin/uname1 /bin/uname
31rm /bin/cmd
32
33ccache -s
34
35echo "Tests skipped"
36#ctest -VV -C Release
37
38echo 'Prepare binaries...'
39cd ..
40mkdir package
41
42QT_PLATFORM_DLL_PATH='/usr/x86_64-w64-mingw32/lib/qt5/plugins/platforms/'
43find build/ -name "yuzu*.exe" -exec cp {} 'package' \;
44
45# copy Qt plugins
46mkdir package/platforms
47cp "${QT_PLATFORM_DLL_PATH}/qwindows.dll" package/platforms/
48cp -rv "${QT_PLATFORM_DLL_PATH}/../mediaservice/" package/
49cp -rv "${QT_PLATFORM_DLL_PATH}/../imageformats/" package/
50rm -f package/mediaservice/*d.dll
51
52for i in package/*.exe; do
53 # we need to process pdb here, however, cv2pdb
54 # does not work here, so we just simply strip all the debug symbols
55 x86_64-w64-mingw32-strip "${i}"
56done
57
58pip3 install pefile
59python3 .travis/linux-mingw/scan_dll.py package/*.exe "package/"
diff --git a/.travis/linux-mingw/scan_dll.py b/.travis/linux-mingw/scan_dll.py
new file mode 100644
index 000000000..163183f2e
--- /dev/null
+++ b/.travis/linux-mingw/scan_dll.py
@@ -0,0 +1,106 @@
1import pefile
2import sys
3import re
4import os
5import queue
6import shutil
7
8# constant definitions
9KNOWN_SYS_DLLS = ['WINMM.DLL', 'MSVCRT.DLL', 'VERSION.DLL', 'MPR.DLL',
10 'DWMAPI.DLL', 'UXTHEME.DLL', 'DNSAPI.DLL', 'IPHLPAPI.DLL']
11# below is for Ubuntu 18.04 with specified PPA enabled, if you are using
12# other distro or different repositories, change the following accordingly
13DLL_PATH = [
14 '/usr/x86_64-w64-mingw32/bin/',
15 '/usr/x86_64-w64-mingw32/lib/',
16 '/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/'
17]
18
19missing = []
20
21
22def parse_imports(file_name):
23 results = []
24 pe = pefile.PE(file_name, fast_load=True)
25 pe.parse_data_directories()
26
27 for entry in pe.DIRECTORY_ENTRY_IMPORT:
28 current = entry.dll.decode()
29 current_u = current.upper() # b/c Windows is often case insensitive
30 # here we filter out system dlls
31 # dll w/ names like *32.dll are likely to be system dlls
32 if current_u.upper() not in KNOWN_SYS_DLLS and not re.match(string=current_u, pattern=r'.*32\.DLL'):
33 results.append(current)
34
35 return results
36
37
38def parse_imports_recursive(file_name, path_list=[]):
39 q = queue.Queue() # create a FIFO queue
40 # file_name can be a string or a list for the convience
41 if isinstance(file_name, str):
42 q.put(file_name)
43 elif isinstance(file_name, list):
44 for i in file_name:
45 q.put(i)
46 full_list = []
47 while q.qsize():
48 current = q.get_nowait()
49 print('> %s' % current)
50 deps = parse_imports(current)
51 # if this dll does not have any import, ignore it
52 if not deps:
53 continue
54 for dep in deps:
55 # the dependency already included in the list, skip
56 if dep in full_list:
57 continue
58 # find the requested dll in the provided paths
59 full_path = find_dll(dep)
60 if not full_path:
61 missing.append(dep)
62 continue
63 full_list.append(dep)
64 q.put(full_path)
65 path_list.append(full_path)
66 return full_list
67
68
69def find_dll(name):
70 for path in DLL_PATH:
71 for root, _, files in os.walk(path):
72 for f in files:
73 if name.lower() == f.lower():
74 return os.path.join(root, f)
75
76
77def deploy(name, dst, dry_run=False):
78 dlls_path = []
79 parse_imports_recursive(name, dlls_path)
80 for dll_entry in dlls_path:
81 if not dry_run:
82 shutil.copy(dll_entry, dst)
83 else:
84 print('[Dry-Run] Copy %s to %s' % (dll_entry, dst))
85 print('Deploy completed.')
86 return dlls_path
87
88
89def main():
90 if len(sys.argv) < 3:
91 print('Usage: %s [files to examine ...] [target deploy directory]')
92 return 1
93 to_deploy = sys.argv[1:-1]
94 tgt_dir = sys.argv[-1]
95 if not os.path.isdir(tgt_dir):
96 print('%s is not a directory.' % tgt_dir)
97 return 1
98 print('Scanning dependencies...')
99 deploy(to_deploy, tgt_dir)
100 if missing:
101 print('Following DLLs are not found: %s' % ('\n'.join(missing)))
102 return 0
103
104
105if __name__ == '__main__':
106 main()
diff --git a/.travis/linux-mingw/upload.sh b/.travis/linux-mingw/upload.sh
new file mode 100755
index 000000000..66e896bc4
--- /dev/null
+++ b/.travis/linux-mingw/upload.sh
@@ -0,0 +1,13 @@
1#!/bin/bash -ex
2
3. .travis/common/pre-upload.sh
4
5REV_NAME="yuzu-windows-mingw-${GITDATE}-${GITREV}"
6ARCHIVE_NAME="${REV_NAME}.tar.gz"
7COMPRESSION_FLAGS="-czvf"
8
9mkdir "$REV_NAME"
10# get around the permission issues
11cp -r package/* "$REV_NAME"
12
13. .travis/common/post-upload.sh
diff --git a/.travis/linux/docker.sh b/.travis/linux/docker.sh
index 459d6bc75..892d2480a 100755
--- a/.travis/linux/docker.sh
+++ b/.travis/linux/docker.sh
@@ -6,7 +6,9 @@ apt-get install --no-install-recommends -y build-essential git libqt5opengl5-dev
6cd /yuzu 6cd /yuzu
7 7
8mkdir build && cd build 8mkdir build && cd build
9cmake .. -DYUZU_BUILD_UNICORN=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/usr/lib/ccache/gcc -DCMAKE_CXX_COMPILER=/usr/lib/ccache/g++ -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -G Ninja 9cmake .. -DYUZU_USE_BUNDLED_UNICORN=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/usr/lib/ccache/gcc -DCMAKE_CXX_COMPILER=/usr/lib/ccache/g++ -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -G Ninja
10ninja 10ninja
11 11
12ccache -s
13
12ctest -VV -C Release 14ctest -VV -C Release
diff --git a/.travis/macos/build.sh b/.travis/macos/build.sh
index b76a153be..e68dc1400 100755
--- a/.travis/macos/build.sh
+++ b/.travis/macos/build.sh
@@ -2,14 +2,16 @@
2 2
3set -o pipefail 3set -o pipefail
4 4
5export MACOSX_DEPLOYMENT_TARGET=10.12 5export MACOSX_DEPLOYMENT_TARGET=10.13
6export Qt5_DIR=$(brew --prefix)/opt/qt5 6export Qt5_DIR=$(brew --prefix)/opt/qt5
7export UNICORNDIR=$(pwd)/externals/unicorn 7export UNICORNDIR=$(pwd)/externals/unicorn
8export PATH="/usr/local/opt/ccache/libexec:$PATH" 8export PATH="/usr/local/opt/ccache/libexec:$PATH"
9 9
10mkdir build && cd build 10mkdir build && cd build
11cmake --version 11cmake --version
12cmake .. -DYUZU_BUILD_UNICORN=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON 12cmake .. -DYUZU_USE_BUNDLED_UNICORN=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON
13make -j4 13make -j4
14 14
15ccache -s
16
15ctest -VV -C Release 17ctest -VV -C Release
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 500d099fc..cd990188e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -123,8 +123,6 @@ else()
123 # Avoid windows.h from including some usually unused libs like winsocks.h, since this might cause some redefinition errors. 123 # Avoid windows.h from including some usually unused libs like winsocks.h, since this might cause some redefinition errors.
124 add_definitions(/DWIN32_LEAN_AND_MEAN) 124 add_definitions(/DWIN32_LEAN_AND_MEAN)
125 125
126 # set up output paths for executable binaries (.exe-files, and .dll-files on DLL-capable platforms)
127 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
128 set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE) 126 set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
129 127
130 # Tweak optimization settings 128 # Tweak optimization settings
@@ -269,10 +267,18 @@ if (YUZU_USE_BUNDLED_UNICORN)
269 267
270 find_package(PythonInterp 2.7 REQUIRED) 268 find_package(PythonInterp 2.7 REQUIRED)
271 269
272 add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY} 270 if (MINGW)
273 COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh macos-universal-no 271 add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY}
274 WORKING_DIRECTORY ${UNICORN_PREFIX} 272 COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh cross-win64
275 ) 273 WORKING_DIRECTORY ${UNICORN_PREFIX}
274 )
275 else()
276 add_custom_command(OUTPUT ${LIBUNICORN_LIBRARY}
277 COMMAND ${CMAKE_COMMAND} -E env UNICORN_ARCHS="aarch64" PYTHON="${PYTHON_EXECUTABLE}" /bin/sh make.sh macos-universal-no
278 WORKING_DIRECTORY ${UNICORN_PREFIX}
279 )
280 endif()
281
276 # ALL makes this custom target build every time 282 # ALL makes this custom target build every time
277 # but it won't actually build if LIBUNICORN_LIBRARY is up to date 283 # but it won't actually build if LIBUNICORN_LIBRARY is up to date
278 add_custom_target(unicorn-build ALL 284 add_custom_target(unicorn-build ALL
@@ -286,6 +292,7 @@ endif()
286 292
287if (UNICORN_FOUND) 293if (UNICORN_FOUND)
288 add_library(unicorn INTERFACE) 294 add_library(unicorn INTERFACE)
295 add_dependencies(unicorn unicorn-build)
289 target_link_libraries(unicorn INTERFACE "${LIBUNICORN_LIBRARY}") 296 target_link_libraries(unicorn INTERFACE "${LIBUNICORN_LIBRARY}")
290 target_include_directories(unicorn INTERFACE "${LIBUNICORN_INCLUDE_DIR}") 297 target_include_directories(unicorn INTERFACE "${LIBUNICORN_INCLUDE_DIR}")
291else() 298else()
@@ -431,8 +438,12 @@ enable_testing()
431add_subdirectory(externals) 438add_subdirectory(externals)
432add_subdirectory(src) 439add_subdirectory(src)
433 440
434# Set yuzu project as default StartUp Project in Visual Studio 441# Set yuzu project or yuzu-cmd project as default StartUp Project in Visual Studio depending on whether QT is enabled or not
435set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu) 442if(ENABLE_QT)
443 set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu)
444else()
445 set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT yuzu-cmd)
446endif()
436 447
437 448
438# Installation instructions 449# Installation instructions
diff --git a/CMakeModules/MinGWCross.cmake b/CMakeModules/MinGWCross.cmake
new file mode 100644
index 000000000..29ecd1ac4
--- /dev/null
+++ b/CMakeModules/MinGWCross.cmake
@@ -0,0 +1,54 @@
1set(MINGW_PREFIX /usr/x86_64-w64-mingw32/)
2set(CMAKE_SYSTEM_NAME Windows)
3set(CMAKE_SYSTEM_PROCESSOR x86_64)
4# Actually a hack, w/o this will cause some strange errors
5set(CMAKE_HOST_WIN32 TRUE)
6
7
8set(CMAKE_FIND_ROOT_PATH ${MINGW_PREFIX})
9set(SDL2_PATH ${MINGW_PREFIX})
10set(MINGW_TOOL_PREFIX ${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32-)
11
12# Specify the cross compiler
13set(CMAKE_C_COMPILER ${MINGW_TOOL_PREFIX}gcc-posix)
14set(CMAKE_CXX_COMPILER ${MINGW_TOOL_PREFIX}g++-posix)
15set(CMAKE_RC_COMPILER ${MINGW_TOOL_PREFIX}windres)
16
17# Mingw tools
18set(STRIP ${MINGW_TOOL_PREFIX}strip)
19set(WINDRES ${MINGW_TOOL_PREFIX}windres)
20set(ENV{PKG_CONFIG} ${MINGW_TOOL_PREFIX}pkg-config)
21
22# ccache wrapper
23option(USE_CCACHE "Use ccache for compilation" OFF)
24if(USE_CCACHE)
25 find_program(CCACHE ccache)
26 if(CCACHE)
27 message(STATUS "Using ccache found in PATH")
28 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE})
29 set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE})
30 else(CCACHE)
31 message(WARNING "USE_CCACHE enabled, but no ccache found")
32 endif(CCACHE)
33endif(USE_CCACHE)
34
35# Search for programs in the build host directories
36set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
37
38
39# Echo modified cmake vars to screen for debugging purposes
40if(NOT DEFINED ENV{MINGW_DEBUG_INFO})
41 message("")
42 message("Custom cmake vars: (blank = system default)")
43 message("-----------------------------------------")
44 message("* CMAKE_C_COMPILER : ${CMAKE_C_COMPILER}")
45 message("* CMAKE_CXX_COMPILER : ${CMAKE_CXX_COMPILER}")
46 message("* CMAKE_RC_COMPILER : ${CMAKE_RC_COMPILER}")
47 message("* WINDRES : ${WINDRES}")
48 message("* ENV{PKG_CONFIG} : $ENV{PKG_CONFIG}")
49 message("* STRIP : ${STRIP}")
50 message("* USE_CCACHE : ${USE_CCACHE}")
51 message("")
52 # So that the debug info only appears once
53 set(ENV{MINGW_DEBUG_INFO} SHOWN)
54endif()
diff --git a/externals/dynarmic b/externals/dynarmic
Subproject 959446573f3adfcba173ef4b0011a4a280f18eb Subproject 4e6848d1c9e8dadc70595c15b5589f8b14aad47
diff --git a/src/audio_core/audio_renderer.cpp b/src/audio_core/audio_renderer.cpp
index 83b75e61f..6f0ff953a 100644
--- a/src/audio_core/audio_renderer.cpp
+++ b/src/audio_core/audio_renderer.cpp
@@ -79,6 +79,10 @@ u32 AudioRenderer::GetMixBufferCount() const {
79 return worker_params.mix_buffer_count; 79 return worker_params.mix_buffer_count;
80} 80}
81 81
82Stream::State AudioRenderer::GetStreamState() const {
83 return stream->GetState();
84}
85
82std::vector<u8> AudioRenderer::UpdateAudioRenderer(const std::vector<u8>& input_params) { 86std::vector<u8> AudioRenderer::UpdateAudioRenderer(const std::vector<u8>& input_params) {
83 // Copy UpdateDataHeader struct 87 // Copy UpdateDataHeader struct
84 UpdateDataHeader config{}; 88 UpdateDataHeader config{};
diff --git a/src/audio_core/audio_renderer.h b/src/audio_core/audio_renderer.h
index 2c4f5ab75..dfef89e1d 100644
--- a/src/audio_core/audio_renderer.h
+++ b/src/audio_core/audio_renderer.h
@@ -170,6 +170,7 @@ public:
170 u32 GetSampleRate() const; 170 u32 GetSampleRate() const;
171 u32 GetSampleCount() const; 171 u32 GetSampleCount() const;
172 u32 GetMixBufferCount() const; 172 u32 GetMixBufferCount() const;
173 Stream::State GetStreamState() const;
173 174
174private: 175private:
175 class VoiceState; 176 class VoiceState;
diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp
index 449db2416..742a5e0a0 100644
--- a/src/audio_core/stream.cpp
+++ b/src/audio_core/stream.cpp
@@ -49,9 +49,14 @@ void Stream::Play() {
49} 49}
50 50
51void Stream::Stop() { 51void Stream::Stop() {
52 state = State::Stopped;
52 ASSERT_MSG(false, "Unimplemented"); 53 ASSERT_MSG(false, "Unimplemented");
53} 54}
54 55
56Stream::State Stream::GetState() const {
57 return state;
58}
59
55s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const { 60s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const {
56 const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()}; 61 const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
57 return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate); 62 return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate);
diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h
index 27db1112f..aebfeb51d 100644
--- a/src/audio_core/stream.h
+++ b/src/audio_core/stream.h
@@ -33,6 +33,12 @@ public:
33 Multi51Channel16, 33 Multi51Channel16,
34 }; 34 };
35 35
36 /// Current state of the stream
37 enum class State {
38 Stopped,
39 Playing,
40 };
41
36 /// Callback function type, used to change guest state on a buffer being released 42 /// Callback function type, used to change guest state on a buffer being released
37 using ReleaseCallback = std::function<void()>; 43 using ReleaseCallback = std::function<void()>;
38 44
@@ -72,13 +78,10 @@ public:
72 /// Gets the number of channels 78 /// Gets the number of channels
73 u32 GetNumChannels() const; 79 u32 GetNumChannels() const;
74 80
75private: 81 /// Get the state
76 /// Current state of the stream 82 State GetState() const;
77 enum class State {
78 Stopped,
79 Playing,
80 };
81 83
84private:
82 /// Plays the next queued buffer in the audio stream, starting playback if necessary 85 /// Plays the next queued buffer in the audio stream, starting playback if necessary
83 void PlayNextBuffer(); 86 void PlayNextBuffer();
84 87
diff --git a/src/audio_core/time_stretch.cpp b/src/audio_core/time_stretch.cpp
index fc14151da..d72d67994 100644
--- a/src/audio_core/time_stretch.cpp
+++ b/src/audio_core/time_stretch.cpp
@@ -59,7 +59,7 @@ std::size_t TimeStretcher::Process(const s16* in, std::size_t num_in, s16* out,
59 m_stretch_ratio = std::max(m_stretch_ratio, 0.05); 59 m_stretch_ratio = std::max(m_stretch_ratio, 0.05);
60 m_sound_touch.setTempo(m_stretch_ratio); 60 m_sound_touch.setTempo(m_stretch_ratio);
61 61
62 LOG_DEBUG(Audio, "{:5}/{:5} ratio:{:0.6f} backlog:{:0.6f}", num_in, num_out, m_stretch_ratio, 62 LOG_TRACE(Audio, "{:5}/{:5} ratio:{:0.6f} backlog:{:0.6f}", num_in, num_out, m_stretch_ratio,
63 backlog_fullness); 63 backlog_fullness);
64 64
65 m_sound_touch.putSamples(in, static_cast<u32>(num_in)); 65 m_sound_touch.putSamples(in, static_cast<u32>(num_in));
diff --git a/src/common/common_paths.h b/src/common/common_paths.h
index df2ce80b1..4f88de768 100644
--- a/src/common/common_paths.h
+++ b/src/common/common_paths.h
@@ -33,6 +33,8 @@
33#define NAND_DIR "nand" 33#define NAND_DIR "nand"
34#define SYSDATA_DIR "sysdata" 34#define SYSDATA_DIR "sysdata"
35#define KEYS_DIR "keys" 35#define KEYS_DIR "keys"
36#define LOAD_DIR "load"
37#define DUMP_DIR "dump"
36#define LOG_DIR "log" 38#define LOG_DIR "log"
37 39
38// Filenames 40// Filenames
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 21a0b9738..548463787 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -705,6 +705,8 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) {
705#endif 705#endif
706 paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP); 706 paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP);
707 paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); 707 paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP);
708 paths.emplace(UserPath::LoadDir, user_path + LOAD_DIR DIR_SEP);
709 paths.emplace(UserPath::DumpDir, user_path + DUMP_DIR DIR_SEP);
708 paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); 710 paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP);
709 paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP); 711 paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP);
710 // TODO: Put the logs in a better location for each OS 712 // TODO: Put the logs in a better location for each OS
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 24c1e413c..3d8fe6264 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -29,6 +29,8 @@ enum class UserPath {
29 NANDDir, 29 NANDDir,
30 RootDir, 30 RootDir,
31 SDMCDir, 31 SDMCDir,
32 LoadDir,
33 DumpDir,
32 SysDataDir, 34 SysDataDir,
33 UserDir, 35 UserDir,
34}; 36};
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index efd776db6..9f5918851 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -183,6 +183,7 @@ void FileBackend::Write(const Entry& entry) {
183 SUB(Service, FS) \ 183 SUB(Service, FS) \
184 SUB(Service, GRC) \ 184 SUB(Service, GRC) \
185 SUB(Service, HID) \ 185 SUB(Service, HID) \
186 SUB(Service, IRS) \
186 SUB(Service, LBL) \ 187 SUB(Service, LBL) \
187 SUB(Service, LDN) \ 188 SUB(Service, LDN) \
188 SUB(Service, LDR) \ 189 SUB(Service, LDR) \
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 4d577524f..abbd056ee 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -70,6 +70,7 @@ enum class Class : ClassType {
70 Service_FS, ///< The FS (Filesystem) service 70 Service_FS, ///< The FS (Filesystem) service
71 Service_GRC, ///< The game recording service 71 Service_GRC, ///< The game recording service
72 Service_HID, ///< The HID (Human interface device) service 72 Service_HID, ///< The HID (Human interface device) service
73 Service_IRS, ///< The IRS service
73 Service_LBL, ///< The LBL (LCD backlight) service 74 Service_LBL, ///< The LBL (LCD backlight) service
74 Service_LDN, ///< The LDN (Local domain network) service 75 Service_LDN, ///< The LDN (Local domain network) service
75 Service_LDR, ///< The loader service 76 Service_LDR, ///< The loader service
diff --git a/src/common/ring_buffer.h b/src/common/ring_buffer.h
index 45926c9ec..abe3b4dc2 100644
--- a/src/common/ring_buffer.h
+++ b/src/common/ring_buffer.h
@@ -9,6 +9,7 @@
9#include <atomic> 9#include <atomic>
10#include <cstddef> 10#include <cstddef>
11#include <cstring> 11#include <cstring>
12#include <new>
12#include <type_traits> 13#include <type_traits>
13#include <vector> 14#include <vector>
14#include "common/common_types.h" 15#include "common/common_types.h"
@@ -29,7 +30,7 @@ class RingBuffer {
29 static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2 / granularity); 30 static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2 / granularity);
30 static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two"); 31 static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");
31 // Ensure lock-free. 32 // Ensure lock-free.
32 static_assert(std::atomic<std::size_t>::is_always_lock_free); 33 static_assert(std::atomic_size_t::is_always_lock_free);
33 34
34public: 35public:
35 /// Pushes slots into the ring buffer 36 /// Pushes slots into the ring buffer
@@ -102,8 +103,15 @@ public:
102private: 103private:
103 // It is important to align the below variables for performance reasons: 104 // It is important to align the below variables for performance reasons:
104 // Having them on the same cache-line would result in false-sharing between them. 105 // Having them on the same cache-line would result in false-sharing between them.
105 alignas(128) std::atomic<std::size_t> m_read_index{0}; 106 // TODO: Remove this ifdef whenever clang and GCC support
106 alignas(128) std::atomic<std::size_t> m_write_index{0}; 107 // std::hardware_destructive_interference_size.
108#if defined(_MSC_VER) && _MSC_VER >= 1911
109 alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_read_index{0};
110 alignas(std::hardware_destructive_interference_size) std::atomic_size_t m_write_index{0};
111#else
112 alignas(128) std::atomic_size_t m_read_index{0};
113 alignas(128) std::atomic_size_t m_write_index{0};
114#endif
107 115
108 std::array<T, granularity * capacity> m_data; 116 std::array<T, granularity * capacity> m_data;
109}; 117};
diff --git a/src/common/thread.h b/src/common/thread.h
index 12a1c095c..6cbdb96a3 100644
--- a/src/common/thread.h
+++ b/src/common/thread.h
@@ -87,14 +87,6 @@ private:
87 87
88void SleepCurrentThread(int ms); 88void SleepCurrentThread(int ms);
89void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms 89void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms
90
91// Use this function during a spin-wait to make the current thread
92// relax while another thread is working. This may be more efficient
93// than using events because event functions use kernel calls.
94inline void YieldCPU() {
95 std::this_thread::yield();
96}
97
98void SetCurrentThreadName(const char* name); 90void SetCurrentThreadName(const char* name);
99 91
100} // namespace Common 92} // namespace Common
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 26f727d96..23fd6e920 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -32,6 +32,8 @@ add_library(core STATIC
32 file_sys/control_metadata.h 32 file_sys/control_metadata.h
33 file_sys/directory.h 33 file_sys/directory.h
34 file_sys/errors.h 34 file_sys/errors.h
35 file_sys/fsmitm_romfsbuild.cpp
36 file_sys/fsmitm_romfsbuild.h
35 file_sys/mode.h 37 file_sys/mode.h
36 file_sys/nca_metadata.cpp 38 file_sys/nca_metadata.cpp
37 file_sys/nca_metadata.h 39 file_sys/nca_metadata.h
@@ -59,10 +61,13 @@ add_library(core STATIC
59 file_sys/vfs.h 61 file_sys/vfs.h
60 file_sys/vfs_concat.cpp 62 file_sys/vfs_concat.cpp
61 file_sys/vfs_concat.h 63 file_sys/vfs_concat.h
64 file_sys/vfs_layered.cpp
65 file_sys/vfs_layered.h
62 file_sys/vfs_offset.cpp 66 file_sys/vfs_offset.cpp
63 file_sys/vfs_offset.h 67 file_sys/vfs_offset.h
64 file_sys/vfs_real.cpp 68 file_sys/vfs_real.cpp
65 file_sys/vfs_real.h 69 file_sys/vfs_real.h
70 file_sys/vfs_static.h
66 file_sys/vfs_vector.cpp 71 file_sys/vfs_vector.cpp
67 file_sys/vfs_vector.h 72 file_sys/vfs_vector.h
68 file_sys/xts_archive.cpp 73 file_sys/xts_archive.cpp
diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h
index 867e34932..59da33f30 100644
--- a/src/core/arm/arm_interface.h
+++ b/src/core/arm/arm_interface.h
@@ -6,7 +6,10 @@
6 6
7#include <array> 7#include <array>
8#include "common/common_types.h" 8#include "common/common_types.h"
9#include "core/hle/kernel/vm_manager.h" 9
10namespace Kernel {
11enum class VMAPermission : u8;
12}
10 13
11namespace Core { 14namespace Core {
12 15
@@ -19,10 +22,16 @@ public:
19 std::array<u64, 31> cpu_registers; 22 std::array<u64, 31> cpu_registers;
20 u64 sp; 23 u64 sp;
21 u64 pc; 24 u64 pc;
22 u64 pstate; 25 u32 pstate;
26 std::array<u8, 4> padding;
23 std::array<u128, 32> vector_registers; 27 std::array<u128, 32> vector_registers;
24 u64 fpcr; 28 u32 fpcr;
29 u32 fpsr;
30 u64 tpidr;
25 }; 31 };
32 // Internally within the kernel, it expects the AArch64 version of the
33 // thread context to be 800 bytes in size.
34 static_assert(sizeof(ThreadContext) == 0x320);
26 35
27 /// Runs the CPU until an event happens 36 /// Runs the CPU until an event happens
28 virtual void Run() = 0; 37 virtual void Run() = 0;
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index 3f072c51f..05cc84458 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -12,8 +12,10 @@
12#include "core/core.h" 12#include "core/core.h"
13#include "core/core_cpu.h" 13#include "core/core_cpu.h"
14#include "core/core_timing.h" 14#include "core/core_timing.h"
15#include "core/gdbstub/gdbstub.h"
15#include "core/hle/kernel/process.h" 16#include "core/hle/kernel/process.h"
16#include "core/hle/kernel/svc.h" 17#include "core/hle/kernel/svc.h"
18#include "core/hle/kernel/vm_manager.h"
17#include "core/memory.h" 19#include "core/memory.h"
18 20
19namespace Core { 21namespace Core {
@@ -79,6 +81,17 @@ public:
79 case Dynarmic::A64::Exception::SendEventLocal: 81 case Dynarmic::A64::Exception::SendEventLocal:
80 case Dynarmic::A64::Exception::Yield: 82 case Dynarmic::A64::Exception::Yield:
81 return; 83 return;
84 case Dynarmic::A64::Exception::Breakpoint:
85 if (GDBStub::IsServerEnabled()) {
86 parent.jit->HaltExecution();
87 parent.SetPC(pc);
88 Kernel::Thread* thread = Kernel::GetCurrentThread();
89 parent.SaveContext(thread->context);
90 GDBStub::Break();
91 GDBStub::SendTrap(thread, 5);
92 return;
93 }
94 [[fallthrough]];
82 default: 95 default:
83 ASSERT_MSG(false, "ExceptionRaised(exception = {}, pc = {:X})", 96 ASSERT_MSG(false, "ExceptionRaised(exception = {}, pc = {:X})",
84 static_cast<std::size_t>(exception), pc); 97 static_cast<std::size_t>(exception), pc);
@@ -116,7 +129,8 @@ public:
116}; 129};
117 130
118std::unique_ptr<Dynarmic::A64::Jit> ARM_Dynarmic::MakeJit() const { 131std::unique_ptr<Dynarmic::A64::Jit> ARM_Dynarmic::MakeJit() const {
119 auto** const page_table = Core::CurrentProcess()->vm_manager.page_table.pointers.data(); 132 auto& current_process = Core::CurrentProcess();
133 auto** const page_table = current_process->VMManager().page_table.pointers.data();
120 134
121 Dynarmic::A64::UserConfig config; 135 Dynarmic::A64::UserConfig config;
122 136
@@ -125,7 +139,7 @@ std::unique_ptr<Dynarmic::A64::Jit> ARM_Dynarmic::MakeJit() const {
125 139
126 // Memory 140 // Memory
127 config.page_table = reinterpret_cast<void**>(page_table); 141 config.page_table = reinterpret_cast<void**>(page_table);
128 config.page_table_address_space_bits = Memory::ADDRESS_SPACE_BITS; 142 config.page_table_address_space_bits = current_process->VMManager().GetAddressSpaceWidth();
129 config.silently_mirror_page_table = false; 143 config.silently_mirror_page_table = false;
130 144
131 // Multi-process state 145 // Multi-process state
@@ -161,7 +175,7 @@ ARM_Dynarmic::ARM_Dynarmic(std::shared_ptr<ExclusiveMonitor> exclusive_monitor,
161 std::size_t core_index) 175 std::size_t core_index)
162 : cb(std::make_unique<ARM_Dynarmic_Callbacks>(*this)), core_index{core_index}, 176 : cb(std::make_unique<ARM_Dynarmic_Callbacks>(*this)), core_index{core_index},
163 exclusive_monitor{std::dynamic_pointer_cast<DynarmicExclusiveMonitor>(exclusive_monitor)} { 177 exclusive_monitor{std::dynamic_pointer_cast<DynarmicExclusiveMonitor>(exclusive_monitor)} {
164 ThreadContext ctx; 178 ThreadContext ctx{};
165 inner_unicorn.SaveContext(ctx); 179 inner_unicorn.SaveContext(ctx);
166 PageTableChanged(); 180 PageTableChanged();
167 LoadContext(ctx); 181 LoadContext(ctx);
@@ -233,15 +247,19 @@ void ARM_Dynarmic::SaveContext(ThreadContext& ctx) {
233 ctx.pstate = jit->GetPstate(); 247 ctx.pstate = jit->GetPstate();
234 ctx.vector_registers = jit->GetVectors(); 248 ctx.vector_registers = jit->GetVectors();
235 ctx.fpcr = jit->GetFpcr(); 249 ctx.fpcr = jit->GetFpcr();
250 ctx.fpsr = jit->GetFpsr();
251 ctx.tpidr = cb->tpidr_el0;
236} 252}
237 253
238void ARM_Dynarmic::LoadContext(const ThreadContext& ctx) { 254void ARM_Dynarmic::LoadContext(const ThreadContext& ctx) {
239 jit->SetRegisters(ctx.cpu_registers); 255 jit->SetRegisters(ctx.cpu_registers);
240 jit->SetSP(ctx.sp); 256 jit->SetSP(ctx.sp);
241 jit->SetPC(ctx.pc); 257 jit->SetPC(ctx.pc);
242 jit->SetPstate(static_cast<u32>(ctx.pstate)); 258 jit->SetPstate(ctx.pstate);
243 jit->SetVectors(ctx.vector_registers); 259 jit->SetVectors(ctx.vector_registers);
244 jit->SetFpcr(static_cast<u32>(ctx.fpcr)); 260 jit->SetFpcr(ctx.fpcr);
261 jit->SetFpsr(ctx.fpsr);
262 SetTPIDR_EL0(ctx.tpidr);
245} 263}
246 264
247void ARM_Dynarmic::PrepareReschedule() { 265void ARM_Dynarmic::PrepareReschedule() {
diff --git a/src/core/arm/dynarmic/arm_dynarmic.h b/src/core/arm/dynarmic/arm_dynarmic.h
index e61382d3d..4ee92ee27 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.h
+++ b/src/core/arm/dynarmic/arm_dynarmic.h
@@ -12,6 +12,10 @@
12#include "core/arm/exclusive_monitor.h" 12#include "core/arm/exclusive_monitor.h"
13#include "core/arm/unicorn/arm_unicorn.h" 13#include "core/arm/unicorn/arm_unicorn.h"
14 14
15namespace Memory {
16struct PageTable;
17}
18
15namespace Core { 19namespace Core {
16 20
17class ARM_Dynarmic_Callbacks; 21class ARM_Dynarmic_Callbacks;
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 50f0a42fb..b6acfb3e4 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -64,7 +64,7 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
64 if (concat.empty()) 64 if (concat.empty())
65 return nullptr; 65 return nullptr;
66 66
67 return FileSys::ConcatenateFiles(concat, dir->GetName()); 67 return FileSys::ConcatenatedVfsFile::MakeConcatenatedFile(concat, dir->GetName());
68 } 68 }
69 69
70 return vfs->OpenFile(path, FileSys::Mode::Read); 70 return vfs->OpenFile(path, FileSys::Mode::Read);
@@ -202,7 +202,7 @@ struct System::Impl {
202 return init_result; 202 return init_result;
203 } 203 }
204 204
205 const Loader::ResultStatus load_result{app_loader->Load(kernel.CurrentProcess())}; 205 const Loader::ResultStatus load_result{app_loader->Load(*kernel.CurrentProcess())};
206 if (load_result != Loader::ResultStatus::Success) { 206 if (load_result != Loader::ResultStatus::Success) {
207 LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result)); 207 LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result));
208 Shutdown(); 208 Shutdown();
diff --git a/src/core/core_cpu.cpp b/src/core/core_cpu.cpp
index 21568ad50..265f8ed9c 100644
--- a/src/core/core_cpu.cpp
+++ b/src/core/core_cpu.cpp
@@ -55,16 +55,16 @@ Cpu::Cpu(std::shared_ptr<ExclusiveMonitor> exclusive_monitor,
55 55
56 if (Settings::values.use_cpu_jit) { 56 if (Settings::values.use_cpu_jit) {
57#ifdef ARCHITECTURE_x86_64 57#ifdef ARCHITECTURE_x86_64
58 arm_interface = std::make_shared<ARM_Dynarmic>(exclusive_monitor, core_index); 58 arm_interface = std::make_unique<ARM_Dynarmic>(exclusive_monitor, core_index);
59#else 59#else
60 arm_interface = std::make_shared<ARM_Unicorn>(); 60 arm_interface = std::make_unique<ARM_Unicorn>();
61 LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available"); 61 LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
62#endif 62#endif
63 } else { 63 } else {
64 arm_interface = std::make_shared<ARM_Unicorn>(); 64 arm_interface = std::make_unique<ARM_Unicorn>();
65 } 65 }
66 66
67 scheduler = std::make_shared<Kernel::Scheduler>(arm_interface.get()); 67 scheduler = std::make_shared<Kernel::Scheduler>(*arm_interface);
68} 68}
69 69
70Cpu::~Cpu() = default; 70Cpu::~Cpu() = default;
diff --git a/src/core/core_cpu.h b/src/core/core_cpu.h
index 685532965..ee7e04abc 100644
--- a/src/core/core_cpu.h
+++ b/src/core/core_cpu.h
@@ -76,7 +76,7 @@ public:
76private: 76private:
77 void Reschedule(); 77 void Reschedule();
78 78
79 std::shared_ptr<ARM_Interface> arm_interface; 79 std::unique_ptr<ARM_Interface> arm_interface;
80 std::shared_ptr<CpuBarrier> cpu_barrier; 80 std::shared_ptr<CpuBarrier> cpu_barrier;
81 std::shared_ptr<Kernel::Scheduler> scheduler; 81 std::shared_ptr<Kernel::Scheduler> scheduler;
82 82
diff --git a/src/core/file_sys/bis_factory.cpp b/src/core/file_sys/bis_factory.cpp
index 205492897..6102ef476 100644
--- a/src/core/file_sys/bis_factory.cpp
+++ b/src/core/file_sys/bis_factory.cpp
@@ -2,13 +2,14 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <fmt/format.h>
5#include "core/file_sys/bis_factory.h" 6#include "core/file_sys/bis_factory.h"
6#include "core/file_sys/registered_cache.h" 7#include "core/file_sys/registered_cache.h"
7 8
8namespace FileSys { 9namespace FileSys {
9 10
10BISFactory::BISFactory(VirtualDir nand_root_) 11BISFactory::BISFactory(VirtualDir nand_root_, VirtualDir load_root_)
11 : nand_root(std::move(nand_root_)), 12 : nand_root(std::move(nand_root_)), load_root(std::move(load_root_)),
12 sysnand_cache(std::make_shared<RegisteredCache>( 13 sysnand_cache(std::make_shared<RegisteredCache>(
13 GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))), 14 GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))),
14 usrnand_cache(std::make_shared<RegisteredCache>( 15 usrnand_cache(std::make_shared<RegisteredCache>(
@@ -24,4 +25,11 @@ std::shared_ptr<RegisteredCache> BISFactory::GetUserNANDContents() const {
24 return usrnand_cache; 25 return usrnand_cache;
25} 26}
26 27
28VirtualDir BISFactory::GetModificationLoadRoot(u64 title_id) const {
29 // LayeredFS doesn't work on updates and title id-less homebrew
30 if (title_id == 0 || (title_id & 0x800) > 0)
31 return nullptr;
32 return GetOrCreateDirectoryRelative(load_root, fmt::format("/{:016X}", title_id));
33}
34
27} // namespace FileSys 35} // namespace FileSys
diff --git a/src/core/file_sys/bis_factory.h b/src/core/file_sys/bis_factory.h
index 9523dd864..c352e0925 100644
--- a/src/core/file_sys/bis_factory.h
+++ b/src/core/file_sys/bis_factory.h
@@ -17,14 +17,17 @@ class RegisteredCache;
17/// registered caches. 17/// registered caches.
18class BISFactory { 18class BISFactory {
19public: 19public:
20 explicit BISFactory(VirtualDir nand_root); 20 explicit BISFactory(VirtualDir nand_root, VirtualDir load_root);
21 ~BISFactory(); 21 ~BISFactory();
22 22
23 std::shared_ptr<RegisteredCache> GetSystemNANDContents() const; 23 std::shared_ptr<RegisteredCache> GetSystemNANDContents() const;
24 std::shared_ptr<RegisteredCache> GetUserNANDContents() const; 24 std::shared_ptr<RegisteredCache> GetUserNANDContents() const;
25 25
26 VirtualDir GetModificationLoadRoot(u64 title_id) const;
27
26private: 28private:
27 VirtualDir nand_root; 29 VirtualDir nand_root;
30 VirtualDir load_root;
28 31
29 std::shared_ptr<RegisteredCache> sysnand_cache; 32 std::shared_ptr<RegisteredCache> sysnand_cache;
30 std::shared_ptr<RegisteredCache> usrnand_cache; 33 std::shared_ptr<RegisteredCache> usrnand_cache;
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp
index 45fc0b574..aa1b3c17d 100644
--- a/src/core/file_sys/content_archive.cpp
+++ b/src/core/file_sys/content_archive.cpp
@@ -463,6 +463,8 @@ NCA::NCA(VirtualFile file_, VirtualFile bktr_base_romfs_, u64 bktr_base_ivfc_off
463 status = Loader::ResultStatus::Success; 463 status = Loader::ResultStatus::Success;
464} 464}
465 465
466NCA::~NCA() = default;
467
466Loader::ResultStatus NCA::GetStatus() const { 468Loader::ResultStatus NCA::GetStatus() const {
467 return status; 469 return status;
468} 470}
diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h
index 00eca52da..f9f66cae9 100644
--- a/src/core/file_sys/content_archive.h
+++ b/src/core/file_sys/content_archive.h
@@ -81,6 +81,8 @@ class NCA : public ReadOnlyVfsDirectory {
81public: 81public:
82 explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr, 82 explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr,
83 u64 bktr_base_ivfc_offset = 0); 83 u64 bktr_base_ivfc_offset = 0);
84 ~NCA() override;
85
84 Loader::ResultStatus GetStatus() const; 86 Loader::ResultStatus GetStatus() const;
85 87
86 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; 88 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
diff --git a/src/core/file_sys/control_metadata.cpp b/src/core/file_sys/control_metadata.cpp
index e76bf77bf..5b1177a03 100644
--- a/src/core/file_sys/control_metadata.cpp
+++ b/src/core/file_sys/control_metadata.cpp
@@ -8,6 +8,14 @@
8 8
9namespace FileSys { 9namespace FileSys {
10 10
11const std::array<const char*, 15> LANGUAGE_NAMES = {
12 "AmericanEnglish", "BritishEnglish", "Japanese",
13 "French", "German", "LatinAmericanSpanish",
14 "Spanish", "Italian", "Dutch",
15 "CanadianFrench", "Portugese", "Russian",
16 "Korean", "Taiwanese", "Chinese",
17};
18
11std::string LanguageEntry::GetApplicationName() const { 19std::string LanguageEntry::GetApplicationName() const {
12 return Common::StringFromFixedZeroTerminatedBuffer(application_name.data(), 0x200); 20 return Common::StringFromFixedZeroTerminatedBuffer(application_name.data(), 0x200);
13} 21}
@@ -20,18 +28,20 @@ NACP::NACP(VirtualFile file) : raw(std::make_unique<RawNACP>()) {
20 file->ReadObject(raw.get()); 28 file->ReadObject(raw.get());
21} 29}
22 30
31NACP::~NACP() = default;
32
23const LanguageEntry& NACP::GetLanguageEntry(Language language) const { 33const LanguageEntry& NACP::GetLanguageEntry(Language language) const {
24 if (language != Language::Default) { 34 if (language != Language::Default) {
25 return raw->language_entries.at(static_cast<u8>(language)); 35 return raw->language_entries.at(static_cast<u8>(language));
26 } else {
27 for (const auto& language_entry : raw->language_entries) {
28 if (!language_entry.GetApplicationName().empty())
29 return language_entry;
30 }
31
32 // Fallback to English
33 return GetLanguageEntry(Language::AmericanEnglish);
34 } 36 }
37
38 for (const auto& language_entry : raw->language_entries) {
39 if (!language_entry.GetApplicationName().empty())
40 return language_entry;
41 }
42
43 // Fallback to English
44 return GetLanguageEntry(Language::AmericanEnglish);
35} 45}
36 46
37std::string NACP::GetApplicationName(Language language) const { 47std::string NACP::GetApplicationName(Language language) const {
diff --git a/src/core/file_sys/control_metadata.h b/src/core/file_sys/control_metadata.h
index 8a510bf46..43d6f0719 100644
--- a/src/core/file_sys/control_metadata.h
+++ b/src/core/file_sys/control_metadata.h
@@ -66,18 +66,15 @@ enum class Language : u8 {
66 Default = 255, 66 Default = 255,
67}; 67};
68 68
69static constexpr std::array<const char*, 15> LANGUAGE_NAMES = { 69extern const std::array<const char*, 15> LANGUAGE_NAMES;
70 "AmericanEnglish", "BritishEnglish", "Japanese",
71 "French", "German", "LatinAmericanSpanish",
72 "Spanish", "Italian", "Dutch",
73 "CanadianFrench", "Portugese", "Russian",
74 "Korean", "Taiwanese", "Chinese"};
75 70
76// A class representing the format used by NX metadata files, typically named Control.nacp. 71// A class representing the format used by NX metadata files, typically named Control.nacp.
77// These store application name, dev name, title id, and other miscellaneous data. 72// These store application name, dev name, title id, and other miscellaneous data.
78class NACP { 73class NACP {
79public: 74public:
80 explicit NACP(VirtualFile file); 75 explicit NACP(VirtualFile file);
76 ~NACP();
77
81 const LanguageEntry& GetLanguageEntry(Language language = Language::Default) const; 78 const LanguageEntry& GetLanguageEntry(Language language = Language::Default) const;
82 std::string GetApplicationName(Language language = Language::Default) const; 79 std::string GetApplicationName(Language language = Language::Default) const;
83 std::string GetDeveloperName(Language language = Language::Default) const; 80 std::string GetDeveloperName(Language language = Language::Default) const;
diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp
new file mode 100644
index 000000000..2a913ce82
--- /dev/null
+++ b/src/core/file_sys/fsmitm_romfsbuild.cpp
@@ -0,0 +1,366 @@
1/*
2 * Copyright (c) 2018 Atmosphère-NX
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/*
18 * Adapted by DarkLordZach for use/interaction with yuzu
19 *
20 * Modifications Copyright 2018 yuzu emulator team
21 * Licensed under GPLv2 or any later version
22 * Refer to the license.txt file included.
23 */
24
25#include <cstring>
26#include "common/alignment.h"
27#include "common/assert.h"
28#include "core/file_sys/fsmitm_romfsbuild.h"
29#include "core/file_sys/vfs.h"
30#include "core/file_sys/vfs_vector.h"
31
32namespace FileSys {
33
34constexpr u64 FS_MAX_PATH = 0x301;
35
36constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
37constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
38
39// Types for building a RomFS.
40struct RomFSHeader {
41 u64 header_size;
42 u64 dir_hash_table_ofs;
43 u64 dir_hash_table_size;
44 u64 dir_table_ofs;
45 u64 dir_table_size;
46 u64 file_hash_table_ofs;
47 u64 file_hash_table_size;
48 u64 file_table_ofs;
49 u64 file_table_size;
50 u64 file_partition_ofs;
51};
52static_assert(sizeof(RomFSHeader) == 0x50, "RomFSHeader has incorrect size.");
53
54struct RomFSDirectoryEntry {
55 u32 parent;
56 u32 sibling;
57 u32 child;
58 u32 file;
59 u32 hash;
60 u32 name_size;
61};
62static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
63
64struct RomFSFileEntry {
65 u32 parent;
66 u32 sibling;
67 u64 offset;
68 u64 size;
69 u32 hash;
70 u32 name_size;
71};
72static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
73
74struct RomFSBuildFileContext;
75
76struct RomFSBuildDirectoryContext {
77 std::string path;
78 u32 cur_path_ofs = 0;
79 u32 path_len = 0;
80 u32 entry_offset = 0;
81 std::shared_ptr<RomFSBuildDirectoryContext> parent;
82 std::shared_ptr<RomFSBuildDirectoryContext> child;
83 std::shared_ptr<RomFSBuildDirectoryContext> sibling;
84 std::shared_ptr<RomFSBuildFileContext> file;
85};
86
87struct RomFSBuildFileContext {
88 std::string path;
89 u32 cur_path_ofs = 0;
90 u32 path_len = 0;
91 u32 entry_offset = 0;
92 u64 offset = 0;
93 u64 size = 0;
94 std::shared_ptr<RomFSBuildDirectoryContext> parent;
95 std::shared_ptr<RomFSBuildFileContext> sibling;
96 VirtualFile source;
97};
98
99static u32 romfs_calc_path_hash(u32 parent, std::string path, u32 start, std::size_t path_len) {
100 u32 hash = parent ^ 123456789;
101 for (u32 i = 0; i < path_len; i++) {
102 hash = (hash >> 5) | (hash << 27);
103 hash ^= path[start + i];
104 }
105
106 return hash;
107}
108
109static u64 romfs_get_hash_table_count(u64 num_entries) {
110 if (num_entries < 3) {
111 return 3;
112 }
113
114 if (num_entries < 19) {
115 return num_entries | 1;
116 }
117
118 u64 count = num_entries;
119 while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
120 count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
121 count++;
122 }
123 return count;
124}
125
126void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs,
127 std::shared_ptr<RomFSBuildDirectoryContext> parent) {
128 std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
129
130 VirtualDir dir;
131
132 if (parent->path_len == 0)
133 dir = root_romfs;
134 else
135 dir = root_romfs->GetDirectoryRelative(parent->path);
136
137 const auto entries = dir->GetEntries();
138
139 for (const auto& kv : entries) {
140 if (kv.second == VfsEntryType::Directory) {
141 const auto child = std::make_shared<RomFSBuildDirectoryContext>();
142 // Set child's path.
143 child->cur_path_ofs = parent->path_len + 1;
144 child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
145 child->path = parent->path + "/" + kv.first;
146
147 // Sanity check on path_len
148 ASSERT(child->path_len < FS_MAX_PATH);
149
150 if (AddDirectory(parent, child)) {
151 child_dirs.push_back(child);
152 }
153 } else {
154 const auto child = std::make_shared<RomFSBuildFileContext>();
155 // Set child's path.
156 child->cur_path_ofs = parent->path_len + 1;
157 child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
158 child->path = parent->path + "/" + kv.first;
159
160 // Sanity check on path_len
161 ASSERT(child->path_len < FS_MAX_PATH);
162
163 child->source = root_romfs->GetFileRelative(child->path);
164
165 child->size = child->source->GetSize();
166
167 AddFile(parent, child);
168 }
169 }
170
171 for (auto& child : child_dirs) {
172 this->VisitDirectory(root_romfs, child);
173 }
174}
175
176bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
177 std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
178 // Check whether it's already in the known directories.
179 const auto existing = directories.find(dir_ctx->path);
180 if (existing != directories.end())
181 return false;
182
183 // Add a new directory.
184 num_dirs++;
185 dir_table_size +=
186 sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
187 dir_ctx->parent = parent_dir_ctx;
188 directories.emplace(dir_ctx->path, dir_ctx);
189
190 return true;
191}
192
193bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
194 std::shared_ptr<RomFSBuildFileContext> file_ctx) {
195 // Check whether it's already in the known files.
196 const auto existing = files.find(file_ctx->path);
197 if (existing != files.end()) {
198 return false;
199 }
200
201 // Add a new file.
202 num_files++;
203 file_table_size +=
204 sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
205 file_ctx->parent = parent_dir_ctx;
206 files.emplace(file_ctx->path, file_ctx);
207
208 return true;
209}
210
211RomFSBuildContext::RomFSBuildContext(VirtualDir base_) : base(std::move(base_)) {
212 root = std::make_shared<RomFSBuildDirectoryContext>();
213 root->path = "\0";
214 directories.emplace(root->path, root);
215 num_dirs = 1;
216 dir_table_size = 0x18;
217
218 VisitDirectory(base, root);
219}
220
221RomFSBuildContext::~RomFSBuildContext() = default;
222
223std::map<u64, VirtualFile> RomFSBuildContext::Build() {
224 const u64 dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
225 const u64 file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
226 dir_hash_table_size = 4 * dir_hash_table_entry_count;
227 file_hash_table_size = 4 * file_hash_table_entry_count;
228
229 // Assign metadata pointers
230 RomFSHeader header{};
231
232 std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
233 std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
234
235 std::vector<u8> dir_table(dir_table_size);
236 std::vector<u8> file_table(file_table_size);
237
238 std::shared_ptr<RomFSBuildFileContext> cur_file;
239
240 // Determine file offsets.
241 u32 entry_offset = 0;
242 std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
243 for (const auto& it : files) {
244 cur_file = it.second;
245 file_partition_size = Common::AlignUp(file_partition_size, 16);
246 cur_file->offset = file_partition_size;
247 file_partition_size += cur_file->size;
248 cur_file->entry_offset = entry_offset;
249 entry_offset += sizeof(RomFSFileEntry) +
250 Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4);
251 prev_file = cur_file;
252 }
253 // Assign deferred parent/sibling ownership.
254 for (auto it = files.rbegin(); it != files.rend(); ++it) {
255 cur_file = it->second;
256 cur_file->sibling = cur_file->parent->file;
257 cur_file->parent->file = cur_file;
258 }
259
260 std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
261
262 // Determine directory offsets.
263 entry_offset = 0;
264 for (const auto& it : directories) {
265 cur_dir = it.second;
266 cur_dir->entry_offset = entry_offset;
267 entry_offset += sizeof(RomFSDirectoryEntry) +
268 Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4);
269 }
270 // Assign deferred parent/sibling ownership.
271 for (auto it = directories.rbegin(); it->second != root; ++it) {
272 cur_dir = it->second;
273 cur_dir->sibling = cur_dir->parent->child;
274 cur_dir->parent->child = cur_dir;
275 }
276
277 std::map<u64, VirtualFile> out;
278
279 // Populate file tables.
280 for (const auto& it : files) {
281 cur_file = it.second;
282 RomFSFileEntry cur_entry{};
283
284 cur_entry.parent = cur_file->parent->entry_offset;
285 cur_entry.sibling =
286 cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
287 cur_entry.offset = cur_file->offset;
288 cur_entry.size = cur_file->size;
289
290 const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
291 const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
292 cur_file->cur_path_ofs, name_size);
293 cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
294 file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
295
296 cur_entry.name_size = name_size;
297
298 out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
299 std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
300 std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
301 Common::AlignUp(cur_entry.name_size, 4));
302 std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
303 cur_file->path.data() + cur_file->cur_path_ofs, name_size);
304 }
305
306 // Populate dir tables.
307 for (const auto& it : directories) {
308 cur_dir = it.second;
309 RomFSDirectoryEntry cur_entry{};
310
311 cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
312 cur_entry.sibling =
313 cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
314 cur_entry.child =
315 cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
316 cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
317
318 const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
319 const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
320 cur_dir->path, cur_dir->cur_path_ofs, name_size);
321 cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
322 dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
323
324 cur_entry.name_size = name_size;
325
326 std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
327 sizeof(RomFSDirectoryEntry));
328 std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
329 Common::AlignUp(cur_entry.name_size, 4));
330 std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
331 cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
332 }
333
334 // Set header fields.
335 header.header_size = sizeof(RomFSHeader);
336 header.file_hash_table_size = file_hash_table_size;
337 header.file_table_size = file_table_size;
338 header.dir_hash_table_size = dir_hash_table_size;
339 header.dir_table_size = dir_table_size;
340 header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
341 header.dir_hash_table_ofs = Common::AlignUp(header.file_partition_ofs + file_partition_size, 4);
342 header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
343 header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
344 header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
345
346 std::vector<u8> header_data(sizeof(RomFSHeader));
347 std::memcpy(header_data.data(), &header, header_data.size());
348 out.emplace(0, std::make_shared<VectorVfsFile>(std::move(header_data)));
349
350 std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
351 dir_table_size);
352 std::size_t index = 0;
353 std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
354 index += dir_hash_table.size() * sizeof(u32);
355 std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
356 index += dir_table.size();
357 std::memcpy(metadata.data() + index, file_hash_table.data(),
358 file_hash_table.size() * sizeof(u32));
359 index += file_hash_table.size() * sizeof(u32);
360 std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
361 out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(std::move(metadata)));
362
363 return out;
364}
365
366} // namespace FileSys
diff --git a/src/core/file_sys/fsmitm_romfsbuild.h b/src/core/file_sys/fsmitm_romfsbuild.h
new file mode 100644
index 000000000..b0c3c123b
--- /dev/null
+++ b/src/core/file_sys/fsmitm_romfsbuild.h
@@ -0,0 +1,70 @@
1/*
2 * Copyright (c) 2018 Atmosphère-NX
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/*
18 * Adapted by DarkLordZach for use/interaction with yuzu
19 *
20 * Modifications Copyright 2018 yuzu emulator team
21 * Licensed under GPLv2 or any later version
22 * Refer to the license.txt file included.
23 */
24
25#pragma once
26
27#include <map>
28#include <memory>
29#include <string>
30#include <boost/detail/container_fwd.hpp>
31#include "common/common_types.h"
32#include "core/file_sys/vfs.h"
33
34namespace FileSys {
35
36struct RomFSBuildDirectoryContext;
37struct RomFSBuildFileContext;
38struct RomFSDirectoryEntry;
39struct RomFSFileEntry;
40
41class RomFSBuildContext {
42public:
43 explicit RomFSBuildContext(VirtualDir base);
44 ~RomFSBuildContext();
45
46 // This finalizes the context.
47 std::map<u64, VirtualFile> Build();
48
49private:
50 VirtualDir base;
51 std::shared_ptr<RomFSBuildDirectoryContext> root;
52 std::map<std::string, std::shared_ptr<RomFSBuildDirectoryContext>, std::less<>> directories;
53 std::map<std::string, std::shared_ptr<RomFSBuildFileContext>, std::less<>> files;
54 u64 num_dirs = 0;
55 u64 num_files = 0;
56 u64 dir_table_size = 0;
57 u64 file_table_size = 0;
58 u64 dir_hash_table_size = 0;
59 u64 file_hash_table_size = 0;
60 u64 file_partition_size = 0;
61
62 void VisitDirectory(VirtualDir filesys, std::shared_ptr<RomFSBuildDirectoryContext> parent);
63
64 bool AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
65 std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx);
66 bool AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
67 std::shared_ptr<RomFSBuildFileContext> file_ctx);
68};
69
70} // namespace FileSys
diff --git a/src/core/file_sys/nca_metadata.cpp b/src/core/file_sys/nca_metadata.cpp
index 479916b69..6f34b7836 100644
--- a/src/core/file_sys/nca_metadata.cpp
+++ b/src/core/file_sys/nca_metadata.cpp
@@ -51,6 +51,8 @@ CNMT::CNMT(CNMTHeader header, OptionalHeader opt_header, std::vector<ContentReco
51 : header(std::move(header)), opt_header(std::move(opt_header)), 51 : header(std::move(header)), opt_header(std::move(opt_header)),
52 content_records(std::move(content_records)), meta_records(std::move(meta_records)) {} 52 content_records(std::move(content_records)), meta_records(std::move(meta_records)) {}
53 53
54CNMT::~CNMT() = default;
55
54u64 CNMT::GetTitleID() const { 56u64 CNMT::GetTitleID() const {
55 return header.title_id; 57 return header.title_id;
56} 58}
diff --git a/src/core/file_sys/nca_metadata.h b/src/core/file_sys/nca_metadata.h
index da5a8dbe8..a05d155f4 100644
--- a/src/core/file_sys/nca_metadata.h
+++ b/src/core/file_sys/nca_metadata.h
@@ -87,6 +87,7 @@ public:
87 explicit CNMT(VirtualFile file); 87 explicit CNMT(VirtualFile file);
88 CNMT(CNMTHeader header, OptionalHeader opt_header, std::vector<ContentRecord> content_records, 88 CNMT(CNMTHeader header, OptionalHeader opt_header, std::vector<ContentRecord> content_records,
89 std::vector<MetaRecord> meta_records); 89 std::vector<MetaRecord> meta_records);
90 ~CNMT();
90 91
91 u64 GetTitleID() const; 92 u64 GetTitleID() const;
92 u32 GetTitleVersion() const; 93 u32 GetTitleVersion() const;
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp
index f5b3b0175..5791c76ff 100644
--- a/src/core/file_sys/partition_filesystem.cpp
+++ b/src/core/file_sys/partition_filesystem.cpp
@@ -72,6 +72,8 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
72 status = Loader::ResultStatus::Success; 72 status = Loader::ResultStatus::Success;
73} 73}
74 74
75PartitionFilesystem::~PartitionFilesystem() = default;
76
75Loader::ResultStatus PartitionFilesystem::GetStatus() const { 77Loader::ResultStatus PartitionFilesystem::GetStatus() const {
76 return status; 78 return status;
77} 79}
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h
index e80d2456b..739c63a7f 100644
--- a/src/core/file_sys/partition_filesystem.h
+++ b/src/core/file_sys/partition_filesystem.h
@@ -25,6 +25,8 @@ namespace FileSys {
25class PartitionFilesystem : public ReadOnlyVfsDirectory { 25class PartitionFilesystem : public ReadOnlyVfsDirectory {
26public: 26public:
27 explicit PartitionFilesystem(std::shared_ptr<VfsFile> file); 27 explicit PartitionFilesystem(std::shared_ptr<VfsFile> file);
28 ~PartitionFilesystem() override;
29
28 Loader::ResultStatus GetStatus() const; 30 Loader::ResultStatus GetStatus() const;
29 31
30 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; 32 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp
index b37b4c68b..4b3b5e665 100644
--- a/src/core/file_sys/patch_manager.cpp
+++ b/src/core/file_sys/patch_manager.cpp
@@ -11,6 +11,7 @@
11#include "core/file_sys/patch_manager.h" 11#include "core/file_sys/patch_manager.h"
12#include "core/file_sys/registered_cache.h" 12#include "core/file_sys/registered_cache.h"
13#include "core/file_sys/romfs.h" 13#include "core/file_sys/romfs.h"
14#include "core/file_sys/vfs_layered.h"
14#include "core/hle/service/filesystem/filesystem.h" 15#include "core/hle/service/filesystem/filesystem.h"
15#include "core/loader/loader.h" 16#include "core/loader/loader.h"
16 17
@@ -31,8 +32,9 @@ std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
31 return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]); 32 return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
32} 33}
33 34
34constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{ 35constexpr std::array<const char*, 2> PATCH_TYPE_NAMES{
35 "Update", 36 "Update",
37 "LayeredFS",
36}; 38};
37 39
38std::string FormatPatchTypeName(PatchType type) { 40std::string FormatPatchTypeName(PatchType type) {
@@ -41,6 +43,8 @@ std::string FormatPatchTypeName(PatchType type) {
41 43
42PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} 44PatchManager::PatchManager(u64 title_id) : title_id(title_id) {}
43 45
46PatchManager::~PatchManager() = default;
47
44VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { 48VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
45 LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id); 49 LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id);
46 50
@@ -64,6 +68,44 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
64 return exefs; 68 return exefs;
65} 69}
66 70
71static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) {
72 const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id);
73 if (type != ContentRecordType::Program || load_dir == nullptr || load_dir->GetSize() <= 0) {
74 return;
75 }
76
77 auto extracted = ExtractRomFS(romfs);
78 if (extracted == nullptr) {
79 return;
80 }
81
82 auto patch_dirs = load_dir->GetSubdirectories();
83 std::sort(patch_dirs.begin(), patch_dirs.end(),
84 [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); });
85
86 std::vector<VirtualDir> layers;
87 layers.reserve(patch_dirs.size() + 1);
88 for (const auto& subdir : patch_dirs) {
89 auto romfs_dir = subdir->GetSubdirectory("romfs");
90 if (romfs_dir != nullptr)
91 layers.push_back(std::move(romfs_dir));
92 }
93 layers.push_back(std::move(extracted));
94
95 auto layered = LayeredVfsDirectory::MakeLayeredDirectory(std::move(layers));
96 if (layered == nullptr) {
97 return;
98 }
99
100 auto packed = CreateRomFS(std::move(layered));
101 if (packed == nullptr) {
102 return;
103 }
104
105 LOG_INFO(Loader, " RomFS: LayeredFS patches applied successfully");
106 romfs = std::move(packed);
107}
108
67VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, 109VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
68 ContentRecordType type) const { 110 ContentRecordType type) const {
69 LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id, 111 LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id,
@@ -87,6 +129,9 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
87 } 129 }
88 } 130 }
89 131
132 // LayeredFS
133 ApplyLayeredFS(romfs, title_id, type);
134
90 return romfs; 135 return romfs;
91} 136}
92 137
@@ -112,6 +157,10 @@ std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const {
112 } 157 }
113 } 158 }
114 159
160 const auto lfs_dir = Service::FileSystem::GetModificationLoadRoot(title_id);
161 if (lfs_dir != nullptr && lfs_dir->GetSize() > 0)
162 out.insert_or_assign(PatchType::LayeredFS, "");
163
115 return out; 164 return out;
116} 165}
117 166
diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h
index b521977b2..464f17515 100644
--- a/src/core/file_sys/patch_manager.h
+++ b/src/core/file_sys/patch_manager.h
@@ -26,6 +26,7 @@ std::string FormatTitleVersion(u32 version,
26 26
27enum class PatchType { 27enum class PatchType {
28 Update, 28 Update,
29 LayeredFS,
29}; 30};
30 31
31std::string FormatPatchTypeName(PatchType type); 32std::string FormatPatchTypeName(PatchType type);
@@ -34,6 +35,7 @@ std::string FormatPatchTypeName(PatchType type);
34class PatchManager { 35class PatchManager {
35public: 36public:
36 explicit PatchManager(u64 title_id); 37 explicit PatchManager(u64 title_id);
38 ~PatchManager();
37 39
38 // Currently tracked ExeFS patches: 40 // Currently tracked ExeFS patches:
39 // - Game Updates 41 // - Game Updates
@@ -41,6 +43,7 @@ public:
41 43
42 // Currently tracked RomFS patches: 44 // Currently tracked RomFS patches:
43 // - Game Updates 45 // - Game Updates
46 // - LayeredFS
44 VirtualFile PatchRomFS(VirtualFile base, u64 ivfc_offset, 47 VirtualFile PatchRomFS(VirtualFile base, u64 ivfc_offset,
45 ContentRecordType type = ContentRecordType::Program) const; 48 ContentRecordType type = ContentRecordType::Program) const;
46 49
diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp
index 9d19aaa6d..8903ed1d3 100644
--- a/src/core/file_sys/program_metadata.cpp
+++ b/src/core/file_sys/program_metadata.cpp
@@ -12,6 +12,10 @@
12 12
13namespace FileSys { 13namespace FileSys {
14 14
15ProgramMetadata::ProgramMetadata() = default;
16
17ProgramMetadata::~ProgramMetadata() = default;
18
15Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) { 19Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) {
16 std::size_t total_size = static_cast<std::size_t>(file->GetSize()); 20 std::size_t total_size = static_cast<std::size_t>(file->GetSize());
17 if (total_size < sizeof(Header)) 21 if (total_size < sizeof(Header))
@@ -79,10 +83,12 @@ void ProgramMetadata::Print() const {
79 83
80 auto address_space = "Unknown"; 84 auto address_space = "Unknown";
81 switch (npdm_header.address_space_type) { 85 switch (npdm_header.address_space_type) {
82 case ProgramAddressSpaceType::Is64Bit: 86 case ProgramAddressSpaceType::Is36Bit:
87 case ProgramAddressSpaceType::Is39Bit:
83 address_space = "64-bit"; 88 address_space = "64-bit";
84 break; 89 break;
85 case ProgramAddressSpaceType::Is32Bit: 90 case ProgramAddressSpaceType::Is32Bit:
91 case ProgramAddressSpaceType::Is32BitNoMap:
86 address_space = "32-bit"; 92 address_space = "32-bit";
87 break; 93 break;
88 } 94 }
diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h
index 3c0a49f16..e4470d6f0 100644
--- a/src/core/file_sys/program_metadata.h
+++ b/src/core/file_sys/program_metadata.h
@@ -17,8 +17,10 @@ enum class ResultStatus : u16;
17namespace FileSys { 17namespace FileSys {
18 18
19enum class ProgramAddressSpaceType : u8 { 19enum class ProgramAddressSpaceType : u8 {
20 Is64Bit = 1, 20 Is32Bit = 0,
21 Is32Bit = 2, 21 Is36Bit = 1,
22 Is32BitNoMap = 2,
23 Is39Bit = 3,
22}; 24};
23 25
24enum class ProgramFilePermission : u64 { 26enum class ProgramFilePermission : u64 {
@@ -36,6 +38,9 @@ enum class ProgramFilePermission : u64 {
36 */ 38 */
37class ProgramMetadata { 39class ProgramMetadata {
38public: 40public:
41 ProgramMetadata();
42 ~ProgramMetadata();
43
39 Loader::ResultStatus Load(VirtualFile file); 44 Loader::ResultStatus Load(VirtualFile file);
40 45
41 bool Is64BitProgram() const; 46 bool Is64BitProgram() const;
diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp
index dad7ae10b..e9b040689 100644
--- a/src/core/file_sys/registered_cache.cpp
+++ b/src/core/file_sys/registered_cache.cpp
@@ -18,6 +18,10 @@
18#include "core/loader/loader.h" 18#include "core/loader/loader.h"
19 19
20namespace FileSys { 20namespace FileSys {
21
22// The size of blocks to use when vfs raw copying into nand.
23constexpr size_t VFS_RC_LARGE_COPY_BLOCK = 0x400000;
24
21std::string RegisteredCacheEntry::DebugInfo() const { 25std::string RegisteredCacheEntry::DebugInfo() const {
22 return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type)); 26 return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type));
23} 27}
@@ -121,7 +125,7 @@ VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
121 if (concat.empty()) 125 if (concat.empty())
122 return nullptr; 126 return nullptr;
123 127
124 file = FileSys::ConcatenateFiles(concat); 128 file = ConcatenatedVfsFile::MakeConcatenatedFile(concat, concat.front()->GetName());
125 } 129 }
126 130
127 return file; 131 return file;
@@ -480,7 +484,8 @@ InstallResult RegisteredCache::RawInstallNCA(std::shared_ptr<NCA> nca, const Vfs
480 auto out = dir->CreateFileRelative(path); 484 auto out = dir->CreateFileRelative(path);
481 if (out == nullptr) 485 if (out == nullptr)
482 return InstallResult::ErrorCopyFailed; 486 return InstallResult::ErrorCopyFailed;
483 return copy(in, out) ? InstallResult::Success : InstallResult::ErrorCopyFailed; 487 return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
488 : InstallResult::ErrorCopyFailed;
484} 489}
485 490
486bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) { 491bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {
diff --git a/src/core/file_sys/registered_cache.h b/src/core/file_sys/registered_cache.h
index f487b0cf0..c0cd59fc5 100644
--- a/src/core/file_sys/registered_cache.h
+++ b/src/core/file_sys/registered_cache.h
@@ -27,7 +27,7 @@ struct ContentRecord;
27 27
28using NcaID = std::array<u8, 0x10>; 28using NcaID = std::array<u8, 0x10>;
29using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>; 29using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
30using VfsCopyFunction = std::function<bool(VirtualFile, VirtualFile)>; 30using VfsCopyFunction = std::function<bool(const VirtualFile&, const VirtualFile&, size_t)>;
31 31
32enum class InstallResult { 32enum class InstallResult {
33 Success, 33 Success,
diff --git a/src/core/file_sys/romfs.cpp b/src/core/file_sys/romfs.cpp
index 9f6e41cdf..5910f7046 100644
--- a/src/core/file_sys/romfs.cpp
+++ b/src/core/file_sys/romfs.cpp
@@ -4,8 +4,10 @@
4 4
5#include "common/common_types.h" 5#include "common/common_types.h"
6#include "common/swap.h" 6#include "common/swap.h"
7#include "core/file_sys/fsmitm_romfsbuild.h"
7#include "core/file_sys/romfs.h" 8#include "core/file_sys/romfs.h"
8#include "core/file_sys/vfs.h" 9#include "core/file_sys/vfs.h"
10#include "core/file_sys/vfs_concat.h"
9#include "core/file_sys/vfs_offset.h" 11#include "core/file_sys/vfs_offset.h"
10#include "core/file_sys/vfs_vector.h" 12#include "core/file_sys/vfs_vector.h"
11 13
@@ -98,7 +100,7 @@ void ProcessDirectory(VirtualFile file, std::size_t dir_offset, std::size_t file
98 } 100 }
99} 101}
100 102
101VirtualDir ExtractRomFS(VirtualFile file) { 103VirtualDir ExtractRomFS(VirtualFile file, RomFSExtractionType type) {
102 RomFSHeader header{}; 104 RomFSHeader header{};
103 if (file->ReadObject(&header) != sizeof(RomFSHeader)) 105 if (file->ReadObject(&header) != sizeof(RomFSHeader))
104 return nullptr; 106 return nullptr;
@@ -117,9 +119,22 @@ VirtualDir ExtractRomFS(VirtualFile file) {
117 119
118 VirtualDir out = std::move(root); 120 VirtualDir out = std::move(root);
119 121
120 while (out->GetSubdirectory("") != nullptr) 122 while (out->GetSubdirectories().size() == 1 && out->GetFiles().empty()) {
121 out = out->GetSubdirectory(""); 123 if (out->GetSubdirectories().front()->GetName() == "data" &&
124 type == RomFSExtractionType::Truncated)
125 break;
126 out = out->GetSubdirectories().front();
127 }
122 128
123 return out; 129 return out;
124} 130}
131
132VirtualFile CreateRomFS(VirtualDir dir) {
133 if (dir == nullptr)
134 return nullptr;
135
136 RomFSBuildContext ctx{dir};
137 return ConcatenatedVfsFile::MakeConcatenatedFile(0, ctx.Build(), dir->GetName());
138}
139
125} // namespace FileSys 140} // namespace FileSys
diff --git a/src/core/file_sys/romfs.h b/src/core/file_sys/romfs.h
index e54a7d7a9..ecd1eb725 100644
--- a/src/core/file_sys/romfs.h
+++ b/src/core/file_sys/romfs.h
@@ -5,6 +5,7 @@
5#pragma once 5#pragma once
6 6
7#include <array> 7#include <array>
8#include <map>
8#include "common/common_funcs.h" 9#include "common/common_funcs.h"
9#include "common/common_types.h" 10#include "common/common_types.h"
10#include "common/swap.h" 11#include "common/swap.h"
@@ -12,6 +13,8 @@
12 13
13namespace FileSys { 14namespace FileSys {
14 15
16struct RomFSHeader;
17
15struct IVFCLevel { 18struct IVFCLevel {
16 u64_le offset; 19 u64_le offset;
17 u64_le size; 20 u64_le size;
@@ -29,8 +32,18 @@ struct IVFCHeader {
29}; 32};
30static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size."); 33static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size.");
31 34
35enum class RomFSExtractionType {
36 Full, // Includes data directory
37 Truncated, // Traverses into data directory
38};
39
32// Converts a RomFS binary blob to VFS Filesystem 40// Converts a RomFS binary blob to VFS Filesystem
33// Returns nullptr on failure 41// Returns nullptr on failure
34VirtualDir ExtractRomFS(VirtualFile file); 42VirtualDir ExtractRomFS(VirtualFile file,
43 RomFSExtractionType type = RomFSExtractionType::Truncated);
44
45// Converts a VFS filesystem into a RomFS binary
46// Returns nullptr on failure
47VirtualFile CreateRomFS(VirtualDir dir);
35 48
36} // namespace FileSys 49} // namespace FileSys
diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp
index d9d90939e..d027a8d59 100644
--- a/src/core/file_sys/romfs_factory.cpp
+++ b/src/core/file_sys/romfs_factory.cpp
@@ -28,11 +28,13 @@ RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) {
28 ivfc_offset = app_loader.ReadRomFSIVFCOffset(); 28 ivfc_offset = app_loader.ReadRomFSIVFCOffset();
29} 29}
30 30
31RomFSFactory::~RomFSFactory() = default;
32
31ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess() { 33ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess() {
32 if (!updatable) 34 if (!updatable)
33 return MakeResult<VirtualFile>(file); 35 return MakeResult<VirtualFile>(file);
34 36
35 const PatchManager patch_manager(Core::CurrentProcess()->program_id); 37 const PatchManager patch_manager(Core::CurrentProcess()->GetTitleID());
36 return MakeResult<VirtualFile>(patch_manager.PatchRomFS(file, ivfc_offset)); 38 return MakeResult<VirtualFile>(patch_manager.PatchRomFS(file, ivfc_offset));
37} 39}
38 40
diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h
index 26b8f46cc..2cace8180 100644
--- a/src/core/file_sys/romfs_factory.h
+++ b/src/core/file_sys/romfs_factory.h
@@ -30,6 +30,7 @@ enum class StorageId : u8 {
30class RomFSFactory { 30class RomFSFactory {
31public: 31public:
32 explicit RomFSFactory(Loader::AppLoader& app_loader); 32 explicit RomFSFactory(Loader::AppLoader& app_loader);
33 ~RomFSFactory();
33 34
34 ResultVal<VirtualFile> OpenCurrentProcess(); 35 ResultVal<VirtualFile> OpenCurrentProcess();
35 ResultVal<VirtualFile> Open(u64 title_id, StorageId storage, ContentRecordType type); 36 ResultVal<VirtualFile> Open(u64 title_id, StorageId storage, ContentRecordType type);
diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp
index e437d34e5..47f2ab9e0 100644
--- a/src/core/file_sys/savedata_factory.cpp
+++ b/src/core/file_sys/savedata_factory.cpp
@@ -20,6 +20,8 @@ std::string SaveDataDescriptor::DebugInfo() const {
20 20
21SaveDataFactory::SaveDataFactory(VirtualDir save_directory) : dir(std::move(save_directory)) {} 21SaveDataFactory::SaveDataFactory(VirtualDir save_directory) : dir(std::move(save_directory)) {}
22 22
23SaveDataFactory::~SaveDataFactory() = default;
24
23ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space, SaveDataDescriptor meta) { 25ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space, SaveDataDescriptor meta) {
24 if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) { 26 if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) {
25 if (meta.zero_1 != 0) { 27 if (meta.zero_1 != 0) {
@@ -79,16 +81,16 @@ std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType typ
79 // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should 81 // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should
80 // be interpreted as the title id of the current process. 82 // be interpreted as the title id of the current process.
81 if (type == SaveDataType::SaveData && title_id == 0) 83 if (type == SaveDataType::SaveData && title_id == 0)
82 title_id = Core::CurrentProcess()->program_id; 84 title_id = Core::CurrentProcess()->GetTitleID();
83 85
84 std::string out; 86 std::string out;
85 87
86 switch (space) { 88 switch (space) {
87 case SaveDataSpaceId::NandSystem: 89 case SaveDataSpaceId::NandSystem:
88 out = "/system/save/"; 90 out = "/system/";
89 break; 91 break;
90 case SaveDataSpaceId::NandUser: 92 case SaveDataSpaceId::NandUser:
91 out = "/user/save/"; 93 out = "/user/";
92 break; 94 break;
93 default: 95 default:
94 ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space)); 96 ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
@@ -96,9 +98,12 @@ std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType typ
96 98
97 switch (type) { 99 switch (type) {
98 case SaveDataType::SystemSaveData: 100 case SaveDataType::SystemSaveData:
99 return fmt::format("{}{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]); 101 return fmt::format("{}save/{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]);
100 case SaveDataType::SaveData: 102 case SaveDataType::SaveData:
101 return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0], 103 return fmt::format("{}save/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
104 title_id);
105 case SaveDataType::TemporaryStorage:
106 return fmt::format("{}temp/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
102 title_id); 107 title_id);
103 default: 108 default:
104 ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type)); 109 ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h
index ba978695b..d69ef6741 100644
--- a/src/core/file_sys/savedata_factory.h
+++ b/src/core/file_sys/savedata_factory.h
@@ -48,6 +48,7 @@ static_assert(sizeof(SaveDataDescriptor) == 0x40, "SaveDataDescriptor has incorr
48class SaveDataFactory { 48class SaveDataFactory {
49public: 49public:
50 explicit SaveDataFactory(VirtualDir dir); 50 explicit SaveDataFactory(VirtualDir dir);
51 ~SaveDataFactory();
51 52
52 ResultVal<VirtualDir> Open(SaveDataSpaceId space, SaveDataDescriptor meta); 53 ResultVal<VirtualDir> Open(SaveDataSpaceId space, SaveDataDescriptor meta);
53 54
diff --git a/src/core/file_sys/submission_package.h b/src/core/file_sys/submission_package.h
index 1120a4920..e85a2b76e 100644
--- a/src/core/file_sys/submission_package.h
+++ b/src/core/file_sys/submission_package.h
@@ -24,7 +24,7 @@ enum class ContentRecordType : u8;
24class NSP : public ReadOnlyVfsDirectory { 24class NSP : public ReadOnlyVfsDirectory {
25public: 25public:
26 explicit NSP(VirtualFile file); 26 explicit NSP(VirtualFile file);
27 ~NSP(); 27 ~NSP() override;
28 28
29 Loader::ResultStatus GetStatus() const; 29 Loader::ResultStatus GetStatus() const;
30 Loader::ResultStatus GetProgramStatus(u64 title_id) const; 30 Loader::ResultStatus GetProgramStatus(u64 title_id) const;
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp
index d7b52abfd..bfe50da73 100644
--- a/src/core/file_sys/vfs.cpp
+++ b/src/core/file_sys/vfs.cpp
@@ -399,6 +399,15 @@ bool VfsDirectory::Copy(std::string_view src, std::string_view dest) {
399 return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize(); 399 return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize();
400} 400}
401 401
402std::map<std::string, VfsEntryType, std::less<>> VfsDirectory::GetEntries() const {
403 std::map<std::string, VfsEntryType, std::less<>> out;
404 for (const auto& dir : GetSubdirectories())
405 out.emplace(dir->GetName(), VfsEntryType::Directory);
406 for (const auto& file : GetFiles())
407 out.emplace(file->GetName(), VfsEntryType::File);
408 return out;
409}
410
402std::string VfsDirectory::GetFullPath() const { 411std::string VfsDirectory::GetFullPath() const {
403 if (IsRoot()) 412 if (IsRoot())
404 return GetName(); 413 return GetName();
@@ -454,13 +463,41 @@ bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t
454 return true; 463 return true;
455} 464}
456 465
457bool VfsRawCopy(VirtualFile src, VirtualFile dest) { 466bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, std::size_t block_size) {
458 if (src == nullptr || dest == nullptr) 467 if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
459 return false; 468 return false;
460 if (!dest->Resize(src->GetSize())) 469 if (!dest->Resize(src->GetSize()))
461 return false; 470 return false;
462 std::vector<u8> data = src->ReadAllBytes(); 471
463 return dest->WriteBytes(data, 0) == data.size(); 472 std::vector<u8> temp(std::min(block_size, src->GetSize()));
473 for (std::size_t i = 0; i < src->GetSize(); i += block_size) {
474 const auto read = std::min(block_size, src->GetSize() - i);
475 const auto block = src->Read(temp.data(), read, i);
476
477 if (dest->Write(temp.data(), read, i) != read)
478 return false;
479 }
480
481 return true;
482}
483
484bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, std::size_t block_size) {
485 if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
486 return false;
487
488 for (const auto& file : src->GetFiles()) {
489 const auto out = dest->CreateFile(file->GetName());
490 if (!VfsRawCopy(file, out, block_size))
491 return false;
492 }
493
494 for (const auto& dir : src->GetSubdirectories()) {
495 const auto out = dest->CreateSubdirectory(dir->GetName());
496 if (!VfsRawCopyD(dir, out, block_size))
497 return false;
498 }
499
500 return true;
464} 501}
465 502
466VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) { 503VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) {
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h
index 74489b452..270291631 100644
--- a/src/core/file_sys/vfs.h
+++ b/src/core/file_sys/vfs.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <map>
7#include <memory> 8#include <memory>
8#include <string> 9#include <string>
9#include <string_view> 10#include <string_view>
@@ -265,6 +266,10 @@ public:
265 // dest. 266 // dest.
266 virtual bool Copy(std::string_view src, std::string_view dest); 267 virtual bool Copy(std::string_view src, std::string_view dest);
267 268
269 // Gets all of the entries directly in the directory (files and dirs), returning a map between
270 // item name -> type.
271 virtual std::map<std::string, VfsEntryType, std::less<>> GetEntries() const;
272
268 // Interprets the file with name file instead as a directory of type directory. 273 // Interprets the file with name file instead as a directory of type directory.
269 // The directory must have a constructor that takes a single argument of type 274 // The directory must have a constructor that takes a single argument of type
270 // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a 275 // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a
@@ -310,13 +315,19 @@ public:
310 bool Rename(std::string_view name) override; 315 bool Rename(std::string_view name) override;
311}; 316};
312 317
313// Compare the two files, byte-for-byte, in increments specificed by block_size 318// Compare the two files, byte-for-byte, in increments specified by block_size
314bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t block_size = 0x200); 319bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2,
320 std::size_t block_size = 0x1000);
315 321
316// A method that copies the raw data between two different implementations of VirtualFile. If you 322// A method that copies the raw data between two different implementations of VirtualFile. If you
317// are using the same implementation, it is probably better to use the Copy method in the parent 323// are using the same implementation, it is probably better to use the Copy method in the parent
318// directory of src/dest. 324// directory of src/dest.
319bool VfsRawCopy(VirtualFile src, VirtualFile dest); 325bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, std::size_t block_size = 0x1000);
326
327// A method that performs a similar function to VfsRawCopy above, but instead copies entire
328// directories. It suffers the same performance penalties as above and an implementation-specific
329// Copy should always be preferred.
330bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, std::size_t block_size = 0x1000);
320 331
321// Checks if the directory at path relative to rel exists. If it does, returns that. If it does not 332// Checks if the directory at path relative to rel exists. If it does, returns that. If it does not
322// it attempts to create it and returns the new dir or nullptr on failure. 333// it attempts to create it and returns the new dir or nullptr on failure.
diff --git a/src/core/file_sys/vfs_concat.cpp b/src/core/file_sys/vfs_concat.cpp
index 25a980cbb..16d801c0c 100644
--- a/src/core/file_sys/vfs_concat.cpp
+++ b/src/core/file_sys/vfs_concat.cpp
@@ -5,17 +5,22 @@
5#include <algorithm> 5#include <algorithm>
6#include <utility> 6#include <utility>
7 7
8#include "common/assert.h"
8#include "core/file_sys/vfs_concat.h" 9#include "core/file_sys/vfs_concat.h"
10#include "core/file_sys/vfs_static.h"
9 11
10namespace FileSys { 12namespace FileSys {
11 13
12VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name) { 14static bool VerifyConcatenationMapContinuity(const std::map<u64, VirtualFile>& map) {
13 if (files.empty()) 15 const auto last_valid = --map.end();
14 return nullptr; 16 for (auto iter = map.begin(); iter != last_valid;) {
15 if (files.size() == 1) 17 const auto old = iter++;
16 return files[0]; 18 if (old->first + old->second->GetSize() != iter->first) {
19 return false;
20 }
21 }
17 22
18 return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name))); 23 return map.begin()->first == 0;
19} 24}
20 25
21ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name) 26ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name)
@@ -27,6 +32,48 @@ ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::s
27 } 32 }
28} 33}
29 34
35ConcatenatedVfsFile::ConcatenatedVfsFile(std::map<u64, VirtualFile> files_, std::string name)
36 : files(std::move(files_)), name(std::move(name)) {
37 ASSERT(VerifyConcatenationMapContinuity(files));
38}
39
40ConcatenatedVfsFile::~ConcatenatedVfsFile() = default;
41
42VirtualFile ConcatenatedVfsFile::MakeConcatenatedFile(std::vector<VirtualFile> files,
43 std::string name) {
44 if (files.empty())
45 return nullptr;
46 if (files.size() == 1)
47 return files[0];
48
49 return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
50}
51
52VirtualFile ConcatenatedVfsFile::MakeConcatenatedFile(u8 filler_byte,
53 std::map<u64, VirtualFile> files,
54 std::string name) {
55 if (files.empty())
56 return nullptr;
57 if (files.size() == 1)
58 return files.begin()->second;
59
60 const auto last_valid = --files.end();
61 for (auto iter = files.begin(); iter != last_valid;) {
62 const auto old = iter++;
63 if (old->first + old->second->GetSize() != iter->first) {
64 files.emplace(old->first + old->second->GetSize(),
65 std::make_shared<StaticVfsFile>(filler_byte, iter->first - old->first -
66 old->second->GetSize()));
67 }
68 }
69
70 // Ensure the map starts at offset 0 (start of file), otherwise pad to fill.
71 if (files.begin()->first != 0)
72 files.emplace(0, std::make_shared<StaticVfsFile>(filler_byte, files.begin()->first));
73
74 return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
75}
76
30std::string ConcatenatedVfsFile::GetName() const { 77std::string ConcatenatedVfsFile::GetName() const {
31 if (files.empty()) 78 if (files.empty())
32 return ""; 79 return "";
@@ -60,7 +107,7 @@ bool ConcatenatedVfsFile::IsReadable() const {
60} 107}
61 108
62std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const { 109std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
63 auto entry = files.end(); 110 auto entry = --files.end();
64 for (auto iter = files.begin(); iter != files.end(); ++iter) { 111 for (auto iter = files.begin(); iter != files.end(); ++iter) {
65 if (iter->first > offset) { 112 if (iter->first > offset) {
66 entry = --iter; 113 entry = --iter;
@@ -68,20 +115,17 @@ std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t
68 } 115 }
69 } 116 }
70 117
71 // Check if the entry should be the last one. The loop above will make it end(). 118 if (entry->first + entry->second->GetSize() <= offset)
72 if (entry == files.end() && offset < files.rbegin()->first + files.rbegin()->second->GetSize())
73 --entry;
74
75 if (entry == files.end())
76 return 0; 119 return 0;
77 120
78 const auto remaining = entry->second->GetSize() + offset - entry->first; 121 const auto read_in =
79 if (length > remaining) { 122 std::min<u64>(entry->first + entry->second->GetSize() - offset, entry->second->GetSize());
80 return entry->second->Read(data, remaining, offset - entry->first) + 123 if (length > read_in) {
81 Read(data + remaining, length - remaining, offset + remaining); 124 return entry->second->Read(data, read_in, offset - entry->first) +
125 Read(data + read_in, length - read_in, offset + read_in);
82 } 126 }
83 127
84 return entry->second->Read(data, length, offset - entry->first); 128 return entry->second->Read(data, std::min<u64>(read_in, length), offset - entry->first);
85} 129}
86 130
87std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) { 131std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
@@ -91,4 +135,5 @@ std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::
91bool ConcatenatedVfsFile::Rename(std::string_view name) { 135bool ConcatenatedVfsFile::Rename(std::string_view name) {
92 return false; 136 return false;
93} 137}
138
94} // namespace FileSys 139} // namespace FileSys
diff --git a/src/core/file_sys/vfs_concat.h b/src/core/file_sys/vfs_concat.h
index 31775db7e..c90f9d5d1 100644
--- a/src/core/file_sys/vfs_concat.h
+++ b/src/core/file_sys/vfs_concat.h
@@ -4,24 +4,30 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <map>
7#include <memory> 8#include <memory>
8#include <string_view> 9#include <string_view>
9#include <boost/container/flat_map.hpp>
10#include "core/file_sys/vfs.h" 10#include "core/file_sys/vfs.h"
11 11
12namespace FileSys { 12namespace FileSys {
13 13
14// Wrapper function to allow for more efficient handling of files.size() == 0, 1 cases.
15VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name = "");
16
17// Class that wraps multiple vfs files and concatenates them, making reads seamless. Currently 14// Class that wraps multiple vfs files and concatenates them, making reads seamless. Currently
18// read-only. 15// read-only.
19class ConcatenatedVfsFile : public VfsFile { 16class ConcatenatedVfsFile : public VfsFile {
20 friend VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name);
21
22 ConcatenatedVfsFile(std::vector<VirtualFile> files, std::string name); 17 ConcatenatedVfsFile(std::vector<VirtualFile> files, std::string name);
18 ConcatenatedVfsFile(std::map<u64, VirtualFile> files, std::string name);
23 19
24public: 20public:
21 ~ConcatenatedVfsFile() override;
22
23 /// Wrapper function to allow for more efficient handling of files.size() == 0, 1 cases.
24 static VirtualFile MakeConcatenatedFile(std::vector<VirtualFile> files, std::string name);
25
26 /// Convenience function that turns a map of offsets to files into a concatenated file, filling
27 /// gaps with a given filler byte.
28 static VirtualFile MakeConcatenatedFile(u8 filler_byte, std::map<u64, VirtualFile> files,
29 std::string name);
30
25 std::string GetName() const override; 31 std::string GetName() const override;
26 std::size_t GetSize() const override; 32 std::size_t GetSize() const override;
27 bool Resize(std::size_t new_size) override; 33 bool Resize(std::size_t new_size) override;
@@ -34,7 +40,7 @@ public:
34 40
35private: 41private:
36 // Maps starting offset to file -- more efficient. 42 // Maps starting offset to file -- more efficient.
37 boost::container::flat_map<u64, VirtualFile> files; 43 std::map<u64, VirtualFile> files;
38 std::string name; 44 std::string name;
39}; 45};
40 46
diff --git a/src/core/file_sys/vfs_layered.cpp b/src/core/file_sys/vfs_layered.cpp
new file mode 100644
index 000000000..bfee01725
--- /dev/null
+++ b/src/core/file_sys/vfs_layered.cpp
@@ -0,0 +1,132 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <utility>
7#include "core/file_sys/vfs_layered.h"
8
9namespace FileSys {
10
11LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name)
12 : dirs(std::move(dirs)), name(std::move(name)) {}
13
14LayeredVfsDirectory::~LayeredVfsDirectory() = default;
15
16VirtualDir LayeredVfsDirectory::MakeLayeredDirectory(std::vector<VirtualDir> dirs,
17 std::string name) {
18 if (dirs.empty())
19 return nullptr;
20 if (dirs.size() == 1)
21 return dirs[0];
22
23 return std::shared_ptr<VfsDirectory>(new LayeredVfsDirectory(std::move(dirs), std::move(name)));
24}
25
26std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFileRelative(std::string_view path) const {
27 for (const auto& layer : dirs) {
28 const auto file = layer->GetFileRelative(path);
29 if (file != nullptr)
30 return file;
31 }
32
33 return nullptr;
34}
35
36std::shared_ptr<VfsDirectory> LayeredVfsDirectory::GetDirectoryRelative(
37 std::string_view path) const {
38 std::vector<VirtualDir> out;
39 for (const auto& layer : dirs) {
40 auto dir = layer->GetDirectoryRelative(path);
41 if (dir != nullptr)
42 out.push_back(std::move(dir));
43 }
44
45 return MakeLayeredDirectory(std::move(out));
46}
47
48std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFile(std::string_view name) const {
49 return GetFileRelative(name);
50}
51
52std::shared_ptr<VfsDirectory> LayeredVfsDirectory::GetSubdirectory(std::string_view name) const {
53 return GetDirectoryRelative(name);
54}
55
56std::string LayeredVfsDirectory::GetFullPath() const {
57 return dirs[0]->GetFullPath();
58}
59
60std::vector<std::shared_ptr<VfsFile>> LayeredVfsDirectory::GetFiles() const {
61 std::vector<VirtualFile> out;
62 for (const auto& layer : dirs) {
63 for (const auto& file : layer->GetFiles()) {
64 if (std::find_if(out.begin(), out.end(), [&file](const VirtualFile& comp) {
65 return comp->GetName() == file->GetName();
66 }) == out.end()) {
67 out.push_back(file);
68 }
69 }
70 }
71
72 return out;
73}
74
75std::vector<std::shared_ptr<VfsDirectory>> LayeredVfsDirectory::GetSubdirectories() const {
76 std::vector<std::string> names;
77 for (const auto& layer : dirs) {
78 for (const auto& sd : layer->GetSubdirectories()) {
79 if (std::find(names.begin(), names.end(), sd->GetName()) == names.end())
80 names.push_back(sd->GetName());
81 }
82 }
83
84 std::vector<VirtualDir> out;
85 out.reserve(names.size());
86 for (const auto& subdir : names)
87 out.push_back(GetSubdirectory(subdir));
88
89 return out;
90}
91
92bool LayeredVfsDirectory::IsWritable() const {
93 return false;
94}
95
96bool LayeredVfsDirectory::IsReadable() const {
97 return true;
98}
99
100std::string LayeredVfsDirectory::GetName() const {
101 return name.empty() ? dirs[0]->GetName() : name;
102}
103
104std::shared_ptr<VfsDirectory> LayeredVfsDirectory::GetParentDirectory() const {
105 return dirs[0]->GetParentDirectory();
106}
107
108std::shared_ptr<VfsDirectory> LayeredVfsDirectory::CreateSubdirectory(std::string_view name) {
109 return nullptr;
110}
111
112std::shared_ptr<VfsFile> LayeredVfsDirectory::CreateFile(std::string_view name) {
113 return nullptr;
114}
115
116bool LayeredVfsDirectory::DeleteSubdirectory(std::string_view name) {
117 return false;
118}
119
120bool LayeredVfsDirectory::DeleteFile(std::string_view name) {
121 return false;
122}
123
124bool LayeredVfsDirectory::Rename(std::string_view name_) {
125 name = name_;
126 return true;
127}
128
129bool LayeredVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
130 return false;
131}
132} // namespace FileSys
diff --git a/src/core/file_sys/vfs_layered.h b/src/core/file_sys/vfs_layered.h
new file mode 100644
index 000000000..d85310f57
--- /dev/null
+++ b/src/core/file_sys/vfs_layered.h
@@ -0,0 +1,50 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include "core/file_sys/vfs.h"
9
10namespace FileSys {
11
12// Class that stacks multiple VfsDirectories on top of each other, attempting to read from the first
13// one and falling back to the one after. The highest priority directory (overwrites all others)
14// should be element 0 in the dirs vector.
15class LayeredVfsDirectory : public VfsDirectory {
16 LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name);
17
18public:
19 ~LayeredVfsDirectory() override;
20
21 /// Wrapper function to allow for more efficient handling of dirs.size() == 0, 1 cases.
22 static VirtualDir MakeLayeredDirectory(std::vector<VirtualDir> dirs, std::string name = "");
23
24 std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override;
25 std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override;
26 std::shared_ptr<VfsFile> GetFile(std::string_view name) const override;
27 std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const override;
28 std::string GetFullPath() const override;
29
30 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
31 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
32 bool IsWritable() const override;
33 bool IsReadable() const override;
34 std::string GetName() const override;
35 std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
36 std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) override;
37 std::shared_ptr<VfsFile> CreateFile(std::string_view name) override;
38 bool DeleteSubdirectory(std::string_view name) override;
39 bool DeleteFile(std::string_view name) override;
40 bool Rename(std::string_view name) override;
41
42protected:
43 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
44
45private:
46 std::vector<VirtualDir> dirs;
47 std::string name;
48};
49
50} // namespace FileSys
diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp
index f5ed291ea..a4c6719a0 100644
--- a/src/core/file_sys/vfs_offset.cpp
+++ b/src/core/file_sys/vfs_offset.cpp
@@ -14,6 +14,8 @@ OffsetVfsFile::OffsetVfsFile(std::shared_ptr<VfsFile> file_, std::size_t size_,
14 : file(file_), offset(offset_), size(size_), name(std::move(name_)), 14 : file(file_), offset(offset_), size(size_), name(std::move(name_)),
15 parent(parent_ == nullptr ? file->GetContainingDirectory() : std::move(parent_)) {} 15 parent(parent_ == nullptr ? file->GetContainingDirectory() : std::move(parent_)) {}
16 16
17OffsetVfsFile::~OffsetVfsFile() = default;
18
17std::string OffsetVfsFile::GetName() const { 19std::string OffsetVfsFile::GetName() const {
18 return name.empty() ? file->GetName() : name; 20 return name.empty() ? file->GetName() : name;
19} 21}
diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h
index 34cb180b3..8062702a7 100644
--- a/src/core/file_sys/vfs_offset.h
+++ b/src/core/file_sys/vfs_offset.h
@@ -19,6 +19,7 @@ class OffsetVfsFile : public VfsFile {
19public: 19public:
20 OffsetVfsFile(std::shared_ptr<VfsFile> file, std::size_t size, std::size_t offset = 0, 20 OffsetVfsFile(std::shared_ptr<VfsFile> file, std::size_t size, std::size_t offset = 0,
21 std::string new_name = "", VirtualDir new_parent = nullptr); 21 std::string new_name = "", VirtualDir new_parent = nullptr);
22 ~OffsetVfsFile() override;
22 23
23 std::string GetName() const override; 24 std::string GetName() const override;
24 std::size_t GetSize() const override; 25 std::size_t GetSize() const override;
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp
index 5e242e20f..9defad04c 100644
--- a/src/core/file_sys/vfs_real.cpp
+++ b/src/core/file_sys/vfs_real.cpp
@@ -413,6 +413,23 @@ std::string RealVfsDirectory::GetFullPath() const {
413 return out; 413 return out;
414} 414}
415 415
416std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
417 if (perms == Mode::Append)
418 return {};
419
420 std::map<std::string, VfsEntryType, std::less<>> out;
421 FileUtil::ForeachDirectoryEntry(
422 nullptr, path,
423 [&out](u64* entries_out, const std::string& directory, const std::string& filename) {
424 const std::string full_path = directory + DIR_SEP + filename;
425 out.emplace(filename, FileUtil::IsDirectory(full_path) ? VfsEntryType::Directory
426 : VfsEntryType::File);
427 return true;
428 });
429
430 return out;
431}
432
416bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { 433bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
417 return false; 434 return false;
418} 435}
diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h
index 681c43e82..5b61db90d 100644
--- a/src/core/file_sys/vfs_real.h
+++ b/src/core/file_sys/vfs_real.h
@@ -98,6 +98,7 @@ public:
98 bool DeleteFile(std::string_view name) override; 98 bool DeleteFile(std::string_view name) override;
99 bool Rename(std::string_view name) override; 99 bool Rename(std::string_view name) override;
100 std::string GetFullPath() const override; 100 std::string GetFullPath() const override;
101 std::map<std::string, VfsEntryType, std::less<>> GetEntries() const override;
101 102
102protected: 103protected:
103 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; 104 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
diff --git a/src/core/file_sys/vfs_static.h b/src/core/file_sys/vfs_static.h
new file mode 100644
index 000000000..44fab51d1
--- /dev/null
+++ b/src/core/file_sys/vfs_static.h
@@ -0,0 +1,79 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <memory>
9#include <string_view>
10
11#include "core/file_sys/vfs.h"
12
13namespace FileSys {
14
15class StaticVfsFile : public VfsFile {
16public:
17 explicit StaticVfsFile(u8 value, std::size_t size = 0, std::string name = "",
18 VirtualDir parent = nullptr)
19 : value{value}, size{size}, name{std::move(name)}, parent{std::move(parent)} {}
20
21 std::string GetName() const override {
22 return name;
23 }
24
25 std::size_t GetSize() const override {
26 return size;
27 }
28
29 bool Resize(std::size_t new_size) override {
30 size = new_size;
31 return true;
32 }
33
34 std::shared_ptr<VfsDirectory> GetContainingDirectory() const override {
35 return parent;
36 }
37
38 bool IsWritable() const override {
39 return false;
40 }
41
42 bool IsReadable() const override {
43 return true;
44 }
45
46 std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override {
47 const auto read = std::min(length, size - offset);
48 std::fill(data, data + read, value);
49 return read;
50 }
51
52 std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override {
53 return 0;
54 }
55
56 boost::optional<u8> ReadByte(std::size_t offset) const override {
57 if (offset < size)
58 return value;
59 return boost::none;
60 }
61
62 std::vector<u8> ReadBytes(std::size_t length, std::size_t offset) const override {
63 const auto read = std::min(length, size - offset);
64 return std::vector<u8>(read, value);
65 }
66
67 bool Rename(std::string_view new_name) override {
68 name = new_name;
69 return true;
70 }
71
72private:
73 u8 value;
74 std::size_t size;
75 std::string name;
76 VirtualDir parent;
77};
78
79} // namespace FileSys
diff --git a/src/core/file_sys/vfs_vector.cpp b/src/core/file_sys/vfs_vector.cpp
index 98e7c4598..389c7e003 100644
--- a/src/core/file_sys/vfs_vector.cpp
+++ b/src/core/file_sys/vfs_vector.cpp
@@ -3,16 +3,72 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm> 5#include <algorithm>
6#include <cstring>
6#include <utility> 7#include <utility>
7#include "core/file_sys/vfs_vector.h" 8#include "core/file_sys/vfs_vector.h"
8 9
9namespace FileSys { 10namespace FileSys {
11VectorVfsFile::VectorVfsFile(std::vector<u8> initial_data, std::string name, VirtualDir parent)
12 : data(std::move(initial_data)), parent(std::move(parent)), name(std::move(name)) {}
13
14VectorVfsFile::~VectorVfsFile() = default;
15
16std::string VectorVfsFile::GetName() const {
17 return name;
18}
19
20size_t VectorVfsFile::GetSize() const {
21 return data.size();
22}
23
24bool VectorVfsFile::Resize(size_t new_size) {
25 data.resize(new_size);
26 return true;
27}
28
29std::shared_ptr<VfsDirectory> VectorVfsFile::GetContainingDirectory() const {
30 return parent;
31}
32
33bool VectorVfsFile::IsWritable() const {
34 return true;
35}
36
37bool VectorVfsFile::IsReadable() const {
38 return true;
39}
40
41std::size_t VectorVfsFile::Read(u8* data_, std::size_t length, std::size_t offset) const {
42 const auto read = std::min(length, data.size() - offset);
43 std::memcpy(data_, data.data() + offset, read);
44 return read;
45}
46
47std::size_t VectorVfsFile::Write(const u8* data_, std::size_t length, std::size_t offset) {
48 if (offset + length > data.size())
49 data.resize(offset + length);
50 const auto write = std::min(length, data.size() - offset);
51 std::memcpy(data.data(), data_, write);
52 return write;
53}
54
55bool VectorVfsFile::Rename(std::string_view name_) {
56 name = name_;
57 return true;
58}
59
60void VectorVfsFile::Assign(std::vector<u8> new_data) {
61 data = std::move(new_data);
62}
63
10VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_, 64VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_,
11 std::vector<VirtualDir> dirs_, std::string name_, 65 std::vector<VirtualDir> dirs_, std::string name_,
12 VirtualDir parent_) 66 VirtualDir parent_)
13 : files(std::move(files_)), dirs(std::move(dirs_)), parent(std::move(parent_)), 67 : files(std::move(files_)), dirs(std::move(dirs_)), parent(std::move(parent_)),
14 name(std::move(name_)) {} 68 name(std::move(name_)) {}
15 69
70VectorVfsDirectory::~VectorVfsDirectory() = default;
71
16std::vector<std::shared_ptr<VfsFile>> VectorVfsDirectory::GetFiles() const { 72std::vector<std::shared_ptr<VfsFile>> VectorVfsDirectory::GetFiles() const {
17 return files; 73 return files;
18} 74}
diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h
index 179f62e4b..48a414c98 100644
--- a/src/core/file_sys/vfs_vector.h
+++ b/src/core/file_sys/vfs_vector.h
@@ -8,6 +8,31 @@
8 8
9namespace FileSys { 9namespace FileSys {
10 10
11// An implementation of VfsFile that is backed by a vector optionally supplied upon construction
12class VectorVfsFile : public VfsFile {
13public:
14 explicit VectorVfsFile(std::vector<u8> initial_data = {}, std::string name = "",
15 VirtualDir parent = nullptr);
16 ~VectorVfsFile() override;
17
18 std::string GetName() const override;
19 std::size_t GetSize() const override;
20 bool Resize(std::size_t new_size) override;
21 std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
22 bool IsWritable() const override;
23 bool IsReadable() const override;
24 std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
25 std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
26 bool Rename(std::string_view name) override;
27
28 virtual void Assign(std::vector<u8> new_data);
29
30private:
31 std::vector<u8> data;
32 VirtualDir parent;
33 std::string name;
34};
35
11// An implementation of VfsDirectory that maintains two vectors for subdirectories and files. 36// An implementation of VfsDirectory that maintains two vectors for subdirectories and files.
12// Vector data is supplied upon construction. 37// Vector data is supplied upon construction.
13class VectorVfsDirectory : public VfsDirectory { 38class VectorVfsDirectory : public VfsDirectory {
@@ -15,6 +40,7 @@ public:
15 explicit VectorVfsDirectory(std::vector<VirtualFile> files = {}, 40 explicit VectorVfsDirectory(std::vector<VirtualFile> files = {},
16 std::vector<VirtualDir> dirs = {}, std::string name = "", 41 std::vector<VirtualDir> dirs = {}, std::string name = "",
17 VirtualDir parent = nullptr); 42 VirtualDir parent = nullptr);
43 ~VectorVfsDirectory() override;
18 44
19 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; 45 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
20 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; 46 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp
index 0173f71c1..b2b164368 100644
--- a/src/core/file_sys/xts_archive.cpp
+++ b/src/core/file_sys/xts_archive.cpp
@@ -30,9 +30,6 @@ static bool CalculateHMAC256(Destination* out, const SourceKey* key, std::size_t
30 mbedtls_md_context_t context; 30 mbedtls_md_context_t context;
31 mbedtls_md_init(&context); 31 mbedtls_md_init(&context);
32 32
33 const auto key_f = reinterpret_cast<const u8*>(key);
34 const std::vector<u8> key_v(key_f, key_f + key_length);
35
36 if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) || 33 if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) ||
37 mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) || 34 mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) ||
38 mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) || 35 mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) ||
@@ -45,7 +42,7 @@ static bool CalculateHMAC256(Destination* out, const SourceKey* key, std::size_t
45 return true; 42 return true;
46} 43}
47 44
48NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { 45NAX::NAX(VirtualFile file_) : header(std::make_unique<NAXHeader>()), file(std::move(file_)) {
49 std::string path = FileUtil::SanitizePath(file->GetFullPath()); 46 std::string path = FileUtil::SanitizePath(file->GetFullPath());
50 static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca", 47 static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca",
51 std::regex_constants::ECMAScript | 48 std::regex_constants::ECMAScript |
@@ -65,13 +62,15 @@ NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NA
65} 62}
66 63
67NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id) 64NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id)
68 : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { 65 : header(std::make_unique<NAXHeader>()), file(std::move(file_)) {
69 Core::Crypto::SHA256Hash hash{}; 66 Core::Crypto::SHA256Hash hash{};
70 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); 67 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
71 status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], 68 status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0],
72 Common::HexArrayToString(nca_id, false))); 69 Common::HexArrayToString(nca_id, false)));
73} 70}
74 71
72NAX::~NAX() = default;
73
75Loader::ResultStatus NAX::Parse(std::string_view path) { 74Loader::ResultStatus NAX::Parse(std::string_view path) {
76 if (file->ReadObject(header.get()) != sizeof(NAXHeader)) 75 if (file->ReadObject(header.get()) != sizeof(NAXHeader))
77 return Loader::ResultStatus::ErrorBadNAXHeader; 76 return Loader::ResultStatus::ErrorBadNAXHeader;
@@ -138,9 +137,9 @@ VirtualFile NAX::GetDecrypted() const {
138 return dec_file; 137 return dec_file;
139} 138}
140 139
141std::shared_ptr<NCA> NAX::AsNCA() const { 140std::unique_ptr<NCA> NAX::AsNCA() const {
142 if (type == NAXContentType::NCA) 141 if (type == NAXContentType::NCA)
143 return std::make_shared<NCA>(GetDecrypted()); 142 return std::make_unique<NCA>(GetDecrypted());
144 return nullptr; 143 return nullptr;
145} 144}
146 145
diff --git a/src/core/file_sys/xts_archive.h b/src/core/file_sys/xts_archive.h
index 55d2154a6..8fedd8585 100644
--- a/src/core/file_sys/xts_archive.h
+++ b/src/core/file_sys/xts_archive.h
@@ -33,12 +33,13 @@ class NAX : public ReadOnlyVfsDirectory {
33public: 33public:
34 explicit NAX(VirtualFile file); 34 explicit NAX(VirtualFile file);
35 explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id); 35 explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id);
36 ~NAX() override;
36 37
37 Loader::ResultStatus GetStatus() const; 38 Loader::ResultStatus GetStatus() const;
38 39
39 VirtualFile GetDecrypted() const; 40 VirtualFile GetDecrypted() const;
40 41
41 std::shared_ptr<NCA> AsNCA() const; 42 std::unique_ptr<NCA> AsNCA() const;
42 43
43 NAXContentType GetContentType() const; 44 NAXContentType GetContentType() const;
44 45
@@ -60,7 +61,7 @@ private:
60 61
61 VirtualFile file; 62 VirtualFile file;
62 Loader::ResultStatus status; 63 Loader::ResultStatus status;
63 NAXContentType type; 64 NAXContentType type{};
64 65
65 VirtualFile dec_file; 66 VirtualFile dec_file;
66 67
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 1b04f68bf..5bc947010 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -37,7 +37,9 @@
37#include "core/core.h" 37#include "core/core.h"
38#include "core/core_cpu.h" 38#include "core/core_cpu.h"
39#include "core/gdbstub/gdbstub.h" 39#include "core/gdbstub/gdbstub.h"
40#include "core/hle/kernel/process.h"
40#include "core/hle/kernel/scheduler.h" 41#include "core/hle/kernel/scheduler.h"
42#include "core/hle/kernel/vm_manager.h"
41#include "core/loader/loader.h" 43#include "core/loader/loader.h"
42#include "core/memory.h" 44#include "core/memory.h"
43 45
@@ -248,7 +250,7 @@ static void RegWrite(std::size_t id, u64 val, Kernel::Thread* thread = nullptr)
248 } else if (id == PC_REGISTER) { 250 } else if (id == PC_REGISTER) {
249 thread->context.pc = val; 251 thread->context.pc = val;
250 } else if (id == PSTATE_REGISTER) { 252 } else if (id == PSTATE_REGISTER) {
251 thread->context.pstate = val; 253 thread->context.pstate = static_cast<u32>(val);
252 } else if (id > PSTATE_REGISTER && id < FPCR_REGISTER) { 254 } else if (id > PSTATE_REGISTER && id < FPCR_REGISTER) {
253 thread->context.vector_registers[id - (PSTATE_REGISTER + 1)][0] = val; 255 thread->context.vector_registers[id - (PSTATE_REGISTER + 1)][0] = val;
254 } 256 }
@@ -585,7 +587,8 @@ static void HandleQuery() {
585 strlen("Xfer:features:read:target.xml:")) == 0) { 587 strlen("Xfer:features:read:target.xml:")) == 0) {
586 SendReply(target_xml); 588 SendReply(target_xml);
587 } else if (strncmp(query, "Offsets", strlen("Offsets")) == 0) { 589 } else if (strncmp(query, "Offsets", strlen("Offsets")) == 0) {
588 std::string buffer = fmt::format("TextSeg={:0x}", Memory::PROCESS_IMAGE_VADDR); 590 const VAddr base_address = Core::CurrentProcess()->VMManager().GetCodeRegionBaseAddress();
591 std::string buffer = fmt::format("TextSeg={:0x}", base_address);
589 SendReply(buffer.c_str()); 592 SendReply(buffer.c_str());
590 } else if (strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0) { 593 } else if (strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0) {
591 std::string val = "m"; 594 std::string val = "m";
@@ -893,11 +896,11 @@ static void ReadMemory() {
893 static u8 reply[GDB_BUFFER_SIZE - 4]; 896 static u8 reply[GDB_BUFFER_SIZE - 4];
894 897
895 auto start_offset = command_buffer + 1; 898 auto start_offset = command_buffer + 1;
896 auto addr_pos = std::find(start_offset, command_buffer + command_length, ','); 899 const auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
897 VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset)); 900 const VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
898 901
899 start_offset = addr_pos + 1; 902 start_offset = addr_pos + 1;
900 u64 len = 903 const u64 len =
901 HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset)); 904 HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
902 905
903 LOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len); 906 LOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len);
@@ -906,7 +909,9 @@ static void ReadMemory() {
906 SendReply("E01"); 909 SendReply("E01");
907 } 910 }
908 911
909 if (addr < Memory::PROCESS_IMAGE_VADDR || addr >= Memory::MAP_REGION_VADDR_END) { 912 const auto& vm_manager = Core::CurrentProcess()->VMManager();
913 if (addr < vm_manager.GetCodeRegionBaseAddress() ||
914 addr >= vm_manager.GetMapRegionEndAddress()) {
910 return SendReply("E00"); 915 return SendReply("E00");
911 } 916 }
912 917
@@ -995,7 +1000,7 @@ static bool CommitBreakpoint(BreakpointType type, VAddr addr, u64 len) {
995 breakpoint.addr = addr; 1000 breakpoint.addr = addr;
996 breakpoint.len = len; 1001 breakpoint.len = len;
997 Memory::ReadBlock(addr, breakpoint.inst.data(), breakpoint.inst.size()); 1002 Memory::ReadBlock(addr, breakpoint.inst.data(), breakpoint.inst.size());
998 static constexpr std::array<u8, 4> btrap{{0xd4, 0x20, 0x7d, 0x0}}; 1003 static constexpr std::array<u8, 4> btrap{{0x00, 0x7d, 0x20, 0xd4}};
999 Memory::WriteBlock(addr, btrap.data(), btrap.size()); 1004 Memory::WriteBlock(addr, btrap.data(), btrap.size());
1000 Core::System::GetInstance().InvalidateCpuInstructionCaches(); 1005 Core::System::GetInstance().InvalidateCpuInstructionCaches();
1001 p.insert({addr, breakpoint}); 1006 p.insert({addr, breakpoint});
diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h
index 7545ecf2a..a4bfe2eb0 100644
--- a/src/core/hle/ipc_helpers.h
+++ b/src/core/hle/ipc_helpers.h
@@ -290,13 +290,6 @@ public:
290 Skip(CommandIdSize, false); 290 Skip(CommandIdSize, false);
291 } 291 }
292 292
293 ResponseBuilder MakeBuilder(u32 normal_params_size, u32 num_handles_to_copy,
294 u32 num_handles_to_move,
295 ResponseBuilder::Flags flags = ResponseBuilder::Flags::None) const {
296 return ResponseBuilder{*context, normal_params_size, num_handles_to_copy,
297 num_handles_to_move, flags};
298 }
299
300 template <typename T> 293 template <typename T>
301 T Pop(); 294 T Pop();
302 295
diff --git a/src/core/hle/kernel/errors.h b/src/core/hle/kernel/errors.h
index 8c2be2681..e5fa67ae8 100644
--- a/src/core/hle/kernel/errors.h
+++ b/src/core/hle/kernel/errors.h
@@ -31,6 +31,7 @@ enum {
31 TooLarge = 119, 31 TooLarge = 119,
32 InvalidEnumValue = 120, 32 InvalidEnumValue = 120,
33 NoSuchEntry = 121, 33 NoSuchEntry = 121,
34 AlreadyRegistered = 122,
34 InvalidState = 125, 35 InvalidState = 125,
35 ResourceLimitExceeded = 132, 36 ResourceLimitExceeded = 132,
36}; 37};
@@ -58,6 +59,7 @@ constexpr ResultCode ERR_INVALID_MEMORY_PERMISSIONS(ErrorModule::Kernel,
58constexpr ResultCode ERR_INVALID_HANDLE(ErrorModule::Kernel, ErrCodes::InvalidHandle); 59constexpr ResultCode ERR_INVALID_HANDLE(ErrorModule::Kernel, ErrCodes::InvalidHandle);
59constexpr ResultCode ERR_INVALID_PROCESSOR_ID(ErrorModule::Kernel, ErrCodes::InvalidProcessorId); 60constexpr ResultCode ERR_INVALID_PROCESSOR_ID(ErrorModule::Kernel, ErrCodes::InvalidProcessorId);
60constexpr ResultCode ERR_INVALID_SIZE(ErrorModule::Kernel, ErrCodes::InvalidSize); 61constexpr ResultCode ERR_INVALID_SIZE(ErrorModule::Kernel, ErrCodes::InvalidSize);
62constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::Kernel, ErrCodes::AlreadyRegistered);
61constexpr ResultCode ERR_INVALID_STATE(ErrorModule::Kernel, ErrCodes::InvalidState); 63constexpr ResultCode ERR_INVALID_STATE(ErrorModule::Kernel, ErrCodes::InvalidState);
62constexpr ResultCode ERR_INVALID_THREAD_PRIORITY(ErrorModule::Kernel, 64constexpr ResultCode ERR_INVALID_THREAD_PRIORITY(ErrorModule::Kernel,
63 ErrCodes::InvalidThreadPriority); 65 ErrCodes::InvalidThreadPriority);
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index 51f4544be..81675eac5 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -16,6 +16,7 @@
16#include "core/hle/kernel/object.h" 16#include "core/hle/kernel/object.h"
17#include "core/hle/kernel/thread.h" 17#include "core/hle/kernel/thread.h"
18#include "core/hle/result.h" 18#include "core/hle/result.h"
19#include "core/memory.h"
19 20
20namespace Kernel { 21namespace Kernel {
21 22
diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h
index b054cbf7d..9eb72315c 100644
--- a/src/core/hle/kernel/object.h
+++ b/src/core/hle/kernel/object.h
@@ -6,7 +6,6 @@
6 6
7#include <atomic> 7#include <atomic>
8#include <string> 8#include <string>
9#include <utility>
10 9
11#include <boost/smart_ptr/intrusive_ptr.hpp> 10#include <boost/smart_ptr/intrusive_ptr.hpp>
12 11
@@ -97,7 +96,7 @@ using SharedPtr = boost::intrusive_ptr<T>;
97template <typename T> 96template <typename T>
98inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) { 97inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) {
99 if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { 98 if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
100 return boost::static_pointer_cast<T>(std::move(object)); 99 return boost::static_pointer_cast<T>(object);
101 } 100 }
102 return nullptr; 101 return nullptr;
103} 102}
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index 7a272d031..dc9fc8470 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -7,10 +7,13 @@
7#include "common/assert.h" 7#include "common/assert.h"
8#include "common/common_funcs.h" 8#include "common/common_funcs.h"
9#include "common/logging/log.h" 9#include "common/logging/log.h"
10#include "core/core.h"
11#include "core/file_sys/program_metadata.h"
10#include "core/hle/kernel/errors.h" 12#include "core/hle/kernel/errors.h"
11#include "core/hle/kernel/kernel.h" 13#include "core/hle/kernel/kernel.h"
12#include "core/hle/kernel/process.h" 14#include "core/hle/kernel/process.h"
13#include "core/hle/kernel/resource_limit.h" 15#include "core/hle/kernel/resource_limit.h"
16#include "core/hle/kernel/scheduler.h"
14#include "core/hle/kernel/thread.h" 17#include "core/hle/kernel/thread.h"
15#include "core/hle/kernel/vm_manager.h" 18#include "core/hle/kernel/vm_manager.h"
16#include "core/memory.h" 19#include "core/memory.h"
@@ -32,14 +35,22 @@ SharedPtr<Process> Process::Create(KernelCore& kernel, std::string&& name) {
32 process->name = std::move(name); 35 process->name = std::move(name);
33 process->flags.raw = 0; 36 process->flags.raw = 0;
34 process->flags.memory_region.Assign(MemoryRegion::APPLICATION); 37 process->flags.memory_region.Assign(MemoryRegion::APPLICATION);
38 process->resource_limit = kernel.ResourceLimitForCategory(ResourceLimitCategory::APPLICATION);
35 process->status = ProcessStatus::Created; 39 process->status = ProcessStatus::Created;
36 process->program_id = 0; 40 process->program_id = 0;
37 process->process_id = kernel.CreateNewProcessID(); 41 process->process_id = kernel.CreateNewProcessID();
42 process->svc_access_mask.set();
38 43
39 kernel.AppendNewProcess(process); 44 kernel.AppendNewProcess(process);
40 return process; 45 return process;
41} 46}
42 47
48void Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) {
49 program_id = metadata.GetTitleID();
50 is_64bit_process = metadata.Is64BitProgram();
51 vm_manager.Reset(metadata.GetAddressSpaceType());
52}
53
43void Process::ParseKernelCaps(const u32* kernel_caps, std::size_t len) { 54void Process::ParseKernelCaps(const u32* kernel_caps, std::size_t len) {
44 for (std::size_t i = 0; i < len; ++i) { 55 for (std::size_t i = 0; i < len; ++i) {
45 u32 descriptor = kernel_caps[i]; 56 u32 descriptor = kernel_caps[i];
@@ -117,7 +128,7 @@ void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) {
117 // TODO(bunnei): This is heap area that should be allocated by the kernel and not mapped as part 128 // TODO(bunnei): This is heap area that should be allocated by the kernel and not mapped as part
118 // of the user address space. 129 // of the user address space.
119 vm_manager 130 vm_manager
120 .MapMemoryBlock(Memory::STACK_AREA_VADDR_END - stack_size, 131 .MapMemoryBlock(vm_manager.GetTLSIORegionEndAddress() - stack_size,
121 std::make_shared<std::vector<u8>>(stack_size, 0), 0, stack_size, 132 std::make_shared<std::vector<u8>>(stack_size, 0), 0, stack_size,
122 MemoryState::Mapped) 133 MemoryState::Mapped)
123 .Unwrap(); 134 .Unwrap();
@@ -125,7 +136,92 @@ void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) {
125 vm_manager.LogLayout(); 136 vm_manager.LogLayout();
126 status = ProcessStatus::Running; 137 status = ProcessStatus::Running;
127 138
128 Kernel::SetupMainThread(kernel, entry_point, main_thread_priority, this); 139 Kernel::SetupMainThread(kernel, entry_point, main_thread_priority, *this);
140}
141
142void Process::PrepareForTermination() {
143 status = ProcessStatus::Exited;
144
145 const auto stop_threads = [this](const std::vector<SharedPtr<Thread>>& thread_list) {
146 for (auto& thread : thread_list) {
147 if (thread->owner_process != this)
148 continue;
149
150 if (thread == GetCurrentThread())
151 continue;
152
153 // TODO(Subv): When are the other running/ready threads terminated?
154 ASSERT_MSG(thread->status == ThreadStatus::WaitSynchAny ||
155 thread->status == ThreadStatus::WaitSynchAll,
156 "Exiting processes with non-waiting threads is currently unimplemented");
157
158 thread->Stop();
159 }
160 };
161
162 auto& system = Core::System::GetInstance();
163 stop_threads(system.Scheduler(0)->GetThreadList());
164 stop_threads(system.Scheduler(1)->GetThreadList());
165 stop_threads(system.Scheduler(2)->GetThreadList());
166 stop_threads(system.Scheduler(3)->GetThreadList());
167}
168
169/**
170 * Finds a free location for the TLS section of a thread.
171 * @param tls_slots The TLS page array of the thread's owner process.
172 * Returns a tuple of (page, slot, alloc_needed) where:
173 * page: The index of the first allocated TLS page that has free slots.
174 * slot: The index of the first free slot in the indicated page.
175 * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full).
176 */
177static std::tuple<std::size_t, std::size_t, bool> FindFreeThreadLocalSlot(
178 const std::vector<std::bitset<8>>& tls_slots) {
179 // Iterate over all the allocated pages, and try to find one where not all slots are used.
180 for (std::size_t page = 0; page < tls_slots.size(); ++page) {
181 const auto& page_tls_slots = tls_slots[page];
182 if (!page_tls_slots.all()) {
183 // We found a page with at least one free slot, find which slot it is
184 for (std::size_t slot = 0; slot < page_tls_slots.size(); ++slot) {
185 if (!page_tls_slots.test(slot)) {
186 return std::make_tuple(page, slot, false);
187 }
188 }
189 }
190 }
191
192 return std::make_tuple(0, 0, true);
193}
194
195VAddr Process::MarkNextAvailableTLSSlotAsUsed(Thread& thread) {
196 auto [available_page, available_slot, needs_allocation] = FindFreeThreadLocalSlot(tls_slots);
197 const VAddr tls_begin = vm_manager.GetTLSIORegionBaseAddress();
198
199 if (needs_allocation) {
200 tls_slots.emplace_back(0); // The page is completely available at the start
201 available_page = tls_slots.size() - 1;
202 available_slot = 0; // Use the first slot in the new page
203
204 // Allocate some memory from the end of the linear heap for this region.
205 auto& tls_memory = thread.GetTLSMemory();
206 tls_memory->insert(tls_memory->end(), Memory::PAGE_SIZE, 0);
207
208 vm_manager.RefreshMemoryBlockMappings(tls_memory.get());
209
210 vm_manager.MapMemoryBlock(tls_begin + available_page * Memory::PAGE_SIZE, tls_memory, 0,
211 Memory::PAGE_SIZE, MemoryState::ThreadLocal);
212 }
213
214 tls_slots[available_page].set(available_slot);
215
216 return tls_begin + available_page * Memory::PAGE_SIZE + available_slot * Memory::TLS_ENTRY_SIZE;
217}
218
219void Process::FreeTLSSlot(VAddr tls_address) {
220 const VAddr tls_base = tls_address - vm_manager.GetTLSIORegionBaseAddress();
221 const VAddr tls_page = tls_base / Memory::PAGE_SIZE;
222 const VAddr tls_slot = (tls_base % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
223
224 tls_slots[tls_page].reset(tls_slot);
129} 225}
130 226
131void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) { 227void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) {
@@ -145,8 +241,8 @@ void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) {
145} 241}
146 242
147ResultVal<VAddr> Process::HeapAllocate(VAddr target, u64 size, VMAPermission perms) { 243ResultVal<VAddr> Process::HeapAllocate(VAddr target, u64 size, VMAPermission perms) {
148 if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END || 244 if (target < vm_manager.GetHeapRegionBaseAddress() ||
149 target + size < target) { 245 target + size > vm_manager.GetHeapRegionEndAddress() || target + size < target) {
150 return ERR_INVALID_ADDRESS; 246 return ERR_INVALID_ADDRESS;
151 } 247 }
152 248
@@ -181,8 +277,8 @@ ResultVal<VAddr> Process::HeapAllocate(VAddr target, u64 size, VMAPermission per
181} 277}
182 278
183ResultCode Process::HeapFree(VAddr target, u32 size) { 279ResultCode Process::HeapFree(VAddr target, u32 size) {
184 if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END || 280 if (target < vm_manager.GetHeapRegionBaseAddress() ||
185 target + size < target) { 281 target + size > vm_manager.GetHeapRegionEndAddress() || target + size < target) {
186 return ERR_INVALID_ADDRESS; 282 return ERR_INVALID_ADDRESS;
187 } 283 }
188 284
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h
index 81538f70c..590e0c73d 100644
--- a/src/core/hle/kernel/process.h
+++ b/src/core/hle/kernel/process.h
@@ -17,6 +17,10 @@
17#include "core/hle/kernel/thread.h" 17#include "core/hle/kernel/thread.h"
18#include "core/hle/kernel/vm_manager.h" 18#include "core/hle/kernel/vm_manager.h"
19 19
20namespace FileSys {
21class ProgramMetadata;
22}
23
20namespace Kernel { 24namespace Kernel {
21 25
22class KernelCore; 26class KernelCore;
@@ -131,6 +135,121 @@ public:
131 return HANDLE_TYPE; 135 return HANDLE_TYPE;
132 } 136 }
133 137
138 /// Gets a reference to the process' memory manager.
139 Kernel::VMManager& VMManager() {
140 return vm_manager;
141 }
142
143 /// Gets a const reference to the process' memory manager.
144 const Kernel::VMManager& VMManager() const {
145 return vm_manager;
146 }
147
148 /// Gets the current status of the process
149 ProcessStatus GetStatus() const {
150 return status;
151 }
152
153 /// Gets the unique ID that identifies this particular process.
154 u32 GetProcessID() const {
155 return process_id;
156 }
157
158 /// Gets the title ID corresponding to this process.
159 u64 GetTitleID() const {
160 return program_id;
161 }
162
163 /// Gets the resource limit descriptor for this process
164 ResourceLimit& GetResourceLimit() {
165 return *resource_limit;
166 }
167
168 /// Gets the resource limit descriptor for this process
169 const ResourceLimit& GetResourceLimit() const {
170 return *resource_limit;
171 }
172
173 /// Gets the default CPU ID for this process
174 u8 GetDefaultProcessorID() const {
175 return ideal_processor;
176 }
177
178 /// Gets the bitmask of allowed CPUs that this process' threads can run on.
179 u32 GetAllowedProcessorMask() const {
180 return allowed_processor_mask;
181 }
182
183 /// Gets the bitmask of allowed thread priorities.
184 u32 GetAllowedThreadPriorityMask() const {
185 return allowed_thread_priority_mask;
186 }
187
188 u32 IsVirtualMemoryEnabled() const {
189 return is_virtual_address_memory_enabled;
190 }
191
192 /// Whether this process is an AArch64 or AArch32 process.
193 bool Is64BitProcess() const {
194 return is_64bit_process;
195 }
196
197 /**
198 * Loads process-specifics configuration info with metadata provided
199 * by an executable.
200 *
201 * @param metadata The provided metadata to load process specific info.
202 */
203 void LoadFromMetadata(const FileSys::ProgramMetadata& metadata);
204
205 /**
206 * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
207 * to this process.
208 */
209 void ParseKernelCaps(const u32* kernel_caps, std::size_t len);
210
211 /**
212 * Applies address space changes and launches the process main thread.
213 */
214 void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size);
215
216 /**
217 * Prepares a process for termination by stopping all of its threads
218 * and clearing any other resources.
219 */
220 void PrepareForTermination();
221
222 void LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr);
223
224 ///////////////////////////////////////////////////////////////////////////////////////////////
225 // Memory Management
226
227 // Marks the next available region as used and returns the address of the slot.
228 VAddr MarkNextAvailableTLSSlotAsUsed(Thread& thread);
229
230 // Frees a used TLS slot identified by the given address
231 void FreeTLSSlot(VAddr tls_address);
232
233 ResultVal<VAddr> HeapAllocate(VAddr target, u64 size, VMAPermission perms);
234 ResultCode HeapFree(VAddr target, u32 size);
235
236 ResultCode MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size);
237
238 ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size);
239
240private:
241 explicit Process(KernelCore& kernel);
242 ~Process() override;
243
244 /// Memory manager for this process.
245 Kernel::VMManager vm_manager;
246
247 /// Current status of the process
248 ProcessStatus status;
249
250 /// The ID of this process
251 u32 process_id = 0;
252
134 /// Title ID corresponding to the process 253 /// Title ID corresponding to the process
135 u64 program_id; 254 u64 program_id;
136 255
@@ -140,7 +259,7 @@ public:
140 /// The process may only call SVCs which have the corresponding bit set. 259 /// The process may only call SVCs which have the corresponding bit set.
141 std::bitset<0x80> svc_access_mask; 260 std::bitset<0x80> svc_access_mask;
142 /// Maximum size of the handle table for the process. 261 /// Maximum size of the handle table for the process.
143 unsigned int handle_table_size = 0x200; 262 u32 handle_table_size = 0x200;
144 /// Special memory ranges mapped into this processes address space. This is used to give 263 /// Special memory ranges mapped into this processes address space. This is used to give
145 /// processes access to specific I/O regions and device memory. 264 /// processes access to specific I/O regions and device memory.
146 boost::container::static_vector<AddressMapping, 8> address_mappings; 265 boost::container::static_vector<AddressMapping, 8> address_mappings;
@@ -154,29 +273,6 @@ public:
154 u32 allowed_processor_mask = THREADPROCESSORID_DEFAULT_MASK; 273 u32 allowed_processor_mask = THREADPROCESSORID_DEFAULT_MASK;
155 u32 allowed_thread_priority_mask = 0xFFFFFFFF; 274 u32 allowed_thread_priority_mask = 0xFFFFFFFF;
156 u32 is_virtual_address_memory_enabled = 0; 275 u32 is_virtual_address_memory_enabled = 0;
157 /// Current status of the process
158 ProcessStatus status;
159
160 /// The ID of this process
161 u32 process_id = 0;
162
163 /**
164 * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
165 * to this process.
166 */
167 void ParseKernelCaps(const u32* kernel_caps, std::size_t len);
168
169 /**
170 * Applies address space changes and launches the process main thread.
171 */
172 void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size);
173
174 void LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr);
175
176 ///////////////////////////////////////////////////////////////////////////////////////////////
177 // Memory Management
178
179 VMManager vm_manager;
180 276
181 // Memory used to back the allocations in the regular heap. A single vector is used to cover 277 // Memory used to back the allocations in the regular heap. A single vector is used to cover
182 // the entire virtual address space extents that bound the allocations, including any holes. 278 // the entire virtual address space extents that bound the allocations, including any holes.
@@ -196,18 +292,12 @@ public:
196 /// This vector will grow as more pages are allocated for new threads. 292 /// This vector will grow as more pages are allocated for new threads.
197 std::vector<std::bitset<8>> tls_slots; 293 std::vector<std::bitset<8>> tls_slots;
198 294
199 std::string name; 295 /// Whether or not this process is AArch64, or AArch32.
200 296 /// By default, we currently assume this is true, unless otherwise
201 ResultVal<VAddr> HeapAllocate(VAddr target, u64 size, VMAPermission perms); 297 /// specified by metadata provided to the process during loading.
202 ResultCode HeapFree(VAddr target, u32 size); 298 bool is_64bit_process = true;
203
204 ResultCode MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size);
205 299
206 ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size); 300 std::string name;
207
208private:
209 explicit Process(KernelCore& kernel);
210 ~Process() override;
211}; 301};
212 302
213} // namespace Kernel 303} // namespace Kernel
diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp
index 69c812f16..1e82cfffb 100644
--- a/src/core/hle/kernel/scheduler.cpp
+++ b/src/core/hle/kernel/scheduler.cpp
@@ -17,7 +17,7 @@ namespace Kernel {
17 17
18std::mutex Scheduler::scheduler_mutex; 18std::mutex Scheduler::scheduler_mutex;
19 19
20Scheduler::Scheduler(Core::ARM_Interface* cpu_core) : cpu_core(cpu_core) {} 20Scheduler::Scheduler(Core::ARM_Interface& cpu_core) : cpu_core(cpu_core) {}
21 21
22Scheduler::~Scheduler() { 22Scheduler::~Scheduler() {
23 for (auto& thread : thread_list) { 23 for (auto& thread : thread_list) {
@@ -59,9 +59,9 @@ void Scheduler::SwitchContext(Thread* new_thread) {
59 // Save context for previous thread 59 // Save context for previous thread
60 if (previous_thread) { 60 if (previous_thread) {
61 previous_thread->last_running_ticks = CoreTiming::GetTicks(); 61 previous_thread->last_running_ticks = CoreTiming::GetTicks();
62 cpu_core->SaveContext(previous_thread->context); 62 cpu_core.SaveContext(previous_thread->context);
63 // Save the TPIDR_EL0 system register in case it was modified. 63 // Save the TPIDR_EL0 system register in case it was modified.
64 previous_thread->tpidr_el0 = cpu_core->GetTPIDR_EL0(); 64 previous_thread->tpidr_el0 = cpu_core.GetTPIDR_EL0();
65 65
66 if (previous_thread->status == ThreadStatus::Running) { 66 if (previous_thread->status == ThreadStatus::Running) {
67 // This is only the case when a reschedule is triggered without the current thread 67 // This is only the case when a reschedule is triggered without the current thread
@@ -88,13 +88,13 @@ void Scheduler::SwitchContext(Thread* new_thread) {
88 88
89 if (previous_process != current_thread->owner_process) { 89 if (previous_process != current_thread->owner_process) {
90 Core::CurrentProcess() = current_thread->owner_process; 90 Core::CurrentProcess() = current_thread->owner_process;
91 SetCurrentPageTable(&Core::CurrentProcess()->vm_manager.page_table); 91 SetCurrentPageTable(&Core::CurrentProcess()->VMManager().page_table);
92 } 92 }
93 93
94 cpu_core->LoadContext(new_thread->context); 94 cpu_core.LoadContext(new_thread->context);
95 cpu_core->SetTlsAddress(new_thread->GetTLSAddress()); 95 cpu_core.SetTlsAddress(new_thread->GetTLSAddress());
96 cpu_core->SetTPIDR_EL0(new_thread->GetTPIDR_EL0()); 96 cpu_core.SetTPIDR_EL0(new_thread->GetTPIDR_EL0());
97 cpu_core->ClearExclusiveState(); 97 cpu_core.ClearExclusiveState();
98 } else { 98 } else {
99 current_thread = nullptr; 99 current_thread = nullptr;
100 // Note: We do not reset the current process and current page table when idling because 100 // Note: We do not reset the current process and current page table when idling because
diff --git a/src/core/hle/kernel/scheduler.h b/src/core/hle/kernel/scheduler.h
index 744990c9b..2c94641ec 100644
--- a/src/core/hle/kernel/scheduler.h
+++ b/src/core/hle/kernel/scheduler.h
@@ -19,7 +19,7 @@ namespace Kernel {
19 19
20class Scheduler final { 20class Scheduler final {
21public: 21public:
22 explicit Scheduler(Core::ARM_Interface* cpu_core); 22 explicit Scheduler(Core::ARM_Interface& cpu_core);
23 ~Scheduler(); 23 ~Scheduler();
24 24
25 /// Returns whether there are any threads that are ready to run. 25 /// Returns whether there are any threads that are ready to run.
@@ -72,7 +72,7 @@ private:
72 72
73 SharedPtr<Thread> current_thread = nullptr; 73 SharedPtr<Thread> current_thread = nullptr;
74 74
75 Core::ARM_Interface* cpu_core; 75 Core::ARM_Interface& cpu_core;
76 76
77 static std::mutex scheduler_mutex; 77 static std::mutex scheduler_mutex;
78}; 78};
diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp
index abb1d09cd..d061e6155 100644
--- a/src/core/hle/kernel/shared_memory.cpp
+++ b/src/core/hle/kernel/shared_memory.cpp
@@ -8,6 +8,7 @@
8#include "common/logging/log.h" 8#include "common/logging/log.h"
9#include "core/core.h" 9#include "core/core.h"
10#include "core/hle/kernel/errors.h" 10#include "core/hle/kernel/errors.h"
11#include "core/hle/kernel/kernel.h"
11#include "core/hle/kernel/shared_memory.h" 12#include "core/hle/kernel/shared_memory.h"
12#include "core/memory.h" 13#include "core/memory.h"
13 14
@@ -34,11 +35,11 @@ SharedPtr<SharedMemory> SharedMemory::Create(KernelCore& kernel, SharedPtr<Proce
34 35
35 // Refresh the address mappings for the current process. 36 // Refresh the address mappings for the current process.
36 if (Core::CurrentProcess() != nullptr) { 37 if (Core::CurrentProcess() != nullptr) {
37 Core::CurrentProcess()->vm_manager.RefreshMemoryBlockMappings( 38 Core::CurrentProcess()->VMManager().RefreshMemoryBlockMappings(
38 shared_memory->backing_block.get()); 39 shared_memory->backing_block.get());
39 } 40 }
40 } else { 41 } else {
41 auto& vm_manager = shared_memory->owner_process->vm_manager; 42 auto& vm_manager = shared_memory->owner_process->VMManager();
42 43
43 // The memory is already available and mapped in the owner process. 44 // The memory is already available and mapped in the owner process.
44 auto vma = vm_manager.FindVMA(address); 45 auto vma = vm_manager.FindVMA(address);
@@ -71,7 +72,8 @@ SharedPtr<SharedMemory> SharedMemory::CreateForApplet(
71 shared_memory->other_permissions = other_permissions; 72 shared_memory->other_permissions = other_permissions;
72 shared_memory->backing_block = std::move(heap_block); 73 shared_memory->backing_block = std::move(heap_block);
73 shared_memory->backing_block_offset = offset; 74 shared_memory->backing_block_offset = offset;
74 shared_memory->base_address = Memory::HEAP_VADDR + offset; 75 shared_memory->base_address =
76 kernel.CurrentProcess()->VMManager().GetHeapRegionBaseAddress() + offset;
75 77
76 return shared_memory; 78 return shared_memory;
77} 79}
@@ -105,7 +107,7 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
105 VAddr target_address = address; 107 VAddr target_address = address;
106 108
107 // Map the memory block into the target process 109 // Map the memory block into the target process
108 auto result = target_process->vm_manager.MapMemoryBlock( 110 auto result = target_process->VMManager().MapMemoryBlock(
109 target_address, backing_block, backing_block_offset, size, MemoryState::Shared); 111 target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
110 if (result.Failed()) { 112 if (result.Failed()) {
111 LOG_ERROR( 113 LOG_ERROR(
@@ -115,14 +117,14 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
115 return result.Code(); 117 return result.Code();
116 } 118 }
117 119
118 return target_process->vm_manager.ReprotectRange(target_address, size, 120 return target_process->VMManager().ReprotectRange(target_address, size,
119 ConvertPermissions(permissions)); 121 ConvertPermissions(permissions));
120} 122}
121 123
122ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) { 124ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) {
123 // TODO(Subv): Verify what happens if the application tries to unmap an address that is not 125 // TODO(Subv): Verify what happens if the application tries to unmap an address that is not
124 // mapped to a SharedMemory. 126 // mapped to a SharedMemory.
125 return target_process->vm_manager.UnmapRange(address, size); 127 return target_process->VMManager().UnmapRange(address, size);
126} 128}
127 129
128VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) { 130VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 371fc439e..1cdaa740a 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -51,8 +51,9 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
51 } 51 }
52 52
53 auto& process = *Core::CurrentProcess(); 53 auto& process = *Core::CurrentProcess();
54 const VAddr heap_base = process.VMManager().GetHeapRegionBaseAddress();
54 CASCADE_RESULT(*heap_addr, 55 CASCADE_RESULT(*heap_addr,
55 process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite)); 56 process.HeapAllocate(heap_base, heap_size, VMAPermission::ReadWrite));
56 return RESULT_SUCCESS; 57 return RESULT_SUCCESS;
57} 58}
58 59
@@ -169,7 +170,7 @@ static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
169 return ERR_INVALID_HANDLE; 170 return ERR_INVALID_HANDLE;
170 } 171 }
171 172
172 *process_id = process->process_id; 173 *process_id = process->GetProcessID();
173 return RESULT_SUCCESS; 174 return RESULT_SUCCESS;
174} 175}
175 176
@@ -325,26 +326,27 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id)
325 LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id, 326 LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
326 info_sub_id, handle); 327 info_sub_id, handle);
327 328
328 const auto& vm_manager = Core::CurrentProcess()->vm_manager; 329 const auto& current_process = Core::CurrentProcess();
330 const auto& vm_manager = current_process->VMManager();
329 331
330 switch (static_cast<GetInfoType>(info_id)) { 332 switch (static_cast<GetInfoType>(info_id)) {
331 case GetInfoType::AllowedCpuIdBitmask: 333 case GetInfoType::AllowedCpuIdBitmask:
332 *result = Core::CurrentProcess()->allowed_processor_mask; 334 *result = current_process->GetAllowedProcessorMask();
333 break; 335 break;
334 case GetInfoType::AllowedThreadPrioBitmask: 336 case GetInfoType::AllowedThreadPrioBitmask:
335 *result = Core::CurrentProcess()->allowed_thread_priority_mask; 337 *result = current_process->GetAllowedThreadPriorityMask();
336 break; 338 break;
337 case GetInfoType::MapRegionBaseAddr: 339 case GetInfoType::MapRegionBaseAddr:
338 *result = Memory::MAP_REGION_VADDR; 340 *result = vm_manager.GetMapRegionBaseAddress();
339 break; 341 break;
340 case GetInfoType::MapRegionSize: 342 case GetInfoType::MapRegionSize:
341 *result = Memory::MAP_REGION_SIZE; 343 *result = vm_manager.GetMapRegionSize();
342 break; 344 break;
343 case GetInfoType::HeapRegionBaseAddr: 345 case GetInfoType::HeapRegionBaseAddr:
344 *result = Memory::HEAP_VADDR; 346 *result = vm_manager.GetHeapRegionBaseAddress();
345 break; 347 break;
346 case GetInfoType::HeapRegionSize: 348 case GetInfoType::HeapRegionSize:
347 *result = Memory::HEAP_SIZE; 349 *result = vm_manager.GetHeapRegionSize();
348 break; 350 break;
349 case GetInfoType::TotalMemoryUsage: 351 case GetInfoType::TotalMemoryUsage:
350 *result = vm_manager.GetTotalMemoryUsage(); 352 *result = vm_manager.GetTotalMemoryUsage();
@@ -359,22 +361,35 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id)
359 *result = 0; 361 *result = 0;
360 break; 362 break;
361 case GetInfoType::AddressSpaceBaseAddr: 363 case GetInfoType::AddressSpaceBaseAddr:
362 *result = vm_manager.GetAddressSpaceBaseAddr(); 364 *result = vm_manager.GetCodeRegionBaseAddress();
363 break; 365 break;
364 case GetInfoType::AddressSpaceSize: 366 case GetInfoType::AddressSpaceSize: {
365 *result = vm_manager.GetAddressSpaceSize(); 367 const u64 width = vm_manager.GetAddressSpaceWidth();
368
369 switch (width) {
370 case 32:
371 *result = 0xFFE00000;
372 break;
373 case 36:
374 *result = 0xFF8000000;
375 break;
376 case 39:
377 *result = 0x7FF8000000;
378 break;
379 }
366 break; 380 break;
381 }
367 case GetInfoType::NewMapRegionBaseAddr: 382 case GetInfoType::NewMapRegionBaseAddr:
368 *result = Memory::NEW_MAP_REGION_VADDR; 383 *result = vm_manager.GetNewMapRegionBaseAddress();
369 break; 384 break;
370 case GetInfoType::NewMapRegionSize: 385 case GetInfoType::NewMapRegionSize:
371 *result = Memory::NEW_MAP_REGION_SIZE; 386 *result = vm_manager.GetNewMapRegionSize();
372 break; 387 break;
373 case GetInfoType::IsVirtualAddressMemoryEnabled: 388 case GetInfoType::IsVirtualAddressMemoryEnabled:
374 *result = Core::CurrentProcess()->is_virtual_address_memory_enabled; 389 *result = current_process->IsVirtualMemoryEnabled();
375 break; 390 break;
376 case GetInfoType::TitleId: 391 case GetInfoType::TitleId:
377 *result = Core::CurrentProcess()->program_id; 392 *result = current_process->GetTitleID();
378 break; 393 break;
379 case GetInfoType::PrivilegedProcessId: 394 case GetInfoType::PrivilegedProcessId:
380 LOG_WARNING(Kernel_SVC, 395 LOG_WARNING(Kernel_SVC,
@@ -400,8 +415,36 @@ static ResultCode SetThreadActivity(Handle handle, u32 unknown) {
400} 415}
401 416
402/// Gets the thread context 417/// Gets the thread context
403static ResultCode GetThreadContext(Handle handle, VAddr addr) { 418static ResultCode GetThreadContext(VAddr thread_context, Handle handle) {
404 LOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, addr=0x{:X}", handle, addr); 419 LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle);
420
421 auto& kernel = Core::System::GetInstance().Kernel();
422 const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(handle);
423 if (!thread) {
424 return ERR_INVALID_HANDLE;
425 }
426
427 const auto current_process = Core::CurrentProcess();
428 if (thread->owner_process != current_process) {
429 return ERR_INVALID_HANDLE;
430 }
431
432 if (thread == GetCurrentThread()) {
433 return ERR_ALREADY_REGISTERED;
434 }
435
436 Core::ARM_Interface::ThreadContext ctx = thread->context;
437 // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
438 ctx.pstate &= 0xFF0FFE20;
439
440 // If 64-bit, we can just write the context registers directly and we're good.
441 // However, if 32-bit, we have to ensure some registers are zeroed out.
442 if (!current_process->Is64BitProcess()) {
443 std::fill(ctx.cpu_registers.begin() + 15, ctx.cpu_registers.end(), 0);
444 std::fill(ctx.vector_registers.begin() + 16, ctx.vector_registers.end(), u128{});
445 }
446
447 Memory::WriteBlock(thread_context, &ctx, sizeof(ctx));
405 return RESULT_SUCCESS; 448 return RESULT_SUCCESS;
406} 449}
407 450
@@ -429,8 +472,8 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) {
429 472
430 // Note: The kernel uses the current process's resource limit instead of 473 // Note: The kernel uses the current process's resource limit instead of
431 // the one from the thread owner's resource limit. 474 // the one from the thread owner's resource limit.
432 SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit; 475 const ResourceLimit& resource_limit = Core::CurrentProcess()->GetResourceLimit();
433 if (resource_limit->GetMaxResourceValue(ResourceType::Priority) > priority) { 476 if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
434 return ERR_NOT_AUTHORIZED; 477 return ERR_NOT_AUTHORIZED;
435 } 478 }
436 479
@@ -504,9 +547,9 @@ static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_i
504 if (!process) { 547 if (!process) {
505 return ERR_INVALID_HANDLE; 548 return ERR_INVALID_HANDLE;
506 } 549 }
507 auto vma = process->vm_manager.FindVMA(addr); 550 auto vma = process->VMManager().FindVMA(addr);
508 memory_info->attributes = 0; 551 memory_info->attributes = 0;
509 if (vma == Core::CurrentProcess()->vm_manager.vma_map.end()) { 552 if (vma == Core::CurrentProcess()->VMManager().vma_map.end()) {
510 memory_info->base_address = 0; 553 memory_info->base_address = 0;
511 memory_info->permission = static_cast<u32>(VMAPermission::None); 554 memory_info->permission = static_cast<u32>(VMAPermission::None);
512 memory_info->size = 0; 555 memory_info->size = 0;
@@ -530,35 +573,13 @@ static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAdd
530 573
531/// Exits the current process 574/// Exits the current process
532static void ExitProcess() { 575static void ExitProcess() {
533 LOG_INFO(Kernel_SVC, "Process {} exiting", Core::CurrentProcess()->process_id); 576 auto& current_process = Core::CurrentProcess();
534 577
535 ASSERT_MSG(Core::CurrentProcess()->status == ProcessStatus::Running, 578 LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
579 ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
536 "Process has already exited"); 580 "Process has already exited");
537 581
538 Core::CurrentProcess()->status = ProcessStatus::Exited; 582 current_process->PrepareForTermination();
539
540 auto stop_threads = [](const std::vector<SharedPtr<Thread>>& thread_list) {
541 for (auto& thread : thread_list) {
542 if (thread->owner_process != Core::CurrentProcess())
543 continue;
544
545 if (thread == GetCurrentThread())
546 continue;
547
548 // TODO(Subv): When are the other running/ready threads terminated?
549 ASSERT_MSG(thread->status == ThreadStatus::WaitSynchAny ||
550 thread->status == ThreadStatus::WaitSynchAll,
551 "Exiting processes with non-waiting threads is currently unimplemented");
552
553 thread->Stop();
554 }
555 };
556
557 auto& system = Core::System::GetInstance();
558 stop_threads(system.Scheduler(0)->GetThreadList());
559 stop_threads(system.Scheduler(1)->GetThreadList());
560 stop_threads(system.Scheduler(2)->GetThreadList());
561 stop_threads(system.Scheduler(3)->GetThreadList());
562 583
563 // Kill the current thread 584 // Kill the current thread
564 GetCurrentThread()->Stop(); 585 GetCurrentThread()->Stop();
@@ -575,14 +596,14 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V
575 return ERR_INVALID_THREAD_PRIORITY; 596 return ERR_INVALID_THREAD_PRIORITY;
576 } 597 }
577 598
578 SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit; 599 const ResourceLimit& resource_limit = Core::CurrentProcess()->GetResourceLimit();
579 if (resource_limit->GetMaxResourceValue(ResourceType::Priority) > priority) { 600 if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
580 return ERR_NOT_AUTHORIZED; 601 return ERR_NOT_AUTHORIZED;
581 } 602 }
582 603
583 if (processor_id == THREADPROCESSORID_DEFAULT) { 604 if (processor_id == THREADPROCESSORID_DEFAULT) {
584 // Set the target CPU to the one specified in the process' exheader. 605 // Set the target CPU to the one specified in the process' exheader.
585 processor_id = Core::CurrentProcess()->ideal_processor; 606 processor_id = Core::CurrentProcess()->GetDefaultProcessorID();
586 ASSERT(processor_id != THREADPROCESSORID_DEFAULT); 607 ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
587 } 608 }
588 609
@@ -909,10 +930,10 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) {
909 } 930 }
910 931
911 if (core == static_cast<u32>(THREADPROCESSORID_DEFAULT)) { 932 if (core == static_cast<u32>(THREADPROCESSORID_DEFAULT)) {
912 ASSERT(thread->owner_process->ideal_processor != 933 ASSERT(thread->owner_process->GetDefaultProcessorID() !=
913 static_cast<u8>(THREADPROCESSORID_DEFAULT)); 934 static_cast<u8>(THREADPROCESSORID_DEFAULT));
914 // Set the target CPU to the one specified in the process' exheader. 935 // Set the target CPU to the one specified in the process' exheader.
915 core = thread->owner_process->ideal_processor; 936 core = thread->owner_process->GetDefaultProcessorID();
916 mask = 1ull << core; 937 mask = 1ull << core;
917 } 938 }
918 939
@@ -1039,7 +1060,7 @@ static const FunctionDef SVC_Table[] = {
1039 {0x2B, nullptr, "FlushDataCache"}, 1060 {0x2B, nullptr, "FlushDataCache"},
1040 {0x2C, nullptr, "MapPhysicalMemory"}, 1061 {0x2C, nullptr, "MapPhysicalMemory"},
1041 {0x2D, nullptr, "UnmapPhysicalMemory"}, 1062 {0x2D, nullptr, "UnmapPhysicalMemory"},
1042 {0x2E, nullptr, "GetNextThreadInfo"}, 1063 {0x2E, nullptr, "GetFutureThreadInfo"},
1043 {0x2F, nullptr, "GetLastThreadInfo"}, 1064 {0x2F, nullptr, "GetLastThreadInfo"},
1044 {0x30, nullptr, "GetResourceLimitLimitValue"}, 1065 {0x30, nullptr, "GetResourceLimitLimitValue"},
1045 {0x31, nullptr, "GetResourceLimitCurrentValue"}, 1066 {0x31, nullptr, "GetResourceLimitCurrentValue"},
@@ -1065,11 +1086,11 @@ static const FunctionDef SVC_Table[] = {
1065 {0x45, nullptr, "CreateEvent"}, 1086 {0x45, nullptr, "CreateEvent"},
1066 {0x46, nullptr, "Unknown"}, 1087 {0x46, nullptr, "Unknown"},
1067 {0x47, nullptr, "Unknown"}, 1088 {0x47, nullptr, "Unknown"},
1068 {0x48, nullptr, "AllocateUnsafeMemory"}, 1089 {0x48, nullptr, "MapPhysicalMemoryUnsafe"},
1069 {0x49, nullptr, "FreeUnsafeMemory"}, 1090 {0x49, nullptr, "UnmapPhysicalMemoryUnsafe"},
1070 {0x4A, nullptr, "SetUnsafeAllocationLimit"}, 1091 {0x4A, nullptr, "SetUnsafeLimit"},
1071 {0x4B, nullptr, "CreateJitMemory"}, 1092 {0x4B, nullptr, "CreateCodeMemory"},
1072 {0x4C, nullptr, "MapJitMemory"}, 1093 {0x4C, nullptr, "ControlCodeMemory"},
1073 {0x4D, nullptr, "SleepSystem"}, 1094 {0x4D, nullptr, "SleepSystem"},
1074 {0x4E, nullptr, "ReadWriteRegister"}, 1095 {0x4E, nullptr, "ReadWriteRegister"},
1075 {0x4F, nullptr, "SetProcessActivity"}, 1096 {0x4F, nullptr, "SetProcessActivity"},
@@ -1104,7 +1125,7 @@ static const FunctionDef SVC_Table[] = {
1104 {0x6C, nullptr, "SetHardwareBreakPoint"}, 1125 {0x6C, nullptr, "SetHardwareBreakPoint"},
1105 {0x6D, nullptr, "GetDebugThreadParam"}, 1126 {0x6D, nullptr, "GetDebugThreadParam"},
1106 {0x6E, nullptr, "Unknown"}, 1127 {0x6E, nullptr, "Unknown"},
1107 {0x6F, nullptr, "GetMemoryInfo"}, 1128 {0x6F, nullptr, "GetSystemInfo"},
1108 {0x70, nullptr, "CreatePort"}, 1129 {0x70, nullptr, "CreatePort"},
1109 {0x71, nullptr, "ManageNamedPort"}, 1130 {0x71, nullptr, "ManageNamedPort"},
1110 {0x72, nullptr, "ConnectToPort"}, 1131 {0x72, nullptr, "ConnectToPort"},
diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h
index fea9ba5ea..22712e64f 100644
--- a/src/core/hle/kernel/svc_wrap.h
+++ b/src/core/hle/kernel/svc_wrap.h
@@ -64,6 +64,11 @@ void SvcWrap() {
64 FuncReturn(func(Param(0), (s32)Param(1)).raw); 64 FuncReturn(func(Param(0), (s32)Param(1)).raw);
65} 65}
66 66
67template <ResultCode func(u64, u32)>
68void SvcWrap() {
69 FuncReturn(func(Param(0), static_cast<u32>(Param(1))).raw);
70}
71
67template <ResultCode func(u64*, u64)> 72template <ResultCode func(u64*, u64)>
68void SvcWrap() { 73void SvcWrap() {
69 u64 param_1 = 0; 74 u64 param_1 = 0;
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index d4183d6e3..b5c16cfbb 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -65,10 +65,7 @@ void Thread::Stop() {
65 wait_objects.clear(); 65 wait_objects.clear();
66 66
67 // Mark the TLS slot in the thread's page as free. 67 // Mark the TLS slot in the thread's page as free.
68 const u64 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE; 68 owner_process->FreeTLSSlot(tls_address);
69 const u64 tls_slot =
70 ((tls_address - Memory::TLS_AREA_VADDR) % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
71 Core::CurrentProcess()->tls_slots[tls_page].reset(tls_slot);
72} 69}
73 70
74void WaitCurrentThread_Sleep() { 71void WaitCurrentThread_Sleep() {
@@ -178,32 +175,6 @@ void Thread::ResumeFromWait() {
178} 175}
179 176
180/** 177/**
181 * Finds a free location for the TLS section of a thread.
182 * @param tls_slots The TLS page array of the thread's owner process.
183 * Returns a tuple of (page, slot, alloc_needed) where:
184 * page: The index of the first allocated TLS page that has free slots.
185 * slot: The index of the first free slot in the indicated page.
186 * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full).
187 */
188static std::tuple<std::size_t, std::size_t, bool> GetFreeThreadLocalSlot(
189 const std::vector<std::bitset<8>>& tls_slots) {
190 // Iterate over all the allocated pages, and try to find one where not all slots are used.
191 for (std::size_t page = 0; page < tls_slots.size(); ++page) {
192 const auto& page_tls_slots = tls_slots[page];
193 if (!page_tls_slots.all()) {
194 // We found a page with at least one free slot, find which slot it is
195 for (std::size_t slot = 0; slot < page_tls_slots.size(); ++slot) {
196 if (!page_tls_slots.test(slot)) {
197 return std::make_tuple(page, slot, false);
198 }
199 }
200 }
201 }
202
203 return std::make_tuple(0, 0, true);
204}
205
206/**
207 * Resets a thread context, making it ready to be scheduled and run by the CPU 178 * Resets a thread context, making it ready to be scheduled and run by the CPU
208 * @param context Thread context to reset 179 * @param context Thread context to reset
209 * @param stack_top Address of the top of the stack 180 * @param stack_top Address of the top of the stack
@@ -264,32 +235,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(KernelCore& kernel, std::string name
264 thread->owner_process = owner_process; 235 thread->owner_process = owner_process;
265 thread->scheduler = Core::System::GetInstance().Scheduler(processor_id); 236 thread->scheduler = Core::System::GetInstance().Scheduler(processor_id);
266 thread->scheduler->AddThread(thread, priority); 237 thread->scheduler->AddThread(thread, priority);
267 238 thread->tls_address = thread->owner_process->MarkNextAvailableTLSSlotAsUsed(*thread);
268 // Find the next available TLS index, and mark it as used
269 auto& tls_slots = owner_process->tls_slots;
270
271 auto [available_page, available_slot, needs_allocation] = GetFreeThreadLocalSlot(tls_slots);
272 if (needs_allocation) {
273 tls_slots.emplace_back(0); // The page is completely available at the start
274 available_page = tls_slots.size() - 1;
275 available_slot = 0; // Use the first slot in the new page
276
277 // Allocate some memory from the end of the linear heap for this region.
278 const std::size_t offset = thread->tls_memory->size();
279 thread->tls_memory->insert(thread->tls_memory->end(), Memory::PAGE_SIZE, 0);
280
281 auto& vm_manager = owner_process->vm_manager;
282 vm_manager.RefreshMemoryBlockMappings(thread->tls_memory.get());
283
284 vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE,
285 thread->tls_memory, 0, Memory::PAGE_SIZE,
286 MemoryState::ThreadLocal);
287 }
288
289 // Mark the slot as used
290 tls_slots[available_page].set(available_slot);
291 thread->tls_address = Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE +
292 available_slot * Memory::TLS_ENTRY_SIZE;
293 239
294 // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used 240 // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used
295 // to initialize the context 241 // to initialize the context
@@ -311,13 +257,14 @@ void Thread::BoostPriority(u32 priority) {
311} 257}
312 258
313SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority, 259SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority,
314 SharedPtr<Process> owner_process) { 260 Process& owner_process) {
315 // Setup page table so we can write to memory 261 // Setup page table so we can write to memory
316 SetCurrentPageTable(&Core::CurrentProcess()->vm_manager.page_table); 262 SetCurrentPageTable(&owner_process.VMManager().page_table);
317 263
318 // Initialize new "main" thread 264 // Initialize new "main" thread
265 const VAddr stack_top = owner_process.VMManager().GetTLSIORegionEndAddress();
319 auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0, THREADPROCESSORID_0, 266 auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0, THREADPROCESSORID_0,
320 Memory::STACK_AREA_VADDR_END, std::move(owner_process)); 267 stack_top, &owner_process);
321 268
322 SharedPtr<Thread> thread = std::move(thread_res).Unwrap(); 269 SharedPtr<Thread> thread = std::move(thread_res).Unwrap();
323 270
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index df4748942..4250144c3 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -62,6 +62,9 @@ enum class ThreadWakeupReason {
62 62
63class Thread final : public WaitObject { 63class Thread final : public WaitObject {
64public: 64public:
65 using TLSMemory = std::vector<u8>;
66 using TLSMemoryPtr = std::shared_ptr<TLSMemory>;
67
65 /** 68 /**
66 * Creates and returns a new thread. The new thread is immediately scheduled 69 * Creates and returns a new thread. The new thread is immediately scheduled
67 * @param kernel The kernel instance this thread will be created under. 70 * @param kernel The kernel instance this thread will be created under.
@@ -134,6 +137,14 @@ public:
134 return thread_id; 137 return thread_id;
135 } 138 }
136 139
140 TLSMemoryPtr& GetTLSMemory() {
141 return tls_memory;
142 }
143
144 const TLSMemoryPtr& GetTLSMemory() const {
145 return tls_memory;
146 }
147
137 /** 148 /**
138 * Resumes a thread from waiting 149 * Resumes a thread from waiting
139 */ 150 */
@@ -269,7 +280,7 @@ private:
269 explicit Thread(KernelCore& kernel); 280 explicit Thread(KernelCore& kernel);
270 ~Thread() override; 281 ~Thread() override;
271 282
272 std::shared_ptr<std::vector<u8>> tls_memory = std::make_shared<std::vector<u8>>(); 283 TLSMemoryPtr tls_memory = std::make_shared<TLSMemory>();
273}; 284};
274 285
275/** 286/**
@@ -281,7 +292,7 @@ private:
281 * @return A shared pointer to the main thread 292 * @return A shared pointer to the main thread
282 */ 293 */
283SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority, 294SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority,
284 SharedPtr<Process> owner_process); 295 Process& owner_process);
285 296
286/** 297/**
287 * Gets the current thread 298 * Gets the current thread
diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp
index 608cbd57b..e412309fd 100644
--- a/src/core/hle/kernel/vm_manager.cpp
+++ b/src/core/hle/kernel/vm_manager.cpp
@@ -9,6 +9,7 @@
9#include "common/logging/log.h" 9#include "common/logging/log.h"
10#include "core/arm/arm_interface.h" 10#include "core/arm/arm_interface.h"
11#include "core/core.h" 11#include "core/core.h"
12#include "core/file_sys/program_metadata.h"
12#include "core/hle/kernel/errors.h" 13#include "core/hle/kernel/errors.h"
13#include "core/hle/kernel/vm_manager.h" 14#include "core/hle/kernel/vm_manager.h"
14#include "core/memory.h" 15#include "core/memory.h"
@@ -54,30 +55,32 @@ bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const {
54} 55}
55 56
56VMManager::VMManager() { 57VMManager::VMManager() {
57 Reset(); 58 // Default to assuming a 39-bit address space. This way we have a sane
59 // starting point with executables that don't provide metadata.
60 Reset(FileSys::ProgramAddressSpaceType::Is39Bit);
58} 61}
59 62
60VMManager::~VMManager() { 63VMManager::~VMManager() {
61 Reset(); 64 Reset(FileSys::ProgramAddressSpaceType::Is39Bit);
62} 65}
63 66
64void VMManager::Reset() { 67void VMManager::Reset(FileSys::ProgramAddressSpaceType type) {
65 vma_map.clear(); 68 Clear();
69
70 InitializeMemoryRegionRanges(type);
71
72 page_table.Resize(address_space_width);
66 73
67 // Initialize the map with a single free region covering the entire managed space. 74 // Initialize the map with a single free region covering the entire managed space.
68 VirtualMemoryArea initial_vma; 75 VirtualMemoryArea initial_vma;
69 initial_vma.size = MAX_ADDRESS; 76 initial_vma.size = address_space_end;
70 vma_map.emplace(initial_vma.base, initial_vma); 77 vma_map.emplace(initial_vma.base, initial_vma);
71 78
72 page_table.pointers.fill(nullptr);
73 page_table.special_regions.clear();
74 page_table.attributes.fill(Memory::PageType::Unmapped);
75
76 UpdatePageTableForVMA(initial_vma); 79 UpdatePageTableForVMA(initial_vma);
77} 80}
78 81
79VMManager::VMAHandle VMManager::FindVMA(VAddr target) const { 82VMManager::VMAHandle VMManager::FindVMA(VAddr target) const {
80 if (target >= MAX_ADDRESS) { 83 if (target >= address_space_end) {
81 return vma_map.end(); 84 return vma_map.end();
82 } else { 85 } else {
83 return std::prev(vma_map.upper_bound(target)); 86 return std::prev(vma_map.upper_bound(target));
@@ -291,7 +294,7 @@ ResultVal<VMManager::VMAIter> VMManager::CarveVMARange(VAddr target, u64 size) {
291 294
292 const VAddr target_end = target + size; 295 const VAddr target_end = target + size;
293 ASSERT(target_end >= target); 296 ASSERT(target_end >= target);
294 ASSERT(target_end <= MAX_ADDRESS); 297 ASSERT(target_end <= address_space_end);
295 ASSERT(size > 0); 298 ASSERT(size > 0);
296 299
297 VMAIter begin_vma = StripIterConstness(FindVMA(target)); 300 VMAIter begin_vma = StripIterConstness(FindVMA(target));
@@ -382,6 +385,85 @@ void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
382 } 385 }
383} 386}
384 387
388void VMManager::InitializeMemoryRegionRanges(FileSys::ProgramAddressSpaceType type) {
389 u64 map_region_size = 0;
390 u64 heap_region_size = 0;
391 u64 new_map_region_size = 0;
392 u64 tls_io_region_size = 0;
393
394 switch (type) {
395 case FileSys::ProgramAddressSpaceType::Is32Bit:
396 address_space_width = 32;
397 code_region_base = 0x200000;
398 code_region_end = code_region_base + 0x3FE00000;
399 map_region_size = 0x40000000;
400 heap_region_size = 0x40000000;
401 break;
402 case FileSys::ProgramAddressSpaceType::Is36Bit:
403 address_space_width = 36;
404 code_region_base = 0x8000000;
405 code_region_end = code_region_base + 0x78000000;
406 map_region_size = 0x180000000;
407 heap_region_size = 0x180000000;
408 break;
409 case FileSys::ProgramAddressSpaceType::Is32BitNoMap:
410 address_space_width = 32;
411 code_region_base = 0x200000;
412 code_region_end = code_region_base + 0x3FE00000;
413 map_region_size = 0;
414 heap_region_size = 0x80000000;
415 break;
416 case FileSys::ProgramAddressSpaceType::Is39Bit:
417 address_space_width = 39;
418 code_region_base = 0x8000000;
419 code_region_end = code_region_base + 0x80000000;
420 map_region_size = 0x1000000000;
421 heap_region_size = 0x180000000;
422 new_map_region_size = 0x80000000;
423 tls_io_region_size = 0x1000000000;
424 break;
425 default:
426 UNREACHABLE_MSG("Invalid address space type specified: {}", static_cast<u32>(type));
427 return;
428 }
429
430 address_space_base = 0;
431 address_space_end = 1ULL << address_space_width;
432
433 map_region_base = code_region_end;
434 map_region_end = map_region_base + map_region_size;
435
436 heap_region_base = map_region_end;
437 heap_region_end = heap_region_base + heap_region_size;
438
439 new_map_region_base = heap_region_end;
440 new_map_region_end = new_map_region_base + new_map_region_size;
441
442 tls_io_region_base = new_map_region_end;
443 tls_io_region_end = tls_io_region_base + tls_io_region_size;
444
445 if (new_map_region_size == 0) {
446 new_map_region_base = address_space_base;
447 new_map_region_end = address_space_end;
448 }
449}
450
451void VMManager::Clear() {
452 ClearVMAMap();
453 ClearPageTable();
454}
455
456void VMManager::ClearVMAMap() {
457 vma_map.clear();
458}
459
460void VMManager::ClearPageTable() {
461 std::fill(page_table.pointers.begin(), page_table.pointers.end(), nullptr);
462 page_table.special_regions.clear();
463 std::fill(page_table.attributes.begin(), page_table.attributes.end(),
464 Memory::PageType::Unmapped);
465}
466
385u64 VMManager::GetTotalMemoryUsage() const { 467u64 VMManager::GetTotalMemoryUsage() const {
386 LOG_WARNING(Kernel, "(STUBBED) called"); 468 LOG_WARNING(Kernel, "(STUBBED) called");
387 return 0xF8000000; 469 return 0xF8000000;
@@ -392,14 +474,80 @@ u64 VMManager::GetTotalHeapUsage() const {
392 return 0x0; 474 return 0x0;
393} 475}
394 476
395VAddr VMManager::GetAddressSpaceBaseAddr() const { 477VAddr VMManager::GetAddressSpaceBaseAddress() const {
396 LOG_WARNING(Kernel, "(STUBBED) called"); 478 return address_space_base;
397 return 0x8000000; 479}
480
481VAddr VMManager::GetAddressSpaceEndAddress() const {
482 return address_space_end;
398} 483}
399 484
400u64 VMManager::GetAddressSpaceSize() const { 485u64 VMManager::GetAddressSpaceSize() const {
401 LOG_WARNING(Kernel, "(STUBBED) called"); 486 return address_space_end - address_space_base;
402 return MAX_ADDRESS; 487}
488
489u64 VMManager::GetAddressSpaceWidth() const {
490 return address_space_width;
491}
492
493VAddr VMManager::GetCodeRegionBaseAddress() const {
494 return code_region_base;
495}
496
497VAddr VMManager::GetCodeRegionEndAddress() const {
498 return code_region_end;
499}
500
501u64 VMManager::GetCodeRegionSize() const {
502 return code_region_end - code_region_base;
503}
504
505VAddr VMManager::GetHeapRegionBaseAddress() const {
506 return heap_region_base;
507}
508
509VAddr VMManager::GetHeapRegionEndAddress() const {
510 return heap_region_end;
511}
512
513u64 VMManager::GetHeapRegionSize() const {
514 return heap_region_end - heap_region_base;
515}
516
517VAddr VMManager::GetMapRegionBaseAddress() const {
518 return map_region_base;
519}
520
521VAddr VMManager::GetMapRegionEndAddress() const {
522 return map_region_end;
523}
524
525u64 VMManager::GetMapRegionSize() const {
526 return map_region_end - map_region_base;
527}
528
529VAddr VMManager::GetNewMapRegionBaseAddress() const {
530 return new_map_region_base;
531}
532
533VAddr VMManager::GetNewMapRegionEndAddress() const {
534 return new_map_region_end;
535}
536
537u64 VMManager::GetNewMapRegionSize() const {
538 return new_map_region_end - new_map_region_base;
539}
540
541VAddr VMManager::GetTLSIORegionBaseAddress() const {
542 return tls_io_region_base;
543}
544
545VAddr VMManager::GetTLSIORegionEndAddress() const {
546 return tls_io_region_end;
547}
548
549u64 VMManager::GetTLSIORegionSize() const {
550 return tls_io_region_end - tls_io_region_base;
403} 551}
404 552
405} // namespace Kernel 553} // namespace Kernel
diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h
index de75036c0..015559a64 100644
--- a/src/core/hle/kernel/vm_manager.h
+++ b/src/core/hle/kernel/vm_manager.h
@@ -12,6 +12,10 @@
12#include "core/memory.h" 12#include "core/memory.h"
13#include "core/memory_hook.h" 13#include "core/memory_hook.h"
14 14
15namespace FileSys {
16enum class ProgramAddressSpaceType : u8;
17}
18
15namespace Kernel { 19namespace Kernel {
16 20
17enum class VMAType : u8 { 21enum class VMAType : u8 {
@@ -111,12 +115,6 @@ struct VirtualMemoryArea {
111class VMManager final { 115class VMManager final {
112public: 116public:
113 /** 117 /**
114 * The maximum amount of address space managed by the kernel.
115 * @todo This was selected arbitrarily, and should be verified for Switch OS.
116 */
117 static constexpr VAddr MAX_ADDRESS{0x1000000000ULL};
118
119 /**
120 * A map covering the entirety of the managed address space, keyed by the `base` field of each 118 * A map covering the entirety of the managed address space, keyed by the `base` field of each
121 * VMA. It must always be modified by splitting or merging VMAs, so that the invariant 119 * VMA. It must always be modified by splitting or merging VMAs, so that the invariant
122 * `elem.base + elem.size == next.base` is preserved, and mergeable regions must always be 120 * `elem.base + elem.size == next.base` is preserved, and mergeable regions must always be
@@ -130,7 +128,7 @@ public:
130 ~VMManager(); 128 ~VMManager();
131 129
132 /// Clears the address space map, re-initializing with a single free area. 130 /// Clears the address space map, re-initializing with a single free area.
133 void Reset(); 131 void Reset(FileSys::ProgramAddressSpaceType type);
134 132
135 /// Finds the VMA in which the given address is included in, or `vma_map.end()`. 133 /// Finds the VMA in which the given address is included in, or `vma_map.end()`.
136 VMAHandle FindVMA(VAddr target) const; 134 VMAHandle FindVMA(VAddr target) const;
@@ -195,12 +193,63 @@ public:
195 /// Gets the total heap usage, used by svcGetInfo 193 /// Gets the total heap usage, used by svcGetInfo
196 u64 GetTotalHeapUsage() const; 194 u64 GetTotalHeapUsage() const;
197 195
198 /// Gets the total address space base address, used by svcGetInfo 196 /// Gets the address space base address
199 VAddr GetAddressSpaceBaseAddr() const; 197 VAddr GetAddressSpaceBaseAddress() const;
200 198
201 /// Gets the total address space address size, used by svcGetInfo 199 /// Gets the address space end address
200 VAddr GetAddressSpaceEndAddress() const;
201
202 /// Gets the total address space address size in bytes
202 u64 GetAddressSpaceSize() const; 203 u64 GetAddressSpaceSize() const;
203 204
205 /// Gets the address space width in bits.
206 u64 GetAddressSpaceWidth() const;
207
208 /// Gets the base address of the code region.
209 VAddr GetCodeRegionBaseAddress() const;
210
211 /// Gets the end address of the code region.
212 VAddr GetCodeRegionEndAddress() const;
213
214 /// Gets the total size of the code region in bytes.
215 u64 GetCodeRegionSize() const;
216
217 /// Gets the base address of the heap region.
218 VAddr GetHeapRegionBaseAddress() const;
219
220 /// Gets the end address of the heap region;
221 VAddr GetHeapRegionEndAddress() const;
222
223 /// Gets the total size of the heap region in bytes.
224 u64 GetHeapRegionSize() const;
225
226 /// Gets the base address of the map region.
227 VAddr GetMapRegionBaseAddress() const;
228
229 /// Gets the end address of the map region.
230 VAddr GetMapRegionEndAddress() const;
231
232 /// Gets the total size of the map region in bytes.
233 u64 GetMapRegionSize() const;
234
235 /// Gets the base address of the new map region.
236 VAddr GetNewMapRegionBaseAddress() const;
237
238 /// Gets the end address of the new map region.
239 VAddr GetNewMapRegionEndAddress() const;
240
241 /// Gets the total size of the new map region in bytes.
242 u64 GetNewMapRegionSize() const;
243
244 /// Gets the base address of the TLS IO region.
245 VAddr GetTLSIORegionBaseAddress() const;
246
247 /// Gets the end address of the TLS IO region.
248 VAddr GetTLSIORegionEndAddress() const;
249
250 /// Gets the total size of the TLS IO region in bytes.
251 u64 GetTLSIORegionSize() const;
252
204 /// Each VMManager has its own page table, which is set as the main one when the owning process 253 /// Each VMManager has its own page table, which is set as the main one when the owning process
205 /// is scheduled. 254 /// is scheduled.
206 Memory::PageTable page_table; 255 Memory::PageTable page_table;
@@ -240,5 +289,36 @@ private:
240 289
241 /// Updates the pages corresponding to this VMA so they match the VMA's attributes. 290 /// Updates the pages corresponding to this VMA so they match the VMA's attributes.
242 void UpdatePageTableForVMA(const VirtualMemoryArea& vma); 291 void UpdatePageTableForVMA(const VirtualMemoryArea& vma);
292
293 /// Initializes memory region ranges to adhere to a given address space type.
294 void InitializeMemoryRegionRanges(FileSys::ProgramAddressSpaceType type);
295
296 /// Clears the underlying map and page table.
297 void Clear();
298
299 /// Clears out the VMA map, unmapping any previously mapped ranges.
300 void ClearVMAMap();
301
302 /// Clears out the page table
303 void ClearPageTable();
304
305 u32 address_space_width = 0;
306 VAddr address_space_base = 0;
307 VAddr address_space_end = 0;
308
309 VAddr code_region_base = 0;
310 VAddr code_region_end = 0;
311
312 VAddr heap_region_base = 0;
313 VAddr heap_region_end = 0;
314
315 VAddr map_region_base = 0;
316 VAddr map_region_end = 0;
317
318 VAddr new_map_region_base = 0;
319 VAddr new_map_region_end = 0;
320
321 VAddr tls_io_region_base = 0;
322 VAddr tls_io_region_end = 0;
243}; 323};
244} // namespace Kernel 324} // namespace Kernel
diff --git a/src/core/hle/kernel/wait_object.h b/src/core/hle/kernel/wait_object.h
index 0bd97133c..f4367ee28 100644
--- a/src/core/hle/kernel/wait_object.h
+++ b/src/core/hle/kernel/wait_object.h
@@ -69,7 +69,7 @@ private:
69template <> 69template <>
70inline SharedPtr<WaitObject> DynamicObjectCast<WaitObject>(SharedPtr<Object> object) { 70inline SharedPtr<WaitObject> DynamicObjectCast<WaitObject>(SharedPtr<Object> object) {
71 if (object != nullptr && object->IsWaitable()) { 71 if (object != nullptr && object->IsWaitable()) {
72 return boost::static_pointer_cast<WaitObject>(std::move(object)); 72 return boost::static_pointer_cast<WaitObject>(object);
73 } 73 }
74 return nullptr; 74 return nullptr;
75} 75}
diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp
index 4d4eb542e..e61748ca3 100644
--- a/src/core/hle/service/acc/acc.cpp
+++ b/src/core/hle/service/acc/acc.cpp
@@ -130,11 +130,10 @@ private:
130 130
131 void GetAccountId(Kernel::HLERequestContext& ctx) { 131 void GetAccountId(Kernel::HLERequestContext& ctx) {
132 LOG_WARNING(Service_ACC, "(STUBBED) called"); 132 LOG_WARNING(Service_ACC, "(STUBBED) called");
133 // TODO(Subv): Find out what this actually does and implement it. Stub it as an error for 133 // Should return a nintendo account ID
134 // now since we do not implement NNID. Returning a bogus id here will cause games to send 134 IPC::ResponseBuilder rb{ctx, 4};
135 // invalid IPC requests after ListOpenUsers is called. 135 rb.Push(RESULT_SUCCESS);
136 IPC::ResponseBuilder rb{ctx, 2}; 136 rb.PushRaw<u64>(1);
137 rb.Push(ResultCode(-1));
138 } 137 }
139}; 138};
140 139
diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index 9c975325a..69bfce1c1 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -462,7 +462,7 @@ private:
462 462
463 std::memcpy(&buffer[offset], data.data(), data.size()); 463 std::memcpy(&buffer[offset], data.data(), data.size());
464 464
465 IPC::ResponseBuilder rb{rp.MakeBuilder(2, 0, 0)}; 465 IPC::ResponseBuilder rb{ctx, 2};
466 rb.Push(RESULT_SUCCESS); 466 rb.Push(RESULT_SUCCESS);
467 467
468 LOG_DEBUG(Service_AM, "called, offset={}", offset); 468 LOG_DEBUG(Service_AM, "called, offset={}", offset);
@@ -478,7 +478,7 @@ private:
478 478
479 ctx.WriteBuffer(buffer.data() + offset, size); 479 ctx.WriteBuffer(buffer.data() + offset, size);
480 480
481 IPC::ResponseBuilder rb{rp.MakeBuilder(2, 0, 0)}; 481 IPC::ResponseBuilder rb{ctx, 2};
482 rb.Push(RESULT_SUCCESS); 482 rb.Push(RESULT_SUCCESS);
483 483
484 LOG_DEBUG(Service_AM, "called, offset={}", offset); 484 LOG_DEBUG(Service_AM, "called, offset={}", offset);
@@ -568,7 +568,7 @@ private:
568 IPC::RequestParser rp{ctx}; 568 IPC::RequestParser rp{ctx};
569 storage_stack.push(rp.PopIpcInterface<AM::IStorage>()); 569 storage_stack.push(rp.PopIpcInterface<AM::IStorage>());
570 570
571 IPC::ResponseBuilder rb{rp.MakeBuilder(2, 0, 0)}; 571 IPC::ResponseBuilder rb{ctx, 2};
572 rb.Push(RESULT_SUCCESS); 572 rb.Push(RESULT_SUCCESS);
573 573
574 LOG_DEBUG(Service_AM, "called"); 574 LOG_DEBUG(Service_AM, "called");
@@ -616,7 +616,7 @@ void ILibraryAppletCreator::CreateStorage(Kernel::HLERequestContext& ctx) {
616 const u64 size{rp.Pop<u64>()}; 616 const u64 size{rp.Pop<u64>()};
617 std::vector<u8> buffer(size); 617 std::vector<u8> buffer(size);
618 618
619 IPC::ResponseBuilder rb{rp.MakeBuilder(2, 0, 1)}; 619 IPC::ResponseBuilder rb{ctx, 2, 0, 1};
620 rb.Push(RESULT_SUCCESS); 620 rb.Push(RESULT_SUCCESS);
621 rb.PushIpcInterface<AM::IStorage>(std::move(buffer)); 621 rb.PushIpcInterface<AM::IStorage>(std::move(buffer));
622 622
diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp
index 80a002322..ff1edefbb 100644
--- a/src/core/hle/service/audio/audout_u.cpp
+++ b/src/core/hle/service/audio/audout_u.cpp
@@ -190,7 +190,7 @@ void AudOutU::ListAudioOutsImpl(Kernel::HLERequestContext& ctx) {
190 190
191 ctx.WriteBuffer(DefaultDevice); 191 ctx.WriteBuffer(DefaultDevice);
192 192
193 IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0); 193 IPC::ResponseBuilder rb{ctx, 3};
194 194
195 rb.Push(RESULT_SUCCESS); 195 rb.Push(RESULT_SUCCESS);
196 rb.Push<u32>(1); // Amount of audio devices 196 rb.Push<u32>(1); // Amount of audio devices
diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp
index e84c4fa2b..6073f4ecd 100644
--- a/src/core/hle/service/audio/audren_u.cpp
+++ b/src/core/hle/service/audio/audren_u.cpp
@@ -10,6 +10,7 @@
10#include "common/alignment.h" 10#include "common/alignment.h"
11#include "common/common_funcs.h" 11#include "common/common_funcs.h"
12#include "common/logging/log.h" 12#include "common/logging/log.h"
13#include "core/core.h"
13#include "core/hle/ipc_helpers.h" 14#include "core/hle/ipc_helpers.h"
14#include "core/hle/kernel/event.h" 15#include "core/hle/kernel/event.h"
15#include "core/hle/kernel/hle_ipc.h" 16#include "core/hle/kernel/hle_ipc.h"
@@ -25,7 +26,7 @@ public:
25 {0, &IAudioRenderer::GetAudioRendererSampleRate, "GetAudioRendererSampleRate"}, 26 {0, &IAudioRenderer::GetAudioRendererSampleRate, "GetAudioRendererSampleRate"},
26 {1, &IAudioRenderer::GetAudioRendererSampleCount, "GetAudioRendererSampleCount"}, 27 {1, &IAudioRenderer::GetAudioRendererSampleCount, "GetAudioRendererSampleCount"},
27 {2, &IAudioRenderer::GetAudioRendererMixBufferCount, "GetAudioRendererMixBufferCount"}, 28 {2, &IAudioRenderer::GetAudioRendererMixBufferCount, "GetAudioRendererMixBufferCount"},
28 {3, nullptr, "GetAudioRendererState"}, 29 {3, &IAudioRenderer::GetAudioRendererState, "GetAudioRendererState"},
29 {4, &IAudioRenderer::RequestUpdateAudioRenderer, "RequestUpdateAudioRenderer"}, 30 {4, &IAudioRenderer::RequestUpdateAudioRenderer, "RequestUpdateAudioRenderer"},
30 {5, &IAudioRenderer::StartAudioRenderer, "StartAudioRenderer"}, 31 {5, &IAudioRenderer::StartAudioRenderer, "StartAudioRenderer"},
31 {6, &IAudioRenderer::StopAudioRenderer, "StopAudioRenderer"}, 32 {6, &IAudioRenderer::StopAudioRenderer, "StopAudioRenderer"},
@@ -62,6 +63,13 @@ private:
62 LOG_DEBUG(Service_Audio, "called"); 63 LOG_DEBUG(Service_Audio, "called");
63 } 64 }
64 65
66 void GetAudioRendererState(Kernel::HLERequestContext& ctx) {
67 IPC::ResponseBuilder rb{ctx, 3};
68 rb.Push(RESULT_SUCCESS);
69 rb.Push<u32>(static_cast<u32>(renderer->GetStreamState()));
70 LOG_DEBUG(Service_Audio, "called");
71 }
72
65 void GetAudioRendererMixBufferCount(Kernel::HLERequestContext& ctx) { 73 void GetAudioRendererMixBufferCount(Kernel::HLERequestContext& ctx) {
66 IPC::ResponseBuilder rb{ctx, 3}; 74 IPC::ResponseBuilder rb{ctx, 3};
67 rb.Push(RESULT_SUCCESS); 75 rb.Push(RESULT_SUCCESS);
@@ -137,7 +145,7 @@ private:
137 constexpr std::array<char, 15> audio_interface{{"AudioInterface"}}; 145 constexpr std::array<char, 15> audio_interface{{"AudioInterface"}};
138 ctx.WriteBuffer(audio_interface); 146 ctx.WriteBuffer(audio_interface);
139 147
140 IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0); 148 IPC::ResponseBuilder rb{ctx, 3};
141 rb.Push(RESULT_SUCCESS); 149 rb.Push(RESULT_SUCCESS);
142 rb.Push<u32>(1); 150 rb.Push<u32>(1);
143 } 151 }
@@ -151,7 +159,7 @@ private:
151 auto file_buffer = ctx.ReadBuffer(); 159 auto file_buffer = ctx.ReadBuffer();
152 auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0'); 160 auto end = std::find(file_buffer.begin(), file_buffer.end(), '\0');
153 161
154 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 162 IPC::ResponseBuilder rb{ctx, 2};
155 rb.Push(RESULT_SUCCESS); 163 rb.Push(RESULT_SUCCESS);
156 } 164 }
157 165
@@ -162,7 +170,7 @@ private:
162 constexpr std::array<char, 12> audio_interface{{"AudioDevice"}}; 170 constexpr std::array<char, 12> audio_interface{{"AudioDevice"}};
163 ctx.WriteBuffer(audio_interface); 171 ctx.WriteBuffer(audio_interface);
164 172
165 IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0); 173 IPC::ResponseBuilder rb{ctx, 3};
166 rb.Push(RESULT_SUCCESS); 174 rb.Push(RESULT_SUCCESS);
167 rb.Push<u32>(1); 175 rb.Push<u32>(1);
168 } 176 }
diff --git a/src/core/hle/service/fatal/fatal.cpp b/src/core/hle/service/fatal/fatal.cpp
index b436ce4e6..2f15ac2a6 100644
--- a/src/core/hle/service/fatal/fatal.cpp
+++ b/src/core/hle/service/fatal/fatal.cpp
@@ -2,8 +2,17 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <array>
6#include <cstring>
7#include <ctime>
8#include <fmt/time.h>
9#include "common/file_util.h"
5#include "common/logging/log.h" 10#include "common/logging/log.h"
11#include "common/scm_rev.h"
12#include "common/swap.h"
13#include "core/core.h"
6#include "core/hle/ipc_helpers.h" 14#include "core/hle/ipc_helpers.h"
15#include "core/hle/kernel/process.h"
7#include "core/hle/service/fatal/fatal.h" 16#include "core/hle/service/fatal/fatal.h"
8#include "core/hle/service/fatal/fatal_p.h" 17#include "core/hle/service/fatal/fatal_p.h"
9#include "core/hle/service/fatal/fatal_u.h" 18#include "core/hle/service/fatal/fatal_u.h"
@@ -15,16 +24,142 @@ Module::Interface::Interface(std::shared_ptr<Module> module, const char* name)
15 24
16Module::Interface::~Interface() = default; 25Module::Interface::~Interface() = default;
17 26
27struct FatalInfo {
28 std::array<u64_le, 31> registers{}; // TODO(ogniK): See if this actually is registers or
29 // not(find a game which has non zero valeus)
30 u64_le unk0{};
31 u64_le unk1{};
32 u64_le unk2{};
33 u64_le unk3{};
34 u64_le unk4{};
35 u64_le unk5{};
36 u64_le unk6{};
37
38 std::array<u64_le, 32> backtrace{};
39 u64_le unk7{};
40 u64_le unk8{};
41 u32_le backtrace_size{};
42 u32_le unk9{};
43 u32_le unk10{}; // TODO(ogniK): Is this even used or is it just padding?
44};
45static_assert(sizeof(FatalInfo) == 0x250, "FatalInfo is an invalid size");
46
47enum class FatalType : u32 {
48 ErrorReportAndScreen = 0,
49 ErrorReport = 1,
50 ErrorScreen = 2,
51};
52
53static void GenerateErrorReport(ResultCode error_code, const FatalInfo& info) {
54 const auto title_id = Core::CurrentProcess()->GetTitleID();
55 std::string crash_report =
56 fmt::format("Yuzu {}-{} crash report\n"
57 "Title ID: {:016x}\n"
58 "Result: 0x{:X} ({:04}-{:04d})\n"
59 "\n",
60 Common::g_scm_branch, Common::g_scm_desc, title_id, error_code.raw,
61 2000 + static_cast<u32>(error_code.module.Value()),
62 static_cast<u32>(error_code.description.Value()), info.unk8, info.unk7);
63 if (info.backtrace_size != 0x0) {
64 crash_report += "Registers:\n";
65 // TODO(ogniK): This is just a guess, find a game which actually has non zero values
66 for (size_t i = 0; i < info.registers.size(); i++) {
67 crash_report +=
68 fmt::format(" X[{:02d}]: {:016x}\n", i, info.registers[i]);
69 }
70 crash_report += fmt::format(" Unknown 0: {:016x}\n", info.unk0);
71 crash_report += fmt::format(" Unknown 1: {:016x}\n", info.unk1);
72 crash_report += fmt::format(" Unknown 2: {:016x}\n", info.unk2);
73 crash_report += fmt::format(" Unknown 3: {:016x}\n", info.unk3);
74 crash_report += fmt::format(" Unknown 4: {:016x}\n", info.unk4);
75 crash_report += fmt::format(" Unknown 5: {:016x}\n", info.unk5);
76 crash_report += fmt::format(" Unknown 6: {:016x}\n", info.unk6);
77 crash_report += "\nBacktrace:\n";
78 for (size_t i = 0; i < info.backtrace_size; i++) {
79 crash_report +=
80 fmt::format(" Backtrace[{:02d}]: {:016x}\n", i, info.backtrace[i]);
81 }
82 crash_report += fmt::format("\nUnknown 7: 0x{:016x}\n", info.unk7);
83 crash_report += fmt::format("Unknown 8: 0x{:016x}\n", info.unk8);
84 crash_report += fmt::format("Unknown 9: 0x{:016x}\n", info.unk9);
85 crash_report += fmt::format("Unknown 10: 0x{:016x}\n", info.unk10);
86 }
87
88 LOG_ERROR(Service_Fatal, "{}", crash_report);
89
90 const std::string crashreport_dir =
91 FileUtil::GetUserPath(FileUtil::UserPath::LogDir) + "crash_logs";
92
93 if (!FileUtil::CreateFullPath(crashreport_dir)) {
94 LOG_ERROR(
95 Service_Fatal,
96 "Unable to create crash report directory. Possible log directory permissions issue.");
97 return;
98 }
99
100 const std::time_t t = std::time(nullptr);
101 const std::string crashreport_filename =
102 fmt::format("{}/{:016x}-{:%F-%H%M%S}.log", crashreport_dir, title_id, *std::localtime(&t));
103
104 auto file = FileUtil::IOFile(crashreport_filename, "wb");
105 if (file.IsOpen()) {
106 file.WriteString(crash_report);
107 LOG_ERROR(Service_Fatal, "Saving error report to {}", crashreport_filename);
108 } else {
109 LOG_ERROR(Service_Fatal, "Failed to save error report to {}", crashreport_filename);
110 }
111}
112
113static void ThrowFatalError(ResultCode error_code, FatalType fatal_type, const FatalInfo& info) {
114 LOG_ERROR(Service_Fatal, "Threw fatal error type {}", static_cast<u32>(fatal_type));
115 switch (fatal_type) {
116 case FatalType::ErrorReportAndScreen:
117 GenerateErrorReport(error_code, info);
118 [[fallthrough]];
119 case FatalType::ErrorScreen:
120 // Since we have no fatal:u error screen. We should just kill execution instead
121 ASSERT(false);
122 break;
123 // Should not throw a fatal screen but should generate an error report
124 case FatalType::ErrorReport:
125 GenerateErrorReport(error_code, info);
126 break;
127 };
128}
129
130void Module::Interface::ThrowFatal(Kernel::HLERequestContext& ctx) {
131 LOG_ERROR(Service_Fatal, "called");
132 IPC::RequestParser rp{ctx};
133 auto error_code = rp.Pop<ResultCode>();
134
135 ThrowFatalError(error_code, FatalType::ErrorScreen, {});
136 IPC::ResponseBuilder rb{ctx, 2};
137 rb.Push(RESULT_SUCCESS);
138}
139
18void Module::Interface::ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx) { 140void Module::Interface::ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx) {
141 LOG_ERROR(Service_Fatal, "called");
19 IPC::RequestParser rp(ctx); 142 IPC::RequestParser rp(ctx);
20 u32 error_code = rp.Pop<u32>(); 143 auto error_code = rp.Pop<ResultCode>();
21 LOG_WARNING(Service_Fatal, "(STUBBED) called, error_code=0x{:X}", error_code); 144 auto fatal_type = rp.PopEnum<FatalType>();
145
146 ThrowFatalError(error_code, fatal_type, {}); // No info is passed with ThrowFatalWithPolicy
22 IPC::ResponseBuilder rb{ctx, 2}; 147 IPC::ResponseBuilder rb{ctx, 2};
23 rb.Push(RESULT_SUCCESS); 148 rb.Push(RESULT_SUCCESS);
24} 149}
25 150
26void Module::Interface::ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx) { 151void Module::Interface::ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx) {
27 LOG_WARNING(Service_Fatal, "(STUBBED) called"); 152 LOG_ERROR(Service_Fatal, "called");
153 IPC::RequestParser rp(ctx);
154 auto error_code = rp.Pop<ResultCode>();
155 auto fatal_type = rp.PopEnum<FatalType>();
156 auto fatal_info = ctx.ReadBuffer();
157 FatalInfo info{};
158
159 ASSERT_MSG(fatal_info.size() == sizeof(FatalInfo), "Invalid fatal info buffer size!");
160 std::memcpy(&info, fatal_info.data(), sizeof(FatalInfo));
161
162 ThrowFatalError(error_code, fatal_type, info);
28 IPC::ResponseBuilder rb{ctx, 2}; 163 IPC::ResponseBuilder rb{ctx, 2};
29 rb.Push(RESULT_SUCCESS); 164 rb.Push(RESULT_SUCCESS);
30} 165}
diff --git a/src/core/hle/service/fatal/fatal.h b/src/core/hle/service/fatal/fatal.h
index 4d9a5be52..09371ff7f 100644
--- a/src/core/hle/service/fatal/fatal.h
+++ b/src/core/hle/service/fatal/fatal.h
@@ -15,6 +15,7 @@ public:
15 explicit Interface(std::shared_ptr<Module> module, const char* name); 15 explicit Interface(std::shared_ptr<Module> module, const char* name);
16 ~Interface() override; 16 ~Interface() override;
17 17
18 void ThrowFatal(Kernel::HLERequestContext& ctx);
18 void ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx); 19 void ThrowFatalWithPolicy(Kernel::HLERequestContext& ctx);
19 void ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx); 20 void ThrowFatalWithCpuContext(Kernel::HLERequestContext& ctx);
20 21
diff --git a/src/core/hle/service/fatal/fatal_u.cpp b/src/core/hle/service/fatal/fatal_u.cpp
index befc307cf..1572a2051 100644
--- a/src/core/hle/service/fatal/fatal_u.cpp
+++ b/src/core/hle/service/fatal/fatal_u.cpp
@@ -8,7 +8,7 @@ namespace Service::Fatal {
8 8
9Fatal_U::Fatal_U(std::shared_ptr<Module> module) : Module::Interface(std::move(module), "fatal:u") { 9Fatal_U::Fatal_U(std::shared_ptr<Module> module) : Module::Interface(std::move(module), "fatal:u") {
10 static const FunctionInfo functions[] = { 10 static const FunctionInfo functions[] = {
11 {0, nullptr, "ThrowFatal"}, 11 {0, &Fatal_U::ThrowFatal, "ThrowFatal"},
12 {1, &Fatal_U::ThrowFatalWithPolicy, "ThrowFatalWithPolicy"}, 12 {1, &Fatal_U::ThrowFatalWithPolicy, "ThrowFatalWithPolicy"},
13 {2, &Fatal_U::ThrowFatalWithCpuContext, "ThrowFatalWithCpuContext"}, 13 {2, &Fatal_U::ThrowFatalWithCpuContext, "ThrowFatalWithCpuContext"},
14 }; 14 };
diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp
index 5c4971724..aed2abb71 100644
--- a/src/core/hle/service/filesystem/filesystem.cpp
+++ b/src/core/hle/service/filesystem/filesystem.cpp
@@ -197,7 +197,7 @@ ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const s
197 auto dir = GetDirectoryRelativeWrapped(backing, path); 197 auto dir = GetDirectoryRelativeWrapped(backing, path);
198 if (dir == nullptr) { 198 if (dir == nullptr) {
199 // TODO(DarkLordZach): Find a better error code for this 199 // TODO(DarkLordZach): Find a better error code for this
200 return ResultCode(-1); 200 return FileSys::ERROR_PATH_NOT_FOUND;
201 } 201 }
202 return MakeResult(dir); 202 return MakeResult(dir);
203} 203}
@@ -343,6 +343,15 @@ std::shared_ptr<FileSys::RegisteredCache> GetSDMCContents() {
343 return sdmc_factory->GetSDMCContents(); 343 return sdmc_factory->GetSDMCContents();
344} 344}
345 345
346FileSys::VirtualDir GetModificationLoadRoot(u64 title_id) {
347 LOG_TRACE(Service_FS, "Opening mod load root for tid={:016X}", title_id);
348
349 if (bis_factory == nullptr)
350 return nullptr;
351
352 return bis_factory->GetModificationLoadRoot(title_id);
353}
354
346void CreateFactories(const FileSys::VirtualFilesystem& vfs, bool overwrite) { 355void CreateFactories(const FileSys::VirtualFilesystem& vfs, bool overwrite) {
347 if (overwrite) { 356 if (overwrite) {
348 bis_factory = nullptr; 357 bis_factory = nullptr;
@@ -354,9 +363,11 @@ void CreateFactories(const FileSys::VirtualFilesystem& vfs, bool overwrite) {
354 FileSys::Mode::ReadWrite); 363 FileSys::Mode::ReadWrite);
355 auto sd_directory = vfs->OpenDirectory(FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir), 364 auto sd_directory = vfs->OpenDirectory(FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir),
356 FileSys::Mode::ReadWrite); 365 FileSys::Mode::ReadWrite);
366 auto load_directory = vfs->OpenDirectory(FileUtil::GetUserPath(FileUtil::UserPath::LoadDir),
367 FileSys::Mode::ReadWrite);
357 368
358 if (bis_factory == nullptr) 369 if (bis_factory == nullptr)
359 bis_factory = std::make_unique<FileSys::BISFactory>(nand_directory); 370 bis_factory = std::make_unique<FileSys::BISFactory>(nand_directory, load_directory);
360 if (save_data_factory == nullptr) 371 if (save_data_factory == nullptr)
361 save_data_factory = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory)); 372 save_data_factory = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory));
362 if (sdmc_factory == nullptr) 373 if (sdmc_factory == nullptr)
diff --git a/src/core/hle/service/filesystem/filesystem.h b/src/core/hle/service/filesystem/filesystem.h
index aab65a2b8..7039a2247 100644
--- a/src/core/hle/service/filesystem/filesystem.h
+++ b/src/core/hle/service/filesystem/filesystem.h
@@ -52,6 +52,8 @@ std::shared_ptr<FileSys::RegisteredCache> GetSystemNANDContents();
52std::shared_ptr<FileSys::RegisteredCache> GetUserNANDContents(); 52std::shared_ptr<FileSys::RegisteredCache> GetUserNANDContents();
53std::shared_ptr<FileSys::RegisteredCache> GetSDMCContents(); 53std::shared_ptr<FileSys::RegisteredCache> GetSDMCContents();
54 54
55FileSys::VirtualDir GetModificationLoadRoot(u64 title_id);
56
55// Creates the SaveData, SDMC, and BIS Factories. Should be called once and before any function 57// Creates the SaveData, SDMC, and BIS Factories. Should be called once and before any function
56// above is called. 58// above is called.
57void CreateFactories(const FileSys::VirtualFilesystem& vfs, bool overwrite = true); 59void CreateFactories(const FileSys::VirtualFilesystem& vfs, bool overwrite = true);
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index 256c49bfc..7c6b0a4e6 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -338,7 +338,7 @@ public:
338 {106, &Hid::AcquireNpadStyleSetUpdateEventHandle, "AcquireNpadStyleSetUpdateEventHandle"}, 338 {106, &Hid::AcquireNpadStyleSetUpdateEventHandle, "AcquireNpadStyleSetUpdateEventHandle"},
339 {107, &Hid::DisconnectNpad, "DisconnectNpad"}, 339 {107, &Hid::DisconnectNpad, "DisconnectNpad"},
340 {108, &Hid::GetPlayerLedPattern, "GetPlayerLedPattern"}, 340 {108, &Hid::GetPlayerLedPattern, "GetPlayerLedPattern"},
341 {109, nullptr, "ActivateNpadWithRevision"}, 341 {109, &Hid::ActivateNpadWithRevision, "ActivateNpadWithRevision"},
342 {120, &Hid::SetNpadJoyHoldType, "SetNpadJoyHoldType"}, 342 {120, &Hid::SetNpadJoyHoldType, "SetNpadJoyHoldType"},
343 {121, &Hid::GetNpadJoyHoldType, "GetNpadJoyHoldType"}, 343 {121, &Hid::GetNpadJoyHoldType, "GetNpadJoyHoldType"},
344 {122, &Hid::SetNpadJoyAssignmentModeSingleByDefault, "SetNpadJoyAssignmentModeSingleByDefault"}, 344 {122, &Hid::SetNpadJoyAssignmentModeSingleByDefault, "SetNpadJoyAssignmentModeSingleByDefault"},
@@ -603,6 +603,12 @@ private:
603 rb.Push(RESULT_SUCCESS); 603 rb.Push(RESULT_SUCCESS);
604 LOG_WARNING(Service_HID, "(STUBBED) called"); 604 LOG_WARNING(Service_HID, "(STUBBED) called");
605 } 605 }
606
607 void ActivateNpadWithRevision(Kernel::HLERequestContext& ctx) {
608 IPC::ResponseBuilder rb{ctx, 2};
609 rb.Push(RESULT_SUCCESS);
610 LOG_WARNING(Service_HID, "(STUBBED) called");
611 }
606}; 612};
607 613
608class HidDbg final : public ServiceFramework<HidDbg> { 614class HidDbg final : public ServiceFramework<HidDbg> {
diff --git a/src/core/hle/service/hid/irs.cpp b/src/core/hle/service/hid/irs.cpp
index e587ad0d8..872e3c344 100644
--- a/src/core/hle/service/hid/irs.cpp
+++ b/src/core/hle/service/hid/irs.cpp
@@ -2,6 +2,11 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/swap.h"
6#include "core/core.h"
7#include "core/core_timing.h"
8#include "core/hle/ipc_helpers.h"
9#include "core/hle/kernel/shared_memory.h"
5#include "core/hle/service/hid/irs.h" 10#include "core/hle/service/hid/irs.h"
6 11
7namespace Service::HID { 12namespace Service::HID {
@@ -9,28 +14,145 @@ namespace Service::HID {
9IRS::IRS() : ServiceFramework{"irs"} { 14IRS::IRS() : ServiceFramework{"irs"} {
10 // clang-format off 15 // clang-format off
11 static const FunctionInfo functions[] = { 16 static const FunctionInfo functions[] = {
12 {302, nullptr, "ActivateIrsensor"}, 17 {302, &IRS::ActivateIrsensor, "ActivateIrsensor"},
13 {303, nullptr, "DeactivateIrsensor"}, 18 {303, &IRS::DeactivateIrsensor, "DeactivateIrsensor"},
14 {304, nullptr, "GetIrsensorSharedMemoryHandle"}, 19 {304, &IRS::GetIrsensorSharedMemoryHandle, "GetIrsensorSharedMemoryHandle"},
15 {305, nullptr, "StopImageProcessor"}, 20 {305, &IRS::StopImageProcessor, "StopImageProcessor"},
16 {306, nullptr, "RunMomentProcessor"}, 21 {306, &IRS::RunMomentProcessor, "RunMomentProcessor"},
17 {307, nullptr, "RunClusteringProcessor"}, 22 {307, &IRS::RunClusteringProcessor, "RunClusteringProcessor"},
18 {308, nullptr, "RunImageTransferProcessor"}, 23 {308, &IRS::RunImageTransferProcessor, "RunImageTransferProcessor"},
19 {309, nullptr, "GetImageTransferProcessorState"}, 24 {309, &IRS::GetImageTransferProcessorState, "GetImageTransferProcessorState"},
20 {310, nullptr, "RunTeraPluginProcessor"}, 25 {310, &IRS::RunTeraPluginProcessor, "RunTeraPluginProcessor"},
21 {311, nullptr, "GetNpadIrCameraHandle"}, 26 {311, &IRS::GetNpadIrCameraHandle, "GetNpadIrCameraHandle"},
22 {312, nullptr, "RunPointingProcessor"}, 27 {312, &IRS::RunPointingProcessor, "RunPointingProcessor"},
23 {313, nullptr, "SuspendImageProcessor"}, 28 {313, &IRS::SuspendImageProcessor, "SuspendImageProcessor"},
24 {314, nullptr, "CheckFirmwareVersion"}, 29 {314, &IRS::CheckFirmwareVersion, "CheckFirmwareVersion"},
25 {315, nullptr, "SetFunctionLevel"}, 30 {315, &IRS::SetFunctionLevel, "SetFunctionLevel"},
26 {316, nullptr, "RunImageTransferExProcessor"}, 31 {316, &IRS::RunImageTransferExProcessor, "RunImageTransferExProcessor"},
27 {317, nullptr, "RunIrLedProcessor"}, 32 {317, &IRS::RunIrLedProcessor, "RunIrLedProcessor"},
28 {318, nullptr, "StopImageProcessorAsync"}, 33 {318, &IRS::StopImageProcessorAsync, "StopImageProcessorAsync"},
29 {319, nullptr, "ActivateIrsensorWithFunctionLevel"}, 34 {319, &IRS::ActivateIrsensorWithFunctionLevel, "ActivateIrsensorWithFunctionLevel"},
30 }; 35 };
31 // clang-format on 36 // clang-format on
32 37
33 RegisterHandlers(functions); 38 RegisterHandlers(functions);
39
40 auto& kernel = Core::System::GetInstance().Kernel();
41 shared_mem = Kernel::SharedMemory::Create(
42 kernel, nullptr, 0x8000, Kernel::MemoryPermission::ReadWrite,
43 Kernel::MemoryPermission::Read, 0, Kernel::MemoryRegion::BASE, "IRS:SharedMemory");
44}
45
46void IRS::ActivateIrsensor(Kernel::HLERequestContext& ctx) {
47 IPC::ResponseBuilder rb{ctx, 2};
48 rb.Push(RESULT_SUCCESS);
49 LOG_WARNING(Service_IRS, "(STUBBED) called");
50}
51
52void IRS::DeactivateIrsensor(Kernel::HLERequestContext& ctx) {
53 IPC::ResponseBuilder rb{ctx, 2};
54 rb.Push(RESULT_SUCCESS);
55 LOG_WARNING(Service_IRS, "(STUBBED) called");
56}
57
58void IRS::GetIrsensorSharedMemoryHandle(Kernel::HLERequestContext& ctx) {
59 IPC::ResponseBuilder rb{ctx, 2, 1};
60 rb.Push(RESULT_SUCCESS);
61 rb.PushCopyObjects(shared_mem);
62 LOG_DEBUG(Service_IRS, "called");
63}
64
65void IRS::StopImageProcessor(Kernel::HLERequestContext& ctx) {
66 IPC::ResponseBuilder rb{ctx, 2};
67 rb.Push(RESULT_SUCCESS);
68 LOG_WARNING(Service_IRS, "(STUBBED) called");
69}
70
71void IRS::RunMomentProcessor(Kernel::HLERequestContext& ctx) {
72 IPC::ResponseBuilder rb{ctx, 2};
73 rb.Push(RESULT_SUCCESS);
74 LOG_WARNING(Service_IRS, "(STUBBED) called");
75}
76
77void IRS::RunClusteringProcessor(Kernel::HLERequestContext& ctx) {
78 IPC::ResponseBuilder rb{ctx, 2};
79 rb.Push(RESULT_SUCCESS);
80 LOG_WARNING(Service_IRS, "(STUBBED) called");
81}
82
83void IRS::RunImageTransferProcessor(Kernel::HLERequestContext& ctx) {
84 IPC::ResponseBuilder rb{ctx, 2};
85 rb.Push(RESULT_SUCCESS);
86 LOG_WARNING(Service_IRS, "(STUBBED) called");
87}
88
89void IRS::GetImageTransferProcessorState(Kernel::HLERequestContext& ctx) {
90 IPC::ResponseBuilder rb{ctx, 5};
91 rb.Push(RESULT_SUCCESS);
92 rb.PushRaw<u64>(CoreTiming::GetTicks());
93 rb.PushRaw<u32>(0);
94 LOG_WARNING(Service_IRS, "(STUBBED) called");
95}
96
97void IRS::RunTeraPluginProcessor(Kernel::HLERequestContext& ctx) {
98 IPC::ResponseBuilder rb{ctx, 2};
99 rb.Push(RESULT_SUCCESS);
100 LOG_WARNING(Service_IRS, "(STUBBED) called");
101}
102
103void IRS::GetNpadIrCameraHandle(Kernel::HLERequestContext& ctx) {
104 IPC::ResponseBuilder rb{ctx, 3};
105 rb.Push(RESULT_SUCCESS);
106 rb.PushRaw<u32>(device_handle);
107 LOG_WARNING(Service_IRS, "(STUBBED) called");
108}
109
110void IRS::RunPointingProcessor(Kernel::HLERequestContext& ctx) {
111 IPC::ResponseBuilder rb{ctx, 2};
112 rb.Push(RESULT_SUCCESS);
113 LOG_WARNING(Service_IRS, "(STUBBED) called");
114}
115
116void IRS::SuspendImageProcessor(Kernel::HLERequestContext& ctx) {
117 IPC::ResponseBuilder rb{ctx, 2};
118 rb.Push(RESULT_SUCCESS);
119 LOG_WARNING(Service_IRS, "(STUBBED) called");
120}
121
122void IRS::CheckFirmwareVersion(Kernel::HLERequestContext& ctx) {
123 IPC::ResponseBuilder rb{ctx, 2};
124 rb.Push(RESULT_SUCCESS);
125 LOG_WARNING(Service_IRS, "(STUBBED) called");
126}
127
128void IRS::SetFunctionLevel(Kernel::HLERequestContext& ctx) {
129 IPC::ResponseBuilder rb{ctx, 2};
130 rb.Push(RESULT_SUCCESS);
131 LOG_WARNING(Service_IRS, "(STUBBED) called");
132}
133
134void IRS::RunImageTransferExProcessor(Kernel::HLERequestContext& ctx) {
135 IPC::ResponseBuilder rb{ctx, 2};
136 rb.Push(RESULT_SUCCESS);
137 LOG_WARNING(Service_IRS, "(STUBBED) called");
138}
139
140void IRS::RunIrLedProcessor(Kernel::HLERequestContext& ctx) {
141 IPC::ResponseBuilder rb{ctx, 2};
142 rb.Push(RESULT_SUCCESS);
143 LOG_WARNING(Service_IRS, "(STUBBED) called");
144}
145
146void IRS::StopImageProcessorAsync(Kernel::HLERequestContext& ctx) {
147 IPC::ResponseBuilder rb{ctx, 2};
148 rb.Push(RESULT_SUCCESS);
149 LOG_WARNING(Service_IRS, "(STUBBED) called");
150}
151
152void IRS::ActivateIrsensorWithFunctionLevel(Kernel::HLERequestContext& ctx) {
153 IPC::ResponseBuilder rb{ctx, 2};
154 rb.Push(RESULT_SUCCESS);
155 LOG_WARNING(Service_IRS, "(STUBBED) called");
34} 156}
35 157
36IRS::~IRS() = default; 158IRS::~IRS() = default;
diff --git a/src/core/hle/service/hid/irs.h b/src/core/hle/service/hid/irs.h
index 6fb16b45d..12de6bfb3 100644
--- a/src/core/hle/service/hid/irs.h
+++ b/src/core/hle/service/hid/irs.h
@@ -4,14 +4,41 @@
4 4
5#pragma once 5#pragma once
6 6
7#include "core/hle/kernel/object.h"
7#include "core/hle/service/service.h" 8#include "core/hle/service/service.h"
8 9
10namespace Kernel {
11class SharedMemory;
12}
13
9namespace Service::HID { 14namespace Service::HID {
10 15
11class IRS final : public ServiceFramework<IRS> { 16class IRS final : public ServiceFramework<IRS> {
12public: 17public:
13 explicit IRS(); 18 explicit IRS();
14 ~IRS() override; 19 ~IRS() override;
20
21private:
22 void ActivateIrsensor(Kernel::HLERequestContext& ctx);
23 void DeactivateIrsensor(Kernel::HLERequestContext& ctx);
24 void GetIrsensorSharedMemoryHandle(Kernel::HLERequestContext& ctx);
25 void StopImageProcessor(Kernel::HLERequestContext& ctx);
26 void RunMomentProcessor(Kernel::HLERequestContext& ctx);
27 void RunClusteringProcessor(Kernel::HLERequestContext& ctx);
28 void RunImageTransferProcessor(Kernel::HLERequestContext& ctx);
29 void GetImageTransferProcessorState(Kernel::HLERequestContext& ctx);
30 void RunTeraPluginProcessor(Kernel::HLERequestContext& ctx);
31 void GetNpadIrCameraHandle(Kernel::HLERequestContext& ctx);
32 void RunPointingProcessor(Kernel::HLERequestContext& ctx);
33 void SuspendImageProcessor(Kernel::HLERequestContext& ctx);
34 void CheckFirmwareVersion(Kernel::HLERequestContext& ctx);
35 void SetFunctionLevel(Kernel::HLERequestContext& ctx);
36 void RunImageTransferExProcessor(Kernel::HLERequestContext& ctx);
37 void RunIrLedProcessor(Kernel::HLERequestContext& ctx);
38 void StopImageProcessorAsync(Kernel::HLERequestContext& ctx);
39 void ActivateIrsensorWithFunctionLevel(Kernel::HLERequestContext& ctx);
40 Kernel::SharedPtr<Kernel::SharedMemory> shared_mem;
41 const u32 device_handle{0xABCD};
15}; 42};
16 43
17class IRS_SYS final : public ServiceFramework<IRS_SYS> { 44class IRS_SYS final : public ServiceFramework<IRS_SYS> {
diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp
index f8d2127d9..8c07a05c2 100644
--- a/src/core/hle/service/nfp/nfp.cpp
+++ b/src/core/hle/service/nfp/nfp.cpp
@@ -3,6 +3,7 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/logging/log.h" 5#include "common/logging/log.h"
6#include "core/core.h"
6#include "core/hle/ipc_helpers.h" 7#include "core/hle/ipc_helpers.h"
7#include "core/hle/kernel/event.h" 8#include "core/hle/kernel/event.h"
8#include "core/hle/service/hid/hid.h" 9#include "core/hle/service/hid/hid.h"
diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp
index ed4f5f539..10611ed6a 100644
--- a/src/core/hle/service/nifm/nifm.cpp
+++ b/src/core/hle/service/nifm/nifm.cpp
@@ -31,7 +31,7 @@ public:
31 {1, &IRequest::GetResult, "GetResult"}, 31 {1, &IRequest::GetResult, "GetResult"},
32 {2, &IRequest::GetSystemEventReadableHandles, "GetSystemEventReadableHandles"}, 32 {2, &IRequest::GetSystemEventReadableHandles, "GetSystemEventReadableHandles"},
33 {3, &IRequest::Cancel, "Cancel"}, 33 {3, &IRequest::Cancel, "Cancel"},
34 {4, nullptr, "Submit"}, 34 {4, &IRequest::Submit, "Submit"},
35 {5, nullptr, "SetRequirement"}, 35 {5, nullptr, "SetRequirement"},
36 {6, nullptr, "SetRequirementPreset"}, 36 {6, nullptr, "SetRequirementPreset"},
37 {8, nullptr, "SetPriority"}, 37 {8, nullptr, "SetPriority"},
@@ -61,6 +61,12 @@ public:
61 } 61 }
62 62
63private: 63private:
64 void Submit(Kernel::HLERequestContext& ctx) {
65 LOG_WARNING(Service_NIFM, "(STUBBED) called");
66 IPC::ResponseBuilder rb{ctx, 2};
67 rb.Push(RESULT_SUCCESS);
68 }
69
64 void GetRequestState(Kernel::HLERequestContext& ctx) { 70 void GetRequestState(Kernel::HLERequestContext& ctx) {
65 LOG_WARNING(Service_NIFM, "(STUBBED) called"); 71 LOG_WARNING(Service_NIFM, "(STUBBED) called");
66 IPC::ResponseBuilder rb{ctx, 3}; 72 IPC::ResponseBuilder rb{ctx, 3};
@@ -114,10 +120,11 @@ public:
114 120
115private: 121private:
116 void GetClientId(Kernel::HLERequestContext& ctx) { 122 void GetClientId(Kernel::HLERequestContext& ctx) {
123 static constexpr u32 client_id = 1;
117 LOG_WARNING(Service_NIFM, "(STUBBED) called"); 124 LOG_WARNING(Service_NIFM, "(STUBBED) called");
118 IPC::ResponseBuilder rb{ctx, 4}; 125 IPC::ResponseBuilder rb{ctx, 4};
119 rb.Push(RESULT_SUCCESS); 126 rb.Push(RESULT_SUCCESS);
120 rb.Push<u64>(0); 127 rb.Push<u64>(client_id); // Client ID needs to be non zero otherwise it's considered invalid
121 } 128 }
122 void CreateScanRequest(Kernel::HLERequestContext& ctx) { 129 void CreateScanRequest(Kernel::HLERequestContext& ctx) {
123 IPC::ResponseBuilder rb{ctx, 2, 0, 1}; 130 IPC::ResponseBuilder rb{ctx, 2, 0, 1};
@@ -141,10 +148,16 @@ private:
141 rb.Push(RESULT_SUCCESS); 148 rb.Push(RESULT_SUCCESS);
142 } 149 }
143 void CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx) { 150 void CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx) {
144 IPC::ResponseBuilder rb{ctx, 2, 0, 1}; 151 ASSERT_MSG(ctx.GetReadBufferSize() == 0x17c, "NetworkProfileData is not the correct size");
152 u128 uuid{};
153 auto buffer = ctx.ReadBuffer();
154 std::memcpy(&uuid, buffer.data() + 8, sizeof(u128));
155
156 IPC::ResponseBuilder rb{ctx, 6, 0, 1};
145 157
146 rb.Push(RESULT_SUCCESS); 158 rb.Push(RESULT_SUCCESS);
147 rb.PushIpcInterface<INetworkProfile>(); 159 rb.PushIpcInterface<INetworkProfile>();
160 rb.PushRaw<u128>(uuid);
148 161
149 LOG_DEBUG(Service_NIFM, "called"); 162 LOG_DEBUG(Service_NIFM, "called");
150 } 163 }
diff --git a/src/core/hle/service/nim/nim.cpp b/src/core/hle/service/nim/nim.cpp
index bd05b0a70..261ad539c 100644
--- a/src/core/hle/service/nim/nim.cpp
+++ b/src/core/hle/service/nim/nim.cpp
@@ -2,6 +2,11 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <chrono>
6#include <ctime>
7#include "core/core.h"
8#include "core/hle/ipc_helpers.h"
9#include "core/hle/kernel/event.h"
5#include "core/hle/service/nim/nim.h" 10#include "core/hle/service/nim/nim.h"
6#include "core/hle/service/service.h" 11#include "core/hle/service/service.h"
7#include "core/hle/service/sm/sm.h" 12#include "core/hle/service/sm/sm.h"
@@ -100,19 +105,111 @@ public:
100 } 105 }
101}; 106};
102 107
108class IEnsureNetworkClockAvailabilityService final
109 : public ServiceFramework<IEnsureNetworkClockAvailabilityService> {
110public:
111 IEnsureNetworkClockAvailabilityService()
112 : ServiceFramework("IEnsureNetworkClockAvailabilityService") {
113 static const FunctionInfo functions[] = {
114 {0, &IEnsureNetworkClockAvailabilityService::StartTask, "StartTask"},
115 {1, &IEnsureNetworkClockAvailabilityService::GetFinishNotificationEvent,
116 "GetFinishNotificationEvent"},
117 {2, &IEnsureNetworkClockAvailabilityService::GetResult, "GetResult"},
118 {3, &IEnsureNetworkClockAvailabilityService::Cancel, "Cancel"},
119 {4, &IEnsureNetworkClockAvailabilityService::IsProcessing, "IsProcessing"},
120 {5, &IEnsureNetworkClockAvailabilityService::GetServerTime, "GetServerTime"},
121 };
122 RegisterHandlers(functions);
123
124 auto& kernel = Core::System::GetInstance().Kernel();
125 finished_event =
126 Kernel::Event::Create(kernel, Kernel::ResetType::OneShot,
127 "IEnsureNetworkClockAvailabilityService:FinishEvent");
128 }
129
130private:
131 Kernel::SharedPtr<Kernel::Event> finished_event;
132
133 void StartTask(Kernel::HLERequestContext& ctx) {
134 // No need to connect to the internet, just finish the task straight away.
135 finished_event->Signal();
136 IPC::ResponseBuilder rb{ctx, 2};
137 rb.Push(RESULT_SUCCESS);
138 LOG_DEBUG(Service_NIM, "called");
139 }
140
141 void GetFinishNotificationEvent(Kernel::HLERequestContext& ctx) {
142 IPC::ResponseBuilder rb{ctx, 2, 1};
143 rb.Push(RESULT_SUCCESS);
144 rb.PushCopyObjects(finished_event);
145 LOG_DEBUG(Service_NIM, "called");
146 }
147
148 void GetResult(Kernel::HLERequestContext& ctx) {
149 IPC::ResponseBuilder rb{ctx, 2};
150 rb.Push(RESULT_SUCCESS);
151 LOG_DEBUG(Service_NIM, "called");
152 }
153
154 void Cancel(Kernel::HLERequestContext& ctx) {
155 finished_event->Clear();
156 IPC::ResponseBuilder rb{ctx, 2};
157 rb.Push(RESULT_SUCCESS);
158 LOG_DEBUG(Service_NIM, "called");
159 }
160
161 void IsProcessing(Kernel::HLERequestContext& ctx) {
162 IPC::ResponseBuilder rb{ctx, 3};
163 rb.Push(RESULT_SUCCESS);
164 rb.PushRaw<u32>(0); // We instantly process the request
165 LOG_DEBUG(Service_NIM, "called");
166 }
167
168 void GetServerTime(Kernel::HLERequestContext& ctx) {
169 const s64 server_time{std::chrono::duration_cast<std::chrono::seconds>(
170 std::chrono::system_clock::now().time_since_epoch())
171 .count()};
172 IPC::ResponseBuilder rb{ctx, 4};
173 rb.Push(RESULT_SUCCESS);
174 rb.PushRaw<s64>(server_time);
175 LOG_DEBUG(Service_NIM, "called");
176 }
177};
178
103class NTC final : public ServiceFramework<NTC> { 179class NTC final : public ServiceFramework<NTC> {
104public: 180public:
105 explicit NTC() : ServiceFramework{"ntc"} { 181 explicit NTC() : ServiceFramework{"ntc"} {
106 // clang-format off 182 // clang-format off
107 static const FunctionInfo functions[] = { 183 static const FunctionInfo functions[] = {
108 {0, nullptr, "OpenEnsureNetworkClockAvailabilityService"}, 184 {0, &NTC::OpenEnsureNetworkClockAvailabilityService, "OpenEnsureNetworkClockAvailabilityService"},
109 {100, nullptr, "SuspendAutonomicTimeCorrection"}, 185 {100, &NTC::SuspendAutonomicTimeCorrection, "SuspendAutonomicTimeCorrection"},
110 {101, nullptr, "ResumeAutonomicTimeCorrection"}, 186 {101, &NTC::ResumeAutonomicTimeCorrection, "ResumeAutonomicTimeCorrection"},
111 }; 187 };
112 // clang-format on 188 // clang-format on
113 189
114 RegisterHandlers(functions); 190 RegisterHandlers(functions);
115 } 191 }
192
193private:
194 void OpenEnsureNetworkClockAvailabilityService(Kernel::HLERequestContext& ctx) {
195 IPC::ResponseBuilder rb{ctx, 2, 0, 1};
196 rb.Push(RESULT_SUCCESS);
197 rb.PushIpcInterface<IEnsureNetworkClockAvailabilityService>();
198 LOG_DEBUG(Service_NIM, "called");
199 }
200
201 // TODO(ogniK): Do we need these?
202 void SuspendAutonomicTimeCorrection(Kernel::HLERequestContext& ctx) {
203 IPC::ResponseBuilder rb{ctx, 2};
204 rb.Push(RESULT_SUCCESS);
205 LOG_WARNING(Service_NIM, "(STUBBED) called");
206 }
207
208 void ResumeAutonomicTimeCorrection(Kernel::HLERequestContext& ctx) {
209 IPC::ResponseBuilder rb{ctx, 2};
210 rb.Push(RESULT_SUCCESS);
211 LOG_WARNING(Service_NIM, "(STUBBED) called");
212 }
116}; 213};
117 214
118void InstallInterfaces(SM::ServiceManager& sm) { 215void InstallInterfaces(SM::ServiceManager& sm) {
diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp
index 1069d103f..4b2f758a8 100644
--- a/src/core/hle/service/ns/pl_u.cpp
+++ b/src/core/hle/service/ns/pl_u.cpp
@@ -317,9 +317,9 @@ void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) {
317 317
318void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { 318void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
319 // Map backing memory for the font data 319 // Map backing memory for the font data
320 Core::CurrentProcess()->vm_manager.MapMemoryBlock(SHARED_FONT_MEM_VADDR, impl->shared_font, 0, 320 Core::CurrentProcess()->VMManager().MapMemoryBlock(SHARED_FONT_MEM_VADDR, impl->shared_font, 0,
321 SHARED_FONT_MEM_SIZE, 321 SHARED_FONT_MEM_SIZE,
322 Kernel::MemoryState::Shared); 322 Kernel::MemoryState::Shared);
323 323
324 // Create shared font memory object 324 // Create shared font memory object
325 auto& kernel = Core::System::GetInstance().Kernel(); 325 auto& kernel = Core::System::GetInstance().Kernel();
diff --git a/src/core/hle/service/sm/controller.cpp b/src/core/hle/service/sm/controller.cpp
index cdf328a26..98f6e4111 100644
--- a/src/core/hle/service/sm/controller.cpp
+++ b/src/core/hle/service/sm/controller.cpp
@@ -2,8 +2,11 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/assert.h"
5#include "common/logging/log.h" 6#include "common/logging/log.h"
6#include "core/hle/ipc_helpers.h" 7#include "core/hle/ipc_helpers.h"
8#include "core/hle/kernel/client_session.h"
9#include "core/hle/kernel/server_session.h"
7#include "core/hle/kernel/session.h" 10#include "core/hle/kernel/session.h"
8#include "core/hle/service/sm/controller.h" 11#include "core/hle/service/sm/controller.h"
9 12
diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp
index 096f0fd52..464e79d01 100644
--- a/src/core/hle/service/sm/sm.cpp
+++ b/src/core/hle/service/sm/sm.cpp
@@ -108,7 +108,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) {
108 108
109 auto client_port = service_manager->GetServicePort(name); 109 auto client_port = service_manager->GetServicePort(name);
110 if (client_port.Failed()) { 110 if (client_port.Failed()) {
111 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 111 IPC::ResponseBuilder rb{ctx, 2};
112 rb.Push(client_port.Code()); 112 rb.Push(client_port.Code());
113 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, client_port.Code().raw); 113 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, client_port.Code().raw);
114 if (name.length() == 0) 114 if (name.length() == 0)
@@ -121,8 +121,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) {
121 ASSERT(session.Succeeded()); 121 ASSERT(session.Succeeded());
122 if (session.Succeeded()) { 122 if (session.Succeeded()) {
123 LOG_DEBUG(Service_SM, "called service={} -> session={}", name, (*session)->GetObjectId()); 123 LOG_DEBUG(Service_SM, "called service={} -> session={}", name, (*session)->GetObjectId());
124 IPC::ResponseBuilder rb = 124 IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
125 rp.MakeBuilder(2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles);
126 rb.Push(session.Code()); 125 rb.Push(session.Code());
127 rb.PushMoveObjects(std::move(session).Unwrap()); 126 rb.PushMoveObjects(std::move(session).Unwrap());
128 } 127 }
diff --git a/src/core/hle/service/ssl/ssl.cpp b/src/core/hle/service/ssl/ssl.cpp
index 63b86e099..bc4f7a437 100644
--- a/src/core/hle/service/ssl/ssl.cpp
+++ b/src/core/hle/service/ssl/ssl.cpp
@@ -71,7 +71,7 @@ private:
71 LOG_WARNING(Service_SSL, "(STUBBED) called"); 71 LOG_WARNING(Service_SSL, "(STUBBED) called");
72 IPC::RequestParser rp{ctx}; 72 IPC::RequestParser rp{ctx};
73 73
74 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 74 IPC::ResponseBuilder rb{ctx, 2};
75 rb.Push(RESULT_SUCCESS); 75 rb.Push(RESULT_SUCCESS);
76 } 76 }
77 77
@@ -103,6 +103,7 @@ public:
103 } 103 }
104 104
105private: 105private:
106 u32 ssl_version{};
106 void CreateContext(Kernel::HLERequestContext& ctx) { 107 void CreateContext(Kernel::HLERequestContext& ctx) {
107 LOG_WARNING(Service_SSL, "(STUBBED) called"); 108 LOG_WARNING(Service_SSL, "(STUBBED) called");
108 109
@@ -112,10 +113,9 @@ private:
112 } 113 }
113 114
114 void SetInterfaceVersion(Kernel::HLERequestContext& ctx) { 115 void SetInterfaceVersion(Kernel::HLERequestContext& ctx) {
115 LOG_WARNING(Service_SSL, "(STUBBED) called"); 116 LOG_DEBUG(Service_SSL, "called");
116 IPC::RequestParser rp{ctx}; 117 IPC::RequestParser rp{ctx};
117 u32 unk1 = rp.Pop<u32>(); // Probably minor/major? 118 ssl_version = rp.Pop<u32>();
118 u32 unk2 = rp.Pop<u32>(); // TODO(ogniK): Figure out what this does
119 119
120 IPC::ResponseBuilder rb{ctx, 2}; 120 IPC::ResponseBuilder rb{ctx, 2};
121 rb.Push(RESULT_SUCCESS); 121 rb.Push(RESULT_SUCCESS);
diff --git a/src/core/hle/service/vi/vi.cpp b/src/core/hle/service/vi/vi.cpp
index d0cde5ede..bbc02abcc 100644
--- a/src/core/hle/service/vi/vi.cpp
+++ b/src/core/hle/service/vi/vi.cpp
@@ -612,7 +612,7 @@ public:
612 {3000, nullptr, "ListDisplayModes"}, 612 {3000, nullptr, "ListDisplayModes"},
613 {3001, nullptr, "ListDisplayRgbRanges"}, 613 {3001, nullptr, "ListDisplayRgbRanges"},
614 {3002, nullptr, "ListDisplayContentTypes"}, 614 {3002, nullptr, "ListDisplayContentTypes"},
615 {3200, nullptr, "GetDisplayMode"}, 615 {3200, &ISystemDisplayService::GetDisplayMode, "GetDisplayMode"},
616 {3201, nullptr, "SetDisplayMode"}, 616 {3201, nullptr, "SetDisplayMode"},
617 {3202, nullptr, "GetDisplayUnderscan"}, 617 {3202, nullptr, "GetDisplayUnderscan"},
618 {3203, nullptr, "SetDisplayUnderscan"}, 618 {3203, nullptr, "SetDisplayUnderscan"},
@@ -650,7 +650,7 @@ private:
650 u64 layer_id = rp.Pop<u64>(); 650 u64 layer_id = rp.Pop<u64>();
651 u64 z_value = rp.Pop<u64>(); 651 u64 z_value = rp.Pop<u64>();
652 652
653 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 653 IPC::ResponseBuilder rb{ctx, 2};
654 rb.Push(RESULT_SUCCESS); 654 rb.Push(RESULT_SUCCESS);
655 } 655 }
656 656
@@ -658,11 +658,29 @@ private:
658 IPC::RequestParser rp{ctx}; 658 IPC::RequestParser rp{ctx};
659 u64 layer_id = rp.Pop<u64>(); 659 u64 layer_id = rp.Pop<u64>();
660 bool visibility = rp.Pop<bool>(); 660 bool visibility = rp.Pop<bool>();
661 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 661 IPC::ResponseBuilder rb{ctx, 2};
662 rb.Push(RESULT_SUCCESS); 662 rb.Push(RESULT_SUCCESS);
663 LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:08X}, visibility={}", layer_id, 663 LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:08X}, visibility={}", layer_id,
664 visibility); 664 visibility);
665 } 665 }
666
667 void GetDisplayMode(Kernel::HLERequestContext& ctx) {
668 IPC::ResponseBuilder rb{ctx, 6};
669 rb.Push(RESULT_SUCCESS);
670
671 if (Settings::values.use_docked_mode) {
672 rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth));
673 rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight));
674 } else {
675 rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth));
676 rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight));
677 }
678
679 rb.PushRaw<float>(60.0f);
680 rb.Push<u32>(0);
681
682 LOG_DEBUG(Service_VI, "called");
683 }
666}; 684};
667 685
668class IManagerDisplayService final : public ServiceFramework<IManagerDisplayService> { 686class IManagerDisplayService final : public ServiceFramework<IManagerDisplayService> {
@@ -747,7 +765,7 @@ private:
747 IPC::RequestParser rp{ctx}; 765 IPC::RequestParser rp{ctx};
748 u64 display = rp.Pop<u64>(); 766 u64 display = rp.Pop<u64>();
749 767
750 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 768 IPC::ResponseBuilder rb{ctx, 2};
751 rb.Push(RESULT_SUCCESS); 769 rb.Push(RESULT_SUCCESS);
752 } 770 }
753 771
@@ -761,7 +779,7 @@ private:
761 779
762 u64 layer_id = nv_flinger->CreateLayer(display); 780 u64 layer_id = nv_flinger->CreateLayer(display);
763 781
764 IPC::ResponseBuilder rb = rp.MakeBuilder(4, 0, 0); 782 IPC::ResponseBuilder rb{ctx, 4};
765 rb.Push(RESULT_SUCCESS); 783 rb.Push(RESULT_SUCCESS);
766 rb.Push(layer_id); 784 rb.Push(layer_id);
767 } 785 }
@@ -772,7 +790,7 @@ private:
772 u32 stack = rp.Pop<u32>(); 790 u32 stack = rp.Pop<u32>();
773 u64 layer_id = rp.Pop<u64>(); 791 u64 layer_id = rp.Pop<u64>();
774 792
775 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 793 IPC::ResponseBuilder rb{ctx, 2};
776 rb.Push(RESULT_SUCCESS); 794 rb.Push(RESULT_SUCCESS);
777 } 795 }
778 796
@@ -780,7 +798,7 @@ private:
780 IPC::RequestParser rp{ctx}; 798 IPC::RequestParser rp{ctx};
781 u64 layer_id = rp.Pop<u64>(); 799 u64 layer_id = rp.Pop<u64>();
782 bool visibility = rp.Pop<bool>(); 800 bool visibility = rp.Pop<bool>();
783 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 801 IPC::ResponseBuilder rb{ctx, 2};
784 rb.Push(RESULT_SUCCESS); 802 rb.Push(RESULT_SUCCESS);
785 LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:X}, visibility={}", layer_id, 803 LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:X}, visibility={}", layer_id,
786 visibility); 804 visibility);
@@ -837,7 +855,7 @@ private:
837 855
838 ASSERT_MSG(name == "Default", "Non-default displays aren't supported yet"); 856 ASSERT_MSG(name == "Default", "Non-default displays aren't supported yet");
839 857
840 IPC::ResponseBuilder rb = rp.MakeBuilder(4, 0, 0); 858 IPC::ResponseBuilder rb{ctx, 4};
841 rb.Push(RESULT_SUCCESS); 859 rb.Push(RESULT_SUCCESS);
842 rb.Push<u64>(nv_flinger->OpenDisplay(name)); 860 rb.Push<u64>(nv_flinger->OpenDisplay(name));
843 } 861 }
@@ -847,7 +865,7 @@ private:
847 IPC::RequestParser rp{ctx}; 865 IPC::RequestParser rp{ctx};
848 u64 display_id = rp.Pop<u64>(); 866 u64 display_id = rp.Pop<u64>();
849 867
850 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 868 IPC::ResponseBuilder rb{ctx, 2};
851 rb.Push(RESULT_SUCCESS); 869 rb.Push(RESULT_SUCCESS);
852 } 870 }
853 871
@@ -856,7 +874,7 @@ private:
856 IPC::RequestParser rp{ctx}; 874 IPC::RequestParser rp{ctx};
857 u64 display_id = rp.Pop<u64>(); 875 u64 display_id = rp.Pop<u64>();
858 876
859 IPC::ResponseBuilder rb = rp.MakeBuilder(6, 0, 0); 877 IPC::ResponseBuilder rb{ctx, 6};
860 rb.Push(RESULT_SUCCESS); 878 rb.Push(RESULT_SUCCESS);
861 879
862 if (Settings::values.use_docked_mode) { 880 if (Settings::values.use_docked_mode) {
@@ -874,7 +892,7 @@ private:
874 u32 scaling_mode = rp.Pop<u32>(); 892 u32 scaling_mode = rp.Pop<u32>();
875 u64 unknown = rp.Pop<u64>(); 893 u64 unknown = rp.Pop<u64>();
876 894
877 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 895 IPC::ResponseBuilder rb{ctx, 2};
878 rb.Push(RESULT_SUCCESS); 896 rb.Push(RESULT_SUCCESS);
879 } 897 }
880 898
@@ -882,7 +900,7 @@ private:
882 IPC::RequestParser rp{ctx}; 900 IPC::RequestParser rp{ctx};
883 DisplayInfo display_info; 901 DisplayInfo display_info;
884 ctx.WriteBuffer(&display_info, sizeof(DisplayInfo)); 902 ctx.WriteBuffer(&display_info, sizeof(DisplayInfo));
885 IPC::ResponseBuilder rb = rp.MakeBuilder(4, 0, 0); 903 IPC::ResponseBuilder rb{ctx, 4};
886 rb.Push(RESULT_SUCCESS); 904 rb.Push(RESULT_SUCCESS);
887 rb.Push<u64>(1); 905 rb.Push<u64>(1);
888 LOG_WARNING(Service_VI, "(STUBBED) called"); 906 LOG_WARNING(Service_VI, "(STUBBED) called");
@@ -903,7 +921,7 @@ private:
903 u32 buffer_queue_id = nv_flinger->GetBufferQueueId(display_id, layer_id); 921 u32 buffer_queue_id = nv_flinger->GetBufferQueueId(display_id, layer_id);
904 922
905 NativeWindow native_window{buffer_queue_id}; 923 NativeWindow native_window{buffer_queue_id};
906 IPC::ResponseBuilder rb = rp.MakeBuilder(4, 0, 0); 924 IPC::ResponseBuilder rb{ctx, 4};
907 rb.Push(RESULT_SUCCESS); 925 rb.Push(RESULT_SUCCESS);
908 rb.Push<u64>(ctx.WriteBuffer(native_window.Serialize())); 926 rb.Push<u64>(ctx.WriteBuffer(native_window.Serialize()));
909 } 927 }
@@ -922,7 +940,7 @@ private:
922 u32 buffer_queue_id = nv_flinger->GetBufferQueueId(display_id, layer_id); 940 u32 buffer_queue_id = nv_flinger->GetBufferQueueId(display_id, layer_id);
923 941
924 NativeWindow native_window{buffer_queue_id}; 942 NativeWindow native_window{buffer_queue_id};
925 IPC::ResponseBuilder rb = rp.MakeBuilder(6, 0, 0); 943 IPC::ResponseBuilder rb{ctx, 6};
926 rb.Push(RESULT_SUCCESS); 944 rb.Push(RESULT_SUCCESS);
927 rb.Push(layer_id); 945 rb.Push(layer_id);
928 rb.Push<u64>(ctx.WriteBuffer(native_window.Serialize())); 946 rb.Push<u64>(ctx.WriteBuffer(native_window.Serialize()));
@@ -934,7 +952,7 @@ private:
934 IPC::RequestParser rp{ctx}; 952 IPC::RequestParser rp{ctx};
935 u64 layer_id = rp.Pop<u64>(); 953 u64 layer_id = rp.Pop<u64>();
936 954
937 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 0, 0); 955 IPC::ResponseBuilder rb{ctx, 2};
938 rb.Push(RESULT_SUCCESS); 956 rb.Push(RESULT_SUCCESS);
939 } 957 }
940 958
@@ -945,7 +963,7 @@ private:
945 963
946 auto vsync_event = nv_flinger->GetVsyncEvent(display_id); 964 auto vsync_event = nv_flinger->GetVsyncEvent(display_id);
947 965
948 IPC::ResponseBuilder rb = rp.MakeBuilder(2, 1, 0); 966 IPC::ResponseBuilder rb{ctx, 2, 1};
949 rb.Push(RESULT_SUCCESS); 967 rb.Push(RESULT_SUCCESS);
950 rb.PushCopyObjects(vsync_event); 968 rb.PushCopyObjects(vsync_event);
951 } 969 }
diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index 2b8f78136..c1824b9c3 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -14,11 +14,9 @@
14#include "core/gdbstub/gdbstub.h" 14#include "core/gdbstub/gdbstub.h"
15#include "core/hle/kernel/kernel.h" 15#include "core/hle/kernel/kernel.h"
16#include "core/hle/kernel/process.h" 16#include "core/hle/kernel/process.h"
17#include "core/hle/kernel/resource_limit.h"
18#include "core/hle/service/filesystem/filesystem.h" 17#include "core/hle/service/filesystem/filesystem.h"
19#include "core/loader/deconstructed_rom_directory.h" 18#include "core/loader/deconstructed_rom_directory.h"
20#include "core/loader/nso.h" 19#include "core/loader/nso.h"
21#include "core/memory.h"
22 20
23namespace Loader { 21namespace Loader {
24 22
@@ -88,8 +86,7 @@ FileType AppLoader_DeconstructedRomDirectory::IdentifyType(const FileSys::Virtua
88 return FileType::Error; 86 return FileType::Error;
89} 87}
90 88
91ResultStatus AppLoader_DeconstructedRomDirectory::Load( 89ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) {
92 Kernel::SharedPtr<Kernel::Process>& process) {
93 if (is_loaded) { 90 if (is_loaded) {
94 return ResultStatus::ErrorAlreadyLoaded; 91 return ResultStatus::ErrorAlreadyLoaded;
95 } 92 }
@@ -127,12 +124,16 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
127 metadata.Print(); 124 metadata.Print();
128 125
129 const FileSys::ProgramAddressSpaceType arch_bits{metadata.GetAddressSpaceType()}; 126 const FileSys::ProgramAddressSpaceType arch_bits{metadata.GetAddressSpaceType()};
130 if (arch_bits == FileSys::ProgramAddressSpaceType::Is32Bit) { 127 if (arch_bits == FileSys::ProgramAddressSpaceType::Is32Bit ||
128 arch_bits == FileSys::ProgramAddressSpaceType::Is32BitNoMap) {
131 return ResultStatus::Error32BitISA; 129 return ResultStatus::Error32BitISA;
132 } 130 }
133 131
132 process.LoadFromMetadata(metadata);
133
134 // Load NSO modules 134 // Load NSO modules
135 VAddr next_load_addr{Memory::PROCESS_IMAGE_VADDR}; 135 const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
136 VAddr next_load_addr = base_address;
136 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", 137 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3",
137 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { 138 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) {
138 const FileSys::VirtualFile module_file = dir->GetFile(module); 139 const FileSys::VirtualFile module_file = dir->GetFile(module);
@@ -145,13 +146,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(
145 } 146 }
146 } 147 }
147 148
148 auto& kernel = Core::System::GetInstance().Kernel(); 149 process.Run(base_address, metadata.GetMainThreadPriority(), metadata.GetMainThreadStackSize());
149 process->program_id = metadata.GetTitleID();
150 process->svc_access_mask.set();
151 process->resource_limit =
152 kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
153 process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(),
154 metadata.GetMainThreadStackSize());
155 150
156 // Find the RomFS by searching for a ".romfs" file in this directory 151 // Find the RomFS by searching for a ".romfs" file in this directory
157 const auto& files = dir->GetFiles(); 152 const auto& files = dir->GetFiles();
diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h
index 8a0dc1b1e..d109ed2b5 100644
--- a/src/core/loader/deconstructed_rom_directory.h
+++ b/src/core/loader/deconstructed_rom_directory.h
@@ -7,7 +7,6 @@
7#include <string> 7#include <string>
8#include "common/common_types.h" 8#include "common/common_types.h"
9#include "core/file_sys/program_metadata.h" 9#include "core/file_sys/program_metadata.h"
10#include "core/hle/kernel/object.h"
11#include "core/loader/loader.h" 10#include "core/loader/loader.h"
12 11
13namespace Loader { 12namespace Loader {
@@ -38,7 +37,7 @@ public:
38 return IdentifyType(file); 37 return IdentifyType(file);
39 } 38 }
40 39
41 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 40 ResultStatus Load(Kernel::Process& process) override;
42 41
43 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 42 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
44 ResultStatus ReadIcon(std::vector<u8>& buffer) override; 43 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index 0e2af20b4..e67b49fc9 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -12,7 +12,7 @@
12#include "core/core.h" 12#include "core/core.h"
13#include "core/hle/kernel/kernel.h" 13#include "core/hle/kernel/kernel.h"
14#include "core/hle/kernel/process.h" 14#include "core/hle/kernel/process.h"
15#include "core/hle/kernel/resource_limit.h" 15#include "core/hle/kernel/vm_manager.h"
16#include "core/loader/elf.h" 16#include "core/loader/elf.h"
17#include "core/memory.h" 17#include "core/memory.h"
18 18
@@ -189,7 +189,7 @@ private:
189 189
190 u32* sectionAddrs; 190 u32* sectionAddrs;
191 bool relocate; 191 bool relocate;
192 u32 entryPoint; 192 VAddr entryPoint;
193 193
194public: 194public:
195 explicit ElfReader(void* ptr); 195 explicit ElfReader(void* ptr);
@@ -205,13 +205,13 @@ public:
205 ElfMachine GetMachine() const { 205 ElfMachine GetMachine() const {
206 return (ElfMachine)(header->e_machine); 206 return (ElfMachine)(header->e_machine);
207 } 207 }
208 u32 GetEntryPoint() const { 208 VAddr GetEntryPoint() const {
209 return entryPoint; 209 return entryPoint;
210 } 210 }
211 u32 GetFlags() const { 211 u32 GetFlags() const {
212 return (u32)(header->e_flags); 212 return (u32)(header->e_flags);
213 } 213 }
214 SharedPtr<CodeSet> LoadInto(u32 vaddr); 214 SharedPtr<CodeSet> LoadInto(VAddr vaddr);
215 215
216 int GetNumSegments() const { 216 int GetNumSegments() const {
217 return (int)(header->e_phnum); 217 return (int)(header->e_phnum);
@@ -274,7 +274,7 @@ const char* ElfReader::GetSectionName(int section) const {
274 return nullptr; 274 return nullptr;
275} 275}
276 276
277SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) { 277SharedPtr<CodeSet> ElfReader::LoadInto(VAddr vaddr) {
278 LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx); 278 LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx);
279 279
280 // Should we relocate? 280 // Should we relocate?
@@ -289,11 +289,11 @@ SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
289 LOG_DEBUG(Loader, "{} segments:", header->e_phnum); 289 LOG_DEBUG(Loader, "{} segments:", header->e_phnum);
290 290
291 // First pass : Get the bits into RAM 291 // First pass : Get the bits into RAM
292 u32 base_addr = relocate ? vaddr : 0; 292 const VAddr base_addr = relocate ? vaddr : 0;
293 293
294 u32 total_image_size = 0; 294 u64 total_image_size = 0;
295 for (unsigned int i = 0; i < header->e_phnum; ++i) { 295 for (unsigned int i = 0; i < header->e_phnum; ++i) {
296 Elf32_Phdr* p = &segments[i]; 296 const Elf32_Phdr* p = &segments[i];
297 if (p->p_type == PT_LOAD) { 297 if (p->p_type == PT_LOAD) {
298 total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; 298 total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF;
299 } 299 }
@@ -306,7 +306,7 @@ SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
306 SharedPtr<CodeSet> codeset = CodeSet::Create(kernel, ""); 306 SharedPtr<CodeSet> codeset = CodeSet::Create(kernel, "");
307 307
308 for (unsigned int i = 0; i < header->e_phnum; ++i) { 308 for (unsigned int i = 0; i < header->e_phnum; ++i) {
309 Elf32_Phdr* p = &segments[i]; 309 const Elf32_Phdr* p = &segments[i];
310 LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, 310 LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type,
311 p->p_vaddr, p->p_filesz, p->p_memsz); 311 p->p_vaddr, p->p_filesz, p->p_memsz);
312 312
@@ -333,8 +333,8 @@ SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
333 continue; 333 continue;
334 } 334 }
335 335
336 u32 segment_addr = base_addr + p->p_vaddr; 336 const VAddr segment_addr = base_addr + p->p_vaddr;
337 u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF; 337 const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF;
338 338
339 codeset_segment->offset = current_image_position; 339 codeset_segment->offset = current_image_position;
340 codeset_segment->addr = segment_addr; 340 codeset_segment->addr = segment_addr;
@@ -387,7 +387,7 @@ FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& file) {
387 return FileType::Error; 387 return FileType::Error;
388} 388}
389 389
390ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) { 390ResultStatus AppLoader_ELF::Load(Kernel::Process& process) {
391 if (is_loaded) 391 if (is_loaded)
392 return ResultStatus::ErrorAlreadyLoaded; 392 return ResultStatus::ErrorAlreadyLoaded;
393 393
@@ -395,19 +395,13 @@ ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) {
395 if (buffer.size() != file->GetSize()) 395 if (buffer.size() != file->GetSize())
396 return ResultStatus::ErrorIncorrectELFFileSize; 396 return ResultStatus::ErrorIncorrectELFFileSize;
397 397
398 const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
398 ElfReader elf_reader(&buffer[0]); 399 ElfReader elf_reader(&buffer[0]);
399 SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); 400 SharedPtr<CodeSet> codeset = elf_reader.LoadInto(base_address);
400 codeset->name = file->GetName(); 401 codeset->name = file->GetName();
401 402
402 process->LoadModule(codeset, codeset->entrypoint); 403 process.LoadModule(codeset, codeset->entrypoint);
403 process->svc_access_mask.set(); 404 process.Run(codeset->entrypoint, 48, Memory::DEFAULT_STACK_SIZE);
404
405 // Attach the default resource limit (APPLICATION) to the process
406 auto& kernel = Core::System::GetInstance().Kernel();
407 process->resource_limit =
408 kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
409
410 process->Run(codeset->entrypoint, 48, Memory::DEFAULT_STACK_SIZE);
411 405
412 is_loaded = true; 406 is_loaded = true;
413 return ResultStatus::Success; 407 return ResultStatus::Success;
diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h
index b8fb982d0..6af76441c 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -8,9 +8,6 @@
8#include "common/common_types.h" 8#include "common/common_types.h"
9#include "core/loader/loader.h" 9#include "core/loader/loader.h"
10 10
11////////////////////////////////////////////////////////////////////////////////////////////////////
12// Loader namespace
13
14namespace Loader { 11namespace Loader {
15 12
16/// Loads an ELF/AXF file 13/// Loads an ELF/AXF file
@@ -29,7 +26,7 @@ public:
29 return IdentifyType(file); 26 return IdentifyType(file);
30 } 27 }
31 28
32 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 29 ResultStatus Load(Kernel::Process& process) override;
33}; 30};
34 31
35} // namespace Loader 32} // namespace Loader
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 843c4bb91..20e66109b 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -12,7 +12,6 @@
12#include <boost/optional.hpp> 12#include <boost/optional.hpp>
13#include "common/common_types.h" 13#include "common/common_types.h"
14#include "core/file_sys/vfs.h" 14#include "core/file_sys/vfs.h"
15#include "core/hle/kernel/object.h"
16 15
17namespace Kernel { 16namespace Kernel {
18struct AddressMapping; 17struct AddressMapping;
@@ -136,7 +135,7 @@ public:
136 * @param process The newly created process. 135 * @param process The newly created process.
137 * @return The status result of the operation. 136 * @return The status result of the operation.
138 */ 137 */
139 virtual ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) = 0; 138 virtual ResultStatus Load(Kernel::Process& process) = 0;
140 139
141 /** 140 /**
142 * Loads the system mode that this application needs. 141 * Loads the system mode that this application needs.
diff --git a/src/core/loader/nax.cpp b/src/core/loader/nax.cpp
index b46d81c02..073fb9d2f 100644
--- a/src/core/loader/nax.cpp
+++ b/src/core/loader/nax.cpp
@@ -11,6 +11,20 @@
11#include "core/loader/nca.h" 11#include "core/loader/nca.h"
12 12
13namespace Loader { 13namespace Loader {
14namespace {
15FileType IdentifyTypeImpl(const FileSys::NAX& nax) {
16 if (nax.GetStatus() != ResultStatus::Success) {
17 return FileType::Error;
18 }
19
20 const auto nca = nax.AsNCA();
21 if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
22 return FileType::Error;
23 }
24
25 return FileType::NAX;
26}
27} // Anonymous namespace
14 28
15AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file) 29AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file)
16 : AppLoader(file), nax(std::make_unique<FileSys::NAX>(file)), 30 : AppLoader(file), nax(std::make_unique<FileSys::NAX>(file)),
@@ -19,17 +33,15 @@ AppLoader_NAX::AppLoader_NAX(FileSys::VirtualFile file)
19AppLoader_NAX::~AppLoader_NAX() = default; 33AppLoader_NAX::~AppLoader_NAX() = default;
20 34
21FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) { 35FileType AppLoader_NAX::IdentifyType(const FileSys::VirtualFile& file) {
22 FileSys::NAX nax(file); 36 const FileSys::NAX nax(file);
23 37 return IdentifyTypeImpl(nax);
24 if (nax.GetStatus() == ResultStatus::Success && nax.AsNCA() != nullptr && 38}
25 nax.AsNCA()->GetStatus() == ResultStatus::Success) {
26 return FileType::NAX;
27 }
28 39
29 return FileType::Error; 40FileType AppLoader_NAX::GetFileType() {
41 return IdentifyTypeImpl(*nax);
30} 42}
31 43
32ResultStatus AppLoader_NAX::Load(Kernel::SharedPtr<Kernel::Process>& process) { 44ResultStatus AppLoader_NAX::Load(Kernel::Process& process) {
33 if (is_loaded) { 45 if (is_loaded) {
34 return ResultStatus::ErrorAlreadyLoaded; 46 return ResultStatus::ErrorAlreadyLoaded;
35 } 47 }
diff --git a/src/core/loader/nax.h b/src/core/loader/nax.h
index 4dbae2918..fc3c01876 100644
--- a/src/core/loader/nax.h
+++ b/src/core/loader/nax.h
@@ -31,11 +31,9 @@ public:
31 */ 31 */
32 static FileType IdentifyType(const FileSys::VirtualFile& file); 32 static FileType IdentifyType(const FileSys::VirtualFile& file);
33 33
34 FileType GetFileType() override { 34 FileType GetFileType() override;
35 return IdentifyType(file);
36 }
37 35
38 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 36 ResultStatus Load(Kernel::Process& process) override;
39 37
40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 38 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
41 ResultStatus ReadProgramId(u64& out_program_id) override; 39 ResultStatus ReadProgramId(u64& out_program_id) override;
diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp
index 6aaffae59..7e1b0d84f 100644
--- a/src/core/loader/nca.cpp
+++ b/src/core/loader/nca.cpp
@@ -30,7 +30,7 @@ FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& file) {
30 return FileType::Error; 30 return FileType::Error;
31} 31}
32 32
33ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) { 33ResultStatus AppLoader_NCA::Load(Kernel::Process& process) {
34 if (is_loaded) { 34 if (is_loaded) {
35 return ResultStatus::ErrorAlreadyLoaded; 35 return ResultStatus::ErrorAlreadyLoaded;
36 } 36 }
diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h
index 10be197c4..95d9b73a1 100644
--- a/src/core/loader/nca.h
+++ b/src/core/loader/nca.h
@@ -6,7 +6,6 @@
6 6
7#include "common/common_types.h" 7#include "common/common_types.h"
8#include "core/file_sys/vfs.h" 8#include "core/file_sys/vfs.h"
9#include "core/hle/kernel/object.h"
10#include "core/loader/loader.h" 9#include "core/loader/loader.h"
11 10
12namespace FileSys { 11namespace FileSys {
@@ -34,7 +33,7 @@ public:
34 return IdentifyType(file); 33 return IdentifyType(file);
35 } 34 }
36 35
37 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 36 ResultStatus Load(Kernel::Process& process) override;
38 37
39 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 38 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
40 u64 ReadRomFSIVFCOffset() const override; 39 u64 ReadRomFSIVFCOffset() const override;
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index c49ec34ab..c10f826a4 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -16,7 +16,7 @@
16#include "core/gdbstub/gdbstub.h" 16#include "core/gdbstub/gdbstub.h"
17#include "core/hle/kernel/kernel.h" 17#include "core/hle/kernel/kernel.h"
18#include "core/hle/kernel/process.h" 18#include "core/hle/kernel/process.h"
19#include "core/hle/kernel/resource_limit.h" 19#include "core/hle/kernel/vm_manager.h"
20#include "core/loader/nro.h" 20#include "core/loader/nro.h"
21#include "core/memory.h" 21#include "core/memory.h"
22 22
@@ -175,23 +175,19 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) {
175 return true; 175 return true;
176} 176}
177 177
178ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) { 178ResultStatus AppLoader_NRO::Load(Kernel::Process& process) {
179 if (is_loaded) { 179 if (is_loaded) {
180 return ResultStatus::ErrorAlreadyLoaded; 180 return ResultStatus::ErrorAlreadyLoaded;
181 } 181 }
182 182
183 // Load NRO 183 // Load NRO
184 static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; 184 const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
185 185
186 if (!LoadNro(file, base_addr)) { 186 if (!LoadNro(file, base_address)) {
187 return ResultStatus::ErrorLoadingNRO; 187 return ResultStatus::ErrorLoadingNRO;
188 } 188 }
189 189
190 auto& kernel = Core::System::GetInstance().Kernel(); 190 process.Run(base_address, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
191 process->svc_access_mask.set();
192 process->resource_limit =
193 kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
194 process->Run(base_addr, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
195 191
196 is_loaded = true; 192 is_loaded = true;
197 return ResultStatus::Success; 193 return ResultStatus::Success;
diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h
index 96d2de305..04b46119a 100644
--- a/src/core/loader/nro.h
+++ b/src/core/loader/nro.h
@@ -6,7 +6,6 @@
6 6
7#include <string> 7#include <string>
8#include "common/common_types.h" 8#include "common/common_types.h"
9#include "core/hle/kernel/object.h"
10#include "core/loader/linker.h" 9#include "core/loader/linker.h"
11#include "core/loader/loader.h" 10#include "core/loader/loader.h"
12 11
@@ -33,7 +32,7 @@ public:
33 return IdentifyType(file); 32 return IdentifyType(file);
34 } 33 }
35 34
36 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 35 ResultStatus Load(Kernel::Process& process) override;
37 36
38 ResultStatus ReadIcon(std::vector<u8>& buffer) override; 37 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
39 ResultStatus ReadProgramId(u64& out_program_id) override; 38 ResultStatus ReadProgramId(u64& out_program_id) override;
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 3c6306818..cbe2a3e53 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -13,7 +13,7 @@
13#include "core/gdbstub/gdbstub.h" 13#include "core/gdbstub/gdbstub.h"
14#include "core/hle/kernel/kernel.h" 14#include "core/hle/kernel/kernel.h"
15#include "core/hle/kernel/process.h" 15#include "core/hle/kernel/process.h"
16#include "core/hle/kernel/resource_limit.h" 16#include "core/hle/kernel/vm_manager.h"
17#include "core/loader/nso.h" 17#include "core/loader/nso.h"
18#include "core/memory.h" 18#include "core/memory.h"
19 19
@@ -32,11 +32,18 @@ static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect
32 32
33struct NsoHeader { 33struct NsoHeader {
34 u32_le magic; 34 u32_le magic;
35 INSERT_PADDING_BYTES(0xc); 35 u32_le version;
36 INSERT_PADDING_WORDS(1);
37 u8 flags;
36 std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order) 38 std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
37 u32_le bss_size; 39 u32_le bss_size;
38 INSERT_PADDING_BYTES(0x1c); 40 INSERT_PADDING_BYTES(0x1c);
39 std::array<u32_le, 3> segments_compressed_size; 41 std::array<u32_le, 3> segments_compressed_size;
42
43 bool IsSegmentCompressed(size_t segment_num) const {
44 ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num);
45 return ((flags >> segment_num) & 1);
46 }
40}; 47};
41static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); 48static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size.");
42static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable."); 49static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable.");
@@ -105,9 +112,11 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) {
105 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(kernel, ""); 112 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(kernel, "");
106 std::vector<u8> program_image; 113 std::vector<u8> program_image;
107 for (std::size_t i = 0; i < nso_header.segments.size(); ++i) { 114 for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
108 const std::vector<u8> compressed_data = 115 std::vector<u8> data =
109 file->ReadBytes(nso_header.segments_compressed_size[i], nso_header.segments[i].offset); 116 file->ReadBytes(nso_header.segments_compressed_size[i], nso_header.segments[i].offset);
110 std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]); 117 if (nso_header.IsSegmentCompressed(i)) {
118 data = DecompressSegment(data, nso_header.segments[i]);
119 }
111 program_image.resize(nso_header.segments[i].location); 120 program_image.resize(nso_header.segments[i].location);
112 program_image.insert(program_image.end(), data.begin(), data.end()); 121 program_image.insert(program_image.end(), data.begin(), data.end());
113 codeset->segments[i].addr = nso_header.segments[i].location; 122 codeset->segments[i].addr = nso_header.segments[i].location;
@@ -144,21 +153,17 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) {
144 return load_base + image_size; 153 return load_base + image_size;
145} 154}
146 155
147ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) { 156ResultStatus AppLoader_NSO::Load(Kernel::Process& process) {
148 if (is_loaded) { 157 if (is_loaded) {
149 return ResultStatus::ErrorAlreadyLoaded; 158 return ResultStatus::ErrorAlreadyLoaded;
150 } 159 }
151 160
152 // Load module 161 // Load module
153 LoadModule(file, Memory::PROCESS_IMAGE_VADDR); 162 const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
154 LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), Memory::PROCESS_IMAGE_VADDR); 163 LoadModule(file, base_address);
164 LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
155 165
156 auto& kernel = Core::System::GetInstance().Kernel(); 166 process.Run(base_address, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
157 process->svc_access_mask.set();
158 process->resource_limit =
159 kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
160 process->Run(Memory::PROCESS_IMAGE_VADDR, Kernel::THREADPRIO_DEFAULT,
161 Memory::DEFAULT_STACK_SIZE);
162 167
163 is_loaded = true; 168 is_loaded = true;
164 return ResultStatus::Success; 169 return ResultStatus::Success;
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index aaeb1f2a9..7f142405b 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -4,9 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <string>
8#include "common/common_types.h" 7#include "common/common_types.h"
9#include "core/hle/kernel/object.h"
10#include "core/loader/linker.h" 8#include "core/loader/linker.h"
11#include "core/loader/loader.h" 9#include "core/loader/loader.h"
12 10
@@ -30,7 +28,7 @@ public:
30 28
31 static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base); 29 static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base);
32 30
33 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 31 ResultStatus Load(Kernel::Process& process) override;
34}; 32};
35 33
36} // namespace Loader 34} // namespace Loader
diff --git a/src/core/loader/nsp.cpp b/src/core/loader/nsp.cpp
index 291a9876d..b7ba77ef4 100644
--- a/src/core/loader/nsp.cpp
+++ b/src/core/loader/nsp.cpp
@@ -10,8 +10,6 @@
10#include "core/file_sys/control_metadata.h" 10#include "core/file_sys/control_metadata.h"
11#include "core/file_sys/nca_metadata.h" 11#include "core/file_sys/nca_metadata.h"
12#include "core/file_sys/patch_manager.h" 12#include "core/file_sys/patch_manager.h"
13#include "core/file_sys/registered_cache.h"
14#include "core/file_sys/romfs.h"
15#include "core/file_sys/submission_package.h" 13#include "core/file_sys/submission_package.h"
16#include "core/hle/kernel/process.h" 14#include "core/hle/kernel/process.h"
17#include "core/loader/deconstructed_rom_directory.h" 15#include "core/loader/deconstructed_rom_directory.h"
@@ -62,7 +60,7 @@ FileType AppLoader_NSP::IdentifyType(const FileSys::VirtualFile& file) {
62 return FileType::Error; 60 return FileType::Error;
63} 61}
64 62
65ResultStatus AppLoader_NSP::Load(Kernel::SharedPtr<Kernel::Process>& process) { 63ResultStatus AppLoader_NSP::Load(Kernel::Process& process) {
66 if (is_loaded) { 64 if (is_loaded) {
67 return ResultStatus::ErrorAlreadyLoaded; 65 return ResultStatus::ErrorAlreadyLoaded;
68 } 66 }
diff --git a/src/core/loader/nsp.h b/src/core/loader/nsp.h
index 7ef810499..eac9b819a 100644
--- a/src/core/loader/nsp.h
+++ b/src/core/loader/nsp.h
@@ -35,7 +35,7 @@ public:
35 return IdentifyType(file); 35 return IdentifyType(file);
36 } 36 }
37 37
38 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 38 ResultStatus Load(Kernel::Process& process) override;
39 39
40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
41 ResultStatus ReadProgramId(u64& out_program_id) override; 41 ResultStatus ReadProgramId(u64& out_program_id) override;
diff --git a/src/core/loader/xci.cpp b/src/core/loader/xci.cpp
index 16509229f..eda67a8c8 100644
--- a/src/core/loader/xci.cpp
+++ b/src/core/loader/xci.cpp
@@ -9,8 +9,6 @@
9#include "core/file_sys/content_archive.h" 9#include "core/file_sys/content_archive.h"
10#include "core/file_sys/control_metadata.h" 10#include "core/file_sys/control_metadata.h"
11#include "core/file_sys/patch_manager.h" 11#include "core/file_sys/patch_manager.h"
12#include "core/file_sys/romfs.h"
13#include "core/file_sys/submission_package.h"
14#include "core/hle/kernel/process.h" 12#include "core/hle/kernel/process.h"
15#include "core/loader/nca.h" 13#include "core/loader/nca.h"
16#include "core/loader/xci.h" 14#include "core/loader/xci.h"
@@ -46,7 +44,7 @@ FileType AppLoader_XCI::IdentifyType(const FileSys::VirtualFile& file) {
46 return FileType::Error; 44 return FileType::Error;
47} 45}
48 46
49ResultStatus AppLoader_XCI::Load(Kernel::SharedPtr<Kernel::Process>& process) { 47ResultStatus AppLoader_XCI::Load(Kernel::Process& process) {
50 if (is_loaded) { 48 if (is_loaded) {
51 return ResultStatus::ErrorAlreadyLoaded; 49 return ResultStatus::ErrorAlreadyLoaded;
52 } 50 }
diff --git a/src/core/loader/xci.h b/src/core/loader/xci.h
index cc4287e17..17e47b658 100644
--- a/src/core/loader/xci.h
+++ b/src/core/loader/xci.h
@@ -35,7 +35,7 @@ public:
35 return IdentifyType(file); 35 return IdentifyType(file);
36 } 36 }
37 37
38 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override; 38 ResultStatus Load(Kernel::Process& process) override;
39 39
40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; 40 ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override;
41 ResultStatus ReadProgramId(u64& out_program_id) override; 41 ResultStatus ReadProgramId(u64& out_program_id) override;
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 316b46820..014298ed6 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -3,7 +3,6 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm> 5#include <algorithm>
6#include <array>
7#include <cstring> 6#include <cstring>
8#include <utility> 7#include <utility>
9 8
@@ -15,11 +14,11 @@
15#include "core/arm/arm_interface.h" 14#include "core/arm/arm_interface.h"
16#include "core/core.h" 15#include "core/core.h"
17#include "core/hle/kernel/process.h" 16#include "core/hle/kernel/process.h"
17#include "core/hle/kernel/vm_manager.h"
18#include "core/hle/lock.h" 18#include "core/hle/lock.h"
19#include "core/memory.h" 19#include "core/memory.h"
20#include "core/memory_setup.h" 20#include "core/memory_setup.h"
21#include "video_core/renderer_base.h" 21#include "video_core/renderer_base.h"
22#include "video_core/video_core.h"
23 22
24namespace Memory { 23namespace Memory {
25 24
@@ -41,6 +40,21 @@ PageTable* GetCurrentPageTable() {
41 return current_page_table; 40 return current_page_table;
42} 41}
43 42
43PageTable::PageTable() = default;
44
45PageTable::PageTable(std::size_t address_space_width_in_bits) {
46 Resize(address_space_width_in_bits);
47}
48
49PageTable::~PageTable() = default;
50
51void PageTable::Resize(std::size_t address_space_width_in_bits) {
52 const std::size_t num_page_table_entries = 1ULL << (address_space_width_in_bits - PAGE_BITS);
53
54 pointers.resize(num_page_table_entries);
55 attributes.resize(num_page_table_entries);
56}
57
44static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, PageType type) { 58static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, PageType type) {
45 LOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(memory), base * PAGE_SIZE, 59 LOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(memory), base * PAGE_SIZE,
46 (base + size) * PAGE_SIZE); 60 (base + size) * PAGE_SIZE);
@@ -50,7 +64,7 @@ static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, Pa
50 64
51 VAddr end = base + size; 65 VAddr end = base + size;
52 while (base != end) { 66 while (base != end) {
53 ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at {:016X}", base); 67 ASSERT_MSG(base < page_table.pointers.size(), "out of range mapping at {:016X}", base);
54 68
55 page_table.attributes[base] = type; 69 page_table.attributes[base] = type;
56 page_table.pointers[base] = memory; 70 page_table.pointers[base] = memory;
@@ -105,7 +119,7 @@ void RemoveDebugHook(PageTable& page_table, VAddr base, u64 size, MemoryHookPoin
105static u8* GetPointerFromVMA(const Kernel::Process& process, VAddr vaddr) { 119static u8* GetPointerFromVMA(const Kernel::Process& process, VAddr vaddr) {
106 u8* direct_pointer = nullptr; 120 u8* direct_pointer = nullptr;
107 121
108 auto& vm_manager = process.vm_manager; 122 auto& vm_manager = process.VMManager();
109 123
110 auto it = vm_manager.FindVMA(vaddr); 124 auto it = vm_manager.FindVMA(vaddr);
111 ASSERT(it != vm_manager.vma_map.end()); 125 ASSERT(it != vm_manager.vma_map.end());
@@ -200,7 +214,7 @@ void Write(const VAddr vaddr, const T data) {
200} 214}
201 215
202bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) { 216bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
203 auto& page_table = process.vm_manager.page_table; 217 const auto& page_table = process.VMManager().page_table;
204 218
205 const u8* page_pointer = page_table.pointers[vaddr >> PAGE_BITS]; 219 const u8* page_pointer = page_table.pointers[vaddr >> PAGE_BITS];
206 if (page_pointer) 220 if (page_pointer)
@@ -323,7 +337,7 @@ void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
323 return; 337 return;
324 } 338 }
325 339
326 VAddr end = start + size; 340 const VAddr end = start + size;
327 341
328 const auto CheckRegion = [&](VAddr region_start, VAddr region_end) { 342 const auto CheckRegion = [&](VAddr region_start, VAddr region_end) {
329 if (start >= region_end || end <= region_start) { 343 if (start >= region_end || end <= region_start) {
@@ -333,7 +347,7 @@ void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
333 347
334 const VAddr overlap_start = std::max(start, region_start); 348 const VAddr overlap_start = std::max(start, region_start);
335 const VAddr overlap_end = std::min(end, region_end); 349 const VAddr overlap_end = std::min(end, region_end);
336 const u64 overlap_size = overlap_end - overlap_start; 350 const VAddr overlap_size = overlap_end - overlap_start;
337 351
338 auto& rasterizer = system_instance.Renderer().Rasterizer(); 352 auto& rasterizer = system_instance.Renderer().Rasterizer();
339 switch (mode) { 353 switch (mode) {
@@ -349,8 +363,10 @@ void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
349 } 363 }
350 }; 364 };
351 365
352 CheckRegion(PROCESS_IMAGE_VADDR, PROCESS_IMAGE_VADDR_END); 366 const auto& vm_manager = Core::CurrentProcess()->VMManager();
353 CheckRegion(HEAP_VADDR, HEAP_VADDR_END); 367
368 CheckRegion(vm_manager.GetCodeRegionBaseAddress(), vm_manager.GetCodeRegionEndAddress());
369 CheckRegion(vm_manager.GetHeapRegionBaseAddress(), vm_manager.GetHeapRegionEndAddress());
354} 370}
355 371
356u8 Read8(const VAddr addr) { 372u8 Read8(const VAddr addr) {
@@ -371,7 +387,7 @@ u64 Read64(const VAddr addr) {
371 387
372void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer, 388void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
373 const std::size_t size) { 389 const std::size_t size) {
374 auto& page_table = process.vm_manager.page_table; 390 const auto& page_table = process.VMManager().page_table;
375 391
376 std::size_t remaining_size = size; 392 std::size_t remaining_size = size;
377 std::size_t page_index = src_addr >> PAGE_BITS; 393 std::size_t page_index = src_addr >> PAGE_BITS;
@@ -436,7 +452,7 @@ void Write64(const VAddr addr, const u64 data) {
436 452
437void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer, 453void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
438 const std::size_t size) { 454 const std::size_t size) {
439 auto& page_table = process.vm_manager.page_table; 455 const auto& page_table = process.VMManager().page_table;
440 std::size_t remaining_size = size; 456 std::size_t remaining_size = size;
441 std::size_t page_index = dest_addr >> PAGE_BITS; 457 std::size_t page_index = dest_addr >> PAGE_BITS;
442 std::size_t page_offset = dest_addr & PAGE_MASK; 458 std::size_t page_offset = dest_addr & PAGE_MASK;
@@ -482,7 +498,7 @@ void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t
482} 498}
483 499
484void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) { 500void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) {
485 auto& page_table = process.vm_manager.page_table; 501 const auto& page_table = process.VMManager().page_table;
486 std::size_t remaining_size = size; 502 std::size_t remaining_size = size;
487 std::size_t page_index = dest_addr >> PAGE_BITS; 503 std::size_t page_index = dest_addr >> PAGE_BITS;
488 std::size_t page_offset = dest_addr & PAGE_MASK; 504 std::size_t page_offset = dest_addr & PAGE_MASK;
@@ -524,7 +540,7 @@ void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std:
524 540
525void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr, 541void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
526 const std::size_t size) { 542 const std::size_t size) {
527 auto& page_table = process.vm_manager.page_table; 543 const auto& page_table = process.VMManager().page_table;
528 std::size_t remaining_size = size; 544 std::size_t remaining_size = size;
529 std::size_t page_index = src_addr >> PAGE_BITS; 545 std::size_t page_index = src_addr >> PAGE_BITS;
530 std::size_t page_offset = src_addr & PAGE_MASK; 546 std::size_t page_offset = src_addr & PAGE_MASK;
diff --git a/src/core/memory.h b/src/core/memory.h
index 2a27c0251..1acf5ce8c 100644
--- a/src/core/memory.h
+++ b/src/core/memory.h
@@ -4,10 +4,10 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
8#include <cstddef> 7#include <cstddef>
9#include <string> 8#include <string>
10#include <tuple> 9#include <tuple>
10#include <vector>
11#include <boost/icl/interval_map.hpp> 11#include <boost/icl/interval_map.hpp>
12#include "common/common_types.h" 12#include "common/common_types.h"
13#include "core/memory_hook.h" 13#include "core/memory_hook.h"
@@ -23,10 +23,8 @@ namespace Memory {
23 * be mapped. 23 * be mapped.
24 */ 24 */
25constexpr std::size_t PAGE_BITS = 12; 25constexpr std::size_t PAGE_BITS = 12;
26constexpr u64 PAGE_SIZE = 1 << PAGE_BITS; 26constexpr u64 PAGE_SIZE = 1ULL << PAGE_BITS;
27constexpr u64 PAGE_MASK = PAGE_SIZE - 1; 27constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
28constexpr std::size_t ADDRESS_SPACE_BITS = 36;
29constexpr std::size_t PAGE_TABLE_NUM_ENTRIES = 1ULL << (ADDRESS_SPACE_BITS - PAGE_BITS);
30 28
31enum class PageType : u8 { 29enum class PageType : u8 {
32 /// Page is unmapped and should cause an access error. 30 /// Page is unmapped and should cause an access error.
@@ -62,32 +60,39 @@ struct SpecialRegion {
62 * mimics the way a real CPU page table works. 60 * mimics the way a real CPU page table works.
63 */ 61 */
64struct PageTable { 62struct PageTable {
63 explicit PageTable();
64 explicit PageTable(std::size_t address_space_width_in_bits);
65 ~PageTable();
66
67 /**
68 * Resizes the page table to be able to accomodate enough pages within
69 * a given address space.
70 *
71 * @param address_space_width_in_bits The address size width in bits.
72 */
73 void Resize(std::size_t address_space_width_in_bits);
74
65 /** 75 /**
66 * Array of memory pointers backing each page. An entry can only be non-null if the 76 * Vector of memory pointers backing each page. An entry can only be non-null if the
67 * corresponding entry in the `attributes` array is of type `Memory`. 77 * corresponding entry in the `attributes` vector is of type `Memory`.
68 */ 78 */
69 std::array<u8*, PAGE_TABLE_NUM_ENTRIES> pointers; 79 std::vector<u8*> pointers;
70 80
71 /** 81 /**
72 * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of 82 * Contains MMIO handlers that back memory regions whose entries in the `attribute` vector is
73 * type `Special`. 83 * of type `Special`.
74 */ 84 */
75 boost::icl::interval_map<VAddr, std::set<SpecialRegion>> special_regions; 85 boost::icl::interval_map<VAddr, std::set<SpecialRegion>> special_regions;
76 86
77 /** 87 /**
78 * Array of fine grained page attributes. If it is set to any value other than `Memory`, then 88 * Vector of fine grained page attributes. If it is set to any value other than `Memory`, then
79 * the corresponding entry in `pointers` MUST be set to null. 89 * the corresponding entry in `pointers` MUST be set to null.
80 */ 90 */
81 std::array<PageType, PAGE_TABLE_NUM_ENTRIES> attributes; 91 std::vector<PageType> attributes;
82}; 92};
83 93
84/// Virtual user-space memory regions 94/// Virtual user-space memory regions
85enum : VAddr { 95enum : VAddr {
86 /// Where the application text, data and bss reside.
87 PROCESS_IMAGE_VADDR = 0x08000000,
88 PROCESS_IMAGE_MAX_SIZE = 0x08000000,
89 PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
90
91 /// Read-only page containing kernel and system configuration values. 96 /// Read-only page containing kernel and system configuration values.
92 CONFIG_MEMORY_VADDR = 0x1FF80000, 97 CONFIG_MEMORY_VADDR = 0x1FF80000,
93 CONFIG_MEMORY_SIZE = 0x00001000, 98 CONFIG_MEMORY_SIZE = 0x00001000,
@@ -98,36 +103,12 @@ enum : VAddr {
98 SHARED_PAGE_SIZE = 0x00001000, 103 SHARED_PAGE_SIZE = 0x00001000,
99 SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE, 104 SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
100 105
101 /// Area where TLS (Thread-Local Storage) buffers are allocated. 106 /// TLS (Thread-Local Storage) related.
102 TLS_AREA_VADDR = 0x40000000,
103 TLS_ENTRY_SIZE = 0x200, 107 TLS_ENTRY_SIZE = 0x200,
104 TLS_AREA_SIZE = 0x10000000,
105 TLS_AREA_VADDR_END = TLS_AREA_VADDR + TLS_AREA_SIZE,
106 108
107 /// Application stack 109 /// Application stack
108 STACK_AREA_VADDR = TLS_AREA_VADDR_END,
109 STACK_AREA_SIZE = 0x10000000,
110 STACK_AREA_VADDR_END = STACK_AREA_VADDR + STACK_AREA_SIZE,
111 DEFAULT_STACK_SIZE = 0x100000, 110 DEFAULT_STACK_SIZE = 0x100000,
112 111
113 /// Application heap
114 /// Size is confirmed to be a static value on fw 3.0.0
115 HEAP_VADDR = 0x108000000,
116 HEAP_SIZE = 0x180000000,
117 HEAP_VADDR_END = HEAP_VADDR + HEAP_SIZE,
118
119 /// New map region
120 /// Size is confirmed to be a static value on fw 3.0.0
121 NEW_MAP_REGION_VADDR = HEAP_VADDR_END,
122 NEW_MAP_REGION_SIZE = 0x80000000,
123 NEW_MAP_REGION_VADDR_END = NEW_MAP_REGION_VADDR + NEW_MAP_REGION_SIZE,
124
125 /// Map region
126 /// Size is confirmed to be a static value on fw 3.0.0
127 MAP_REGION_VADDR = NEW_MAP_REGION_VADDR_END,
128 MAP_REGION_SIZE = 0x1000000000,
129 MAP_REGION_VADDR_END = MAP_REGION_VADDR + MAP_REGION_SIZE,
130
131 /// Kernel Virtual Address Range 112 /// Kernel Virtual Address Range
132 KERNEL_REGION_VADDR = 0xFFFFFF8000000000, 113 KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
133 KERNEL_REGION_SIZE = 0x7FFFE00000, 114 KERNEL_REGION_SIZE = 0x7FFFE00000,
diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp
index 7c69fc26e..c0a57e71f 100644
--- a/src/tests/core/arm/arm_test_common.cpp
+++ b/src/tests/core/arm/arm_test_common.cpp
@@ -2,6 +2,8 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
6
5#include "core/core.h" 7#include "core/core.h"
6#include "core/hle/kernel/process.h" 8#include "core/hle/kernel/process.h"
7#include "core/memory.h" 9#include "core/memory.h"
@@ -14,11 +16,12 @@ TestEnvironment::TestEnvironment(bool mutable_memory_)
14 : mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) { 16 : mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) {
15 17
16 Core::CurrentProcess() = Kernel::Process::Create(kernel, ""); 18 Core::CurrentProcess() = Kernel::Process::Create(kernel, "");
17 page_table = &Core::CurrentProcess()->vm_manager.page_table; 19 page_table = &Core::CurrentProcess()->VMManager().page_table;
18 20
19 page_table->pointers.fill(nullptr); 21 std::fill(page_table->pointers.begin(), page_table->pointers.end(), nullptr);
20 page_table->special_regions.clear(); 22 page_table->special_regions.clear();
21 page_table->attributes.fill(Memory::PageType::Unmapped); 23 std::fill(page_table->attributes.begin(), page_table->attributes.end(),
24 Memory::PageType::Unmapped);
22 25
23 Memory::MapIoRegion(*page_table, 0x00000000, 0x80000000, test_memory); 26 Memory::MapIoRegion(*page_table, 0x00000000, 0x80000000, test_memory);
24 Memory::MapIoRegion(*page_table, 0x80000000, 0x80000000, test_memory); 27 Memory::MapIoRegion(*page_table, 0x80000000, 0x80000000, test_memory);
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h
index b81b0723d..9f5581045 100644
--- a/src/video_core/engines/maxwell_3d.h
+++ b/src/video_core/engines/maxwell_3d.h
@@ -41,6 +41,7 @@ public:
41 static constexpr std::size_t NumCBData = 16; 41 static constexpr std::size_t NumCBData = 16;
42 static constexpr std::size_t NumVertexArrays = 32; 42 static constexpr std::size_t NumVertexArrays = 32;
43 static constexpr std::size_t NumVertexAttributes = 32; 43 static constexpr std::size_t NumVertexAttributes = 32;
44 static constexpr std::size_t NumTextureSamplers = 32;
44 static constexpr std::size_t MaxShaderProgram = 6; 45 static constexpr std::size_t MaxShaderProgram = 6;
45 static constexpr std::size_t MaxShaderStage = 5; 46 static constexpr std::size_t MaxShaderStage = 5;
46 // Maximum number of const buffers per shader stage. 47 // Maximum number of const buffers per shader stage.
@@ -461,7 +462,11 @@ public:
461 u32 entry; 462 u32 entry;
462 } macros; 463 } macros;
463 464
464 INSERT_PADDING_WORDS(0x1B8); 465 INSERT_PADDING_WORDS(0x189);
466
467 u32 tfb_enabled;
468
469 INSERT_PADDING_WORDS(0x2E);
465 470
466 RenderTargetConfig rt[NumRenderTargets]; 471 RenderTargetConfig rt[NumRenderTargets];
467 472
@@ -594,7 +599,9 @@ public:
594 599
595 u32 depth_write_enabled; 600 u32 depth_write_enabled;
596 601
597 INSERT_PADDING_WORDS(0x7); 602 u32 alpha_test_enabled;
603
604 INSERT_PADDING_WORDS(0x6);
598 605
599 u32 d3d_cull_mode; 606 u32 d3d_cull_mode;
600 607
@@ -635,7 +642,11 @@ public:
635 642
636 u32 vb_element_base; 643 u32 vb_element_base;
637 644
638 INSERT_PADDING_WORDS(0x40); 645 INSERT_PADDING_WORDS(0x38);
646
647 float point_size;
648
649 INSERT_PADDING_WORDS(0x7);
639 650
640 u32 zeta_enable; 651 u32 zeta_enable;
641 652
@@ -977,6 +988,7 @@ private:
977 "Field " #field_name " has invalid position") 988 "Field " #field_name " has invalid position")
978 989
979ASSERT_REG_POSITION(macros, 0x45); 990ASSERT_REG_POSITION(macros, 0x45);
991ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
980ASSERT_REG_POSITION(rt, 0x200); 992ASSERT_REG_POSITION(rt, 0x200);
981ASSERT_REG_POSITION(viewport_transform[0], 0x280); 993ASSERT_REG_POSITION(viewport_transform[0], 0x280);
982ASSERT_REG_POSITION(viewport, 0x300); 994ASSERT_REG_POSITION(viewport, 0x300);
@@ -996,6 +1008,7 @@ ASSERT_REG_POSITION(zeta_height, 0x48b);
996ASSERT_REG_POSITION(depth_test_enable, 0x4B3); 1008ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
997ASSERT_REG_POSITION(independent_blend_enable, 0x4B9); 1009ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
998ASSERT_REG_POSITION(depth_write_enabled, 0x4BA); 1010ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
1011ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
999ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2); 1012ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
1000ASSERT_REG_POSITION(depth_test_func, 0x4C3); 1013ASSERT_REG_POSITION(depth_test_func, 0x4C3);
1001ASSERT_REG_POSITION(blend, 0x4CF); 1014ASSERT_REG_POSITION(blend, 0x4CF);
@@ -1009,6 +1022,7 @@ ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
1009ASSERT_REG_POSITION(stencil_front_mask, 0x4E7); 1022ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
1010ASSERT_REG_POSITION(screen_y_control, 0x4EB); 1023ASSERT_REG_POSITION(screen_y_control, 0x4EB);
1011ASSERT_REG_POSITION(vb_element_base, 0x50D); 1024ASSERT_REG_POSITION(vb_element_base, 0x50D);
1025ASSERT_REG_POSITION(point_size, 0x546);
1012ASSERT_REG_POSITION(zeta_enable, 0x54E); 1026ASSERT_REG_POSITION(zeta_enable, 0x54E);
1013ASSERT_REG_POSITION(tsc, 0x557); 1027ASSERT_REG_POSITION(tsc, 0x557);
1014ASSERT_REG_POSITION(tic, 0x55D); 1028ASSERT_REG_POSITION(tic, 0x55D);
diff --git a/src/video_core/engines/maxwell_compute.cpp b/src/video_core/engines/maxwell_compute.cpp
index e4e5f9e5e..59e28b22d 100644
--- a/src/video_core/engines/maxwell_compute.cpp
+++ b/src/video_core/engines/maxwell_compute.cpp
@@ -2,12 +2,29 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/logging/log.h"
6#include "core/core.h"
5#include "video_core/engines/maxwell_compute.h" 7#include "video_core/engines/maxwell_compute.h"
6 8
7namespace Tegra { 9namespace Tegra {
8namespace Engines { 10namespace Engines {
9 11
10void MaxwellCompute::WriteReg(u32 method, u32 value) {} 12void MaxwellCompute::WriteReg(u32 method, u32 value) {
13 ASSERT_MSG(method < Regs::NUM_REGS,
14 "Invalid MaxwellCompute register, increase the size of the Regs structure");
15
16 regs.reg_array[method] = value;
17
18 switch (method) {
19 case MAXWELL_COMPUTE_REG_INDEX(compute): {
20 LOG_CRITICAL(HW_GPU, "Compute shaders are not implemented");
21 UNREACHABLE();
22 break;
23 }
24 default:
25 break;
26 }
27}
11 28
12} // namespace Engines 29} // namespace Engines
13} // namespace Tegra 30} // namespace Tegra
diff --git a/src/video_core/engines/maxwell_compute.h b/src/video_core/engines/maxwell_compute.h
index 2b3e4ced6..6ea934fb9 100644
--- a/src/video_core/engines/maxwell_compute.h
+++ b/src/video_core/engines/maxwell_compute.h
@@ -4,17 +4,53 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
8#include "common/assert.h"
9#include "common/bit_field.h"
10#include "common/common_funcs.h"
7#include "common/common_types.h" 11#include "common/common_types.h"
8 12
9namespace Tegra::Engines { 13namespace Tegra::Engines {
10 14
15#define MAXWELL_COMPUTE_REG_INDEX(field_name) \
16 (offsetof(Tegra::Engines::MaxwellCompute::Regs, field_name) / sizeof(u32))
17
11class MaxwellCompute final { 18class MaxwellCompute final {
12public: 19public:
13 MaxwellCompute() = default; 20 MaxwellCompute() = default;
14 ~MaxwellCompute() = default; 21 ~MaxwellCompute() = default;
15 22
23 struct Regs {
24 static constexpr std::size_t NUM_REGS = 0xCF8;
25
26 union {
27 struct {
28 INSERT_PADDING_WORDS(0x281);
29
30 union {
31 u32 compute_end;
32 BitField<0, 1, u32> unknown;
33 } compute;
34
35 INSERT_PADDING_WORDS(0xA76);
36 };
37 std::array<u32, NUM_REGS> reg_array;
38 };
39 } regs{};
40
41 static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32),
42 "MaxwellCompute Regs has wrong size");
43
16 /// Write the value to the register identified by method. 44 /// Write the value to the register identified by method.
17 void WriteReg(u32 method, u32 value); 45 void WriteReg(u32 method, u32 value);
18}; 46};
19 47
48#define ASSERT_REG_POSITION(field_name, position) \
49 static_assert(offsetof(MaxwellCompute::Regs, field_name) == position * 4, \
50 "Field " #field_name " has invalid position")
51
52ASSERT_REG_POSITION(compute, 0x281);
53
54#undef ASSERT_REG_POSITION
55
20} // namespace Tegra::Engines 56} // namespace Tegra::Engines
diff --git a/src/video_core/engines/shader_bytecode.h b/src/video_core/engines/shader_bytecode.h
index 7e1de0fa1..b1f137b9c 100644
--- a/src/video_core/engines/shader_bytecode.h
+++ b/src/video_core/engines/shader_bytecode.h
@@ -5,9 +5,8 @@
5#pragma once 5#pragma once
6 6
7#include <bitset> 7#include <bitset>
8#include <cstring>
9#include <map>
10#include <string> 8#include <string>
9#include <tuple>
11#include <vector> 10#include <vector>
12 11
13#include <boost/optional.hpp> 12#include <boost/optional.hpp>
@@ -315,17 +314,29 @@ enum class TextureMiscMode : u64 {
315 PTP, 314 PTP,
316}; 315};
317 316
318enum class IpaInterpMode : u64 { Linear = 0, Perspective = 1, Flat = 2, Sc = 3 }; 317enum class IpaInterpMode : u64 {
319enum class IpaSampleMode : u64 { Default = 0, Centroid = 1, Offset = 2 }; 318 Linear = 0,
319 Perspective = 1,
320 Flat = 2,
321 Sc = 3,
322};
323
324enum class IpaSampleMode : u64 {
325 Default = 0,
326 Centroid = 1,
327 Offset = 2,
328};
320 329
321struct IpaMode { 330struct IpaMode {
322 IpaInterpMode interpolation_mode; 331 IpaInterpMode interpolation_mode;
323 IpaSampleMode sampling_mode; 332 IpaSampleMode sampling_mode;
324 inline bool operator==(const IpaMode& a) { 333
325 return (a.interpolation_mode == interpolation_mode) && (a.sampling_mode == sampling_mode); 334 bool operator==(const IpaMode& a) const {
335 return std::tie(interpolation_mode, sampling_mode) ==
336 std::tie(a.interpolation_mode, a.sampling_mode);
326 } 337 }
327 inline bool operator!=(const IpaMode& a) { 338 bool operator!=(const IpaMode& a) const {
328 return !((*this) == a); 339 return !operator==(a);
329 } 340 }
330}; 341};
331 342
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 274c2dbcf..14d82a7bc 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -383,7 +383,7 @@ void RasterizerOpenGL::Clear() {
383 bool use_stencil{}; 383 bool use_stencil{};
384 384
385 OpenGLState clear_state; 385 OpenGLState clear_state;
386 clear_state.draw.draw_framebuffer = state.draw.draw_framebuffer; 386 clear_state.draw.draw_framebuffer = framebuffer.handle;
387 clear_state.color_mask.red_enabled = regs.clear_buffers.R ? GL_TRUE : GL_FALSE; 387 clear_state.color_mask.red_enabled = regs.clear_buffers.R ? GL_TRUE : GL_FALSE;
388 clear_state.color_mask.green_enabled = regs.clear_buffers.G ? GL_TRUE : GL_FALSE; 388 clear_state.color_mask.green_enabled = regs.clear_buffers.G ? GL_TRUE : GL_FALSE;
389 clear_state.color_mask.blue_enabled = regs.clear_buffers.B ? GL_TRUE : GL_FALSE; 389 clear_state.color_mask.blue_enabled = regs.clear_buffers.B ? GL_TRUE : GL_FALSE;
@@ -450,6 +450,9 @@ void RasterizerOpenGL::DrawArrays() {
450 SyncBlendState(); 450 SyncBlendState();
451 SyncLogicOpState(); 451 SyncLogicOpState();
452 SyncCullMode(); 452 SyncCullMode();
453 SyncAlphaTest();
454 SyncTransformFeedback();
455 SyncPointState();
453 456
454 // TODO(bunnei): Sync framebuffer_scale uniform here 457 // TODO(bunnei): Sync framebuffer_scale uniform here
455 // TODO(bunnei): Sync scissorbox uniform(s) here 458 // TODO(bunnei): Sync scissorbox uniform(s) here
@@ -484,8 +487,13 @@ void RasterizerOpenGL::DrawArrays() {
484 GLintptr index_buffer_offset = 0; 487 GLintptr index_buffer_offset = 0;
485 if (is_indexed) { 488 if (is_indexed) {
486 MICROPROFILE_SCOPE(OpenGL_Index); 489 MICROPROFILE_SCOPE(OpenGL_Index);
487 index_buffer_offset = 490
488 buffer_cache.UploadMemory(regs.index_array.StartAddress(), index_buffer_size); 491 // Adjust the index buffer offset so it points to the first desired index.
492 auto index_start = regs.index_array.StartAddress();
493 index_start += static_cast<size_t>(regs.index_array.first) *
494 static_cast<size_t>(regs.index_array.FormatSizeInBytes());
495
496 index_buffer_offset = buffer_cache.UploadMemory(index_start, index_buffer_size);
489 } 497 }
490 498
491 SetupShaders(); 499 SetupShaders();
@@ -499,10 +507,6 @@ void RasterizerOpenGL::DrawArrays() {
499 if (is_indexed) { 507 if (is_indexed) {
500 const GLint base_vertex{static_cast<GLint>(regs.vb_element_base)}; 508 const GLint base_vertex{static_cast<GLint>(regs.vb_element_base)};
501 509
502 // Adjust the index buffer offset so it points to the first desired index.
503 index_buffer_offset += static_cast<GLintptr>(regs.index_array.first) *
504 static_cast<GLintptr>(regs.index_array.FormatSizeInBytes());
505
506 if (gpu.state.current_instance > 0) { 510 if (gpu.state.current_instance > 0) {
507 glDrawElementsInstancedBaseVertexBaseInstance( 511 glDrawElementsInstancedBaseVertexBaseInstance(
508 primitive_mode, regs.index_array.count, 512 primitive_mode, regs.index_array.count,
@@ -734,7 +738,7 @@ u32 RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, Shader& shader,
734 } 738 }
735 739
736 texture_samplers[current_bindpoint].SyncWithConfig(texture.tsc); 740 texture_samplers[current_bindpoint].SyncWithConfig(texture.tsc);
737 Surface surface = res_cache.GetTextureSurface(texture); 741 Surface surface = res_cache.GetTextureSurface(texture, entry);
738 if (surface != nullptr) { 742 if (surface != nullptr) {
739 state.texture_units[current_bindpoint].texture = surface->Texture().handle; 743 state.texture_units[current_bindpoint].texture = surface->Texture().handle;
740 state.texture_units[current_bindpoint].target = surface->Target(); 744 state.texture_units[current_bindpoint].target = surface->Target();
@@ -882,4 +886,30 @@ void RasterizerOpenGL::SyncLogicOpState() {
882 state.logic_op.operation = MaxwellToGL::LogicOp(regs.logic_op.operation); 886 state.logic_op.operation = MaxwellToGL::LogicOp(regs.logic_op.operation);
883} 887}
884 888
889void RasterizerOpenGL::SyncAlphaTest() {
890 const auto& regs = Core::System::GetInstance().GPU().Maxwell3D().regs;
891
892 // TODO(Rodrigo): Alpha testing is a legacy OpenGL feature, but it can be
893 // implemented with a test+discard in fragment shaders.
894 if (regs.alpha_test_enabled != 0) {
895 LOG_CRITICAL(Render_OpenGL, "Alpha testing is not implemented");
896 UNREACHABLE();
897 }
898}
899
900void RasterizerOpenGL::SyncTransformFeedback() {
901 const auto& regs = Core::System::GetInstance().GPU().Maxwell3D().regs;
902
903 if (regs.tfb_enabled != 0) {
904 LOG_CRITICAL(Render_OpenGL, "Transform feedbacks are not implemented");
905 UNREACHABLE();
906 }
907}
908
909void RasterizerOpenGL::SyncPointState() {
910 const auto& regs = Core::System::GetInstance().GPU().Maxwell3D().regs;
911
912 state.point.size = regs.point_size;
913}
914
885} // namespace OpenGL 915} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index bf9560bdc..4c8ecbd1c 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -158,6 +158,15 @@ private:
158 /// Syncs the LogicOp state to match the guest state 158 /// Syncs the LogicOp state to match the guest state
159 void SyncLogicOpState(); 159 void SyncLogicOpState();
160 160
161 /// Syncs the alpha test state to match the guest state
162 void SyncAlphaTest();
163
164 /// Syncs the transform feedback state to match the guest state
165 void SyncTransformFeedback();
166
167 /// Syncs the point state to match the guest state
168 void SyncPointState();
169
161 bool has_ARB_direct_state_access = false; 170 bool has_ARB_direct_state_access = false;
162 bool has_ARB_multi_bind = false; 171 bool has_ARB_multi_bind = false;
163 bool has_ARB_separate_shader_objects = false; 172 bool has_ARB_separate_shader_objects = false;
@@ -178,7 +187,7 @@ private:
178 OGLVertexArray> 187 OGLVertexArray>
179 vertex_array_cache; 188 vertex_array_cache;
180 189
181 std::array<SamplerInfo, GLShader::NumTextureSamplers> texture_samplers; 190 std::array<SamplerInfo, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_samplers;
182 191
183 static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024; 192 static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
184 OGLBufferCache buffer_cache; 193 OGLBufferCache buffer_cache;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 86682d7cb..ce967c4d6 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -41,7 +41,7 @@ static VAddr TryGetCpuAddr(Tegra::GPUVAddr gpu_addr) {
41} 41}
42 42
43/*static*/ SurfaceParams SurfaceParams::CreateForTexture( 43/*static*/ SurfaceParams SurfaceParams::CreateForTexture(
44 const Tegra::Texture::FullTextureInfo& config) { 44 const Tegra::Texture::FullTextureInfo& config, const GLShader::SamplerEntry& entry) {
45 SurfaceParams params{}; 45 SurfaceParams params{};
46 params.addr = TryGetCpuAddr(config.tic.Address()); 46 params.addr = TryGetCpuAddr(config.tic.Address());
47 params.is_tiled = config.tic.IsTiled(); 47 params.is_tiled = config.tic.IsTiled();
@@ -60,9 +60,23 @@ static VAddr TryGetCpuAddr(Tegra::GPUVAddr gpu_addr) {
60 case SurfaceTarget::Texture2D: 60 case SurfaceTarget::Texture2D:
61 params.depth = 1; 61 params.depth = 1;
62 break; 62 break;
63 case SurfaceTarget::TextureCubemap:
64 params.depth = config.tic.Depth() * 6;
65 break;
63 case SurfaceTarget::Texture3D: 66 case SurfaceTarget::Texture3D:
67 params.depth = config.tic.Depth();
68 break;
64 case SurfaceTarget::Texture2DArray: 69 case SurfaceTarget::Texture2DArray:
65 params.depth = config.tic.Depth(); 70 params.depth = config.tic.Depth();
71 if (!entry.IsArray()) {
72 // TODO(bunnei): We have seen games re-use a Texture2D as Texture2DArray with depth of
73 // one, but sample the texture in the shader as if it were not an array texture. This
74 // probably is valid on hardware, but we still need to write a test to confirm this. In
75 // emulation, the workaround here is to continue to treat this as a Texture2D. An
76 // example game that does this is Super Mario Odyssey (in Cloud Kingdom).
77 ASSERT(params.depth == 1);
78 params.target = SurfaceTarget::Texture2D;
79 }
66 break; 80 break;
67 default: 81 default:
68 LOG_CRITICAL(HW_GPU, "Unknown depth for target={}", static_cast<u32>(params.target)); 82 LOG_CRITICAL(HW_GPU, "Unknown depth for target={}", static_cast<u32>(params.target));
@@ -71,7 +85,11 @@ static VAddr TryGetCpuAddr(Tegra::GPUVAddr gpu_addr) {
71 break; 85 break;
72 } 86 }
73 87
74 params.size_in_bytes = params.SizeInBytes(); 88 params.size_in_bytes_total = params.SizeInBytesTotal();
89 params.size_in_bytes_2d = params.SizeInBytes2D();
90 params.max_mip_level = config.tic.max_mip_level + 1;
91 params.rt = {};
92
75 return params; 93 return params;
76} 94}
77 95
@@ -89,7 +107,16 @@ static VAddr TryGetCpuAddr(Tegra::GPUVAddr gpu_addr) {
89 params.unaligned_height = config.height; 107 params.unaligned_height = config.height;
90 params.target = SurfaceTarget::Texture2D; 108 params.target = SurfaceTarget::Texture2D;
91 params.depth = 1; 109 params.depth = 1;
92 params.size_in_bytes = params.SizeInBytes(); 110 params.size_in_bytes_total = params.SizeInBytesTotal();
111 params.size_in_bytes_2d = params.SizeInBytes2D();
112 params.max_mip_level = 0;
113
114 // Render target specific parameters, not used for caching
115 params.rt.index = static_cast<u32>(index);
116 params.rt.array_mode = config.array_mode;
117 params.rt.layer_stride = config.layer_stride;
118 params.rt.base_layer = config.base_layer;
119
93 return params; 120 return params;
94} 121}
95 122
@@ -108,7 +135,11 @@ static VAddr TryGetCpuAddr(Tegra::GPUVAddr gpu_addr) {
108 params.unaligned_height = zeta_height; 135 params.unaligned_height = zeta_height;
109 params.target = SurfaceTarget::Texture2D; 136 params.target = SurfaceTarget::Texture2D;
110 params.depth = 1; 137 params.depth = 1;
111 params.size_in_bytes = params.SizeInBytes(); 138 params.size_in_bytes_total = params.SizeInBytesTotal();
139 params.size_in_bytes_2d = params.SizeInBytes2D();
140 params.max_mip_level = 0;
141 params.rt = {};
142
112 return params; 143 return params;
113} 144}
114 145
@@ -141,8 +172,8 @@ static constexpr std::array<FormatTuple, SurfaceParams::MaxPixelFormat> tex_form
141 {GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm, 172 {GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
142 true}, // BC7U 173 true}, // BC7U
143 {GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, 174 {GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB, GL_RGB, GL_UNSIGNED_INT_8_8_8_8,
144 ComponentType::UNorm, true}, // BC6H_UF16 175 ComponentType::Float, true}, // BC6H_UF16
145 {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm, 176 {GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::Float,
146 true}, // BC6H_SF16 177 true}, // BC6H_SF16
147 {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4 178 {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4
148 {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // G8R8U 179 {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // G8R8U
@@ -400,9 +431,13 @@ static constexpr std::array<void (*)(u32, u32, u32, u8*, std::size_t, VAddr),
400 // clang-format on 431 // clang-format on
401}; 432};
402 433
403static bool BlitTextures(GLuint src_tex, const MathUtil::Rectangle<u32>& src_rect, GLuint dst_tex, 434static bool BlitSurface(const Surface& src_surface, const Surface& dst_surface,
404 const MathUtil::Rectangle<u32>& dst_rect, SurfaceType type, 435 GLuint read_fb_handle, GLuint draw_fb_handle, GLenum src_attachment = 0,
405 GLuint read_fb_handle, GLuint draw_fb_handle) { 436 GLenum dst_attachment = 0, std::size_t cubemap_face = 0) {
437
438 const auto& src_params{src_surface->GetSurfaceParams()};
439 const auto& dst_params{dst_surface->GetSurfaceParams()};
440
406 OpenGLState prev_state{OpenGLState::GetCurState()}; 441 OpenGLState prev_state{OpenGLState::GetCurState()};
407 SCOPE_EXIT({ prev_state.Apply(); }); 442 SCOPE_EXIT({ prev_state.Apply(); });
408 443
@@ -413,47 +448,203 @@ static bool BlitTextures(GLuint src_tex, const MathUtil::Rectangle<u32>& src_rec
413 448
414 u32 buffers{}; 449 u32 buffers{};
415 450
416 if (type == SurfaceType::ColorTexture) { 451 if (src_params.type == SurfaceType::ColorTexture) {
417 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, src_tex, 452 switch (src_params.target) {
418 0); 453 case SurfaceParams::SurfaceTarget::Texture2D:
419 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 454 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
420 0); 455 GL_TEXTURE_2D, src_surface->Texture().handle, 0);
456 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
457 0, 0);
458 break;
459 case SurfaceParams::SurfaceTarget::TextureCubemap:
460 glFramebufferTexture2D(
461 GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
462 static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemap_face),
463 src_surface->Texture().handle, 0);
464 glFramebufferTexture2D(
465 GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
466 static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemap_face), 0, 0);
467 break;
468 case SurfaceParams::SurfaceTarget::Texture2DArray:
469 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
470 src_surface->Texture().handle, 0, 0);
471 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, 0, 0, 0);
472 break;
473 case SurfaceParams::SurfaceTarget::Texture3D:
474 glFramebufferTexture3D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
475 SurfaceTargetToGL(src_params.target),
476 src_surface->Texture().handle, 0, 0);
477 glFramebufferTexture3D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
478 SurfaceTargetToGL(src_params.target), 0, 0, 0);
479 break;
480 default:
481 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
482 GL_TEXTURE_2D, src_surface->Texture().handle, 0);
483 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
484 0, 0);
485 break;
486 }
487
488 switch (dst_params.target) {
489 case SurfaceParams::SurfaceTarget::Texture2D:
490 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
491 GL_TEXTURE_2D, dst_surface->Texture().handle, 0);
492 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
493 0, 0);
494 break;
495 case SurfaceParams::SurfaceTarget::TextureCubemap:
496 glFramebufferTexture2D(
497 GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
498 static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemap_face),
499 dst_surface->Texture().handle, 0);
500 glFramebufferTexture2D(
501 GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
502 static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemap_face), 0, 0);
503 break;
504 case SurfaceParams::SurfaceTarget::Texture2DArray:
505 glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
506 dst_surface->Texture().handle, 0, 0);
507 glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, 0, 0, 0);
508 break;
421 509
422 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_tex, 510 case SurfaceParams::SurfaceTarget::Texture3D:
423 0); 511 glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
424 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 512 SurfaceTargetToGL(dst_params.target),
425 0); 513 dst_surface->Texture().handle, 0, 0);
514 glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
515 SurfaceTargetToGL(dst_params.target), 0, 0, 0);
516 break;
517 default:
518 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
519 GL_TEXTURE_2D, dst_surface->Texture().handle, 0);
520 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
521 0, 0);
522 break;
523 }
426 524
427 buffers = GL_COLOR_BUFFER_BIT; 525 buffers = GL_COLOR_BUFFER_BIT;
428 } else if (type == SurfaceType::Depth) { 526 } else if (src_params.type == SurfaceType::Depth) {
429 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 527 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
430 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, src_tex, 0); 528 GL_TEXTURE_2D, 0, 0);
529 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
530 src_surface->Texture().handle, 0);
431 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0); 531 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
432 532
433 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 533 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
434 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dst_tex, 0); 534 GL_TEXTURE_2D, 0, 0);
535 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
536 dst_surface->Texture().handle, 0);
435 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0); 537 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
436 538
437 buffers = GL_DEPTH_BUFFER_BIT; 539 buffers = GL_DEPTH_BUFFER_BIT;
438 } else if (type == SurfaceType::DepthStencil) { 540 } else if (src_params.type == SurfaceType::DepthStencil) {
439 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 541 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + src_attachment,
542 GL_TEXTURE_2D, 0, 0);
440 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 543 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
441 src_tex, 0); 544 src_surface->Texture().handle, 0);
442 545
443 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 546 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + dst_attachment,
547 GL_TEXTURE_2D, 0, 0);
444 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 548 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
445 dst_tex, 0); 549 dst_surface->Texture().handle, 0);
446 550
447 buffers = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; 551 buffers = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
448 } 552 }
449 553
450 glBlitFramebuffer(src_rect.left, src_rect.bottom, src_rect.right, src_rect.top, dst_rect.left, 554 const auto& rect{src_params.GetRect()};
451 dst_rect.bottom, dst_rect.right, dst_rect.top, buffers, 555 glBlitFramebuffer(rect.left, rect.bottom, rect.right, rect.top, rect.left, rect.bottom,
556 rect.right, rect.top, buffers,
452 buffers == GL_COLOR_BUFFER_BIT ? GL_LINEAR : GL_NEAREST); 557 buffers == GL_COLOR_BUFFER_BIT ? GL_LINEAR : GL_NEAREST);
453 558
454 return true; 559 return true;
455} 560}
456 561
562static void CopySurface(const Surface& src_surface, const Surface& dst_surface,
563 GLuint copy_pbo_handle, GLenum src_attachment = 0,
564 GLenum dst_attachment = 0, std::size_t cubemap_face = 0) {
565 ASSERT_MSG(dst_attachment == 0, "Unimplemented");
566
567 const auto& src_params{src_surface->GetSurfaceParams()};
568 const auto& dst_params{dst_surface->GetSurfaceParams()};
569
570 auto source_format = GetFormatTuple(src_params.pixel_format, src_params.component_type);
571 auto dest_format = GetFormatTuple(dst_params.pixel_format, dst_params.component_type);
572
573 std::size_t buffer_size =
574 std::max(src_params.size_in_bytes_total, dst_params.size_in_bytes_total);
575
576 glBindBuffer(GL_PIXEL_PACK_BUFFER, copy_pbo_handle);
577 glBufferData(GL_PIXEL_PACK_BUFFER, buffer_size, nullptr, GL_STREAM_DRAW_ARB);
578 if (source_format.compressed) {
579 glGetCompressedTextureImage(src_surface->Texture().handle, src_attachment,
580 static_cast<GLsizei>(src_params.size_in_bytes_total), nullptr);
581 } else {
582 glGetTextureImage(src_surface->Texture().handle, src_attachment, source_format.format,
583 source_format.type, static_cast<GLsizei>(src_params.size_in_bytes_total),
584 nullptr);
585 }
586 // If the new texture is bigger than the previous one, we need to fill in the rest with data
587 // from the CPU.
588 if (src_params.size_in_bytes_total < dst_params.size_in_bytes_total) {
589 // Upload the rest of the memory.
590 if (dst_params.is_tiled) {
591 // TODO(Subv): We might have to de-tile the subtexture and re-tile it with the rest
592 // of the data in this case. Games like Super Mario Odyssey seem to hit this case
593 // when drawing, it re-uses the memory of a previous texture as a bigger framebuffer
594 // but it doesn't clear it beforehand, the texture is already full of zeros.
595 LOG_DEBUG(HW_GPU, "Trying to upload extra texture data from the CPU during "
596 "reinterpretation but the texture is tiled.");
597 }
598 std::size_t remaining_size =
599 dst_params.size_in_bytes_total - src_params.size_in_bytes_total;
600 std::vector<u8> data(remaining_size);
601 Memory::ReadBlock(dst_params.addr + src_params.size_in_bytes_total, data.data(),
602 data.size());
603 glBufferSubData(GL_PIXEL_PACK_BUFFER, src_params.size_in_bytes_total, remaining_size,
604 data.data());
605 }
606
607 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
608
609 const GLsizei width{static_cast<GLsizei>(
610 std::min(src_params.GetRect().GetWidth(), dst_params.GetRect().GetWidth()))};
611 const GLsizei height{static_cast<GLsizei>(
612 std::min(src_params.GetRect().GetHeight(), dst_params.GetRect().GetHeight()))};
613
614 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, copy_pbo_handle);
615 if (dest_format.compressed) {
616 LOG_CRITICAL(HW_GPU, "Compressed copy is unimplemented!");
617 UNREACHABLE();
618 } else {
619 switch (dst_params.target) {
620 case SurfaceParams::SurfaceTarget::Texture1D:
621 glTextureSubImage1D(dst_surface->Texture().handle, 0, 0, width, dest_format.format,
622 dest_format.type, nullptr);
623 break;
624 case SurfaceParams::SurfaceTarget::Texture2D:
625 glTextureSubImage2D(dst_surface->Texture().handle, 0, 0, 0, width, height,
626 dest_format.format, dest_format.type, nullptr);
627 break;
628 case SurfaceParams::SurfaceTarget::Texture3D:
629 case SurfaceParams::SurfaceTarget::Texture2DArray:
630 glTextureSubImage3D(dst_surface->Texture().handle, 0, 0, 0, 0, width, height,
631 static_cast<GLsizei>(dst_params.depth), dest_format.format,
632 dest_format.type, nullptr);
633 break;
634 case SurfaceParams::SurfaceTarget::TextureCubemap:
635 glTextureSubImage3D(dst_surface->Texture().handle, 0, 0, 0,
636 static_cast<GLint>(cubemap_face), width, height, 1,
637 dest_format.format, dest_format.type, nullptr);
638 break;
639 default:
640 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
641 static_cast<u32>(dst_params.target));
642 UNREACHABLE();
643 }
644 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
645 }
646}
647
457CachedSurface::CachedSurface(const SurfaceParams& params) 648CachedSurface::CachedSurface(const SurfaceParams& params)
458 : params(params), gl_target(SurfaceTargetToGL(params.target)) { 649 : params(params), gl_target(SurfaceTargetToGL(params.target)) {
459 texture.Create(); 650 texture.Create();
@@ -481,6 +672,7 @@ CachedSurface::CachedSurface(const SurfaceParams& params)
481 rect.GetWidth()); 672 rect.GetWidth());
482 break; 673 break;
483 case SurfaceParams::SurfaceTarget::Texture2D: 674 case SurfaceParams::SurfaceTarget::Texture2D:
675 case SurfaceParams::SurfaceTarget::TextureCubemap:
484 glTexStorage2D(SurfaceTargetToGL(params.target), 1, format_tuple.internal_format, 676 glTexStorage2D(SurfaceTargetToGL(params.target), 1, format_tuple.internal_format,
485 rect.GetWidth(), rect.GetHeight()); 677 rect.GetWidth(), rect.GetHeight());
486 break; 678 break;
@@ -501,6 +693,9 @@ CachedSurface::CachedSurface(const SurfaceParams& params)
501 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MIN_FILTER, GL_LINEAR); 693 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MIN_FILTER, GL_LINEAR);
502 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 694 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
503 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 695 glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
696
697 VideoCore::LabelGLObject(GL_TEXTURE, texture.handle, params.addr,
698 SurfaceParams::SurfaceTargetName(params.target));
504} 699}
505 700
506static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height) { 701static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height) {
@@ -582,29 +777,39 @@ void CachedSurface::LoadGLBuffer() {
582 777
583 const u32 bytes_per_pixel = GetGLBytesPerPixel(params.pixel_format); 778 const u32 bytes_per_pixel = GetGLBytesPerPixel(params.pixel_format);
584 const u32 copy_size = params.width * params.height * bytes_per_pixel; 779 const u32 copy_size = params.width * params.height * bytes_per_pixel;
780 const std::size_t total_size = copy_size * params.depth;
585 781
586 MICROPROFILE_SCOPE(OpenGL_SurfaceLoad); 782 MICROPROFILE_SCOPE(OpenGL_SurfaceLoad);
587 783
588 if (params.is_tiled) { 784 if (params.is_tiled) {
785 gl_buffer.resize(total_size);
786
589 // TODO(bunnei): This only unswizzles and copies a 2D texture - we do not yet know how to do 787 // TODO(bunnei): This only unswizzles and copies a 2D texture - we do not yet know how to do
590 // this for 3D textures, etc. 788 // this for 3D textures, etc.
591 switch (params.target) { 789 switch (params.target) {
592 case SurfaceParams::SurfaceTarget::Texture2D: 790 case SurfaceParams::SurfaceTarget::Texture2D:
593 // Pass impl. to the fallback code below 791 // Pass impl. to the fallback code below
594 break; 792 break;
793 case SurfaceParams::SurfaceTarget::Texture2DArray:
794 case SurfaceParams::SurfaceTarget::TextureCubemap:
795 for (std::size_t index = 0; index < params.depth; ++index) {
796 const std::size_t offset{index * copy_size};
797 morton_to_gl_fns[static_cast<std::size_t>(params.pixel_format)](
798 params.width, params.block_height, params.height, gl_buffer.data() + offset,
799 copy_size, params.addr + offset);
800 }
801 break;
595 default: 802 default:
596 LOG_CRITICAL(HW_GPU, "Unimplemented tiled load for target={}", 803 LOG_CRITICAL(HW_GPU, "Unimplemented tiled load for target={}",
597 static_cast<u32>(params.target)); 804 static_cast<u32>(params.target));
598 UNREACHABLE(); 805 UNREACHABLE();
599 } 806 }
600 807
601 gl_buffer.resize(static_cast<std::size_t>(params.depth) * copy_size);
602 morton_to_gl_fns[static_cast<std::size_t>(params.pixel_format)]( 808 morton_to_gl_fns[static_cast<std::size_t>(params.pixel_format)](
603 params.width, params.block_height, params.height, gl_buffer.data(), copy_size, 809 params.width, params.block_height, params.height, gl_buffer.data(), copy_size,
604 params.addr); 810 params.addr);
605 } else { 811 } else {
606 const u8* const texture_src_data_end{texture_src_data + 812 const u8* const texture_src_data_end{texture_src_data + total_size};
607 (static_cast<std::size_t>(params.depth) * copy_size)};
608 gl_buffer.assign(texture_src_data, texture_src_data_end); 813 gl_buffer.assign(texture_src_data, texture_src_data_end);
609 } 814 }
610 815
@@ -631,7 +836,7 @@ void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle
631 // Load data from memory to the surface 836 // Load data from memory to the surface
632 const GLint x0 = static_cast<GLint>(rect.left); 837 const GLint x0 = static_cast<GLint>(rect.left);
633 const GLint y0 = static_cast<GLint>(rect.bottom); 838 const GLint y0 = static_cast<GLint>(rect.bottom);
634 const std::size_t buffer_offset = 839 std::size_t buffer_offset =
635 static_cast<std::size_t>(static_cast<std::size_t>(y0) * params.width + 840 static_cast<std::size_t>(static_cast<std::size_t>(y0) * params.width +
636 static_cast<std::size_t>(x0)) * 841 static_cast<std::size_t>(x0)) *
637 GetGLBytesPerPixel(params.pixel_format); 842 GetGLBytesPerPixel(params.pixel_format);
@@ -660,15 +865,25 @@ void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle
660 glCompressedTexImage2D( 865 glCompressedTexImage2D(
661 SurfaceTargetToGL(params.target), 0, tuple.internal_format, 866 SurfaceTargetToGL(params.target), 0, tuple.internal_format,
662 static_cast<GLsizei>(params.width), static_cast<GLsizei>(params.height), 0, 867 static_cast<GLsizei>(params.width), static_cast<GLsizei>(params.height), 0,
663 static_cast<GLsizei>(params.size_in_bytes), &gl_buffer[buffer_offset]); 868 static_cast<GLsizei>(params.size_in_bytes_2d), &gl_buffer[buffer_offset]);
664 break; 869 break;
665 case SurfaceParams::SurfaceTarget::Texture3D: 870 case SurfaceParams::SurfaceTarget::Texture3D:
666 case SurfaceParams::SurfaceTarget::Texture2DArray: 871 case SurfaceParams::SurfaceTarget::Texture2DArray:
667 glCompressedTexImage3D( 872 glCompressedTexImage3D(
668 SurfaceTargetToGL(params.target), 0, tuple.internal_format, 873 SurfaceTargetToGL(params.target), 0, tuple.internal_format,
669 static_cast<GLsizei>(params.width), static_cast<GLsizei>(params.height), 874 static_cast<GLsizei>(params.width), static_cast<GLsizei>(params.height),
670 static_cast<GLsizei>(params.depth), 0, static_cast<GLsizei>(params.size_in_bytes), 875 static_cast<GLsizei>(params.depth), 0,
671 &gl_buffer[buffer_offset]); 876 static_cast<GLsizei>(params.size_in_bytes_total), &gl_buffer[buffer_offset]);
877 break;
878 case SurfaceParams::SurfaceTarget::TextureCubemap:
879 for (std::size_t face = 0; face < params.depth; ++face) {
880 glCompressedTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face),
881 0, tuple.internal_format, static_cast<GLsizei>(params.width),
882 static_cast<GLsizei>(params.height), 0,
883 static_cast<GLsizei>(params.size_in_bytes_2d),
884 &gl_buffer[buffer_offset]);
885 buffer_offset += params.size_in_bytes_2d;
886 }
672 break; 887 break;
673 default: 888 default:
674 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}", 889 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
@@ -676,8 +891,8 @@ void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle
676 UNREACHABLE(); 891 UNREACHABLE();
677 glCompressedTexImage2D( 892 glCompressedTexImage2D(
678 GL_TEXTURE_2D, 0, tuple.internal_format, static_cast<GLsizei>(params.width), 893 GL_TEXTURE_2D, 0, tuple.internal_format, static_cast<GLsizei>(params.width),
679 static_cast<GLsizei>(params.height), 0, static_cast<GLsizei>(params.size_in_bytes), 894 static_cast<GLsizei>(params.height), 0,
680 &gl_buffer[buffer_offset]); 895 static_cast<GLsizei>(params.size_in_bytes_2d), &gl_buffer[buffer_offset]);
681 } 896 }
682 } else { 897 } else {
683 898
@@ -700,6 +915,15 @@ void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle
700 static_cast<GLsizei>(rect.GetHeight()), params.depth, tuple.format, 915 static_cast<GLsizei>(rect.GetHeight()), params.depth, tuple.format,
701 tuple.type, &gl_buffer[buffer_offset]); 916 tuple.type, &gl_buffer[buffer_offset]);
702 break; 917 break;
918 case SurfaceParams::SurfaceTarget::TextureCubemap:
919 for (std::size_t face = 0; face < params.depth; ++face) {
920 glTexSubImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), 0, x0,
921 y0, static_cast<GLsizei>(rect.GetWidth()),
922 static_cast<GLsizei>(rect.GetHeight()), tuple.format, tuple.type,
923 &gl_buffer[buffer_offset]);
924 buffer_offset += params.size_in_bytes_2d;
925 }
926 break;
703 default: 927 default:
704 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}", 928 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
705 static_cast<u32>(params.target)); 929 static_cast<u32>(params.target));
@@ -719,8 +943,9 @@ RasterizerCacheOpenGL::RasterizerCacheOpenGL() {
719 copy_pbo.Create(); 943 copy_pbo.Create();
720} 944}
721 945
722Surface RasterizerCacheOpenGL::GetTextureSurface(const Tegra::Texture::FullTextureInfo& config) { 946Surface RasterizerCacheOpenGL::GetTextureSurface(const Tegra::Texture::FullTextureInfo& config,
723 return GetSurface(SurfaceParams::CreateForTexture(config)); 947 const GLShader::SamplerEntry& entry) {
948 return GetSurface(SurfaceParams::CreateForTexture(config, entry));
724} 949}
725 950
726Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) { 951Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) {
@@ -808,98 +1033,69 @@ Surface RasterizerCacheOpenGL::GetUncachedSurface(const SurfaceParams& params) {
808 return surface; 1033 return surface;
809} 1034}
810 1035
811Surface RasterizerCacheOpenGL::RecreateSurface(const Surface& surface, 1036Surface RasterizerCacheOpenGL::RecreateSurface(const Surface& old_surface,
812 const SurfaceParams& new_params) { 1037 const SurfaceParams& new_params) {
813 // Verify surface is compatible for blitting 1038 // Verify surface is compatible for blitting
814 const auto& params{surface->GetSurfaceParams()}; 1039 auto old_params{old_surface->GetSurfaceParams()};
815 1040
816 // Get a new surface with the new parameters, and blit the previous surface to it 1041 // Get a new surface with the new parameters, and blit the previous surface to it
817 Surface new_surface{GetUncachedSurface(new_params)}; 1042 Surface new_surface{GetUncachedSurface(new_params)};
818 1043
819 if (params.pixel_format == new_params.pixel_format || 1044 // If the format is the same, just do a framebuffer blit. This is significantly faster than
820 !Settings::values.use_accurate_framebuffers) { 1045 // using PBOs. The is also likely less accurate, as textures will be converted rather than
821 // If the format is the same, just do a framebuffer blit. This is significantly faster than 1046 // reinterpreted. When use_accurate_framebuffers setting is enabled, perform a more accurate
822 // using PBOs. The is also likely less accurate, as textures will be converted rather than 1047 // surface copy, where pixels are reinterpreted as a new format (without conversion). This
823 // reinterpreted. 1048 // code path uses OpenGL PBOs and is quite slow.
824 1049 const bool is_blit{old_params.pixel_format == new_params.pixel_format ||
825 BlitTextures(surface->Texture().handle, params.GetRect(), new_surface->Texture().handle, 1050 !Settings::values.use_accurate_framebuffers};
826 params.GetRect(), params.type, read_framebuffer.handle,
827 draw_framebuffer.handle);
828 } else {
829 // When use_accurate_framebuffers setting is enabled, perform a more accurate surface copy,
830 // where pixels are reinterpreted as a new format (without conversion). This code path uses
831 // OpenGL PBOs and is quite slow.
832
833 auto source_format = GetFormatTuple(params.pixel_format, params.component_type);
834 auto dest_format = GetFormatTuple(new_params.pixel_format, new_params.component_type);
835 1051
836 std::size_t buffer_size = std::max(params.SizeInBytes(), new_params.SizeInBytes()); 1052 switch (new_params.target) {
837 1053 case SurfaceParams::SurfaceTarget::Texture2D:
838 glBindBuffer(GL_PIXEL_PACK_BUFFER, copy_pbo.handle); 1054 if (is_blit) {
839 glBufferData(GL_PIXEL_PACK_BUFFER, buffer_size, nullptr, GL_STREAM_DRAW_ARB); 1055 BlitSurface(old_surface, new_surface, read_framebuffer.handle, draw_framebuffer.handle);
840 if (source_format.compressed) {
841 glGetCompressedTextureImage(surface->Texture().handle, 0,
842 static_cast<GLsizei>(params.SizeInBytes()), nullptr);
843 } else { 1056 } else {
844 glGetTextureImage(surface->Texture().handle, 0, source_format.format, 1057 CopySurface(old_surface, new_surface, copy_pbo.handle);
845 source_format.type, static_cast<GLsizei>(params.SizeInBytes()),
846 nullptr);
847 } 1058 }
848 // If the new texture is bigger than the previous one, we need to fill in the rest with data 1059 break;
849 // from the CPU. 1060 case SurfaceParams::SurfaceTarget::TextureCubemap: {
850 if (params.SizeInBytes() < new_params.SizeInBytes()) { 1061 if (old_params.rt.array_mode != 1) {
851 // Upload the rest of the memory. 1062 // TODO(bunnei): This is used by Breath of the Wild, I'm not sure how to implement this
852 if (new_params.is_tiled) { 1063 // yet (array rendering used as a cubemap texture).
853 // TODO(Subv): We might have to de-tile the subtexture and re-tile it with the rest 1064 LOG_CRITICAL(HW_GPU, "Unhandled rendertarget array_mode {}", old_params.rt.array_mode);
854 // of the data in this case. Games like Super Mario Odyssey seem to hit this case 1065 UNREACHABLE();
855 // when drawing, it re-uses the memory of a previous texture as a bigger framebuffer 1066 return new_surface;
856 // but it doesn't clear it beforehand, the texture is already full of zeros.
857 LOG_DEBUG(HW_GPU, "Trying to upload extra texture data from the CPU during "
858 "reinterpretation but the texture is tiled.");
859 }
860 std::size_t remaining_size = new_params.SizeInBytes() - params.SizeInBytes();
861 std::vector<u8> data(remaining_size);
862 Memory::ReadBlock(new_params.addr + params.SizeInBytes(), data.data(), data.size());
863 glBufferSubData(GL_PIXEL_PACK_BUFFER, params.SizeInBytes(), remaining_size,
864 data.data());
865 } 1067 }
866 1068
867 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); 1069 // This seems to be used for render-to-cubemap texture
868 1070 ASSERT_MSG(old_params.target == SurfaceParams::SurfaceTarget::Texture2D, "Unexpected");
869 const auto& dest_rect{new_params.GetRect()}; 1071 ASSERT_MSG(old_params.pixel_format == new_params.pixel_format, "Unexpected");
870 1072 ASSERT_MSG(old_params.rt.base_layer == 0, "Unimplemented");
871 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, copy_pbo.handle); 1073
872 if (dest_format.compressed) { 1074 // TODO(bunnei): Verify the below - this stride seems to be in 32-bit words, not pixels.
873 LOG_CRITICAL(HW_GPU, "Compressed copy is unimplemented!"); 1075 // Tested with Splatoon 2, Super Mario Odyssey, and Breath of the Wild.
874 UNREACHABLE(); 1076 const std::size_t byte_stride{old_params.rt.layer_stride * sizeof(u32)};
875 } else { 1077
876 switch (new_params.target) { 1078 for (std::size_t index = 0; index < new_params.depth; ++index) {
877 case SurfaceParams::SurfaceTarget::Texture1D: 1079 Surface face_surface{TryGetReservedSurface(old_params)};
878 glTextureSubImage1D(new_surface->Texture().handle, 0, 0, 1080 ASSERT_MSG(face_surface, "Unexpected");
879 static_cast<GLsizei>(dest_rect.GetWidth()), dest_format.format, 1081
880 dest_format.type, nullptr); 1082 if (is_blit) {
881 break; 1083 BlitSurface(face_surface, new_surface, read_framebuffer.handle,
882 case SurfaceParams::SurfaceTarget::Texture2D: 1084 draw_framebuffer.handle, face_surface->GetSurfaceParams().rt.index,
883 glTextureSubImage2D(new_surface->Texture().handle, 0, 0, 0, 1085 new_params.rt.index, index);
884 static_cast<GLsizei>(dest_rect.GetWidth()), 1086 } else {
885 static_cast<GLsizei>(dest_rect.GetHeight()), dest_format.format, 1087 CopySurface(face_surface, new_surface, copy_pbo.handle,
886 dest_format.type, nullptr); 1088 face_surface->GetSurfaceParams().rt.index, new_params.rt.index, index);
887 break;
888 case SurfaceParams::SurfaceTarget::Texture3D:
889 case SurfaceParams::SurfaceTarget::Texture2DArray:
890 glTextureSubImage3D(new_surface->Texture().handle, 0, 0, 0, 0,
891 static_cast<GLsizei>(dest_rect.GetWidth()),
892 static_cast<GLsizei>(dest_rect.GetHeight()),
893 static_cast<GLsizei>(new_params.depth), dest_format.format,
894 dest_format.type, nullptr);
895 break;
896 default:
897 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
898 static_cast<u32>(params.target));
899 UNREACHABLE();
900 } 1089 }
1090
1091 old_params.addr += byte_stride;
901 } 1092 }
902 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); 1093 break;
1094 }
1095 default:
1096 LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
1097 static_cast<u32>(new_params.target));
1098 UNREACHABLE();
903 } 1099 }
904 1100
905 return new_surface; 1101 return new_surface;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index d7a4bc37f..49025a3fe 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -9,12 +9,14 @@
9#include <memory> 9#include <memory>
10#include <vector> 10#include <vector>
11 11
12#include "common/alignment.h"
12#include "common/common_types.h" 13#include "common/common_types.h"
13#include "common/hash.h" 14#include "common/hash.h"
14#include "common/math_util.h" 15#include "common/math_util.h"
15#include "video_core/engines/maxwell_3d.h" 16#include "video_core/engines/maxwell_3d.h"
16#include "video_core/rasterizer_cache.h" 17#include "video_core/rasterizer_cache.h"
17#include "video_core/renderer_opengl/gl_resource_manager.h" 18#include "video_core/renderer_opengl/gl_resource_manager.h"
19#include "video_core/renderer_opengl/gl_shader_gen.h"
18#include "video_core/textures/texture.h" 20#include "video_core/textures/texture.h"
19 21
20namespace OpenGL { 22namespace OpenGL {
@@ -126,6 +128,8 @@ struct SurfaceParams {
126 case Tegra::Texture::TextureType::Texture2D: 128 case Tegra::Texture::TextureType::Texture2D:
127 case Tegra::Texture::TextureType::Texture2DNoMipmap: 129 case Tegra::Texture::TextureType::Texture2DNoMipmap:
128 return SurfaceTarget::Texture2D; 130 return SurfaceTarget::Texture2D;
131 case Tegra::Texture::TextureType::TextureCubemap:
132 return SurfaceTarget::TextureCubemap;
129 case Tegra::Texture::TextureType::Texture1DArray: 133 case Tegra::Texture::TextureType::Texture1DArray:
130 return SurfaceTarget::Texture1DArray; 134 return SurfaceTarget::Texture1DArray;
131 case Tegra::Texture::TextureType::Texture2DArray: 135 case Tegra::Texture::TextureType::Texture2DArray:
@@ -137,6 +141,27 @@ struct SurfaceParams {
137 } 141 }
138 } 142 }
139 143
144 static std::string SurfaceTargetName(SurfaceTarget target) {
145 switch (target) {
146 case SurfaceTarget::Texture1D:
147 return "Texture1D";
148 case SurfaceTarget::Texture2D:
149 return "Texture2D";
150 case SurfaceTarget::Texture3D:
151 return "Texture3D";
152 case SurfaceTarget::Texture1DArray:
153 return "Texture1DArray";
154 case SurfaceTarget::Texture2DArray:
155 return "Texture2DArray";
156 case SurfaceTarget::TextureCubemap:
157 return "TextureCubemap";
158 default:
159 LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
160 UNREACHABLE();
161 return fmt::format("TextureUnknown({})", static_cast<u32>(target));
162 }
163 }
164
140 /** 165 /**
141 * Gets the compression factor for the specified PixelFormat. This applies to just the 166 * Gets the compression factor for the specified PixelFormat. This applies to just the
142 * "compressed width" and "compressed height", not the overall compression factor of a 167 * "compressed width" and "compressed height", not the overall compression factor of a
@@ -668,17 +693,23 @@ struct SurfaceParams {
668 /// Returns the rectangle corresponding to this surface 693 /// Returns the rectangle corresponding to this surface
669 MathUtil::Rectangle<u32> GetRect() const; 694 MathUtil::Rectangle<u32> GetRect() const;
670 695
671 /// Returns the size of this surface in bytes, adjusted for compression 696 /// Returns the size of this surface as a 2D texture in bytes, adjusted for compression
672 std::size_t SizeInBytes() const { 697 std::size_t SizeInBytes2D() const {
673 const u32 compression_factor{GetCompressionFactor(pixel_format)}; 698 const u32 compression_factor{GetCompressionFactor(pixel_format)};
674 ASSERT(width % compression_factor == 0); 699 ASSERT(width % compression_factor == 0);
675 ASSERT(height % compression_factor == 0); 700 ASSERT(height % compression_factor == 0);
676 return (width / compression_factor) * (height / compression_factor) * 701 return (width / compression_factor) * (height / compression_factor) *
677 GetFormatBpp(pixel_format) * depth / CHAR_BIT; 702 GetFormatBpp(pixel_format) / CHAR_BIT;
703 }
704
705 /// Returns the total size of this surface in bytes, adjusted for compression
706 std::size_t SizeInBytesTotal() const {
707 return SizeInBytes2D() * depth;
678 } 708 }
679 709
680 /// Creates SurfaceParams from a texture configuration 710 /// Creates SurfaceParams from a texture configuration
681 static SurfaceParams CreateForTexture(const Tegra::Texture::FullTextureInfo& config); 711 static SurfaceParams CreateForTexture(const Tegra::Texture::FullTextureInfo& config,
712 const GLShader::SamplerEntry& entry);
682 713
683 /// Creates SurfaceParams from a framebuffer configuration 714 /// Creates SurfaceParams from a framebuffer configuration
684 static SurfaceParams CreateForFramebuffer(std::size_t index); 715 static SurfaceParams CreateForFramebuffer(std::size_t index);
@@ -690,8 +721,9 @@ struct SurfaceParams {
690 721
691 /// Checks if surfaces are compatible for caching 722 /// Checks if surfaces are compatible for caching
692 bool IsCompatibleSurface(const SurfaceParams& other) const { 723 bool IsCompatibleSurface(const SurfaceParams& other) const {
693 return std::tie(pixel_format, type, width, height) == 724 return std::tie(pixel_format, type, width, height, target, depth) ==
694 std::tie(other.pixel_format, other.type, other.width, other.height); 725 std::tie(other.pixel_format, other.type, other.width, other.height, other.target,
726 other.depth);
695 } 727 }
696 728
697 VAddr addr; 729 VAddr addr;
@@ -704,8 +736,18 @@ struct SurfaceParams {
704 u32 height; 736 u32 height;
705 u32 depth; 737 u32 depth;
706 u32 unaligned_height; 738 u32 unaligned_height;
707 std::size_t size_in_bytes; 739 std::size_t size_in_bytes_total;
740 std::size_t size_in_bytes_2d;
708 SurfaceTarget target; 741 SurfaceTarget target;
742 u32 max_mip_level;
743
744 // Render target specific parameters, not used in caching
745 struct {
746 u32 index;
747 u32 array_mode;
748 u32 layer_stride;
749 u32 base_layer;
750 } rt;
709}; 751};
710 752
711}; // namespace OpenGL 753}; // namespace OpenGL
@@ -715,6 +757,7 @@ struct SurfaceReserveKey : Common::HashableStruct<OpenGL::SurfaceParams> {
715 static SurfaceReserveKey Create(const OpenGL::SurfaceParams& params) { 757 static SurfaceReserveKey Create(const OpenGL::SurfaceParams& params) {
716 SurfaceReserveKey res; 758 SurfaceReserveKey res;
717 res.state = params; 759 res.state = params;
760 res.state.rt = {}; // Ignore rt config in caching
718 return res; 761 return res;
719 } 762 }
720}; 763};
@@ -738,7 +781,7 @@ public:
738 } 781 }
739 782
740 std::size_t GetSizeInBytes() const { 783 std::size_t GetSizeInBytes() const {
741 return params.size_in_bytes; 784 return params.size_in_bytes_total;
742 } 785 }
743 786
744 const OGLTexture& Texture() const { 787 const OGLTexture& Texture() const {
@@ -779,7 +822,8 @@ public:
779 RasterizerCacheOpenGL(); 822 RasterizerCacheOpenGL();
780 823
781 /// Get a surface based on the texture configuration 824 /// Get a surface based on the texture configuration
782 Surface GetTextureSurface(const Tegra::Texture::FullTextureInfo& config); 825 Surface GetTextureSurface(const Tegra::Texture::FullTextureInfo& config,
826 const GLShader::SamplerEntry& entry);
783 827
784 /// Get the depth surface based on the framebuffer configuration 828 /// Get the depth surface based on the framebuffer configuration
785 Surface GetDepthBufferSurface(bool preserve_contents); 829 Surface GetDepthBufferSurface(bool preserve_contents);
@@ -801,7 +845,7 @@ private:
801 Surface GetUncachedSurface(const SurfaceParams& params); 845 Surface GetUncachedSurface(const SurfaceParams& params);
802 846
803 /// Recreates a surface with new parameters 847 /// Recreates a surface with new parameters
804 Surface RecreateSurface(const Surface& surface, const SurfaceParams& new_params); 848 Surface RecreateSurface(const Surface& old_surface, const SurfaceParams& new_params);
805 849
806 /// Reserves a unique surface that can be reused later 850 /// Reserves a unique surface that can be reused later
807 void ReserveSurface(const Surface& surface); 851 void ReserveSurface(const Surface& surface);
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp
index 894fe6eae..7cd8f91e4 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp
@@ -8,6 +8,7 @@
8#include "video_core/engines/maxwell_3d.h" 8#include "video_core/engines/maxwell_3d.h"
9#include "video_core/renderer_opengl/gl_shader_cache.h" 9#include "video_core/renderer_opengl/gl_shader_cache.h"
10#include "video_core/renderer_opengl/gl_shader_manager.h" 10#include "video_core/renderer_opengl/gl_shader_manager.h"
11#include "video_core/utils.h"
11 12
12namespace OpenGL { 13namespace OpenGL {
13 14
@@ -83,6 +84,7 @@ CachedShader::CachedShader(VAddr addr, Maxwell::ShaderProgram program_type)
83 shader.Create(program_result.first.c_str(), gl_type); 84 shader.Create(program_result.first.c_str(), gl_type);
84 program.Create(true, shader.handle); 85 program.Create(true, shader.handle);
85 SetShaderUniformBlockBindings(program.handle); 86 SetShaderUniformBlockBindings(program.handle);
87 VideoCore::LabelGLObject(GL_PROGRAM, program.handle, addr);
86} 88}
87 89
88GLuint CachedShader::GetProgramResourceIndex(const GLShader::ConstBufferEntry& buffer) { 90GLuint CachedShader::GetProgramResourceIndex(const GLShader::ConstBufferEntry& buffer) {
diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
index 702ffbe9c..579a78702 100644
--- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
@@ -2000,6 +2000,14 @@ private:
2000 } 2000 }
2001 break; 2001 break;
2002 } 2002 }
2003 case Tegra::Shader::TextureType::TextureCube: {
2004 ASSERT_MSG(!is_array, "Unimplemented");
2005 std::string x = regs.GetRegisterAsFloat(instr.gpr8);
2006 std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
2007 std::string z = regs.GetRegisterAsFloat(instr.gpr20);
2008 coord = "vec3 coords = vec3(" + x + ", " + y + ", " + z + ");";
2009 break;
2010 }
2003 default: 2011 default:
2004 LOG_CRITICAL(HW_GPU, "Unhandled texture type {}", 2012 LOG_CRITICAL(HW_GPU, "Unhandled texture type {}",
2005 static_cast<u32>(texture_type)); 2013 static_cast<u32>(texture_type));
diff --git a/src/video_core/renderer_opengl/gl_shader_manager.h b/src/video_core/renderer_opengl/gl_shader_manager.h
index b86cd96e8..3de15ba9b 100644
--- a/src/video_core/renderer_opengl/gl_shader_manager.h
+++ b/src/video_core/renderer_opengl/gl_shader_manager.h
@@ -11,9 +11,6 @@
11 11
12namespace OpenGL::GLShader { 12namespace OpenGL::GLShader {
13 13
14/// Number of OpenGL texture samplers that can be used in the fragment shader
15static constexpr std::size_t NumTextureSamplers = 32;
16
17using Tegra::Engines::Maxwell3D; 14using Tegra::Engines::Maxwell3D;
18 15
19/// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned 16/// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
diff --git a/src/video_core/renderer_opengl/gl_state.cpp b/src/video_core/renderer_opengl/gl_state.cpp
index af99132ba..1fe26a2a9 100644
--- a/src/video_core/renderer_opengl/gl_state.cpp
+++ b/src/video_core/renderer_opengl/gl_state.cpp
@@ -4,6 +4,7 @@
4 4
5#include <iterator> 5#include <iterator>
6#include <glad/glad.h> 6#include <glad/glad.h>
7#include "common/assert.h"
7#include "common/logging/log.h" 8#include "common/logging/log.h"
8#include "video_core/renderer_opengl/gl_state.h" 9#include "video_core/renderer_opengl/gl_state.h"
9 10
@@ -78,6 +79,8 @@ OpenGLState::OpenGLState() {
78 viewport.height = 0; 79 viewport.height = 0;
79 80
80 clip_distance = {}; 81 clip_distance = {};
82
83 point.size = 1;
81} 84}
82 85
83void OpenGLState::Apply() const { 86void OpenGLState::Apply() const {
@@ -204,9 +207,6 @@ void OpenGLState::Apply() const {
204 glActiveTexture(TextureUnits::MaxwellTexture(static_cast<int>(i)).Enum()); 207 glActiveTexture(TextureUnits::MaxwellTexture(static_cast<int>(i)).Enum());
205 glBindTexture(texture_unit.target, texture_unit.texture); 208 glBindTexture(texture_unit.target, texture_unit.texture);
206 } 209 }
207 if (texture_unit.sampler != cur_state_texture_unit.sampler) {
208 glBindSampler(static_cast<GLuint>(i), texture_unit.sampler);
209 }
210 // Update the texture swizzle 210 // Update the texture swizzle
211 if (texture_unit.swizzle.r != cur_state_texture_unit.swizzle.r || 211 if (texture_unit.swizzle.r != cur_state_texture_unit.swizzle.r ||
212 texture_unit.swizzle.g != cur_state_texture_unit.swizzle.g || 212 texture_unit.swizzle.g != cur_state_texture_unit.swizzle.g ||
@@ -218,6 +218,27 @@ void OpenGLState::Apply() const {
218 } 218 }
219 } 219 }
220 220
221 // Samplers
222 {
223 bool has_delta{};
224 std::size_t first{}, last{};
225 std::array<GLuint, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> samplers;
226 for (std::size_t i = 0; i < std::size(samplers); ++i) {
227 samplers[i] = texture_units[i].sampler;
228 if (samplers[i] != cur_state.texture_units[i].sampler) {
229 if (!has_delta) {
230 first = i;
231 has_delta = true;
232 }
233 last = i;
234 }
235 }
236 if (has_delta) {
237 glBindSamplers(static_cast<GLuint>(first), static_cast<GLsizei>(last - first + 1),
238 samplers.data());
239 }
240 }
241
221 // Framebuffer 242 // Framebuffer
222 if (draw.read_framebuffer != cur_state.draw.read_framebuffer) { 243 if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
223 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer); 244 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
@@ -282,6 +303,11 @@ void OpenGLState::Apply() const {
282 } 303 }
283 } 304 }
284 305
306 // Point
307 if (point.size != cur_state.point.size) {
308 glPointSize(point.size);
309 }
310
285 cur_state = *this; 311 cur_state = *this;
286} 312}
287 313
diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h
index e3e24b9e7..dc21a2ee3 100644
--- a/src/video_core/renderer_opengl/gl_state.h
+++ b/src/video_core/renderer_opengl/gl_state.h
@@ -6,13 +6,10 @@
6 6
7#include <array> 7#include <array>
8#include <glad/glad.h> 8#include <glad/glad.h>
9
10#include "video_core/engines/maxwell_3d.h" 9#include "video_core/engines/maxwell_3d.h"
11 10
12namespace OpenGL { 11namespace OpenGL {
13 12
14using Regs = Tegra::Engines::Maxwell3D::Regs;
15
16namespace TextureUnits { 13namespace TextureUnits {
17 14
18struct TextureUnit { 15struct TextureUnit {
@@ -118,7 +115,7 @@ public:
118 target = GL_TEXTURE_2D; 115 target = GL_TEXTURE_2D;
119 } 116 }
120 }; 117 };
121 std::array<TextureUnit, 32> texture_units; 118 std::array<TextureUnit, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_units;
122 119
123 struct { 120 struct {
124 GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING 121 GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
@@ -145,6 +142,10 @@ public:
145 GLsizei height; 142 GLsizei height;
146 } viewport; 143 } viewport;
147 144
145 struct {
146 float size; // GL_POINT_SIZE
147 } point;
148
148 std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE 149 std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE
149 150
150 OpenGLState(); 151 OpenGLState();
diff --git a/src/video_core/renderer_opengl/gl_stream_buffer.cpp b/src/video_core/renderer_opengl/gl_stream_buffer.cpp
index 664f3ca20..e409228cc 100644
--- a/src/video_core/renderer_opengl/gl_stream_buffer.cpp
+++ b/src/video_core/renderer_opengl/gl_stream_buffer.cpp
@@ -74,7 +74,7 @@ std::tuple<u8*, GLintptr, bool> OGLStreamBuffer::Map(GLsizeiptr size, GLintptr a
74 } 74 }
75 } 75 }
76 76
77 if (invalidate | !persistent) { 77 if (invalidate || !persistent) {
78 GLbitfield flags = GL_MAP_WRITE_BIT | (persistent ? GL_MAP_PERSISTENT_BIT : 0) | 78 GLbitfield flags = GL_MAP_WRITE_BIT | (persistent ? GL_MAP_PERSISTENT_BIT : 0) |
79 (coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT) | 79 (coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT) |
80 (invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_UNSYNCHRONIZED_BIT); 80 (invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_UNSYNCHRONIZED_BIT);
diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp
index 20ba6d4f6..3d5476e5d 100644
--- a/src/video_core/textures/decoders.cpp
+++ b/src/video_core/textures/decoders.cpp
@@ -13,47 +13,20 @@
13namespace Tegra::Texture { 13namespace Tegra::Texture {
14 14
15/** 15/**
16 * This table represents the internal swizzle of a gob,
17 * in format 16 bytes x 2 sector packing.
16 * Calculates the offset of an (x, y) position within a swizzled texture. 18 * Calculates the offset of an (x, y) position within a swizzled texture.
17 * Taken from the Tegra X1 TRM. 19 * Taken from the Tegra X1 Technical Reference Manual. pages 1187-1188
18 */ 20 */
19static u32 GetSwizzleOffset(u32 x, u32 y, u32 image_width, u32 bytes_per_pixel, u32 block_height) { 21template <std::size_t N, std::size_t M, u32 Align>
20 // Round up to the next gob
21 const u32 image_width_in_gobs{(image_width * bytes_per_pixel + 63) / 64};
22
23 u32 GOB_address = 0 + (y / (8 * block_height)) * 512 * block_height * image_width_in_gobs +
24 (x * bytes_per_pixel / 64) * 512 * block_height +
25 (y % (8 * block_height) / 8) * 512;
26 x *= bytes_per_pixel;
27 u32 address = GOB_address + ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 +
28 (y % 2) * 16 + (x % 16);
29
30 return address;
31}
32
33void CopySwizzledData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
34 u8* swizzled_data, u8* unswizzled_data, bool unswizzle, u32 block_height) {
35 u8* data_ptrs[2];
36 for (unsigned y = 0; y < height; ++y) {
37 for (unsigned x = 0; x < width; ++x) {
38 u32 swizzle_offset = GetSwizzleOffset(x, y, width, bytes_per_pixel, block_height);
39 u32 pixel_index = (x + y * width) * out_bytes_per_pixel;
40
41 data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
42 data_ptrs[!unswizzle] = &unswizzled_data[pixel_index];
43
44 std::memcpy(data_ptrs[0], data_ptrs[1], bytes_per_pixel);
45 }
46 }
47}
48
49template <std::size_t N, std::size_t M>
50struct alignas(64) SwizzleTable { 22struct alignas(64) SwizzleTable {
23 static_assert(M * Align == 64, "Swizzle Table does not align to GOB");
51 constexpr SwizzleTable() { 24 constexpr SwizzleTable() {
52 for (u32 y = 0; y < N; ++y) { 25 for (u32 y = 0; y < N; ++y) {
53 for (u32 x = 0; x < M; ++x) { 26 for (u32 x = 0; x < M; ++x) {
54 const u32 x2 = x * 16; 27 const u32 x2 = x * Align;
55 values[y][x] = static_cast<u16>(((x2 % 64) / 32) * 256 + ((y % 8) / 2) * 64 + 28 values[y][x] = static_cast<u16>(((x2 % 64) / 32) * 256 + ((y % 8) / 2) * 64 +
56 ((x2 % 32) / 16) * 32 + (y % 2) * 16); 29 ((x2 % 32) / 16) * 32 + (y % 2) * 16 + (x2 % 16));
57 } 30 }
58 } 31 }
59 } 32 }
@@ -63,24 +36,60 @@ struct alignas(64) SwizzleTable {
63 std::array<std::array<u16, M>, N> values{}; 36 std::array<std::array<u16, M>, N> values{};
64}; 37};
65 38
66constexpr auto swizzle_table = SwizzleTable<8, 4>(); 39constexpr auto legacy_swizzle_table = SwizzleTable<8, 64, 1>();
40constexpr auto fast_swizzle_table = SwizzleTable<8, 4, 16>();
67 41
68void FastSwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u8* swizzled_data, 42static void LegacySwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
69 u8* unswizzled_data, bool unswizzle, u32 block_height) { 43 u8* swizzled_data, u8* unswizzled_data, bool unswizzle,
44 u32 block_height) {
45 std::array<u8*, 2> data_ptrs;
46 const std::size_t stride = width * bytes_per_pixel;
47 const std::size_t gobs_in_x = 64;
48 const std::size_t gobs_in_y = 8;
49 const std::size_t gobs_size = gobs_in_x * gobs_in_y;
50 const std::size_t image_width_in_gobs{(stride + gobs_in_x - 1) / gobs_in_x};
51 for (std::size_t y = 0; y < height; ++y) {
52 const std::size_t gob_y_address =
53 (y / (gobs_in_y * block_height)) * gobs_size * block_height * image_width_in_gobs +
54 (y % (gobs_in_y * block_height) / gobs_in_y) * gobs_size;
55 const auto& table = legacy_swizzle_table[y % gobs_in_y];
56 for (std::size_t x = 0; x < width; ++x) {
57 const std::size_t gob_address =
58 gob_y_address + (x * bytes_per_pixel / gobs_in_x) * gobs_size * block_height;
59 const std::size_t x2 = x * bytes_per_pixel;
60 const std::size_t swizzle_offset = gob_address + table[x2 % gobs_in_x];
61 const std::size_t pixel_index = (x + y * width) * out_bytes_per_pixel;
62
63 data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
64 data_ptrs[!unswizzle] = unswizzled_data + pixel_index;
65
66 std::memcpy(data_ptrs[0], data_ptrs[1], bytes_per_pixel);
67 }
68 }
69}
70
71static void FastSwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
72 u8* swizzled_data, u8* unswizzled_data, bool unswizzle,
73 u32 block_height) {
70 std::array<u8*, 2> data_ptrs; 74 std::array<u8*, 2> data_ptrs;
71 const std::size_t stride{width * bytes_per_pixel}; 75 const std::size_t stride{width * bytes_per_pixel};
72 const std::size_t image_width_in_gobs{(stride + 63) / 64}; 76 const std::size_t gobs_in_x = 64;
77 const std::size_t gobs_in_y = 8;
78 const std::size_t gobs_size = gobs_in_x * gobs_in_y;
79 const std::size_t image_width_in_gobs{(stride + gobs_in_x - 1) / gobs_in_x};
73 const std::size_t copy_size{16}; 80 const std::size_t copy_size{16};
74 for (std::size_t y = 0; y < height; ++y) { 81 for (std::size_t y = 0; y < height; ++y) {
75 const std::size_t initial_gob = 82 const std::size_t initial_gob =
76 (y / (8 * block_height)) * 512 * block_height * image_width_in_gobs + 83 (y / (gobs_in_y * block_height)) * gobs_size * block_height * image_width_in_gobs +
77 (y % (8 * block_height) / 8) * 512; 84 (y % (gobs_in_y * block_height) / gobs_in_y) * gobs_size;
78 const std::size_t pixel_base{y * width * bytes_per_pixel}; 85 const std::size_t pixel_base{y * width * out_bytes_per_pixel};
79 const auto& table = swizzle_table[y % 8]; 86 const auto& table = fast_swizzle_table[y % gobs_in_y];
80 for (std::size_t xb = 0; xb < stride; xb += copy_size) { 87 for (std::size_t xb = 0; xb < stride; xb += copy_size) {
81 const std::size_t gob_address{initial_gob + (xb / 64) * 512 * block_height}; 88 const std::size_t gob_address{initial_gob +
89 (xb / gobs_in_x) * gobs_size * block_height};
82 const std::size_t swizzle_offset{gob_address + table[(xb / 16) % 4]}; 90 const std::size_t swizzle_offset{gob_address + table[(xb / 16) % 4]};
83 const std::size_t pixel_index{xb + pixel_base}; 91 const std::size_t out_x = xb * out_bytes_per_pixel / bytes_per_pixel;
92 const std::size_t pixel_index{out_x + pixel_base};
84 data_ptrs[unswizzle] = swizzled_data + swizzle_offset; 93 data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
85 data_ptrs[!unswizzle] = unswizzled_data + pixel_index; 94 data_ptrs[!unswizzle] = unswizzled_data + pixel_index;
86 std::memcpy(data_ptrs[0], data_ptrs[1], copy_size); 95 std::memcpy(data_ptrs[0], data_ptrs[1], copy_size);
@@ -88,6 +97,17 @@ void FastSwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u8* swizzled_da
88 } 97 }
89} 98}
90 99
100void CopySwizzledData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
101 u8* swizzled_data, u8* unswizzled_data, bool unswizzle, u32 block_height) {
102 if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % 16 == 0) {
103 FastSwizzleData(width, height, bytes_per_pixel, out_bytes_per_pixel, swizzled_data,
104 unswizzled_data, unswizzle, block_height);
105 } else {
106 LegacySwizzleData(width, height, bytes_per_pixel, out_bytes_per_pixel, swizzled_data,
107 unswizzled_data, unswizzle, block_height);
108 }
109}
110
91u32 BytesPerPixel(TextureFormat format) { 111u32 BytesPerPixel(TextureFormat format) {
92 switch (format) { 112 switch (format) {
93 case TextureFormat::DXT1: 113 case TextureFormat::DXT1:
@@ -134,13 +154,8 @@ u32 BytesPerPixel(TextureFormat format) {
134std::vector<u8> UnswizzleTexture(VAddr address, u32 tile_size, u32 bytes_per_pixel, u32 width, 154std::vector<u8> UnswizzleTexture(VAddr address, u32 tile_size, u32 bytes_per_pixel, u32 width,
135 u32 height, u32 block_height) { 155 u32 height, u32 block_height) {
136 std::vector<u8> unswizzled_data(width * height * bytes_per_pixel); 156 std::vector<u8> unswizzled_data(width * height * bytes_per_pixel);
137 if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % 16 == 0) { 157 CopySwizzledData(width / tile_size, height / tile_size, bytes_per_pixel, bytes_per_pixel,
138 FastSwizzleData(width / tile_size, height / tile_size, bytes_per_pixel, 158 Memory::GetPointer(address), unswizzled_data.data(), true, block_height);
139 Memory::GetPointer(address), unswizzled_data.data(), true, block_height);
140 } else {
141 CopySwizzledData(width / tile_size, height / tile_size, bytes_per_pixel, bytes_per_pixel,
142 Memory::GetPointer(address), unswizzled_data.data(), true, block_height);
143 }
144 return unswizzled_data; 159 return unswizzled_data;
145} 160}
146 161
diff --git a/src/video_core/textures/texture.h b/src/video_core/textures/texture.h
index c2fb824b2..14aea4838 100644
--- a/src/video_core/textures/texture.h
+++ b/src/video_core/textures/texture.h
@@ -165,6 +165,8 @@ struct TICEntry {
165 165
166 // High 16 bits of the pitch value 166 // High 16 bits of the pitch value
167 BitField<0, 16, u32> pitch_high; 167 BitField<0, 16, u32> pitch_high;
168
169 BitField<28, 4, u32> max_mip_level;
168 }; 170 };
169 union { 171 union {
170 BitField<0, 16, u32> width_minus_1; 172 BitField<0, 16, u32> width_minus_1;
diff --git a/src/video_core/utils.h b/src/video_core/utils.h
index e0a14d48f..681919ae3 100644
--- a/src/video_core/utils.h
+++ b/src/video_core/utils.h
@@ -161,4 +161,26 @@ static inline void MortonCopyPixels128(u32 width, u32 height, u32 bytes_per_pixe
161 } 161 }
162} 162}
163 163
164static void LabelGLObject(GLenum identifier, GLuint handle, VAddr addr,
165 std::string extra_info = "") {
166 if (!GLAD_GL_KHR_debug) {
167 return; // We don't need to throw an error as this is just for debugging
168 }
169 const std::string nice_addr = fmt::format("0x{:016x}", addr);
170 std::string object_label;
171
172 switch (identifier) {
173 case GL_TEXTURE:
174 object_label = extra_info + "@" + nice_addr;
175 break;
176 case GL_PROGRAM:
177 object_label = "ShaderProgram@" + nice_addr;
178 break;
179 default:
180 object_label = fmt::format("Object(0x{:x})@{}", identifier, nice_addr);
181 break;
182 }
183 glObjectLabel(identifier, handle, -1, static_cast<const GLchar*>(object_label.c_str()));
184}
185
164} // namespace VideoCore 186} // namespace VideoCore
diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp
index f2a7e23f0..a3b1fd357 100644
--- a/src/yuzu/debugger/wait_tree.cpp
+++ b/src/yuzu/debugger/wait_tree.cpp
@@ -15,6 +15,7 @@
15#include "core/hle/kernel/thread.h" 15#include "core/hle/kernel/thread.h"
16#include "core/hle/kernel/timer.h" 16#include "core/hle/kernel/timer.h"
17#include "core/hle/kernel/wait_object.h" 17#include "core/hle/kernel/wait_object.h"
18#include "core/memory.h"
18 19
19WaitTreeItem::WaitTreeItem() = default; 20WaitTreeItem::WaitTreeItem() = default;
20WaitTreeItem::~WaitTreeItem() = default; 21WaitTreeItem::~WaitTreeItem() = default;
diff --git a/src/yuzu/game_list.cpp b/src/yuzu/game_list.cpp
index 3b3b551bb..67890455a 100644
--- a/src/yuzu/game_list.cpp
+++ b/src/yuzu/game_list.cpp
@@ -26,10 +26,10 @@
26#include "yuzu/main.h" 26#include "yuzu/main.h"
27#include "yuzu/ui_settings.h" 27#include "yuzu/ui_settings.h"
28 28
29GameList::SearchField::KeyReleaseEater::KeyReleaseEater(GameList* gamelist) : gamelist{gamelist} {} 29GameListSearchField::KeyReleaseEater::KeyReleaseEater(GameList* gamelist) : gamelist{gamelist} {}
30 30
31// EventFilter in order to process systemkeys while editing the searchfield 31// EventFilter in order to process systemkeys while editing the searchfield
32bool GameList::SearchField::KeyReleaseEater::eventFilter(QObject* obj, QEvent* event) { 32bool GameListSearchField::KeyReleaseEater::eventFilter(QObject* obj, QEvent* event) {
33 // If it isn't a KeyRelease event then continue with standard event processing 33 // If it isn't a KeyRelease event then continue with standard event processing
34 if (event->type() != QEvent::KeyRelease) 34 if (event->type() != QEvent::KeyRelease)
35 return QObject::eventFilter(obj, event); 35 return QObject::eventFilter(obj, event);
@@ -88,29 +88,21 @@ bool GameList::SearchField::KeyReleaseEater::eventFilter(QObject* obj, QEvent* e
88 return QObject::eventFilter(obj, event); 88 return QObject::eventFilter(obj, event);
89} 89}
90 90
91void GameList::SearchField::setFilterResult(int visible, int total) { 91void GameListSearchField::setFilterResult(int visible, int total) {
92 QString result_of_text = tr("of"); 92 label_filter_result->setText(tr("%1 of %n result(s)", "", total).arg(visible));
93 QString result_text;
94 if (total == 1) {
95 result_text = tr("result");
96 } else {
97 result_text = tr("results");
98 }
99 label_filter_result->setText(
100 QString("%1 %2 %3 %4").arg(visible).arg(result_of_text).arg(total).arg(result_text));
101} 93}
102 94
103void GameList::SearchField::clear() { 95void GameListSearchField::clear() {
104 edit_filter->setText(""); 96 edit_filter->setText("");
105} 97}
106 98
107void GameList::SearchField::setFocus() { 99void GameListSearchField::setFocus() {
108 if (edit_filter->isVisible()) { 100 if (edit_filter->isVisible()) {
109 edit_filter->setFocus(); 101 edit_filter->setFocus();
110 } 102 }
111} 103}
112 104
113GameList::SearchField::SearchField(GameList* parent) : QWidget{parent} { 105GameListSearchField::GameListSearchField(GameList* parent) : QWidget{parent} {
114 KeyReleaseEater* keyReleaseEater = new KeyReleaseEater(parent); 106 KeyReleaseEater* keyReleaseEater = new KeyReleaseEater(parent);
115 layout_filter = new QHBoxLayout; 107 layout_filter = new QHBoxLayout;
116 layout_filter->setMargin(8); 108 layout_filter->setMargin(8);
@@ -210,7 +202,7 @@ GameList::GameList(FileSys::VirtualFilesystem vfs, GMainWindow* parent)
210 this->main_window = parent; 202 this->main_window = parent;
211 layout = new QVBoxLayout; 203 layout = new QVBoxLayout;
212 tree_view = new QTreeView; 204 tree_view = new QTreeView;
213 search_field = new SearchField(this); 205 search_field = new GameListSearchField(this);
214 item_model = new QStandardItemModel(tree_view); 206 item_model = new QStandardItemModel(tree_view);
215 tree_view->setModel(item_model); 207 tree_view->setModel(item_model);
216 208
@@ -326,9 +318,14 @@ void GameList::PopupContextMenu(const QPoint& menu_location) {
326 int row = item_model->itemFromIndex(item)->row(); 318 int row = item_model->itemFromIndex(item)->row();
327 QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME); 319 QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
328 u64 program_id = child_file->data(GameListItemPath::ProgramIdRole).toULongLong(); 320 u64 program_id = child_file->data(GameListItemPath::ProgramIdRole).toULongLong();
321 std::string path = child_file->data(GameListItemPath::FullPathRole).toString().toStdString();
329 322
330 QMenu context_menu; 323 QMenu context_menu;
331 QAction* open_save_location = context_menu.addAction(tr("Open Save Data Location")); 324 QAction* open_save_location = context_menu.addAction(tr("Open Save Data Location"));
325 QAction* open_lfs_location = context_menu.addAction(tr("Open Mod Data Location"));
326 context_menu.addSeparator();
327 QAction* dump_romfs = context_menu.addAction(tr("Dump RomFS"));
328 QAction* copy_tid = context_menu.addAction(tr("Copy Title ID to Clipboard"));
332 QAction* navigate_to_gamedb_entry = context_menu.addAction(tr("Navigate to GameDB entry")); 329 QAction* navigate_to_gamedb_entry = context_menu.addAction(tr("Navigate to GameDB entry"));
333 330
334 open_save_location->setEnabled(program_id != 0); 331 open_save_location->setEnabled(program_id != 0);
@@ -337,6 +334,10 @@ void GameList::PopupContextMenu(const QPoint& menu_location) {
337 334
338 connect(open_save_location, &QAction::triggered, 335 connect(open_save_location, &QAction::triggered,
339 [&]() { emit OpenFolderRequested(program_id, GameListOpenTarget::SaveData); }); 336 [&]() { emit OpenFolderRequested(program_id, GameListOpenTarget::SaveData); });
337 connect(open_lfs_location, &QAction::triggered,
338 [&]() { emit OpenFolderRequested(program_id, GameListOpenTarget::ModData); });
339 connect(dump_romfs, &QAction::triggered, [&]() { emit DumpRomFSRequested(program_id, path); });
340 connect(copy_tid, &QAction::triggered, [&]() { emit CopyTIDRequested(program_id); });
340 connect(navigate_to_gamedb_entry, &QAction::triggered, 341 connect(navigate_to_gamedb_entry, &QAction::triggered,
341 [&]() { emit NavigateToGamedbEntryRequested(program_id, compatibility_list); }); 342 [&]() { emit NavigateToGamedbEntryRequested(program_id, compatibility_list); });
342 343
diff --git a/src/yuzu/game_list.h b/src/yuzu/game_list.h
index 2713e7b54..05e115e19 100644
--- a/src/yuzu/game_list.h
+++ b/src/yuzu/game_list.h
@@ -22,13 +22,17 @@
22#include "yuzu/compatibility_list.h" 22#include "yuzu/compatibility_list.h"
23 23
24class GameListWorker; 24class GameListWorker;
25class GameListSearchField;
25class GMainWindow; 26class GMainWindow;
26 27
27namespace FileSys { 28namespace FileSys {
28class VfsFilesystem; 29class VfsFilesystem;
29} 30}
30 31
31enum class GameListOpenTarget { SaveData }; 32enum class GameListOpenTarget {
33 SaveData,
34 ModData,
35};
32 36
33class GameList : public QWidget { 37class GameList : public QWidget {
34 Q_OBJECT 38 Q_OBJECT
@@ -43,33 +47,6 @@ public:
43 COLUMN_COUNT, // Number of columns 47 COLUMN_COUNT, // Number of columns
44 }; 48 };
45 49
46 class SearchField : public QWidget {
47 public:
48 void setFilterResult(int visible, int total);
49 void clear();
50 void setFocus();
51 explicit SearchField(GameList* parent = nullptr);
52
53 private:
54 class KeyReleaseEater : public QObject {
55 public:
56 explicit KeyReleaseEater(GameList* gamelist);
57
58 private:
59 GameList* gamelist = nullptr;
60 QString edit_filter_text_old;
61
62 protected:
63 bool eventFilter(QObject* obj, QEvent* event) override;
64 };
65 QHBoxLayout* layout_filter = nullptr;
66 QTreeView* tree_view = nullptr;
67 QLabel* label_filter = nullptr;
68 QLineEdit* edit_filter = nullptr;
69 QLabel* label_filter_result = nullptr;
70 QToolButton* button_filter_close = nullptr;
71 };
72
73 explicit GameList(std::shared_ptr<FileSys::VfsFilesystem> vfs, GMainWindow* parent = nullptr); 50 explicit GameList(std::shared_ptr<FileSys::VfsFilesystem> vfs, GMainWindow* parent = nullptr);
74 ~GameList() override; 51 ~GameList() override;
75 52
@@ -89,6 +66,8 @@ signals:
89 void GameChosen(QString game_path); 66 void GameChosen(QString game_path);
90 void ShouldCancelWorker(); 67 void ShouldCancelWorker();
91 void OpenFolderRequested(u64 program_id, GameListOpenTarget target); 68 void OpenFolderRequested(u64 program_id, GameListOpenTarget target);
69 void DumpRomFSRequested(u64 program_id, const std::string& game_path);
70 void CopyTIDRequested(u64 program_id);
92 void NavigateToGamedbEntryRequested(u64 program_id, 71 void NavigateToGamedbEntryRequested(u64 program_id,
93 const CompatibilityList& compatibility_list); 72 const CompatibilityList& compatibility_list);
94 73
@@ -105,7 +84,7 @@ private:
105 void RefreshGameDirectory(); 84 void RefreshGameDirectory();
106 85
107 std::shared_ptr<FileSys::VfsFilesystem> vfs; 86 std::shared_ptr<FileSys::VfsFilesystem> vfs;
108 SearchField* search_field; 87 GameListSearchField* search_field;
109 GMainWindow* main_window = nullptr; 88 GMainWindow* main_window = nullptr;
110 QVBoxLayout* layout = nullptr; 89 QVBoxLayout* layout = nullptr;
111 QTreeView* tree_view = nullptr; 90 QTreeView* tree_view = nullptr;
@@ -113,6 +92,8 @@ private:
113 GameListWorker* current_worker = nullptr; 92 GameListWorker* current_worker = nullptr;
114 QFileSystemWatcher* watcher = nullptr; 93 QFileSystemWatcher* watcher = nullptr;
115 CompatibilityList compatibility_list; 94 CompatibilityList compatibility_list;
95
96 friend class GameListSearchField;
116}; 97};
117 98
118Q_DECLARE_METATYPE(GameListOpenTarget); 99Q_DECLARE_METATYPE(GameListOpenTarget);
diff --git a/src/yuzu/game_list_p.h b/src/yuzu/game_list_p.h
index b6272d536..3db0e90da 100644
--- a/src/yuzu/game_list_p.h
+++ b/src/yuzu/game_list_p.h
@@ -16,6 +16,7 @@
16#include <QObject> 16#include <QObject>
17#include <QStandardItem> 17#include <QStandardItem>
18#include <QString> 18#include <QString>
19#include <QWidget>
19 20
20#include "common/common_types.h" 21#include "common/common_types.h"
21#include "common/logging/log.h" 22#include "common/logging/log.h"
@@ -68,7 +69,7 @@ public:
68 if (!picture.loadFromData(picture_data.data(), static_cast<u32>(picture_data.size()))) { 69 if (!picture.loadFromData(picture_data.data(), static_cast<u32>(picture_data.size()))) {
69 picture = GetDefaultIcon(size); 70 picture = GetDefaultIcon(size);
70 } 71 }
71 picture = picture.scaled(size, size); 72 picture = picture.scaled(size, size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
72 73
73 setData(picture, Qt::DecorationRole); 74 setData(picture, Qt::DecorationRole);
74 } 75 }
@@ -176,3 +177,42 @@ public:
176 return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong(); 177 return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong();
177 } 178 }
178}; 179};
180
181class GameList;
182class QHBoxLayout;
183class QTreeView;
184class QLabel;
185class QLineEdit;
186class QToolButton;
187
188class GameListSearchField : public QWidget {
189 Q_OBJECT
190
191public:
192 explicit GameListSearchField(GameList* parent = nullptr);
193
194 void setFilterResult(int visible, int total);
195
196 void clear();
197 void setFocus();
198
199private:
200 class KeyReleaseEater : public QObject {
201 public:
202 explicit KeyReleaseEater(GameList* gamelist);
203
204 private:
205 GameList* gamelist = nullptr;
206 QString edit_filter_text_old;
207
208 protected:
209 // EventFilter in order to process systemkeys while editing the searchfield
210 bool eventFilter(QObject* obj, QEvent* event) override;
211 };
212 QHBoxLayout* layout_filter = nullptr;
213 QTreeView* tree_view = nullptr;
214 QLabel* label_filter = nullptr;
215 QLineEdit* edit_filter = nullptr;
216 QLabel* label_filter_result = nullptr;
217 QToolButton* button_filter_close = nullptr;
218};
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index 45bb1d1d1..27015d02c 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -7,6 +7,22 @@
7#include <memory> 7#include <memory>
8#include <thread> 8#include <thread>
9 9
10// VFS includes must be before glad as they will conflict with Windows file api, which uses defines.
11#include "core/file_sys/vfs.h"
12#include "core/file_sys/vfs_real.h"
13
14// These are wrappers to avoid the calls to CreateDirectory and CreateFile becuase of the Windows
15// defines.
16static FileSys::VirtualDir VfsFilesystemCreateDirectoryWrapper(
17 const FileSys::VirtualFilesystem& vfs, const std::string& path, FileSys::Mode mode) {
18 return vfs->CreateDirectory(path, mode);
19}
20
21static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::VirtualDir& dir,
22 const std::string& path) {
23 return dir->CreateFile(path);
24}
25
10#include <fmt/ostream.h> 26#include <fmt/ostream.h>
11#include <glad/glad.h> 27#include <glad/glad.h>
12 28
@@ -30,16 +46,18 @@
30#include "common/telemetry.h" 46#include "common/telemetry.h"
31#include "core/core.h" 47#include "core/core.h"
32#include "core/crypto/key_manager.h" 48#include "core/crypto/key_manager.h"
49#include "core/file_sys/bis_factory.h"
33#include "core/file_sys/card_image.h" 50#include "core/file_sys/card_image.h"
34#include "core/file_sys/content_archive.h" 51#include "core/file_sys/content_archive.h"
35#include "core/file_sys/control_metadata.h" 52#include "core/file_sys/control_metadata.h"
36#include "core/file_sys/patch_manager.h" 53#include "core/file_sys/patch_manager.h"
37#include "core/file_sys/registered_cache.h" 54#include "core/file_sys/registered_cache.h"
55#include "core/file_sys/romfs.h"
38#include "core/file_sys/savedata_factory.h" 56#include "core/file_sys/savedata_factory.h"
39#include "core/file_sys/submission_package.h" 57#include "core/file_sys/submission_package.h"
40#include "core/file_sys/vfs_real.h"
41#include "core/hle/kernel/process.h" 58#include "core/hle/kernel/process.h"
42#include "core/hle/service/filesystem/filesystem.h" 59#include "core/hle/service/filesystem/filesystem.h"
60#include "core/hle/service/filesystem/fsp_ldr.h"
43#include "core/loader/loader.h" 61#include "core/loader/loader.h"
44#include "core/perf_stats.h" 62#include "core/perf_stats.h"
45#include "core/settings.h" 63#include "core/settings.h"
@@ -362,6 +380,8 @@ void GMainWindow::RestoreUIState() {
362void GMainWindow::ConnectWidgetEvents() { 380void GMainWindow::ConnectWidgetEvents() {
363 connect(game_list, &GameList::GameChosen, this, &GMainWindow::OnGameListLoadFile); 381 connect(game_list, &GameList::GameChosen, this, &GMainWindow::OnGameListLoadFile);
364 connect(game_list, &GameList::OpenFolderRequested, this, &GMainWindow::OnGameListOpenFolder); 382 connect(game_list, &GameList::OpenFolderRequested, this, &GMainWindow::OnGameListOpenFolder);
383 connect(game_list, &GameList::DumpRomFSRequested, this, &GMainWindow::OnGameListDumpRomFS);
384 connect(game_list, &GameList::CopyTIDRequested, this, &GMainWindow::OnGameListCopyTID);
365 connect(game_list, &GameList::NavigateToGamedbEntryRequested, this, 385 connect(game_list, &GameList::NavigateToGamedbEntryRequested, this,
366 &GMainWindow::OnGameListNavigateToGamedbEntry); 386 &GMainWindow::OnGameListNavigateToGamedbEntry);
367 387
@@ -602,9 +622,9 @@ void GMainWindow::BootGame(const QString& filename) {
602 std::string title_name; 622 std::string title_name;
603 const auto res = Core::System::GetInstance().GetGameName(title_name); 623 const auto res = Core::System::GetInstance().GetGameName(title_name);
604 if (res != Loader::ResultStatus::Success) { 624 if (res != Loader::ResultStatus::Success) {
605 const u64 program_id = Core::System::GetInstance().CurrentProcess()->program_id; 625 const u64 title_id = Core::System::GetInstance().CurrentProcess()->GetTitleID();
606 626
607 const auto [nacp, icon_file] = FileSys::PatchManager(program_id).GetControlMetadata(); 627 const auto [nacp, icon_file] = FileSys::PatchManager(title_id).GetControlMetadata();
608 if (nacp != nullptr) 628 if (nacp != nullptr)
609 title_name = nacp->GetApplicationName(); 629 title_name = nacp->GetApplicationName();
610 630
@@ -713,6 +733,12 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target
713 program_id, user_id, 0); 733 program_id, user_id, 0);
714 break; 734 break;
715 } 735 }
736 case GameListOpenTarget::ModData: {
737 open_target = "Mod Data";
738 const auto load_dir = FileUtil::GetUserPath(FileUtil::UserPath::LoadDir);
739 path = fmt::format("{}{:016X}", load_dir, program_id);
740 break;
741 }
716 default: 742 default:
717 UNIMPLEMENTED(); 743 UNIMPLEMENTED();
718 } 744 }
@@ -730,6 +756,120 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target
730 QDesktopServices::openUrl(QUrl::fromLocalFile(qpath)); 756 QDesktopServices::openUrl(QUrl::fromLocalFile(qpath));
731} 757}
732 758
759static std::size_t CalculateRomFSEntrySize(const FileSys::VirtualDir& dir, bool full) {
760 std::size_t out = 0;
761
762 for (const auto& subdir : dir->GetSubdirectories()) {
763 out += 1 + CalculateRomFSEntrySize(subdir, full);
764 }
765
766 return out + (full ? dir->GetFiles().size() : 0);
767}
768
769static bool RomFSRawCopy(QProgressDialog& dialog, const FileSys::VirtualDir& src,
770 const FileSys::VirtualDir& dest, std::size_t block_size, bool full) {
771 if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
772 return false;
773 if (dialog.wasCanceled())
774 return false;
775
776 if (full) {
777 for (const auto& file : src->GetFiles()) {
778 const auto out = VfsDirectoryCreateFileWrapper(dest, file->GetName());
779 if (!FileSys::VfsRawCopy(file, out, block_size))
780 return false;
781 dialog.setValue(dialog.value() + 1);
782 if (dialog.wasCanceled())
783 return false;
784 }
785 }
786
787 for (const auto& dir : src->GetSubdirectories()) {
788 const auto out = dest->CreateSubdirectory(dir->GetName());
789 if (!RomFSRawCopy(dialog, dir, out, block_size, full))
790 return false;
791 dialog.setValue(dialog.value() + 1);
792 if (dialog.wasCanceled())
793 return false;
794 }
795
796 return true;
797}
798
799void GMainWindow::OnGameListDumpRomFS(u64 program_id, const std::string& game_path) {
800 const auto path = fmt::format("{}{:016X}/romfs",
801 FileUtil::GetUserPath(FileUtil::UserPath::DumpDir), program_id);
802
803 const auto failed = [this, &path] {
804 QMessageBox::warning(this, tr("RomFS Extraction Failed!"),
805 tr("There was an error copying the RomFS files or the user "
806 "cancelled the operation."));
807 vfs->DeleteDirectory(path);
808 };
809
810 const auto loader = Loader::GetLoader(vfs->OpenFile(game_path, FileSys::Mode::Read));
811 if (loader == nullptr) {
812 failed();
813 return;
814 }
815
816 FileSys::VirtualFile file;
817 if (loader->ReadRomFS(file) != Loader::ResultStatus::Success) {
818 failed();
819 return;
820 }
821
822 const auto romfs =
823 loader->IsRomFSUpdatable()
824 ? FileSys::PatchManager(program_id).PatchRomFS(file, loader->ReadRomFSIVFCOffset())
825 : file;
826
827 const auto extracted = FileSys::ExtractRomFS(romfs, FileSys::RomFSExtractionType::Full);
828 if (extracted == nullptr) {
829 failed();
830 return;
831 }
832
833 const auto out = VfsFilesystemCreateDirectoryWrapper(vfs, path, FileSys::Mode::ReadWrite);
834
835 if (out == nullptr) {
836 failed();
837 return;
838 }
839
840 bool ok;
841 const auto res = QInputDialog::getItem(
842 this, tr("Select RomFS Dump Mode"),
843 tr("Please select the how you would like the RomFS dumped.<br>Full will copy all of the "
844 "files into the new directory while <br>skeleton will only create the directory "
845 "structure."),
846 {"Full", "Skeleton"}, 0, false, &ok);
847 if (!ok)
848 failed();
849
850 const auto full = res == "Full";
851 const auto entry_size = CalculateRomFSEntrySize(extracted, full);
852
853 QProgressDialog progress(tr("Extracting RomFS..."), tr("Cancel"), 0, entry_size, this);
854 progress.setWindowModality(Qt::WindowModal);
855 progress.setMinimumDuration(100);
856
857 if (RomFSRawCopy(progress, extracted, out, 0x400000, full)) {
858 progress.close();
859 QMessageBox::information(this, tr("RomFS Extraction Succeeded!"),
860 tr("The operation completed successfully."));
861 QDesktopServices::openUrl(QUrl::fromLocalFile(QString::fromStdString(path)));
862 } else {
863 progress.close();
864 failed();
865 }
866}
867
868void GMainWindow::OnGameListCopyTID(u64 program_id) {
869 QClipboard* clipboard = QGuiApplication::clipboard();
870 clipboard->setText(QString::fromStdString(fmt::format("{:016X}", program_id)));
871}
872
733void GMainWindow::OnGameListNavigateToGamedbEntry(u64 program_id, 873void GMainWindow::OnGameListNavigateToGamedbEntry(u64 program_id,
734 const CompatibilityList& compatibility_list) { 874 const CompatibilityList& compatibility_list) {
735 const auto it = FindMatchingCompatibilityEntry(compatibility_list, program_id); 875 const auto it = FindMatchingCompatibilityEntry(compatibility_list, program_id);
@@ -790,7 +930,8 @@ void GMainWindow::OnMenuInstallToNAND() {
790 return; 930 return;
791 } 931 }
792 932
793 const auto qt_raw_copy = [this](FileSys::VirtualFile src, FileSys::VirtualFile dest) { 933 const auto qt_raw_copy = [this](const FileSys::VirtualFile& src,
934 const FileSys::VirtualFile& dest, std::size_t block_size) {
794 if (src == nullptr || dest == nullptr) 935 if (src == nullptr || dest == nullptr)
795 return false; 936 return false;
796 if (!dest->Resize(src->GetSize())) 937 if (!dest->Resize(src->GetSize()))
@@ -914,11 +1055,21 @@ void GMainWindow::OnMenuInstallToNAND() {
914 return; 1055 return;
915 } 1056 }
916 1057
917 if (index >= 5) 1058 // If index is equal to or past Game, add the jump in TitleType.
918 index += 0x7B; 1059 if (index >= 5) {
1060 index += static_cast<size_t>(FileSys::TitleType::Application) -
1061 static_cast<size_t>(FileSys::TitleType::FirmwarePackageB);
1062 }
1063
1064 FileSys::InstallResult res;
1065 if (index >= static_cast<size_t>(FileSys::TitleType::Application)) {
1066 res = Service::FileSystem::GetUserNANDContents()->InstallEntry(
1067 nca, static_cast<FileSys::TitleType>(index), false, qt_raw_copy);
1068 } else {
1069 res = Service::FileSystem::GetSystemNANDContents()->InstallEntry(
1070 nca, static_cast<FileSys::TitleType>(index), false, qt_raw_copy);
1071 }
919 1072
920 const auto res = Service::FileSystem::GetUserNANDContents()->InstallEntry(
921 nca, static_cast<FileSys::TitleType>(index), false, qt_raw_copy);
922 if (res == FileSys::InstallResult::Success) { 1073 if (res == FileSys::InstallResult::Success) {
923 success(); 1074 success();
924 } else if (res == FileSys::InstallResult::ErrorAlreadyExists) { 1075 } else if (res == FileSys::InstallResult::ErrorAlreadyExists) {
diff --git a/src/yuzu/main.h b/src/yuzu/main.h
index 552e3e61c..8ee9242b1 100644
--- a/src/yuzu/main.h
+++ b/src/yuzu/main.h
@@ -138,6 +138,8 @@ private slots:
138 /// Called whenever a user selects a game in the game list widget. 138 /// Called whenever a user selects a game in the game list widget.
139 void OnGameListLoadFile(QString game_path); 139 void OnGameListLoadFile(QString game_path);
140 void OnGameListOpenFolder(u64 program_id, GameListOpenTarget target); 140 void OnGameListOpenFolder(u64 program_id, GameListOpenTarget target);
141 void OnGameListDumpRomFS(u64 program_id, const std::string& game_path);
142 void OnGameListCopyTID(u64 program_id);
141 void OnGameListNavigateToGamedbEntry(u64 program_id, 143 void OnGameListNavigateToGamedbEntry(u64 program_id,
142 const CompatibilityList& compatibility_list); 144 const CompatibilityList& compatibility_list);
143 void OnMenuLoadFile(); 145 void OnMenuLoadFile();
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index 7ec1f5110..a478b0a56 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -120,7 +120,7 @@ void Config::ReadValues() {
120 sdl2_config->Get("Data Storage", "nand_directory", 120 sdl2_config->Get("Data Storage", "nand_directory",
121 FileUtil::GetUserPath(FileUtil::UserPath::NANDDir))); 121 FileUtil::GetUserPath(FileUtil::UserPath::NANDDir)));
122 FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir, 122 FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir,
123 sdl2_config->Get("Data Storage", "nand_directory", 123 sdl2_config->Get("Data Storage", "sdmc_directory",
124 FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir))); 124 FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir)));
125 125
126 // System 126 // System
diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp
index b1c364fbb..b2559b717 100644
--- a/src/yuzu_cmd/yuzu.cpp
+++ b/src/yuzu_cmd/yuzu.cpp
@@ -20,8 +20,10 @@
20#include "common/string_util.h" 20#include "common/string_util.h"
21#include "common/telemetry.h" 21#include "common/telemetry.h"
22#include "core/core.h" 22#include "core/core.h"
23#include "core/crypto/key_manager.h"
23#include "core/file_sys/vfs_real.h" 24#include "core/file_sys/vfs_real.h"
24#include "core/gdbstub/gdbstub.h" 25#include "core/gdbstub/gdbstub.h"
26#include "core/hle/service/filesystem/filesystem.h"
25#include "core/loader/loader.h" 27#include "core/loader/loader.h"
26#include "core/settings.h" 28#include "core/settings.h"
27#include "core/telemetry_session.h" 29#include "core/telemetry_session.h"
@@ -29,7 +31,6 @@
29#include "yuzu_cmd/emu_window/emu_window_sdl2.h" 31#include "yuzu_cmd/emu_window/emu_window_sdl2.h"
30 32
31#include <getopt.h> 33#include <getopt.h>
32#include "core/crypto/key_manager.h"
33#ifndef _MSC_VER 34#ifndef _MSC_VER
34#include <unistd.h> 35#include <unistd.h>
35#endif 36#endif
@@ -169,6 +170,7 @@ int main(int argc, char** argv) {
169 170
170 Core::System& system{Core::System::GetInstance()}; 171 Core::System& system{Core::System::GetInstance()};
171 system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>()); 172 system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
173 Service::FileSystem::CreateFactories(system.GetFilesystem());
172 174
173 SCOPE_EXIT({ system.Shutdown(); }); 175 SCOPE_EXIT({ system.Shutdown(); });
174 176