chromium/sandbox/linux/bpf_dsl/seccomp_macros.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SANDBOX_LINUX_BPF_DSL_SECCOMP_MACROS_H_
#define SANDBOX_LINUX_BPF_DSL_SECCOMP_MACROS_H_

#include <sys/types.h>  // For __BIONIC__.
// Old Bionic versions do not have sys/user.h.  The if can be removed once we no
// longer need to support these old Bionic versions.
// All x86_64 builds use a new enough bionic to have sys/user.h.
#if !defined(__BIONIC__) || defined(__x86_64__)
#include <sys/user.h>
#if defined(__mips__)
// sys/user.h in eglibc misses size_t definition
#include <stddef.h>
#endif
#endif

#include "build/build_config.h"
#include "sandbox/linux/system_headers/linux_seccomp.h"  // For AUDIT_ARCH_*

// Impose some reasonable maximum BPF program size. Realistically, the
// kernel probably has much lower limits. But by limiting to less than
// 30 bits, we can ease requirements on some of our data types.
#define SECCOMP_MAX_PROGRAM_SIZE

#if defined(__i386__)
#define SECCOMP_ARCH

#define SECCOMP_REG
#define SECCOMP_RESULT
#define SECCOMP_SYSCALL
#define SECCOMP_IP
#define SECCOMP_PARM1
#define SECCOMP_PARM2
#define SECCOMP_PARM3
#define SECCOMP_PARM4
#define SECCOMP_PARM5
#define SECCOMP_PARM6
#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX
#define SECCOMP_ARG_LSB_IDX

#if defined(__BIONIC__)
// Old Bionic versions don't have sys/user.h, so we just define regs_struct
// directly.  This can be removed once we no longer need to support these old
// Bionic versions.
struct regs_struct {
  long int ebx;
  long int ecx;
  long int edx;
  long int esi;
  long int edi;
  long int ebp;
  long int eax;
  long int xds;
  long int xes;
  long int xfs;
  long int xgs;
  long int orig_eax;
  long int eip;
  long int xcs;
  long int eflags;
  long int esp;
  long int xss;
};
#else
typedef user_regs_struct regs_struct;
#endif

#define SECCOMP_PT_RESULT
#define SECCOMP_PT_SYSCALL
#define SECCOMP_PT_IP
#define SECCOMP_PT_PARM1
#define SECCOMP_PT_PARM2
#define SECCOMP_PT_PARM3
#define SECCOMP_PT_PARM4
#define SECCOMP_PT_PARM5
#define SECCOMP_PT_PARM6

#elif defined(__x86_64__)
#define SECCOMP_ARCH

#define SECCOMP_REG(_ctx, _reg)
#define SECCOMP_RESULT(_ctx)
#define SECCOMP_SYSCALL(_ctx)
#define SECCOMP_IP(_ctx)
#define SECCOMP_PARM1(_ctx)
#define SECCOMP_PARM2(_ctx)
#define SECCOMP_PARM3(_ctx)
#define SECCOMP_PARM4(_ctx)
#define SECCOMP_PARM5(_ctx)
#define SECCOMP_PARM6(_ctx)
#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX(nr)
#define SECCOMP_ARG_LSB_IDX(nr)

regs_struct;
#define SECCOMP_PT_RESULT(_regs)
#define SECCOMP_PT_SYSCALL(_regs)
#define SECCOMP_PT_IP(_regs)
#define SECCOMP_PT_PARM1(_regs)
#define SECCOMP_PT_PARM2(_regs)
#define SECCOMP_PT_PARM3(_regs)
#define SECCOMP_PT_PARM4(_regs)
#define SECCOMP_PT_PARM5(_regs)
#define SECCOMP_PT_PARM6(_regs)

#elif defined(__arm__) && (defined(__thumb__) || defined(__ARM_EABI__))
#define SECCOMP_ARCH

// ARM sigcontext_t is different from i386/x86_64.
// See </arch/arm/include/asm/sigcontext.h> in the Linux kernel.
#define SECCOMP_REG
// ARM EABI syscall convention.
#define SECCOMP_RESULT
#define SECCOMP_SYSCALL
#define SECCOMP_IP
#define SECCOMP_PARM1
#define SECCOMP_PARM2
#define SECCOMP_PARM3
#define SECCOMP_PARM4
#define SECCOMP_PARM5
#define SECCOMP_PARM6
#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX
#define SECCOMP_ARG_LSB_IDX

#if defined(__BIONIC__)
// Old Bionic versions don't have sys/user.h, so we just define regs_struct
// directly.  This can be removed once we no longer need to support these old
// Bionic versions.
struct regs_struct {
  unsigned long uregs[18];
};
#else
typedef user_regs regs_struct;
#endif

#define REG_cpsr
#define REG_pc
#define REG_lr
#define REG_sp
#define REG_ip
#define REG_fp
#define REG_r10
#define REG_r9
#define REG_r8
#define REG_r7
#define REG_r6
#define REG_r5
#define REG_r4
#define REG_r3
#define REG_r2
#define REG_r1
#define REG_r0
#define REG_ORIG_r0

#define SECCOMP_PT_RESULT
#define SECCOMP_PT_SYSCALL
#define SECCOMP_PT_IP
#define SECCOMP_PT_PARM1
#define SECCOMP_PT_PARM2
#define SECCOMP_PT_PARM3
#define SECCOMP_PT_PARM4
#define SECCOMP_PT_PARM5
#define SECCOMP_PT_PARM6

#elif defined(ARCH_CPU_MIPS_FAMILY) && defined(ARCH_CPU_32_BITS)
#define SECCOMP_ARCH
#define SYSCALL_EIGHT_ARGS
// MIPS sigcontext_t is different from i386/x86_64 and ARM.
// See </arch/mips/include/uapi/asm/sigcontext.h> in the Linux kernel.
#define SECCOMP_REG
// Based on MIPS o32 ABI syscall convention.
// On MIPS, when indirect syscall is being made (syscall(__NR_foo)),
// real identificator (__NR_foo) is not in v0, but in a0
#define SECCOMP_RESULT
#define SECCOMP_SYSCALL
#define SECCOMP_IP
#define SECCOMP_PARM1
#define SECCOMP_PARM2
#define SECCOMP_PARM3
#define SECCOMP_PARM4
// Only the first 4 arguments of syscall are in registers.
// The rest are on the stack.
#define SECCOMP_STACKPARM
#define SECCOMP_PARM5
#define SECCOMP_PARM6
#define SECCOMP_PARM7
#define SECCOMP_PARM8
#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX
#define SECCOMP_ARG_LSB_IDX

// On MIPS we don't have structures like user_regs or user_regs_struct in
// sys/user.h that we could use, so we just define regs_struct directly.
struct regs_struct {
  unsigned long long regs[32];
};

#define REG_a3
#define REG_a2
#define REG_a1
#define REG_a0
#define REG_v1
#define REG_v0

#define SECCOMP_PT_RESULT
#define SECCOMP_PT_SYSCALL
#define SECCOMP_PT_PARM1
#define SECCOMP_PT_PARM2
#define SECCOMP_PT_PARM3
#define SECCOMP_PT_PARM4

#elif defined(ARCH_CPU_MIPS_FAMILY) && defined(ARCH_CPU_64_BITS)
#define SECCOMP_ARCH
#define SYSCALL_EIGHT_ARGS
// MIPS sigcontext_t is different from i386/x86_64 and ARM.
// See </arch/mips/include/uapi/asm/sigcontext.h> in the Linux kernel.
#define SECCOMP_REG
// Based on MIPS n64 ABI syscall convention.
// On MIPS, when an indirect syscall is being made (syscall(__NR_foo)),
// the real identifier (__NR_foo) is not in v0, but in a0.
#define SECCOMP_RESULT
#define SECCOMP_SYSCALL
#define SECCOMP_IP
#define SECCOMP_PARM1
#define SECCOMP_PARM2
#define SECCOMP_PARM3
#define SECCOMP_PARM4
#define SECCOMP_PARM5
#define SECCOMP_PARM6
#define SECCOMP_PARM7
#define SECCOMP_PARM8
#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX
#define SECCOMP_ARG_LSB_IDX

// On MIPS we don't have structures like user_regs or user_regs_struct in
// sys/user.h that we could use, so we just define regs_struct directly.
struct regs_struct {
  unsigned long long regs[32];
};

#define REG_a7
#define REG_a6
#define REG_a5
#define REG_a4
#define REG_a3
#define REG_a2
#define REG_a1
#define REG_a0
#define REG_v1
#define REG_v0

#define SECCOMP_PT_RESULT
#define SECCOMP_PT_SYSCALL
#define SECCOMP_PT_PARM1
#define SECCOMP_PT_PARM2
#define SECCOMP_PT_PARM3
#define SECCOMP_PT_PARM4
#define SECCOMP_PT_PARM5
#define SECCOMP_PT_PARM6
#define SECCOMP_PT_PARM7
#define SECCOMP_PT_PARM8

#elif defined(__aarch64__)
struct regs_struct {
  unsigned long long regs[31];
  unsigned long long sp;
  unsigned long long pc;
  unsigned long long pstate;
};

#define SECCOMP_ARCH

#define SECCOMP_REG

#define SECCOMP_RESULT
#define SECCOMP_SYSCALL
#define SECCOMP_IP
#define SECCOMP_PARM1
#define SECCOMP_PARM2
#define SECCOMP_PARM3
#define SECCOMP_PARM4
#define SECCOMP_PARM5
#define SECCOMP_PARM6

#define SECCOMP_NR_IDX
#define SECCOMP_ARCH_IDX
#define SECCOMP_IP_MSB_IDX
#define SECCOMP_IP_LSB_IDX
#define SECCOMP_ARG_MSB_IDX
#define SECCOMP_ARG_LSB_IDX

#define SECCOMP_PT_RESULT
#define SECCOMP_PT_SYSCALL
#define SECCOMP_PT_IP
#define SECCOMP_PT_PARM1
#define SECCOMP_PT_PARM2
#define SECCOMP_PT_PARM3
#define SECCOMP_PT_PARM4
#define SECCOMP_PT_PARM5
#define SECCOMP_PT_PARM6
#else
#error Unsupported target platform

#endif

#endif  // SANDBOX_LINUX_BPF_DSL_SECCOMP_MACROS_H_