sysdeps.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef TRUSTY_SYSDEPS_H_
  25. #define TRUSTY_SYSDEPS_H_
  26. /*
  27. * Change these includes to match your platform to bring in the equivalent
  28. * types available in a normal C runtime. At least things like uint64_t,
  29. * uintptr_t, and bool (with |false|, |true| keywords) must be present.
  30. */
  31. #include <stdarg.h>
  32. #include <stdbool.h>
  33. #include <stddef.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "osi_api.h"
  38. /*
  39. * These attribute macros may need to be adjusted if not using gcc or clang.
  40. */
  41. #define TRUSTY_ATTR_PACKED __attribute__((packed))
  42. #define TRUSTY_ATTR_NO_RETURN __attribute__((noreturn))
  43. #define TRUSTY_ATTR_SENTINEL __attribute__((__sentinel__))
  44. #define TRUSTY_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  45. #define PAGE_SHIFT 12
  46. #define PAGE_SIZE (1 << PAGE_SHIFT)
  47. /*
  48. * Struct containing attributes for memory to be shared with secure size.
  49. */
  50. struct ns_mem_page_info {
  51. uint64_t attr;
  52. };
  53. struct trusty_dev;
  54. /*
  55. * Lock/unlock mutex. These can be safely empty in a single threaded environment.
  56. *
  57. */
  58. void trusty_lock(osiMutex_t *lock);
  59. void trusty_unlock(osiMutex_t *lock);
  60. /*
  61. * Disable/enable IRQ interrupts and save/restore @state
  62. */
  63. void trusty_local_irq_disable(unsigned long* state);
  64. void trusty_local_irq_restore(unsigned long* state);
  65. /*
  66. * Put in standby state waiting for interrupt.
  67. *
  68. * @dev: Trusty device initialized with trusty_dev_init
  69. * @event_poll: If true this function is entered after trusty_ipc_poll_for_event
  70. * returned TRUSTY_EVENT_NONE. In this case this call needs to
  71. * return without waiting for another interrupt if a secondary CPU
  72. * (any CPU other than the CPU that is waiting for a message) has
  73. * entered idle since the last such call. If there is only one CPU
  74. * calling into trusty this argument can be ignored.
  75. */
  76. void trusty_idle(struct trusty_dev* dev, bool event_poll);
  77. /*
  78. * Aborts the program or reboots the device.
  79. */
  80. void trusty_abort(void);
  81. /*
  82. * Print a formatted string. @format must point to a NULL-terminated string, and
  83. * is followed by arguments to be printed.
  84. */
  85. void trusty_printf(const char* format, ...);
  86. /*
  87. * Copy @n bytes from @src to @dest.
  88. */
  89. void* trusty_memcpy(void* dest, const void* src, size_t n);
  90. /*
  91. * Set @n bytes starting at @dest to @c. Returns @dest.
  92. */
  93. void* trusty_memset(void* dest, const int c, size_t n);
  94. /*
  95. * Compare s1 to s2 with @n bytes. Returns 0 if s1 equal to s2, otherwise non-zero.
  96. */
  97. size_t trusty_memcmp(void* s1, void* s2, size_t n);
  98. /*
  99. * Copy string from @src to @dest, including the terminating NULL byte.
  100. *
  101. * The size of the array at @dest should be long enough to contain the string
  102. * at @src, and should not overlap in memory with @src.
  103. */
  104. char* trusty_strcpy(char* dest, const char* src);
  105. /*
  106. * Returns the length of @str, excluding the terminating NULL byte.
  107. */
  108. size_t trusty_strlen(const char* str);
  109. /*
  110. * Compare two strings, returns zero if strings are identical, else returns
  111. * integer less than or greater than zero respectively if @str1 if less than
  112. * or greater than @str2.
  113. */
  114. int trusty_strcmp(const char* str1, const char* str2);
  115. /*
  116. * Allocate @n elements of size @size. Initializes memory to 0, returns pointer
  117. * to it.
  118. */
  119. void* trusty_calloc(size_t n, size_t size) TRUSTY_ATTR_WARN_UNUSED_RESULT;
  120. /*
  121. * Free memory at @addr allocated with trusty_calloc.
  122. */
  123. void trusty_free(void* addr);
  124. /*
  125. * Allocate @count contiguous pages to be shared with secure side.
  126. *
  127. * Returns: vaddr of allocated memory
  128. */
  129. void* trusty_alloc_pages(unsigned count) TRUSTY_ATTR_WARN_UNUSED_RESULT;
  130. /*
  131. * Free @count pages at @vaddr allocated by trusty_alloc_pages
  132. */
  133. void trusty_free_pages(void* vaddr, unsigned count);
  134. #endif /* TRUSTY_SYSDEPS_H_ */