runtime_svc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2013-2014, ARM Limited and Contributors. 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 are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of ARM nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without specific
  16. * prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <arch_helpers.h>
  35. #include <console.h>
  36. #include <platform.h>
  37. #include <semihosting.h>
  38. #include <bl_common.h>
  39. #include <psci.h>
  40. #include <runtime_svc.h>
  41. #include <context.h>
  42. #include <debug.h>
  43. #include <context_mgmt.h>
  44. /*******************************************************************************
  45. * The 'rt_svc_descs' array holds the runtime service descriptors exported by
  46. * services by placing them in the 'rt_svc_descs' linker section.
  47. * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
  48. * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
  49. * type[31] bit in the function id are combined to get an index into the
  50. * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
  51. * 'rt_svc_descs' array which contains the SMC handler.
  52. ******************************************************************************/
  53. #define RT_SVC_DESCS_START ((uint64_t) (&__RT_SVC_DESCS_START__))
  54. #define RT_SVC_DESCS_END ((uint64_t) (&__RT_SVC_DESCS_END__))
  55. uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
  56. static rt_svc_desc *rt_svc_descs;
  57. /*******************************************************************************
  58. * Simple routine to sanity check a runtime service descriptor before using it
  59. ******************************************************************************/
  60. static int32_t validate_rt_svc_desc(rt_svc_desc *desc)
  61. {
  62. if (desc == NULL)
  63. return -EINVAL;
  64. if (desc->start_oen > desc->end_oen)
  65. return -EINVAL;
  66. if (desc->end_oen >= OEN_LIMIT)
  67. return -EINVAL;
  68. if (desc->call_type != SMC_TYPE_FAST && desc->call_type != SMC_TYPE_STD)
  69. return -EINVAL;
  70. /* A runtime service having no init or handle function doesn't make sense */
  71. if (desc->init == NULL && desc->handle == NULL)
  72. return -EINVAL;
  73. return 0;
  74. }
  75. /*******************************************************************************
  76. * This function calls the initialisation routine in the descriptor exported by
  77. * a runtime service. Once a descriptor has been validated, its start & end
  78. * owning entity numbers and the call type are combined to form a unique oen.
  79. * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
  80. * The index of the runtime service descriptor is stored at this index.
  81. ******************************************************************************/
  82. void runtime_svc_init()
  83. {
  84. int32_t rc = 0;
  85. uint32_t index, start_idx, end_idx;
  86. uint64_t rt_svc_descs_num;
  87. /* If no runtime services are implemented then simply bail out */
  88. rt_svc_descs_num = RT_SVC_DESCS_END - RT_SVC_DESCS_START;
  89. rt_svc_descs_num /= sizeof(rt_svc_desc);
  90. if (rt_svc_descs_num == 0)
  91. return;
  92. /* Initialise internal variables to invalid state */
  93. memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
  94. rt_svc_descs = (rt_svc_desc *) RT_SVC_DESCS_START;
  95. for (index = 0; index < rt_svc_descs_num; index++) {
  96. /*
  97. * An invalid descriptor is an error condition since it is
  98. * difficult to predict the system behaviour in the absence
  99. * of this service.
  100. */
  101. rc = validate_rt_svc_desc(&rt_svc_descs[index]);
  102. if (rc) {
  103. ERROR("Invalid runtime service descriptor 0x%x (%s)\n",
  104. &rt_svc_descs[index],
  105. rt_svc_descs[index].name);
  106. goto error;
  107. }
  108. /* Call the initialisation routine for this runtime service */
  109. rc = rt_svc_descs[index].init();
  110. if (rc) {
  111. ERROR("Error initializing runtime service %s\n",
  112. rt_svc_descs[index].name);
  113. } else {
  114. /*
  115. * Fill the indices corresponding to the start and end
  116. * owning entity numbers with the index of the
  117. * descriptor which will handle the SMCs for this owning
  118. * entity range.
  119. */
  120. start_idx = get_unique_oen(rt_svc_descs[index].start_oen,
  121. rt_svc_descs[index].call_type);
  122. end_idx = get_unique_oen(rt_svc_descs[index].end_oen,
  123. rt_svc_descs[index].call_type);
  124. for (; start_idx <= end_idx; start_idx++)
  125. rt_svc_descs_indices[start_idx] = index;
  126. }
  127. }
  128. return;
  129. error:
  130. panic();
  131. }
  132. void fault_handler(void *handle)
  133. {
  134. gp_regs *gpregs_ctx = get_gpregs_ctx(handle);
  135. ERROR("Unhandled synchronous fault. Register dump @ 0x%x \n",
  136. gpregs_ctx);
  137. panic();
  138. }