Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 1 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 2 | * The MobiCore Driver Kernel Module is a Linux device driver, which represents |
| 3 | * the command proxy on the lowest layer to the secure world (Swd). Additional |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 4 | * services like memory allocation via mmap and generation of a MMU tables for |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 5 | * given virtual memory are also supported. IRQ functionality receives |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 6 | * information from the SWd in the non secure world (NWd). |
| 7 | * As customary the driver is handled as linux device driver with "open", |
| 8 | * "close" and "ioctl" commands. Access to the driver is possible after the |
| 9 | * device "/dev/mobicore" has been opened. |
| 10 | * The MobiCore Driver Kernel Module must be installed via |
| 11 | * "insmod mcDrvModule.ko". |
| 12 | * |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 13 | * <-- Copyright Giesecke & Devrient GmbH 2010-2012 --> |
| 14 | * <-- Copyright Trustonic Limited 2013 --> |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * 3. The name of the author may not be used to endorse or promote |
| 25 | * products derived from this software without specific prior |
| 26 | * written permission. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 30 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 32 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 36 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 37 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | */ |
| 40 | |
| 41 | #ifndef _MC_LINUX_H_ |
| 42 | #define _MC_LINUX_H_ |
| 43 | |
| 44 | #include "version.h" |
| 45 | |
Oana Medvesan | 74024a1 | 2013-07-04 14:49:22 +0200 | [diff] [blame] | 46 | #ifndef __KERNEL__ |
| 47 | #include <stdint.h> |
| 48 | #endif |
| 49 | |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 50 | #define MC_ADMIN_DEVNODE "mobicore" |
| 51 | #define MC_USER_DEVNODE "mobicore-user" |
| 52 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 53 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 54 | * Data exchange structure of the MC_DRV_MODULE_INIT ioctl command. |
| 55 | * INIT request data to SWD |
| 56 | */ |
| 57 | struct mc_ioctl_init { |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 58 | /* length of notification queue */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 59 | uint32_t nq_length; |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 60 | /* mcp buffer start/length [16:16] [start, length] */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 61 | uint32_t mcp_offset; |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 62 | /* length of mcp buffer */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 63 | uint32_t mcp_length; |
| 64 | }; |
| 65 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 66 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 67 | * Data exchange structure of the MC_DRV_MODULE_INFO ioctl command. |
| 68 | * INFO request data to the SWD |
| 69 | */ |
| 70 | struct mc_ioctl_info { |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 71 | uint32_t ext_info_id; /* extended info ID */ |
| 72 | uint32_t state; /* state */ |
| 73 | uint32_t ext_info; /* extended info */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 76 | /* |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 77 | * Data exchange structure of the MC_IO_MAP_WSM and MC_IO_MAP_MCI commands. |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 78 | * |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 79 | * Allocate a contiguous memory buffer for a process. |
| 80 | * The physical address can be used as for later calls to mmap. |
| 81 | * The handle can be used to communicate about this buffer to the Daemon. |
| 82 | * For MC_IO_MAP_MCI command, the reused field indicates that MCI was set up |
| 83 | * already. I.e. Daemon was restarted. |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 84 | */ |
| 85 | struct mc_ioctl_map { |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 86 | size_t len; /* Buffer length */ |
| 87 | uint32_t handle; /* WSM handle */ |
| 88 | uint64_t phys_addr; /* physical address of WSM (or 0) */ |
| 89 | unsigned long addr; /* Virtual address */ |
| 90 | bool reused; /* if WSM memory was reused, or new allocated */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 93 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 94 | * Data exchange structure of the MC_IO_REG_WSM command. |
| 95 | * |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 96 | * Allocates a physical MMU table and maps the buffer into this page. |
| 97 | * Returns the physical address of the MMU table. |
| 98 | * The page alignment will be created and the appropriated pSize and pOffsetMMU |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 99 | * will be modified to the used values. |
| 100 | */ |
| 101 | struct mc_ioctl_reg_wsm { |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 102 | uint32_t buffer; /* base address of the virtual address */ |
| 103 | uint32_t len; /* size of the virtual address space */ |
| 104 | uint32_t pid; /* process id */ |
| 105 | uint32_t handle; /* driver handle for locked memory */ |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 106 | uint64_t table_phys; /* physical address of the MMU table */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 109 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 110 | * Data exchange structure of the MC_IO_RESOLVE_CONT_WSM ioctl command. |
| 111 | */ |
| 112 | struct mc_ioctl_resolv_cont_wsm { |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 113 | /* driver handle for buffer */ |
| 114 | uint32_t handle; |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 115 | /* length memory */ |
| 116 | uint32_t length; |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 117 | /* base address of memory */ |
| 118 | uint64_t phys; |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 119 | /* fd to owner of the buffer */ |
| 120 | int32_t fd; |
| 121 | }; |
| 122 | |
| 123 | /* |
| 124 | * Data exchange structure of the MC_IO_RESOLVE_WSM ioctl command. |
| 125 | */ |
| 126 | struct mc_ioctl_resolv_wsm { |
| 127 | /* driver handle for buffer */ |
| 128 | uint32_t handle; |
| 129 | /* fd to owner of the buffer */ |
| 130 | int32_t fd; |
| 131 | /* base address of memory */ |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 132 | uint64_t phys; |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 136 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 137 | * defines for the ioctl mobicore driver module function call from user space. |
| 138 | */ |
| 139 | /* MobiCore IOCTL magic number */ |
| 140 | #define MC_IOC_MAGIC 'M' |
| 141 | |
| 142 | #define MC_IO_INIT _IOWR(MC_IOC_MAGIC, 0, struct mc_ioctl_init) |
| 143 | #define MC_IO_INFO _IOWR(MC_IOC_MAGIC, 1, struct mc_ioctl_info) |
| 144 | #define MC_IO_VERSION _IOR(MC_IOC_MAGIC, 2, uint32_t) |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 145 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 146 | * ioctl parameter to send the YIELD command to the SWD. |
| 147 | * Only possible in Privileged Mode. |
| 148 | * ioctl(fd, MC_DRV_MODULE_YIELD) |
| 149 | */ |
| 150 | #define MC_IO_YIELD _IO(MC_IOC_MAGIC, 3) |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 151 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 152 | * ioctl parameter to send the NSIQ signal to the SWD. |
| 153 | * Only possible in Privileged Mode |
| 154 | * ioctl(fd, MC_DRV_MODULE_NSIQ) |
| 155 | */ |
| 156 | #define MC_IO_NSIQ _IO(MC_IOC_MAGIC, 4) |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 157 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 158 | * Free's memory which is formerly allocated by the driver's mmap |
| 159 | * command. The parameter must be this mmaped address. |
| 160 | * The internal instance data regarding to this address are deleted as |
| 161 | * well as each according memory page and its appropriated reserved bit |
| 162 | * is cleared (ClearPageReserved). |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 163 | * Usage: ioctl(fd, MC_DRV_MODULE_FREE, &address) with address being of |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 164 | * type long address |
| 165 | */ |
| 166 | #define MC_IO_FREE _IO(MC_IOC_MAGIC, 5) |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 167 | /* |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 168 | * Creates a MMU Table of the given base address and the size of the |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 169 | * data. |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 170 | * Parameter: mc_ioctl_reg_wsm |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 171 | */ |
| 172 | #define MC_IO_REG_WSM _IOWR(MC_IOC_MAGIC, 6, struct mc_ioctl_reg_wsm) |
| 173 | #define MC_IO_UNREG_WSM _IO(MC_IOC_MAGIC, 7) |
| 174 | #define MC_IO_LOCK_WSM _IO(MC_IOC_MAGIC, 8) |
| 175 | #define MC_IO_UNLOCK_WSM _IO(MC_IOC_MAGIC, 9) |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 176 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 177 | /* |
| 178 | * Allocate contiguous memory for a process for later mapping with mmap. |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 179 | * MC_IO_MAP_WSM usual operation, pages are registered in |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 180 | * device structure and freed later. |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 181 | * MC_IO_MAP_MCI get Instance of MCI, allocates or mmaps |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 182 | * the MCI to daemon |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 183 | */ |
| 184 | #define MC_IO_MAP_WSM _IOWR(MC_IOC_MAGIC, 11, struct mc_ioctl_map) |
| 185 | #define MC_IO_MAP_MCI _IOWR(MC_IOC_MAGIC, 12, struct mc_ioctl_map) |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 186 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 187 | /* |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 188 | * Clean orphaned WSM buffers. Only available to the daemon and should |
| 189 | * only be carried out if the TLC crashes or otherwise calls exit() in |
| 190 | * an unexpected manner. |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 191 | * The clean is needed together with the lock/unlock mechanism so the daemon |
| 192 | * has clear control of the mapped buffers so it can close a Trustlet before |
| 193 | * release all the WSM buffers, otherwise the Trustlet would be able to write |
| 194 | * to possibly kernel memory areas |
| 195 | */ |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 196 | #define MC_IO_CLEAN_WSM _IO(MC_IOC_MAGIC, 14) |
| 197 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 198 | /* |
Oana Medvesan | 8dfbf01 | 2013-12-20 15:40:49 +0100 | [diff] [blame] | 199 | * Get MMU phys address of a buffer handle allocated to the user. |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 200 | * Only available to the daemon. |
| 201 | */ |
| 202 | #define MC_IO_RESOLVE_WSM _IOWR(MC_IOC_MAGIC, 15, \ |
| 203 | struct mc_ioctl_resolv_wsm) |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 204 | |
Oana Medvesan | 8f5df3a | 2013-03-04 19:04:03 +0100 | [diff] [blame] | 205 | /* |
| 206 | * Get the phys address & length of a allocated contiguous buffer. |
| 207 | * Only available to the daemon */ |
| 208 | #define MC_IO_RESOLVE_CONT_WSM _IOWR(MC_IOC_MAGIC, 16, \ |
| 209 | struct mc_ioctl_resolv_cont_wsm) |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 210 | |
Oana Medvesan | edfb720 | 2013-02-06 21:58:30 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Setup the mem traces when called. |
| 213 | * Only available to the daemon */ |
| 214 | #define MC_IO_LOG_SETUP _IO(MC_IOC_MAGIC, 17) |
| 215 | |
Lukas Hänel | db4b53e | 2012-10-22 13:49:41 +0200 | [diff] [blame] | 216 | #endif /* _MC_LINUX_H_ */ |