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 {
19namespace Syscalls {
20
21namespace {
22static uint8_t const gMmapCode[] = {
23 0xb8, 0x00, 0x00, 0x00, 0x00, // 00: movl $sysno, %eax
24 0x31, 0xdb, // 05: xorl %ebx, %ebx
25 0xb9, 0x00, 0x00, 0x00, 0x00, // 07: movl $XXXXXXXX, %ecx
26 0xba, 0x00, 0x00, 0x00, 0x00, // 0c: movl $XXXXXXXX, %edx
27 0xbe, 0x00, 0x00, 0x00, 0x00, // 11: movl $XXXXXXXX, %esi
28 0xbf, 0xff, 0xff, 0xff, 0xff, // 16: movl $-1, %edi
29 0x31, 0xed, // 1b: xorl %ebp, %ebp
30 0xcd, 0x80, // 1d: int $0x80
31 0xcc // 1f: int3
32};
33
34static uint8_t const gMunmapCode[] = {
35 0xb8, 0x00, 0x00, 0x00, 0x00, // 00: movl $sysno, %eax
36 0xbb, 0x00, 0x00, 0x00, 0x00, // 05: movl $XXXXXXXX, %ebx
37 0xb9, 0x00, 0x00, 0x00, 0x00, // 0a: movl $XXXXXXXX, %ecx
38 0xcd, 0x80, // 0f: int $0x80
39 0xcc // 10: int3
40};
41} // namespace
42
43static inline void PrepareMmapCode(size_t size, int protection,
44 ByteVector &codestr) {
45 codestr.assign(&gMmapCode[0], &gMmapCode[sizeof(gMmapCode)]);
46
47 uint8_t *code = &codestr[0];
48 *reinterpret_cast<uint32_t *>(code + 0x01) = 192; // __NR_mmap2
49 *reinterpret_cast<uint32_t *>(code + 0x08) = size;
50 *reinterpret_cast<uint32_t *>(code + 0x0d) = protection;
51 *reinterpret_cast<uint32_t *>(code + 0x12) = MAP_ANON | MAP_PRIVATE;
52}
53
54static inline void PrepareMunmapCode(uint32_t address, size_t size,
55 ByteVector &codestr) {
56 codestr.assign(&gMunmapCode[0], &gMunmapCode[sizeof(gMunmapCode)]);
57
58 uint8_t *code = &codestr[0];
59 *reinterpret_cast<uint32_t *>(code + 0x01) = 91; // __NR_munmap
60 *reinterpret_cast<uint32_t *>(code + 0x06) = address;
61 *reinterpret_cast<uint32_t *>(code + 0x0b) = size;
62}
63} // namespace Syscalls
64} // namespace X86
65} // namespace Linux
66} // namespace Host
67} // namespace ds2