The Pedigree Project  0.1
modules/subsys/native/user/ipc/Ipc.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/native/ipc/Ipc.h"
21 
22 #include "pedigree/native/native-syscall.h"
23 #include "pedigree/native/nativeSyscallNumbers.h"
24 
25 using namespace PedigreeIpc;
26 
27 PedigreeIpc::StandardIpcMessage::StandardIpcMessage() : m_vAddr(0)
28 {
29 }
30 
31 PedigreeIpc::StandardIpcMessage::~StandardIpcMessage()
32 {
33  syscall1(IPC_DESTROY_MESSAGE, reinterpret_cast<uintptr_t>(this));
34 }
35 
36 PedigreeIpc::SharedIpcMessage::SharedIpcMessage(size_t nBytes, void *handle)
37  : StandardIpcMessage(), m_nBytes(nBytes), m_pHandle(handle)
38 {
39 }
40 
41 PedigreeIpc::SharedIpcMessage::~SharedIpcMessage()
42 {
43  syscall1(IPC_DESTROY_MESSAGE, reinterpret_cast<uintptr_t>(this));
44 }
45 
46 PedigreeIpc::StandardIpcMessage::StandardIpcMessage(void *pKernelMessage)
47 {
48  m_vAddr = reinterpret_cast<void *>(syscall2(
49  IPC_RECV_PHASE2, reinterpret_cast<uintptr_t>(this),
50  reinterpret_cast<uintptr_t>(pKernelMessage)));
51 }
52 
53 bool PedigreeIpc::StandardIpcMessage::initialise()
54 {
55  m_vAddr = reinterpret_cast<void *>(syscall1(
56  IPC_CREATE_STANDARD_MESSAGE, reinterpret_cast<uintptr_t>(this)));
57 
58  return m_vAddr != 0;
59 }
60 
61 bool PedigreeIpc::SharedIpcMessage::initialise()
62 {
63  m_vAddr = reinterpret_cast<void *>(syscall3(
64  IPC_CREATE_SHARED_MESSAGE, reinterpret_cast<uintptr_t>(this), m_nBytes,
65  reinterpret_cast<uintptr_t>(m_pHandle)));
66  m_pHandle = reinterpret_cast<void *>(
67  syscall1(IPC_GET_SHARED_REGION, reinterpret_cast<uintptr_t>(this)));
68 
69  return (m_vAddr != 0) && (m_pHandle != 0);
70 }
71 
72 bool PedigreeIpc::send(
73  IpcEndpoint *pEndpoint, IpcMessage *pMessage, bool bAsync)
74 {
75  return static_cast<bool>(syscall3(
76  IPC_SEND_IPC, reinterpret_cast<uintptr_t>(pEndpoint),
77  reinterpret_cast<uintptr_t>(pMessage), static_cast<uintptr_t>(bAsync)));
78 }
79 
80 bool PedigreeIpc::recv(
81  IpcEndpoint *pEndpoint, IpcMessage **pMessage, bool bAsync)
82 {
83  void *kernelPointer = reinterpret_cast<void *>(syscall2(
84  IPC_RECV_PHASE1, reinterpret_cast<uintptr_t>(pEndpoint),
85  static_cast<uintptr_t>(bAsync)));
86  if (!kernelPointer)
87  return false;
88 
89  *pMessage = new StandardIpcMessage(kernelPointer);
90 
91  return true;
92 }
93 
94 void PedigreeIpc::createEndpoint(const char *name)
95 {
96  syscall1(IPC_CREATE_ENDPOINT, reinterpret_cast<uintptr_t>(name));
97 }
98 
99 void PedigreeIpc::removeEndpoint(const char *name)
100 {
101  syscall1(IPC_REMOVE_ENDPOINT, reinterpret_cast<uintptr_t>(name));
102 }
103 
104 IpcEndpoint *PedigreeIpc::getEndpoint(const char *name)
105 {
106  return reinterpret_cast<IpcEndpoint *>(
107  syscall1(IPC_GET_ENDPOINT, reinterpret_cast<uintptr_t>(name)));
108 }
A standard IPC message that is less than 4 KB in size.