hw_sem.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* This file is part of the program psim.
  2. Copyright (C) 1997,2008, Joel Sherrill <joel@OARcorp.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_SEM_C_
  15. #define _HW_SEM_C_
  16. #include "device_table.h"
  17. #include <string.h>
  18. #include <sys/ipc.h>
  19. #include <sys/sem.h>
  20. #include <errno.h>
  21. /* DEVICE
  22. sem - provide access to a unix semaphore
  23. DESCRIPTION
  24. This device implements an interface to a unix semaphore.
  25. PROPERTIES
  26. reg = <address> <size> (required)
  27. Determine where the memory lives in the parents address space.
  28. key = <integer> (required)
  29. This is the key of the unix semaphore.
  30. EXAMPLES
  31. Enable tracing of the sem:
  32. | bash$ psim -t sem-device \
  33. Configure a UNIX semaphore using key 0x12345678 mapped into psim
  34. address space at 0xfff00000:
  35. | -o '/sem@0xfff00000/reg 0xfff00000 0x80000' \
  36. | -o '/sem@0xfff00000/key 0x12345678' \
  37. sim/ppc/run -o '/#address-cells 1' \
  38. -o '/sem@0xfff00000/reg 0xfff00000 12' \
  39. -o '/sem@0xfff00000/key 0x12345678' ../psim-hello/hello
  40. REGISTERS
  41. offset 0 - lock count
  42. offset 4 - lock operation
  43. offset 8 - unlock operation
  44. All reads return the current or resulting count.
  45. BUGS
  46. None known.
  47. */
  48. typedef struct _hw_sem_device {
  49. unsigned_word physical_address;
  50. key_t key;
  51. int id;
  52. int initial;
  53. int count;
  54. } hw_sem_device;
  55. #ifndef HAVE_UNION_SEMUN
  56. union semun {
  57. int val;
  58. struct semid_ds *buf;
  59. unsigned short int *array;
  60. #if defined(__linux__)
  61. struct seminfo *__buf;
  62. #endif
  63. };
  64. #endif
  65. static void
  66. hw_sem_init_data(device *me)
  67. {
  68. hw_sem_device *sem = (hw_sem_device*)device_data(me);
  69. const device_unit *d;
  70. int status;
  71. union semun help;
  72. /* initialize the properties of the sem */
  73. if (device_find_property(me, "key") == NULL)
  74. error("sem_init_data() required key property is missing\n");
  75. if (device_find_property(me, "value") == NULL)
  76. error("sem_init_data() required value property is missing\n");
  77. sem->key = (key_t) device_find_integer_property(me, "key");
  78. DTRACE(sem, ("semaphore key (%d)\n", sem->key) );
  79. sem->initial = (int) device_find_integer_property(me, "value");
  80. DTRACE(sem, ("semaphore initial value (%d)\n", sem->initial) );
  81. d = device_unit_address(me);
  82. sem->physical_address = d->cells[ d->nr_cells-1 ];
  83. DTRACE(sem, ("semaphore physical_address=0x%x\n", sem->physical_address));
  84. /* Now to initialize the semaphore */
  85. if ( sem->initial != -1 ) {
  86. sem->id = semget(sem->key, 1, IPC_CREAT | 0660);
  87. if (sem->id == -1)
  88. error("hw_sem_init_data() semget failed\n");
  89. help.val = sem->initial;
  90. status = semctl( sem->id, 0, SETVAL, help );
  91. if (status == -1)
  92. error("hw_sem_init_data() semctl -- set value failed\n");
  93. } else {
  94. sem->id = semget(sem->key, 1, 0660);
  95. if (sem->id == -1)
  96. error("hw_sem_init_data() semget failed\n");
  97. }
  98. sem->count = semctl( sem->id, 0, GETVAL, help );
  99. if (sem->count == -1)
  100. error("hw_sem_init_data() semctl -- get value failed\n");
  101. DTRACE(sem, ("semaphore OS value (%d)\n", sem->count) );
  102. }
  103. static void
  104. hw_sem_attach_address_callback(device *me,
  105. attach_type attach,
  106. int space,
  107. unsigned_word addr,
  108. unsigned nr_bytes,
  109. access_type access,
  110. device *client) /*callback/default*/
  111. {
  112. hw_sem_device *sem = (hw_sem_device*)device_data(me);
  113. if (space != 0)
  114. error("sem_attach_address_callback() invalid address space\n");
  115. if (nr_bytes == 12)
  116. error("sem_attach_address_callback() invalid size\n");
  117. sem->physical_address = addr;
  118. DTRACE(sem, ("semaphore physical_address=0x%x\n", addr));
  119. }
  120. static unsigned
  121. hw_sem_io_read_buffer(device *me,
  122. void *dest,
  123. int space,
  124. unsigned_word addr,
  125. unsigned nr_bytes,
  126. cpu *processor,
  127. unsigned_word cia)
  128. {
  129. hw_sem_device *sem = (hw_sem_device*)device_data(me);
  130. struct sembuf sb;
  131. int status;
  132. uint32_t u32;
  133. union semun help;
  134. /* do we need to worry about out of range addresses? */
  135. DTRACE(sem, ("semaphore read addr=0x%x length=%d\n", addr, nr_bytes));
  136. if (!(addr >= sem->physical_address && addr <= sem->physical_address + 11))
  137. error("hw_sem_io_read_buffer() invalid address - out of range\n");
  138. if ((addr % 4) != 0)
  139. error("hw_sem_io_read_buffer() invalid address - alignment\n");
  140. if (nr_bytes != 4)
  141. error("hw_sem_io_read_buffer() invalid length\n");
  142. switch ( (addr - sem->physical_address) / 4 ) {
  143. case 0: /* OBTAIN CURRENT VALUE */
  144. break;
  145. case 1: /* LOCK */
  146. sb.sem_num = 0;
  147. sb.sem_op = -1;
  148. sb.sem_flg = 0;
  149. status = semop(sem->id, &sb, 1);
  150. if (status == -1) {
  151. perror( "hw_sem.c: lock" );
  152. error("hw_sem_io_read_buffer() sem lock\n");
  153. }
  154. DTRACE(sem, ("semaphore lock %d\n", sem->count));
  155. break;
  156. case 2: /* UNLOCK */
  157. sb.sem_num = 0;
  158. sb.sem_op = 1;
  159. sb.sem_flg = 0;
  160. status = semop(sem->id, &sb, 1);
  161. if (status == -1) {
  162. perror( "hw_sem.c: unlock" );
  163. error("hw_sem_io_read_buffer() sem unlock\n");
  164. }
  165. DTRACE(sem, ("semaphore unlock %d\n", sem->count));
  166. break;
  167. default:
  168. error("hw_sem_io_read_buffer() invalid address - unknown error\n");
  169. break;
  170. }
  171. /* assume target is big endian */
  172. u32 = H2T_4(semctl( sem->id, 0, GETVAL, help ));
  173. DTRACE(sem, ("semaphore OS value (%d)\n", u32) );
  174. if (u32 == 0xffffffff) {
  175. perror( "hw_sem.c: getval" );
  176. error("hw_sem_io_read_buffer() semctl -- get value failed\n");
  177. }
  178. memcpy(dest, &u32, nr_bytes);
  179. return nr_bytes;
  180. }
  181. static device_callbacks const hw_sem_callbacks = {
  182. { generic_device_init_address, hw_sem_init_data },
  183. { hw_sem_attach_address_callback, }, /* address */
  184. { hw_sem_io_read_buffer, NULL }, /* IO */
  185. { NULL, }, /* DMA */
  186. { NULL, }, /* interrupt */
  187. { NULL, }, /* unit */
  188. NULL,
  189. };
  190. static void *
  191. hw_sem_create(const char *name,
  192. const device_unit *unit_address,
  193. const char *args)
  194. {
  195. hw_sem_device *sem = ZALLOC(hw_sem_device);
  196. return sem;
  197. }
  198. const device_descriptor hw_sem_device_descriptor[] = {
  199. { "sem", hw_sem_create, &hw_sem_callbacks },
  200. { NULL },
  201. };
  202. #endif /* _HW_SEM_C_ */