DebugServer2
Loading...
Searching...
No Matches
Syscalls.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include <sys/mman.h>
14
15namespace ds2 {
16namespace Host {
17namespace Linux {
18namespace X86_64 {
19namespace Syscalls {
20
21namespace {
22static uint8_t const gMmapCode[] = {
23 0x48, 0xc7, 0xc0, 0x00, 0x00, 0x00, 0x00, // 00: movq $sysno, %rax
24 0x48, 0x31, 0xff, // 07: xorq %rdi, %rdi
25 0x48, 0xc7, 0xc6, 0x00, 0x00, 0x00, 0x00, // 0a: movq $XXXXXXXX, %rsi
26 0x48, 0xc7, 0xc2, 0x00, 0x00, 0x00, 0x00, // 11: movq $XXXXXXXX, %rdx
27 0x49, 0xc7, 0xc2, 0x00, 0x00, 0x00, 0x00, // 18: movq $XXXXXXXX, %r10
28 0x49, 0xc7, 0xc0, 0xff, 0xff, 0xff, 0xff, // 1f: movq $-1, %r8
29 0x4d, 0x31, 0xc9, // 26: xorq %r9, %r9
30 0x0f, 0x05, // 29: syscall
31 0xcc // 2b: int3
32};
33
34static uint8_t const gMunmapCode[] = {
35 0x48, 0xc7, 0xc0, 0x00, 0x00, 0x00, 0x00, // 00: movq $sysno, %rax
36 0x48, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, // 07: movq $XXXXXXXXXXXXXXXX, %rdi
38 0x48, 0xc7, 0xc6, 0x00, 0x00, 0x00, 0x00, // 11: movq $XXXXXXXX, %rsi
39 0x0f, 0x05, // 18: syscall
40 0xcc // 1a: int3
41};
42} // namespace
43
44static inline void PrepareMmapCode(size_t size, int protection,
45 ByteVector &codestr) {
46 codestr.assign(&gMmapCode[0], &gMmapCode[sizeof(gMmapCode)]);
47
48 uint8_t *code = &codestr[0];
49 *reinterpret_cast<uint32_t *>(code + 0x03) = 9; // __NR_mmap
50 *reinterpret_cast<uint32_t *>(code + 0x0d) = size;
51 *reinterpret_cast<uint32_t *>(code + 0x14) = protection;
52 *reinterpret_cast<uint32_t *>(code + 0x1b) = MAP_ANON | MAP_PRIVATE;
53}
54
55static inline void PrepareMunmapCode(uint64_t address, size_t size,
56 ByteVector &codestr) {
57 codestr.assign(&gMunmapCode[0], &gMunmapCode[sizeof(gMunmapCode)]);
58
59 uint8_t *code = &codestr[0];
60 *reinterpret_cast<uint32_t *>(code + 0x03) = 11; // __NR_munmap
61 *reinterpret_cast<uint64_t *>(code + 0x09) = address;
62 *reinterpret_cast<uint32_t *>(code + 0x14) = size;
63}
64} // namespace Syscalls
65} // namespace X86_64
66} // namespace Linux
67} // namespace Host
68} // namespace ds2