1/*
2 * Copyright (C) 2013 Intel Corporation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef Syscall_h
27#define Syscall_h
28
29#if ENABLE(SECCOMP_FILTERS)
30
31#if CPU(X86_64)
32#define REG_SYSCALL REG_RAX
33#define REG_ARG0    REG_RDI
34#define REG_ARG1    REG_RSI
35#define REG_ARG2    REG_RDX
36#define REG_ARG3    REG_R10
37#elif CPU(X86)
38#define REG_SYSCALL REG_EAX
39#define REG_ARG0    REG_EBX
40#define REG_ARG1    REG_ECX
41#define REG_ARG2    REG_EDX
42#define REG_ARG3    REG_ESI
43#else
44#error "CPU not supported."
45#endif
46
47#include <signal.h>
48#include <sys/types.h>
49#include <wtf/Noncopyable.h>
50#include <wtf/PassOwnPtr.h>
51
52namespace CoreIPC {
53class ArgumentDecoder;
54class ArgumentEncoder;
55}
56
57namespace WebKit {
58
59class SyscallResult;
60class SyscallPolicy;
61
62class Syscall {
63    WTF_MAKE_NONCOPYABLE(Syscall);
64
65public:
66    virtual ~Syscall() { }
67
68    static PassOwnPtr<Syscall> createFromContext(ucontext_t*);
69    static PassOwnPtr<Syscall> createFromDecoder(CoreIPC::ArgumentDecoder*);
70
71    int type() const { return m_type; }
72
73    void setContext(mcontext_t* context) { m_context = context; }
74    mcontext_t* context() const { return m_context; }
75
76    virtual void setResult(const SyscallResult*) = 0;
77    virtual PassOwnPtr<SyscallResult> execute(const SyscallPolicy&) = 0;
78    virtual void encode(CoreIPC::ArgumentEncoder&) const = 0;
79    virtual bool decode(CoreIPC::ArgumentDecoder*) = 0;
80
81protected:
82    Syscall(int type, mcontext_t*);
83
84private:
85    int m_type;
86    mcontext_t* m_context;
87};
88
89class SyscallResult {
90    WTF_MAKE_NONCOPYABLE(SyscallResult);
91
92public:
93    virtual ~SyscallResult() { }
94
95    static PassOwnPtr<SyscallResult> createFromDecoder(CoreIPC::ArgumentDecoder*, int fd);
96
97    int type() const { return m_type; }
98
99    virtual void encode(CoreIPC::ArgumentEncoder&) const = 0;
100    virtual bool decode(CoreIPC::ArgumentDecoder*, int fd=-1) = 0;
101
102protected:
103    SyscallResult(int type);
104
105private:
106    int m_type;
107};
108
109} // namespace WebKit
110
111#endif // ENABLE(SECCOMP_FILTERS)
112
113#endif // Syscall_h
114