hw_sem.c 6.7 KB

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