1 /*
2     Copyright (c) 2014-2016 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 
8       * Redistributions of source code must retain the above copyright
9         notice, this list of conditions and the following disclaimer.
10       * Redistributions in binary form must reproduce the above copyright
11         notice, this list of conditions and the following disclaimer in the
12         documentation and/or other materials provided with the distribution.
13       * Neither the name of Intel Corporation nor the names of its
14         contributors may be used to endorse or promote products derived
15         from this software without specific prior written permission.
16 
17     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef COI_COMMON_H_INCLUDED
31 #define COI_COMMON_H_INCLUDED
32 
33 #include <common/COIMacros_common.h>
34 #include <common/COIPerf_common.h>
35 #include <source/COIEngine_source.h>
36 #include <source/COIProcess_source.h>
37 #include <source/COIPipeline_source.h>
38 #include <source/COIBuffer_source.h>
39 #include <source/COIEvent_source.h>
40 
41 #include <assert.h>
42 #include <dirent.h>
43 #include <dlfcn.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <pthread.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/mman.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
54 
55 
56 /* Environment variable for path to 'target' files.  */
57 #define MIC_DIR_ENV		  "OFFLOAD_MIC_DIR"
58 
59 /* Environment variable for engine index.  */
60 #define MIC_INDEX_ENV		  "OFFLOAD_MIC_INDEX"
61 
62 /* Environment variable for target executable run command.  */
63 #define OFFLOAD_EMUL_RUN_ENV      "OFFLOAD_EMUL_RUN"
64 
65 /* Environment variable for number of emulated devices.  */
66 #define OFFLOAD_EMUL_NUM_ENV	  "OFFLOAD_EMUL_NUM"
67 
68 
69 /* Path to engine directory.  */
70 #define ENGINE_PATH		  "/tmp/offload_XXXXXX"
71 
72 /* Relative path to directory with pipes.  */
73 #define PIPES_PATH		  "/pipes"
74 
75 /* Non-numerical part of host-to-target pipe file name.  */
76 #define PIPE_HOST2TGT_NAME	  PIPES_PATH "/host2tgt_"
77 
78 /* Non-numerical part of target-to-host pipe file name.  */
79 #define PIPE_TGT2HOST_NAME	  PIPES_PATH "/tgt2host_"
80 
81 /* Non-numerical part of shared memory file name.  */
82 #define SHM_NAME		  "/offload_shm_"
83 
84 
85 /* Use secure getenv if it's supported.  */
86 #ifdef HAVE_SECURE_GETENV
87   #define getenv(x)	      secure_getenv(x)
88 #elif HAVE___SECURE_GETENV
89   #define getenv(x)	      __secure_getenv(x)
90 #endif
91 
92 
93 /* Wrapper for malloc.  */
94 #define MALLOC(type, ptr, size)			\
95 {						\
96   type p = (type) malloc (size);		\
97   if (p == NULL)				\
98     COIERROR ("Cannot allocate memory.");	\
99   ptr = p;					\
100 }
101 
102 /* Like MALLOC, but return NULL instead of COIRESULT.  */
103 #define MALLOCN(type, ptr, size)		\
104 {						\
105   type p = (type) malloc (size);		\
106   if (p == NULL)				\
107     COIERRORN ("Cannot allocate memory.");	\
108   ptr = p;					\
109 }
110 
111 /* Wrapper for strdup.  */
112 #define STRDUP(ptr, str)			\
113 {						\
114   char *p = strdup (str);			\
115   if (p == NULL)				\
116     COIERROR ("Cannot allocate memory.");	\
117   ptr = p;					\
118 }
119 
120 /* Wrapper for pipe reading.  */
121 #define READ(pipe, ptr, size)			\
122 {						\
123   int s = (int) size;				\
124   if (read (pipe, ptr, s) != s)			\
125     COIERROR ("Cannot read from pipe.");	\
126 }
127 
128 /* Like READ, but return NULL instead of COIRESULT.  */
129 #define READN(pipe, ptr, size)			\
130 {						\
131   int s = (int) size;				\
132   if (read (pipe, ptr, s) != s)			\
133     COIERRORN ("Cannot read from pipe.");	\
134 }
135 
136 /* Wrapper for pipe writing.  */
137 #define WRITE(pipe, ptr, size)			\
138 {						\
139   int s = (int) size;				\
140   if (write (pipe, ptr, s) != s)		\
141     COIERROR ("Cannot write in pipe.");		\
142 }
143 
144 /* Like WRITE, but return NULL instead of COIRESULT.  */
145 #define WRITEN(pipe, ptr, size)			\
146 {						\
147   int s = (int) size;				\
148   if (write (pipe, ptr, s) != s)		\
149     COIERRORN ("Cannot write in pipe.");	\
150 }
151 
152 
153 /* Command codes enum.  */
154 typedef enum
155 {
156   CMD_BUFFER_COPY,
157   CMD_BUFFER_MAP,
158   CMD_BUFFER_UNMAP,
159   CMD_GET_FUNCTION_HANDLE,
160   CMD_OPEN_LIBRARY,
161   CMD_CLOSE_LIBRARY,
162   CMD_PIPELINE_CREATE,
163   CMD_PIPELINE_DESTROY,
164   CMD_PIPELINE_RUN_FUNCTION,
165   CMD_SHUTDOWN
166 } cmd_t;
167 
168 #endif // COI_COMMON_H_INCLUDED
169