diff options
Diffstat (limited to 'security/tf_driver/tf_defs.h')
| -rw-r--r-- | security/tf_driver/tf_defs.h | 538 |
1 files changed, 538 insertions, 0 deletions
diff --git a/security/tf_driver/tf_defs.h b/security/tf_driver/tf_defs.h new file mode 100644 index 00000000000..ac209370c55 --- /dev/null +++ b/security/tf_driver/tf_defs.h | |||
| @@ -0,0 +1,538 @@ | |||
| 1 | /** | ||
| 2 | * Copyright (c) 2011 Trusted Logic S.A. | ||
| 3 | * All Rights Reserved. | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU General Public License | ||
| 7 | * version 2 as published by the Free Software Foundation. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
| 17 | * MA 02111-1307 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef __TF_DEFS_H__ | ||
| 21 | #define __TF_DEFS_H__ | ||
| 22 | |||
| 23 | #include <linux/atomic.h> | ||
| 24 | #include <linux/version.h> | ||
| 25 | #include <linux/fs.h> | ||
| 26 | #include <linux/cdev.h> | ||
| 27 | #include <linux/completion.h> | ||
| 28 | #include <linux/list.h> | ||
| 29 | #include <linux/spinlock.h> | ||
| 30 | #include <linux/sysfs.h> | ||
| 31 | #include <linux/sched.h> | ||
| 32 | #include <linux/semaphore.h> | ||
| 33 | #ifdef CONFIG_HAS_WAKELOCK | ||
| 34 | #include <linux/wakelock.h> | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #include "tf_protocol.h" | ||
| 38 | |||
| 39 | /*----------------------------------------------------------------------------*/ | ||
| 40 | |||
| 41 | #define SIZE_1KB 0x400 | ||
| 42 | |||
| 43 | /* | ||
| 44 | * Maximum number of shared memory blocks that can be reigsters in a connection | ||
| 45 | */ | ||
| 46 | #define TF_SHMEM_MAX_COUNT (64) | ||
| 47 | |||
| 48 | /* | ||
| 49 | * Describes the possible types of shared memories | ||
| 50 | * | ||
| 51 | * TF_SHMEM_TYPE_PREALLOC_REGISTERED_SHMEM : | ||
| 52 | * The descriptor describes a registered shared memory. | ||
| 53 | * Its coarse pages are preallocated when initializing the | ||
| 54 | * connection | ||
| 55 | * TF_SHMEM_TYPE_REGISTERED_SHMEM : | ||
| 56 | * The descriptor describes a registered shared memory. | ||
| 57 | * Its coarse pages are not preallocated | ||
| 58 | * TF_SHMEM_TYPE_PM_HIBERNATE : | ||
| 59 | * The descriptor describes a power management shared memory. | ||
| 60 | */ | ||
| 61 | enum TF_SHMEM_TYPE { | ||
| 62 | TF_SHMEM_TYPE_PREALLOC_REGISTERED_SHMEM = 0, | ||
| 63 | TF_SHMEM_TYPE_REGISTERED_SHMEM, | ||
| 64 | TF_SHMEM_TYPE_PM_HIBERNATE, | ||
| 65 | }; | ||
| 66 | |||
| 67 | |||
| 68 | /* | ||
| 69 | * This structure contains a pointer on a coarse page table | ||
| 70 | */ | ||
| 71 | struct tf_coarse_page_table { | ||
| 72 | /* | ||
| 73 | * Identifies the coarse page table descriptor in | ||
| 74 | * free_coarse_page_tables list | ||
| 75 | */ | ||
| 76 | struct list_head list; | ||
| 77 | |||
| 78 | /* | ||
| 79 | * The address of the coarse page table | ||
| 80 | */ | ||
| 81 | u32 *descriptors; | ||
| 82 | |||
| 83 | /* | ||
| 84 | * The address of the array containing this coarse page table | ||
| 85 | */ | ||
| 86 | struct tf_coarse_page_table_array *parent; | ||
| 87 | }; | ||
| 88 | |||
| 89 | |||
| 90 | #define TF_PAGE_DESCRIPTOR_TYPE_NORMAL 0 | ||
| 91 | #define TF_PAGE_DESCRIPTOR_TYPE_PREALLOCATED 1 | ||
| 92 | |||
| 93 | /* | ||
| 94 | * This structure describes an array of up to 4 coarse page tables | ||
| 95 | * allocated within a single 4KB page. | ||
| 96 | */ | ||
| 97 | struct tf_coarse_page_table_array { | ||
| 98 | /* | ||
| 99 | * identifies the element in the coarse_page_table_arrays list | ||
| 100 | */ | ||
| 101 | struct list_head list; | ||
| 102 | |||
| 103 | /* | ||
| 104 | * Type of page descriptor | ||
| 105 | * can take any of TF_PAGE_DESCRIPTOR_TYPE_XXX value | ||
| 106 | */ | ||
| 107 | u32 type; | ||
| 108 | |||
| 109 | struct tf_coarse_page_table coarse_page_tables[4]; | ||
| 110 | |||
| 111 | /* | ||
| 112 | * A counter of the number of coarse pages currently used | ||
| 113 | * the max value should be 4 (one coarse page table is 1KB while one | ||
| 114 | * page is 4KB) | ||
| 115 | */ | ||
| 116 | u8 ref_count; | ||
| 117 | }; | ||
| 118 | |||
| 119 | |||
| 120 | /* | ||
| 121 | * This structure describes a list of coarse page table arrays | ||
| 122 | * with some of the coarse page tables free. It is used | ||
| 123 | * when the driver needs to allocate a new coarse page | ||
| 124 | * table. | ||
| 125 | */ | ||
| 126 | struct tf_coarse_page_table_allocation_context { | ||
| 127 | /* | ||
| 128 | * The spin lock protecting concurrent access to the structure. | ||
| 129 | */ | ||
| 130 | spinlock_t lock; | ||
| 131 | |||
| 132 | /* | ||
| 133 | * The list of allocated coarse page table arrays | ||
| 134 | */ | ||
| 135 | struct list_head coarse_page_table_arrays; | ||
| 136 | |||
| 137 | /* | ||
| 138 | * The list of free coarse page tables | ||
| 139 | */ | ||
| 140 | struct list_head free_coarse_page_tables; | ||
| 141 | }; | ||
| 142 | |||
| 143 | |||
| 144 | /* | ||
| 145 | * Fully describes a shared memory block | ||
| 146 | */ | ||
| 147 | struct tf_shmem_desc { | ||
| 148 | /* | ||
| 149 | * Identifies the shared memory descriptor in the list of free shared | ||
| 150 | * memory descriptors | ||
| 151 | */ | ||
| 152 | struct list_head list; | ||
| 153 | |||
| 154 | /* | ||
| 155 | * Identifies the type of shared memory descriptor | ||
| 156 | */ | ||
| 157 | enum TF_SHMEM_TYPE type; | ||
| 158 | |||
| 159 | /* | ||
| 160 | * The identifier of the block of shared memory, as returned by the | ||
| 161 | * Secure World. | ||
| 162 | * This identifier is block field of a REGISTER_SHARED_MEMORY answer | ||
| 163 | */ | ||
| 164 | u32 block_identifier; | ||
| 165 | |||
| 166 | /* Client buffer */ | ||
| 167 | u8 *client_buffer; | ||
| 168 | |||
| 169 | /* Up to eight coarse page table context */ | ||
| 170 | struct tf_coarse_page_table *coarse_pg_table[TF_MAX_COARSE_PAGES]; | ||
| 171 | |||
| 172 | u32 coarse_pg_table_count; | ||
| 173 | |||
| 174 | /* Reference counter */ | ||
| 175 | atomic_t ref_count; | ||
| 176 | }; | ||
| 177 | |||
| 178 | |||
| 179 | /*----------------------------------------------------------------------------*/ | ||
| 180 | |||
| 181 | /* | ||
| 182 | * This structure describes the communication with the Secure World | ||
| 183 | * | ||
| 184 | * Note that this driver supports only one instance of the Secure World | ||
| 185 | */ | ||
| 186 | struct tf_comm { | ||
| 187 | /* | ||
| 188 | * The spin lock protecting concurrent access to the structure. | ||
| 189 | */ | ||
| 190 | spinlock_t lock; | ||
| 191 | |||
| 192 | /* | ||
| 193 | * Bit vector with the following possible flags: | ||
| 194 | * - TF_COMM_FLAG_IRQ_REQUESTED: If set, indicates that | ||
| 195 | * the IRQ has been successfuly requested. | ||
| 196 | * - TF_COMM_FLAG_TERMINATING: If set, indicates that the | ||
| 197 | * communication with the Secure World is being terminated. | ||
| 198 | * Transmissions to the Secure World are not permitted | ||
| 199 | * - TF_COMM_FLAG_W3B_ALLOCATED: If set, indicates that the | ||
| 200 | * W3B buffer has been allocated. | ||
| 201 | * | ||
| 202 | * This bit vector must be accessed with the kernel's atomic bitwise | ||
| 203 | * operations. | ||
| 204 | */ | ||
| 205 | unsigned long flags; | ||
| 206 | |||
| 207 | /* | ||
| 208 | * The virtual address of the L1 shared buffer. | ||
| 209 | */ | ||
| 210 | struct tf_l1_shared_buffer *l1_buffer; | ||
| 211 | |||
| 212 | /* | ||
| 213 | * The wait queue the client threads are waiting on. | ||
| 214 | */ | ||
| 215 | wait_queue_head_t wait_queue; | ||
| 216 | |||
| 217 | #ifdef CONFIG_TF_TRUSTZONE | ||
| 218 | /* | ||
| 219 | * The interrupt line used by the Secure World. | ||
| 220 | */ | ||
| 221 | int soft_int_irq; | ||
| 222 | |||
| 223 | /* ----- W3B ----- */ | ||
| 224 | /* shared memory descriptor to identify the W3B */ | ||
| 225 | struct tf_shmem_desc w3b_shmem_desc; | ||
| 226 | |||
| 227 | /* Virtual address of the kernel allocated shared memory */ | ||
| 228 | u32 w3b; | ||
| 229 | |||
| 230 | /* offset of data in shared memory coarse pages */ | ||
| 231 | u32 w3b_shmem_offset; | ||
| 232 | |||
| 233 | u32 w3b_shmem_size; | ||
| 234 | |||
| 235 | struct tf_coarse_page_table_allocation_context | ||
| 236 | w3b_cpt_alloc_context; | ||
| 237 | #endif | ||
| 238 | #ifdef CONFIG_TF_ZEBRA | ||
| 239 | /* | ||
| 240 | * The SE SDP can only be initialized once... | ||
| 241 | */ | ||
| 242 | int se_initialized; | ||
| 243 | |||
| 244 | /* | ||
| 245 | * Lock to be held by a client when executing an RPC | ||
| 246 | */ | ||
| 247 | struct mutex rpc_mutex; | ||
| 248 | |||
| 249 | /* | ||
| 250 | * Lock to protect concurrent accesses to DMA channels | ||
| 251 | */ | ||
| 252 | struct mutex dma_mutex; | ||
| 253 | #endif | ||
| 254 | }; | ||
| 255 | |||
| 256 | |||
| 257 | #define TF_COMM_FLAG_IRQ_REQUESTED (0) | ||
| 258 | #define TF_COMM_FLAG_PA_AVAILABLE (1) | ||
| 259 | #define TF_COMM_FLAG_TERMINATING (2) | ||
| 260 | #define TF_COMM_FLAG_W3B_ALLOCATED (3) | ||
| 261 | #define TF_COMM_FLAG_L1_SHARED_ALLOCATED (4) | ||
| 262 | |||
| 263 | /*----------------------------------------------------------------------------*/ | ||
| 264 | |||
| 265 | struct tf_device_stats { | ||
| 266 | atomic_t stat_pages_allocated; | ||
| 267 | atomic_t stat_memories_allocated; | ||
| 268 | atomic_t stat_pages_locked; | ||
| 269 | }; | ||
| 270 | |||
| 271 | /* | ||
| 272 | * This structure describes the information about one device handled by the | ||
| 273 | * driver. Note that the driver supports only a single device. see the global | ||
| 274 | * variable g_tf_dev | ||
| 275 | |||
| 276 | */ | ||
| 277 | struct tf_device { | ||
| 278 | /* | ||
| 279 | * The kernel object for the device | ||
| 280 | */ | ||
| 281 | struct kobject kobj; | ||
| 282 | |||
| 283 | /* | ||
| 284 | * The device number for the device. | ||
| 285 | */ | ||
| 286 | dev_t dev_number; | ||
| 287 | |||
| 288 | /* | ||
| 289 | * Interfaces the char device with the kernel. | ||
| 290 | */ | ||
| 291 | struct cdev cdev; | ||
| 292 | |||
| 293 | #ifdef CONFIG_TF_TEEC | ||
| 294 | struct cdev cdev_teec; | ||
| 295 | #endif | ||
| 296 | |||
| 297 | #ifdef CONFIG_TF_ZEBRA | ||
| 298 | struct cdev cdev_ctrl; | ||
| 299 | |||
| 300 | /* | ||
| 301 | * Globals for CUS | ||
| 302 | */ | ||
| 303 | /* Current key handles loaded in HWAs */ | ||
| 304 | u32 aes1_key_context; | ||
| 305 | u32 des_key_context; | ||
| 306 | bool sham1_is_public; | ||
| 307 | |||
| 308 | /* Object used to serialize HWA accesses */ | ||
| 309 | struct semaphore aes1_sema; | ||
| 310 | struct semaphore des_sema; | ||
| 311 | struct semaphore sha_sema; | ||
| 312 | |||
| 313 | /* | ||
| 314 | * An aligned and correctly shaped pre-allocated buffer used for DMA | ||
| 315 | * transfers | ||
| 316 | */ | ||
| 317 | u32 dma_buffer_length; | ||
| 318 | u8 *dma_buffer; | ||
| 319 | dma_addr_t dma_buffer_phys; | ||
| 320 | |||
| 321 | /* Workspace allocated at boot time and reserved to the Secure World */ | ||
| 322 | u32 workspace_addr; | ||
| 323 | u32 workspace_size; | ||
| 324 | |||
| 325 | /* | ||
| 326 | * A Mutex to provide exclusive locking of the ioctl() | ||
| 327 | */ | ||
| 328 | struct mutex dev_mutex; | ||
| 329 | #endif | ||
| 330 | |||
| 331 | /* | ||
| 332 | * Communications with the SM. | ||
| 333 | */ | ||
| 334 | struct tf_comm sm; | ||
| 335 | |||
| 336 | /* | ||
| 337 | * Lists the connections attached to this device. A connection is | ||
| 338 | * created each time a user space application "opens" a file descriptor | ||
| 339 | * on the driver | ||
| 340 | */ | ||
| 341 | struct list_head connection_list; | ||
| 342 | |||
| 343 | /* | ||
| 344 | * The spin lock used to protect concurrent access to the connection | ||
| 345 | * list. | ||
| 346 | */ | ||
| 347 | spinlock_t connection_list_lock; | ||
| 348 | |||
| 349 | struct tf_device_stats stats; | ||
| 350 | }; | ||
| 351 | |||
| 352 | /*----------------------------------------------------------------------------*/ | ||
| 353 | /* | ||
| 354 | * This type describes a connection state. | ||
| 355 | * This is used to determine whether a message is valid or not. | ||
| 356 | * | ||
| 357 | * Messages are only valid in a certain device state. | ||
| 358 | * Messages may be invalidated between the start of the ioctl call and the | ||
| 359 | * moment the message is sent to the Secure World. | ||
| 360 | * | ||
| 361 | * TF_CONN_STATE_NO_DEVICE_CONTEXT : | ||
| 362 | * The connection has no DEVICE_CONTEXT created and no | ||
| 363 | * CREATE_DEVICE_CONTEXT being processed by the Secure World | ||
| 364 | * TF_CONN_STATE_CREATE_DEVICE_CONTEXT_SENT : | ||
| 365 | * The connection has a CREATE_DEVICE_CONTEXT being processed by the Secure | ||
| 366 | * World | ||
| 367 | * TF_CONN_STATE_VALID_DEVICE_CONTEXT : | ||
| 368 | * The connection has a DEVICE_CONTEXT created and no | ||
| 369 | * DESTROY_DEVICE_CONTEXT is being processed by the Secure World | ||
| 370 | * TF_CONN_STATE_DESTROY_DEVICE_CONTEXT_SENT : | ||
| 371 | * The connection has a DESTROY_DEVICE_CONTEXT being processed by the Secure | ||
| 372 | * World | ||
| 373 | */ | ||
| 374 | enum TF_CONN_STATE { | ||
| 375 | TF_CONN_STATE_NO_DEVICE_CONTEXT = 0, | ||
| 376 | TF_CONN_STATE_CREATE_DEVICE_CONTEXT_SENT, | ||
| 377 | TF_CONN_STATE_VALID_DEVICE_CONTEXT, | ||
| 378 | TF_CONN_STATE_DESTROY_DEVICE_CONTEXT_SENT | ||
| 379 | }; | ||
| 380 | |||
| 381 | |||
| 382 | /* | ||
| 383 | * This type describes the status of the command. | ||
| 384 | * | ||
| 385 | * PENDING: | ||
| 386 | * The initial state; the command has not been sent yet. | ||
| 387 | * SENT: | ||
| 388 | * The command has been sent, we are waiting for an answer. | ||
| 389 | * ABORTED: | ||
| 390 | * The command cannot be sent because the device context is invalid. | ||
| 391 | * Note that this only covers the case where some other thread | ||
| 392 | * sent a DESTROY_DEVICE_CONTEXT command. | ||
| 393 | */ | ||
| 394 | enum TF_COMMAND_STATE { | ||
| 395 | TF_COMMAND_STATE_PENDING = 0, | ||
| 396 | TF_COMMAND_STATE_SENT, | ||
| 397 | TF_COMMAND_STATE_ABORTED | ||
| 398 | }; | ||
| 399 | |||
| 400 | /* | ||
| 401 | * The origin of connection parameters such as login data and | ||
| 402 | * memory reference pointers. | ||
| 403 | * | ||
| 404 | * PROCESS: the calling process. All arguments must be validated. | ||
| 405 | * KERNEL: kernel code. All arguments can be trusted by this driver. | ||
| 406 | */ | ||
| 407 | enum TF_CONNECTION_OWNER { | ||
| 408 | TF_CONNECTION_OWNER_PROCESS = 0, | ||
| 409 | TF_CONNECTION_OWNER_KERNEL, | ||
| 410 | }; | ||
| 411 | |||
| 412 | |||
| 413 | /* | ||
| 414 | * This structure describes a connection to the driver | ||
| 415 | * A connection is created each time an application opens a file descriptor on | ||
| 416 | * the driver | ||
| 417 | */ | ||
| 418 | struct tf_connection { | ||
| 419 | /* | ||
| 420 | * Identifies the connection in the list of the connections attached to | ||
| 421 | * the same device. | ||
| 422 | */ | ||
| 423 | struct list_head list; | ||
| 424 | |||
| 425 | /* | ||
| 426 | * State of the connection. | ||
| 427 | */ | ||
| 428 | enum TF_CONN_STATE state; | ||
| 429 | |||
| 430 | /* | ||
| 431 | * A pointer to the corresponding device structure | ||
| 432 | */ | ||
| 433 | struct tf_device *dev; | ||
| 434 | |||
| 435 | /* | ||
| 436 | * A spinlock to use to access state | ||
| 437 | */ | ||
| 438 | spinlock_t state_lock; | ||
| 439 | |||
| 440 | /* | ||
| 441 | * Counts the number of operations currently pending on the connection. | ||
| 442 | * (for debug only) | ||
| 443 | */ | ||
| 444 | atomic_t pending_op_count; | ||
| 445 | |||
| 446 | /* | ||
| 447 | * A handle for the device context | ||
| 448 | */ | ||
| 449 | u32 device_context; | ||
| 450 | |||
| 451 | /* | ||
| 452 | * Lists the used shared memory descriptors | ||
| 453 | */ | ||
| 454 | struct list_head used_shmem_list; | ||
| 455 | |||
| 456 | /* | ||
| 457 | * Lists the free shared memory descriptors | ||
| 458 | */ | ||
| 459 | struct list_head free_shmem_list; | ||
| 460 | |||
| 461 | /* | ||
| 462 | * A mutex to use to access this structure | ||
| 463 | */ | ||
| 464 | struct mutex shmem_mutex; | ||
| 465 | |||
| 466 | /* | ||
| 467 | * Counts the number of shared memories registered. | ||
| 468 | */ | ||
| 469 | atomic_t shmem_count; | ||
| 470 | |||
| 471 | /* | ||
| 472 | * Page to retrieve memory properties when | ||
| 473 | * registering shared memory through REGISTER_SHARED_MEMORY | ||
| 474 | * messages | ||
| 475 | */ | ||
| 476 | struct vm_area_struct **vmas; | ||
| 477 | |||
| 478 | /* | ||
| 479 | * coarse page table allocation context | ||
| 480 | */ | ||
| 481 | struct tf_coarse_page_table_allocation_context cpt_alloc_context; | ||
| 482 | |||
| 483 | /* The origin of connection parameters such as login data and | ||
| 484 | memory reference pointers. */ | ||
| 485 | enum TF_CONNECTION_OWNER owner; | ||
| 486 | |||
| 487 | #ifdef CONFIG_TF_ZEBRA | ||
| 488 | /* Lists all the Cryptoki Update Shortcuts */ | ||
| 489 | struct list_head shortcut_list; | ||
| 490 | |||
| 491 | /* Lock to protect concurrent accesses to shortcut_list */ | ||
| 492 | spinlock_t shortcut_list_lock; | ||
| 493 | #endif | ||
| 494 | }; | ||
| 495 | |||
| 496 | /*----------------------------------------------------------------------------*/ | ||
| 497 | |||
| 498 | /* | ||
| 499 | * The operation_id field of a message points to this structure. | ||
| 500 | * It is used to identify the thread that triggered the message transmission | ||
| 501 | * Whoever reads an answer can wake up that thread using the completion event | ||
| 502 | */ | ||
| 503 | struct tf_answer_struct { | ||
| 504 | bool answer_copied; | ||
| 505 | union tf_answer *answer; | ||
| 506 | }; | ||
| 507 | |||
| 508 | /*----------------------------------------------------------------------------*/ | ||
| 509 | |||
| 510 | /** | ||
| 511 | * The ASCII-C string representation of the base name of the devices managed by | ||
| 512 | * this driver. | ||
| 513 | */ | ||
| 514 | #define TF_DEVICE_BASE_NAME "tf_driver" | ||
| 515 | |||
| 516 | |||
| 517 | /** | ||
| 518 | * The major and minor numbers of the registered character device driver. | ||
| 519 | * Only 1 instance of the driver is supported. | ||
| 520 | */ | ||
| 521 | #define TF_DEVICE_MINOR_NUMBER (0) | ||
| 522 | |||
| 523 | struct tf_device *tf_get_device(void); | ||
| 524 | |||
| 525 | #define CLEAN_CACHE_CFG_MASK (~0xC) /* 1111 0011 */ | ||
| 526 | |||
| 527 | /*----------------------------------------------------------------------------*/ | ||
| 528 | /* | ||
| 529 | * Kernel Differences | ||
| 530 | */ | ||
| 531 | |||
| 532 | #ifdef CONFIG_ANDROID | ||
| 533 | #define GROUP_INFO get_current_groups() | ||
| 534 | #else | ||
| 535 | #define GROUP_INFO (current->group_info) | ||
| 536 | #endif | ||
| 537 | |||
| 538 | #endif /* !defined(__TF_DEFS_H__) */ | ||
