cpu-all.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * defines common to all virtual CPUs
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef CPU_ALL_H
  20. #define CPU_ALL_H
  21. #include "qemu-common.h"
  22. #include "cpu-common.h"
  23. /* some important defines:
  24. *
  25. * WORDS_ALIGNED : if defined, the host cpu can only make word aligned
  26. * memory accesses.
  27. *
  28. * HOST_WORDS_BIGENDIAN : if defined, the host cpu is big endian and
  29. * otherwise little endian.
  30. *
  31. * (TARGET_WORDS_ALIGNED : same for target cpu (not supported yet))
  32. *
  33. * TARGET_WORDS_BIGENDIAN : same for target cpu
  34. */
  35. #include "softfloat.h"
  36. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  37. #define BSWAP_NEEDED
  38. #endif
  39. #ifdef BSWAP_NEEDED
  40. static inline uint16_t tswap16(uint16_t s)
  41. {
  42. return bswap16(s);
  43. }
  44. static inline uint32_t tswap32(uint32_t s)
  45. {
  46. return bswap32(s);
  47. }
  48. static inline uint64_t tswap64(uint64_t s)
  49. {
  50. return bswap64(s);
  51. }
  52. static inline void tswap16s(uint16_t *s)
  53. {
  54. *s = bswap16(*s);
  55. }
  56. static inline void tswap32s(uint32_t *s)
  57. {
  58. *s = bswap32(*s);
  59. }
  60. static inline void tswap64s(uint64_t *s)
  61. {
  62. *s = bswap64(*s);
  63. }
  64. #else
  65. static inline uint16_t tswap16(uint16_t s)
  66. {
  67. return s;
  68. }
  69. static inline uint32_t tswap32(uint32_t s)
  70. {
  71. return s;
  72. }
  73. static inline uint64_t tswap64(uint64_t s)
  74. {
  75. return s;
  76. }
  77. static inline void tswap16s(uint16_t *s)
  78. {
  79. }
  80. static inline void tswap32s(uint32_t *s)
  81. {
  82. }
  83. static inline void tswap64s(uint64_t *s)
  84. {
  85. }
  86. #endif
  87. #if TARGET_LONG_SIZE == 4
  88. #define tswapl(s) tswap32(s)
  89. #define tswapls(s) tswap32s((uint32_t *)(s))
  90. #define bswaptls(s) bswap32s(s)
  91. #else
  92. #define tswapl(s) tswap64(s)
  93. #define tswapls(s) tswap64s((uint64_t *)(s))
  94. #define bswaptls(s) bswap64s(s)
  95. #endif
  96. typedef union {
  97. float32 f;
  98. uint32_t l;
  99. } CPU_FloatU;
  100. /* NOTE: arm FPA is horrible as double 32 bit words are stored in big
  101. endian ! */
  102. typedef union {
  103. float64 d;
  104. #if defined(HOST_WORDS_BIGENDIAN)
  105. struct {
  106. uint32_t upper;
  107. uint32_t lower;
  108. } l;
  109. #else
  110. struct {
  111. uint32_t lower;
  112. uint32_t upper;
  113. } l;
  114. #endif
  115. uint64_t ll;
  116. } CPU_DoubleU;
  117. typedef union {
  118. floatx80 d;
  119. struct {
  120. uint64_t lower;
  121. uint16_t upper;
  122. } l;
  123. } CPU_LDoubleU;
  124. typedef union {
  125. float128 q;
  126. #if defined(HOST_WORDS_BIGENDIAN)
  127. struct {
  128. uint32_t upmost;
  129. uint32_t upper;
  130. uint32_t lower;
  131. uint32_t lowest;
  132. } l;
  133. struct {
  134. uint64_t upper;
  135. uint64_t lower;
  136. } ll;
  137. #else
  138. struct {
  139. uint32_t lowest;
  140. uint32_t lower;
  141. uint32_t upper;
  142. uint32_t upmost;
  143. } l;
  144. struct {
  145. uint64_t lower;
  146. uint64_t upper;
  147. } ll;
  148. #endif
  149. } CPU_QuadU;
  150. /* CPU memory access without any memory or io remapping */
  151. /*
  152. * the generic syntax for the memory accesses is:
  153. *
  154. * load: ld{type}{sign}{size}{endian}_{access_type}(ptr)
  155. *
  156. * store: st{type}{size}{endian}_{access_type}(ptr, val)
  157. *
  158. * type is:
  159. * (empty): integer access
  160. * f : float access
  161. *
  162. * sign is:
  163. * (empty): for floats or 32 bit size
  164. * u : unsigned
  165. * s : signed
  166. *
  167. * size is:
  168. * b: 8 bits
  169. * w: 16 bits
  170. * l: 32 bits
  171. * q: 64 bits
  172. *
  173. * endian is:
  174. * (empty): target cpu endianness or 8 bit access
  175. * r : reversed target cpu endianness (not implemented yet)
  176. * be : big endian (not implemented yet)
  177. * le : little endian (not implemented yet)
  178. *
  179. * access_type is:
  180. * raw : host memory access
  181. * user : user mode access using soft MMU
  182. * kernel : kernel mode access using soft MMU
  183. */
  184. static inline int ldub_p(const void *ptr)
  185. {
  186. return *(uint8_t *)ptr;
  187. }
  188. static inline int ldsb_p(const void *ptr)
  189. {
  190. return *(int8_t *)ptr;
  191. }
  192. static inline void stb_p(void *ptr, int v)
  193. {
  194. *(uint8_t *)ptr = v;
  195. }
  196. /* NOTE: on arm, putting 2 in /proc/sys/debug/alignment so that the
  197. kernel handles unaligned load/stores may give better results, but
  198. it is a system wide setting : bad */
  199. #if defined(HOST_WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
  200. /* conservative code for little endian unaligned accesses */
  201. static inline int lduw_le_p(const void *ptr)
  202. {
  203. #ifdef _ARCH_PPC
  204. int val;
  205. __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
  206. return val;
  207. #else
  208. const uint8_t *p = ptr;
  209. return p[0] | (p[1] << 8);
  210. #endif
  211. }
  212. static inline int ldsw_le_p(const void *ptr)
  213. {
  214. #ifdef _ARCH_PPC
  215. int val;
  216. __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
  217. return (int16_t)val;
  218. #else
  219. const uint8_t *p = ptr;
  220. return (int16_t)(p[0] | (p[1] << 8));
  221. #endif
  222. }
  223. static inline int ldl_le_p(const void *ptr)
  224. {
  225. #ifdef _ARCH_PPC
  226. int val;
  227. __asm__ __volatile__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (ptr));
  228. return val;
  229. #else
  230. const uint8_t *p = ptr;
  231. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  232. #endif
  233. }
  234. static inline uint64_t ldq_le_p(const void *ptr)
  235. {
  236. const uint8_t *p = ptr;
  237. uint32_t v1, v2;
  238. v1 = ldl_le_p(p);
  239. v2 = ldl_le_p(p + 4);
  240. return v1 | ((uint64_t)v2 << 32);
  241. }
  242. static inline void stw_le_p(void *ptr, int v)
  243. {
  244. #ifdef _ARCH_PPC
  245. __asm__ __volatile__ ("sthbrx %1,0,%2" : "=m" (*(uint16_t *)ptr) : "r" (v), "r" (ptr));
  246. #else
  247. uint8_t *p = ptr;
  248. p[0] = v;
  249. p[1] = v >> 8;
  250. #endif
  251. }
  252. static inline void stl_le_p(void *ptr, int v)
  253. {
  254. #ifdef _ARCH_PPC
  255. __asm__ __volatile__ ("stwbrx %1,0,%2" : "=m" (*(uint32_t *)ptr) : "r" (v), "r" (ptr));
  256. #else
  257. uint8_t *p = ptr;
  258. p[0] = v;
  259. p[1] = v >> 8;
  260. p[2] = v >> 16;
  261. p[3] = v >> 24;
  262. #endif
  263. }
  264. static inline void stq_le_p(void *ptr, uint64_t v)
  265. {
  266. uint8_t *p = ptr;
  267. stl_le_p(p, (uint32_t)v);
  268. stl_le_p(p + 4, v >> 32);
  269. }
  270. /* float access */
  271. static inline float32 ldfl_le_p(const void *ptr)
  272. {
  273. union {
  274. float32 f;
  275. uint32_t i;
  276. } u;
  277. u.i = ldl_le_p(ptr);
  278. return u.f;
  279. }
  280. static inline void stfl_le_p(void *ptr, float32 v)
  281. {
  282. union {
  283. float32 f;
  284. uint32_t i;
  285. } u;
  286. u.f = v;
  287. stl_le_p(ptr, u.i);
  288. }
  289. static inline float64 ldfq_le_p(const void *ptr)
  290. {
  291. CPU_DoubleU u;
  292. u.l.lower = ldl_le_p(ptr);
  293. u.l.upper = ldl_le_p(ptr + 4);
  294. return u.d;
  295. }
  296. static inline void stfq_le_p(void *ptr, float64 v)
  297. {
  298. CPU_DoubleU u;
  299. u.d = v;
  300. stl_le_p(ptr, u.l.lower);
  301. stl_le_p(ptr + 4, u.l.upper);
  302. }
  303. #else
  304. static inline int lduw_le_p(const void *ptr)
  305. {
  306. return *(uint16_t *)ptr;
  307. }
  308. static inline int ldsw_le_p(const void *ptr)
  309. {
  310. return *(int16_t *)ptr;
  311. }
  312. static inline int ldl_le_p(const void *ptr)
  313. {
  314. return *(uint32_t *)ptr;
  315. }
  316. static inline uint64_t ldq_le_p(const void *ptr)
  317. {
  318. return *(uint64_t *)ptr;
  319. }
  320. static inline void stw_le_p(void *ptr, int v)
  321. {
  322. *(uint16_t *)ptr = v;
  323. }
  324. static inline void stl_le_p(void *ptr, int v)
  325. {
  326. *(uint32_t *)ptr = v;
  327. }
  328. static inline void stq_le_p(void *ptr, uint64_t v)
  329. {
  330. *(uint64_t *)ptr = v;
  331. }
  332. /* float access */
  333. static inline float32 ldfl_le_p(const void *ptr)
  334. {
  335. return *(float32 *)ptr;
  336. }
  337. static inline float64 ldfq_le_p(const void *ptr)
  338. {
  339. return *(float64 *)ptr;
  340. }
  341. static inline void stfl_le_p(void *ptr, float32 v)
  342. {
  343. *(float32 *)ptr = v;
  344. }
  345. static inline void stfq_le_p(void *ptr, float64 v)
  346. {
  347. *(float64 *)ptr = v;
  348. }
  349. #endif
  350. #if !defined(HOST_WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
  351. static inline int lduw_be_p(const void *ptr)
  352. {
  353. #if defined(__i386__)
  354. int val;
  355. asm volatile ("movzwl %1, %0\n"
  356. "xchgb %b0, %h0\n"
  357. : "=q" (val)
  358. : "m" (*(uint16_t *)ptr));
  359. return val;
  360. #else
  361. const uint8_t *b = ptr;
  362. return ((b[0] << 8) | b[1]);
  363. #endif
  364. }
  365. static inline int ldsw_be_p(const void *ptr)
  366. {
  367. #if defined(__i386__)
  368. int val;
  369. asm volatile ("movzwl %1, %0\n"
  370. "xchgb %b0, %h0\n"
  371. : "=q" (val)
  372. : "m" (*(uint16_t *)ptr));
  373. return (int16_t)val;
  374. #else
  375. const uint8_t *b = ptr;
  376. return (int16_t)((b[0] << 8) | b[1]);
  377. #endif
  378. }
  379. static inline int ldl_be_p(const void *ptr)
  380. {
  381. #if defined(__i386__) || defined(__x86_64__)
  382. int val;
  383. asm volatile ("movl %1, %0\n"
  384. "bswap %0\n"
  385. : "=r" (val)
  386. : "m" (*(uint32_t *)ptr));
  387. return val;
  388. #else
  389. const uint8_t *b = ptr;
  390. return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
  391. #endif
  392. }
  393. static inline uint64_t ldq_be_p(const void *ptr)
  394. {
  395. uint32_t a,b;
  396. a = ldl_be_p(ptr);
  397. b = ldl_be_p((uint8_t *)ptr + 4);
  398. return (((uint64_t)a<<32)|b);
  399. }
  400. static inline void stw_be_p(void *ptr, int v)
  401. {
  402. #if defined(__i386__)
  403. asm volatile ("xchgb %b0, %h0\n"
  404. "movw %w0, %1\n"
  405. : "=q" (v)
  406. : "m" (*(uint16_t *)ptr), "0" (v));
  407. #else
  408. uint8_t *d = (uint8_t *) ptr;
  409. d[0] = v >> 8;
  410. d[1] = v;
  411. #endif
  412. }
  413. static inline void stl_be_p(void *ptr, int v)
  414. {
  415. #if defined(__i386__) || defined(__x86_64__)
  416. asm volatile ("bswap %0\n"
  417. "movl %0, %1\n"
  418. : "=r" (v)
  419. : "m" (*(uint32_t *)ptr), "0" (v));
  420. #else
  421. uint8_t *d = (uint8_t *) ptr;
  422. d[0] = v >> 24;
  423. d[1] = v >> 16;
  424. d[2] = v >> 8;
  425. d[3] = v;
  426. #endif
  427. }
  428. static inline void stq_be_p(void *ptr, uint64_t v)
  429. {
  430. stl_be_p(ptr, v >> 32);
  431. stl_be_p((uint8_t *)ptr + 4, v);
  432. }
  433. /* float access */
  434. static inline float32 ldfl_be_p(const void *ptr)
  435. {
  436. union {
  437. float32 f;
  438. uint32_t i;
  439. } u;
  440. u.i = ldl_be_p(ptr);
  441. return u.f;
  442. }
  443. static inline void stfl_be_p(void *ptr, float32 v)
  444. {
  445. union {
  446. float32 f;
  447. uint32_t i;
  448. } u;
  449. u.f = v;
  450. stl_be_p(ptr, u.i);
  451. }
  452. static inline float64 ldfq_be_p(const void *ptr)
  453. {
  454. CPU_DoubleU u;
  455. u.l.upper = ldl_be_p(ptr);
  456. u.l.lower = ldl_be_p((uint8_t *)ptr + 4);
  457. return u.d;
  458. }
  459. static inline void stfq_be_p(void *ptr, float64 v)
  460. {
  461. CPU_DoubleU u;
  462. u.d = v;
  463. stl_be_p(ptr, u.l.upper);
  464. stl_be_p((uint8_t *)ptr + 4, u.l.lower);
  465. }
  466. #else
  467. static inline int lduw_be_p(const void *ptr)
  468. {
  469. return *(uint16_t *)ptr;
  470. }
  471. static inline int ldsw_be_p(const void *ptr)
  472. {
  473. return *(int16_t *)ptr;
  474. }
  475. static inline int ldl_be_p(const void *ptr)
  476. {
  477. return *(uint32_t *)ptr;
  478. }
  479. static inline uint64_t ldq_be_p(const void *ptr)
  480. {
  481. return *(uint64_t *)ptr;
  482. }
  483. static inline void stw_be_p(void *ptr, int v)
  484. {
  485. *(uint16_t *)ptr = v;
  486. }
  487. static inline void stl_be_p(void *ptr, int v)
  488. {
  489. *(uint32_t *)ptr = v;
  490. }
  491. static inline void stq_be_p(void *ptr, uint64_t v)
  492. {
  493. *(uint64_t *)ptr = v;
  494. }
  495. /* float access */
  496. static inline float32 ldfl_be_p(const void *ptr)
  497. {
  498. return *(float32 *)ptr;
  499. }
  500. static inline float64 ldfq_be_p(const void *ptr)
  501. {
  502. return *(float64 *)ptr;
  503. }
  504. static inline void stfl_be_p(void *ptr, float32 v)
  505. {
  506. *(float32 *)ptr = v;
  507. }
  508. static inline void stfq_be_p(void *ptr, float64 v)
  509. {
  510. *(float64 *)ptr = v;
  511. }
  512. #endif
  513. /* target CPU memory access functions */
  514. #if defined(TARGET_WORDS_BIGENDIAN)
  515. #define lduw_p(p) lduw_be_p(p)
  516. #define ldsw_p(p) ldsw_be_p(p)
  517. #define ldl_p(p) ldl_be_p(p)
  518. #define ldq_p(p) ldq_be_p(p)
  519. #define ldfl_p(p) ldfl_be_p(p)
  520. #define ldfq_p(p) ldfq_be_p(p)
  521. #define stw_p(p, v) stw_be_p(p, v)
  522. #define stl_p(p, v) stl_be_p(p, v)
  523. #define stq_p(p, v) stq_be_p(p, v)
  524. #define stfl_p(p, v) stfl_be_p(p, v)
  525. #define stfq_p(p, v) stfq_be_p(p, v)
  526. #else
  527. #define lduw_p(p) lduw_le_p(p)
  528. #define ldsw_p(p) ldsw_le_p(p)
  529. #define ldl_p(p) ldl_le_p(p)
  530. #define ldq_p(p) ldq_le_p(p)
  531. #define ldfl_p(p) ldfl_le_p(p)
  532. #define ldfq_p(p) ldfq_le_p(p)
  533. #define stw_p(p, v) stw_le_p(p, v)
  534. #define stl_p(p, v) stl_le_p(p, v)
  535. #define stq_p(p, v) stq_le_p(p, v)
  536. #define stfl_p(p, v) stfl_le_p(p, v)
  537. #define stfq_p(p, v) stfq_le_p(p, v)
  538. #endif
  539. /* MMU memory access macros */
  540. #if defined(CONFIG_USER_ONLY)
  541. #include <assert.h>
  542. #include "qemu-types.h"
  543. /* On some host systems the guest address space is reserved on the host.
  544. * This allows the guest address space to be offset to a convenient location.
  545. */
  546. #if defined(CONFIG_USE_GUEST_BASE)
  547. extern unsigned long guest_base;
  548. extern int have_guest_base;
  549. extern unsigned long reserved_va;
  550. #define GUEST_BASE guest_base
  551. #define RESERVED_VA reserved_va
  552. #else
  553. #define GUEST_BASE 0ul
  554. #define RESERVED_VA 0ul
  555. #endif
  556. /* All direct uses of g2h and h2g need to go away for usermode softmmu. */
  557. #define g2h(x) ((void *)((unsigned long)(x) + GUEST_BASE))
  558. #if HOST_LONG_BITS <= TARGET_VIRT_ADDR_SPACE_BITS
  559. #define h2g_valid(x) 1
  560. #else
  561. #define h2g_valid(x) ({ \
  562. unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \
  563. __guest < (1ul << TARGET_VIRT_ADDR_SPACE_BITS); \
  564. })
  565. #endif
  566. #define h2g(x) ({ \
  567. unsigned long __ret = (unsigned long)(x) - GUEST_BASE; \
  568. /* Check if given address fits target address space */ \
  569. assert(h2g_valid(x)); \
  570. (abi_ulong)__ret; \
  571. })
  572. #define saddr(x) g2h(x)
  573. #define laddr(x) g2h(x)
  574. #else /* !CONFIG_USER_ONLY */
  575. /* NOTE: we use double casts if pointers and target_ulong have
  576. different sizes */
  577. #define saddr(x) (uint8_t *)(long)(x)
  578. #define laddr(x) (uint8_t *)(long)(x)
  579. #endif
  580. #define ldub_raw(p) ldub_p(laddr((p)))
  581. #define ldsb_raw(p) ldsb_p(laddr((p)))
  582. #define lduw_raw(p) lduw_p(laddr((p)))
  583. #define ldsw_raw(p) ldsw_p(laddr((p)))
  584. #define ldl_raw(p) ldl_p(laddr((p)))
  585. #define ldq_raw(p) ldq_p(laddr((p)))
  586. #define ldfl_raw(p) ldfl_p(laddr((p)))
  587. #define ldfq_raw(p) ldfq_p(laddr((p)))
  588. #define stb_raw(p, v) stb_p(saddr((p)), v)
  589. #define stw_raw(p, v) stw_p(saddr((p)), v)
  590. #define stl_raw(p, v) stl_p(saddr((p)), v)
  591. #define stq_raw(p, v) stq_p(saddr((p)), v)
  592. #define stfl_raw(p, v) stfl_p(saddr((p)), v)
  593. #define stfq_raw(p, v) stfq_p(saddr((p)), v)
  594. #if defined(CONFIG_USER_ONLY)
  595. /* if user mode, no other memory access functions */
  596. #define ldub(p) ldub_raw(p)
  597. #define ldsb(p) ldsb_raw(p)
  598. #define lduw(p) lduw_raw(p)
  599. #define ldsw(p) ldsw_raw(p)
  600. #define ldl(p) ldl_raw(p)
  601. #define ldq(p) ldq_raw(p)
  602. #define ldfl(p) ldfl_raw(p)
  603. #define ldfq(p) ldfq_raw(p)
  604. #define stb(p, v) stb_raw(p, v)
  605. #define stw(p, v) stw_raw(p, v)
  606. #define stl(p, v) stl_raw(p, v)
  607. #define stq(p, v) stq_raw(p, v)
  608. #define stfl(p, v) stfl_raw(p, v)
  609. #define stfq(p, v) stfq_raw(p, v)
  610. #define ldub_code(p) ldub_raw(p)
  611. #define ldsb_code(p) ldsb_raw(p)
  612. #define lduw_code(p) lduw_raw(p)
  613. #define ldsw_code(p) ldsw_raw(p)
  614. #define ldl_code(p) ldl_raw(p)
  615. #define ldq_code(p) ldq_raw(p)
  616. #define ldub_kernel(p) ldub_raw(p)
  617. #define ldsb_kernel(p) ldsb_raw(p)
  618. #define lduw_kernel(p) lduw_raw(p)
  619. #define ldsw_kernel(p) ldsw_raw(p)
  620. #define ldl_kernel(p) ldl_raw(p)
  621. #define ldq_kernel(p) ldq_raw(p)
  622. #define ldfl_kernel(p) ldfl_raw(p)
  623. #define ldfq_kernel(p) ldfq_raw(p)
  624. #define stb_kernel(p, v) stb_raw(p, v)
  625. #define stw_kernel(p, v) stw_raw(p, v)
  626. #define stl_kernel(p, v) stl_raw(p, v)
  627. #define stq_kernel(p, v) stq_raw(p, v)
  628. #define stfl_kernel(p, v) stfl_raw(p, v)
  629. #define stfq_kernel(p, vt) stfq_raw(p, v)
  630. #endif /* defined(CONFIG_USER_ONLY) */
  631. /* page related stuff */
  632. #define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
  633. #define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
  634. #define TARGET_PAGE_ALIGN(addr) (((addr) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK)
  635. /* ??? These should be the larger of unsigned long and target_ulong. */
  636. extern unsigned long qemu_real_host_page_size;
  637. extern unsigned long qemu_host_page_bits;
  638. extern unsigned long qemu_host_page_size;
  639. extern unsigned long qemu_host_page_mask;
  640. #define HOST_PAGE_ALIGN(addr) (((addr) + qemu_host_page_size - 1) & qemu_host_page_mask)
  641. /* same as PROT_xxx */
  642. #define PAGE_READ 0x0001
  643. #define PAGE_WRITE 0x0002
  644. #define PAGE_EXEC 0x0004
  645. #define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
  646. #define PAGE_VALID 0x0008
  647. /* original state of the write flag (used when tracking self-modifying
  648. code */
  649. #define PAGE_WRITE_ORG 0x0010
  650. #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
  651. /* FIXME: Code that sets/uses this is broken and needs to go away. */
  652. #define PAGE_RESERVED 0x0020
  653. #endif
  654. #if defined(CONFIG_USER_ONLY)
  655. void page_dump(FILE *f);
  656. typedef int (*walk_memory_regions_fn)(void *, abi_ulong,
  657. abi_ulong, unsigned long);
  658. int walk_memory_regions(void *, walk_memory_regions_fn);
  659. int page_get_flags(target_ulong address);
  660. void page_set_flags(target_ulong start, target_ulong end, int flags);
  661. int page_check_range(target_ulong start, target_ulong len, int flags);
  662. #endif
  663. CPUState *cpu_copy(CPUState *env);
  664. CPUState *qemu_get_cpu(int cpu);
  665. #define CPU_DUMP_CODE 0x00010000
  666. void cpu_dump_state(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
  667. int flags);
  668. void cpu_dump_statistics(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
  669. int flags);
  670. void QEMU_NORETURN cpu_abort(CPUState *env, const char *fmt, ...)
  671. GCC_FMT_ATTR(2, 3);
  672. extern CPUState *first_cpu;
  673. extern CPUState *cpu_single_env;
  674. /* Flags for use in ENV->INTERRUPT_PENDING.
  675. The numbers assigned here are non-sequential in order to preserve
  676. binary compatibility with the vmstate dump. Bit 0 (0x0001) was
  677. previously used for CPU_INTERRUPT_EXIT, and is cleared when loading
  678. the vmstate dump. */
  679. /* External hardware interrupt pending. This is typically used for
  680. interrupts from devices. */
  681. #define CPU_INTERRUPT_HARD 0x0002
  682. /* Exit the current TB. This is typically used when some system-level device
  683. makes some change to the memory mapping. E.g. the a20 line change. */
  684. #define CPU_INTERRUPT_EXITTB 0x0004
  685. /* Halt the CPU. */
  686. #define CPU_INTERRUPT_HALT 0x0020
  687. /* Debug event pending. */
  688. #define CPU_INTERRUPT_DEBUG 0x0080
  689. /* Several target-specific external hardware interrupts. Each target/cpu.h
  690. should define proper names based on these defines. */
  691. #define CPU_INTERRUPT_TGT_EXT_0 0x0008
  692. #define CPU_INTERRUPT_TGT_EXT_1 0x0010
  693. #define CPU_INTERRUPT_TGT_EXT_2 0x0040
  694. #define CPU_INTERRUPT_TGT_EXT_3 0x0200
  695. #define CPU_INTERRUPT_TGT_EXT_4 0x1000
  696. /* Several target-specific internal interrupts. These differ from the
  697. preceeding target-specific interrupts in that they are intended to
  698. originate from within the cpu itself, typically in response to some
  699. instruction being executed. These, therefore, are not masked while
  700. single-stepping within the debugger. */
  701. #define CPU_INTERRUPT_TGT_INT_0 0x0100
  702. #define CPU_INTERRUPT_TGT_INT_1 0x0400
  703. #define CPU_INTERRUPT_TGT_INT_2 0x0800
  704. /* First unused bit: 0x2000. */
  705. /* The set of all bits that should be masked when single-stepping. */
  706. #define CPU_INTERRUPT_SSTEP_MASK \
  707. (CPU_INTERRUPT_HARD \
  708. | CPU_INTERRUPT_TGT_EXT_0 \
  709. | CPU_INTERRUPT_TGT_EXT_1 \
  710. | CPU_INTERRUPT_TGT_EXT_2 \
  711. | CPU_INTERRUPT_TGT_EXT_3 \
  712. | CPU_INTERRUPT_TGT_EXT_4)
  713. #ifndef CONFIG_USER_ONLY
  714. typedef void (*CPUInterruptHandler)(CPUState *, int);
  715. extern CPUInterruptHandler cpu_interrupt_handler;
  716. static inline void cpu_interrupt(CPUState *s, int mask)
  717. {
  718. cpu_interrupt_handler(s, mask);
  719. }
  720. #else /* USER_ONLY */
  721. void cpu_interrupt(CPUState *env, int mask);
  722. #endif /* USER_ONLY */
  723. void cpu_reset_interrupt(CPUState *env, int mask);
  724. void cpu_exit(CPUState *s);
  725. bool qemu_cpu_has_work(CPUState *env);
  726. /* Breakpoint/watchpoint flags */
  727. #define BP_MEM_READ 0x01
  728. #define BP_MEM_WRITE 0x02
  729. #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
  730. #define BP_STOP_BEFORE_ACCESS 0x04
  731. #define BP_WATCHPOINT_HIT 0x08
  732. #define BP_GDB 0x10
  733. #define BP_CPU 0x20
  734. int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
  735. CPUBreakpoint **breakpoint);
  736. int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags);
  737. void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint);
  738. void cpu_breakpoint_remove_all(CPUState *env, int mask);
  739. int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
  740. int flags, CPUWatchpoint **watchpoint);
  741. int cpu_watchpoint_remove(CPUState *env, target_ulong addr,
  742. target_ulong len, int flags);
  743. void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint);
  744. void cpu_watchpoint_remove_all(CPUState *env, int mask);
  745. #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
  746. #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
  747. #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
  748. void cpu_single_step(CPUState *env, int enabled);
  749. void cpu_reset(CPUState *s);
  750. int cpu_is_stopped(CPUState *env);
  751. void run_on_cpu(CPUState *env, void (*func)(void *data), void *data);
  752. #define CPU_LOG_TB_OUT_ASM (1 << 0)
  753. #define CPU_LOG_TB_IN_ASM (1 << 1)
  754. #define CPU_LOG_TB_OP (1 << 2)
  755. #define CPU_LOG_TB_OP_OPT (1 << 3)
  756. #define CPU_LOG_INT (1 << 4)
  757. #define CPU_LOG_EXEC (1 << 5)
  758. #define CPU_LOG_PCALL (1 << 6)
  759. #define CPU_LOG_IOPORT (1 << 7)
  760. #define CPU_LOG_TB_CPU (1 << 8)
  761. #define CPU_LOG_RESET (1 << 9)
  762. /* define log items */
  763. typedef struct CPULogItem {
  764. int mask;
  765. const char *name;
  766. const char *help;
  767. } CPULogItem;
  768. extern const CPULogItem cpu_log_items[];
  769. void cpu_set_log(int log_flags);
  770. void cpu_set_log_filename(const char *filename);
  771. int cpu_str_to_log_mask(const char *str);
  772. #if !defined(CONFIG_USER_ONLY)
  773. /* Return the physical page corresponding to a virtual one. Use it
  774. only for debugging because no protection checks are done. Return -1
  775. if no page found. */
  776. target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr);
  777. /* memory API */
  778. extern int phys_ram_fd;
  779. extern ram_addr_t ram_size;
  780. /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
  781. #define RAM_PREALLOC_MASK (1 << 0)
  782. typedef struct RAMBlock {
  783. uint8_t *host;
  784. ram_addr_t offset;
  785. ram_addr_t length;
  786. uint32_t flags;
  787. char idstr[256];
  788. QLIST_ENTRY(RAMBlock) next;
  789. #if defined(__linux__) && !defined(TARGET_S390X)
  790. int fd;
  791. #endif
  792. } RAMBlock;
  793. typedef struct RAMList {
  794. uint8_t *phys_dirty;
  795. QLIST_HEAD(ram, RAMBlock) blocks;
  796. } RAMList;
  797. extern RAMList ram_list;
  798. extern const char *mem_path;
  799. extern int mem_prealloc;
  800. /* physical memory access */
  801. /* MMIO pages are identified by a combination of an IO device index and
  802. 3 flags. The ROMD code stores the page ram offset in iotlb entry,
  803. so only a limited number of ids are avaiable. */
  804. #define IO_MEM_NB_ENTRIES (1 << (TARGET_PAGE_BITS - IO_MEM_SHIFT))
  805. /* Flags stored in the low bits of the TLB virtual address. These are
  806. defined so that fast path ram access is all zeros. */
  807. /* Zero if TLB entry is valid. */
  808. #define TLB_INVALID_MASK (1 << 3)
  809. /* Set if TLB entry references a clean RAM page. The iotlb entry will
  810. contain the page physical address. */
  811. #define TLB_NOTDIRTY (1 << 4)
  812. /* Set if TLB entry is an IO callback. */
  813. #define TLB_MMIO (1 << 5)
  814. #define VGA_DIRTY_FLAG 0x01
  815. #define CODE_DIRTY_FLAG 0x02
  816. #define MIGRATION_DIRTY_FLAG 0x08
  817. /* read dirty bit (return 0 or 1) */
  818. static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
  819. {
  820. return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
  821. }
  822. static inline int cpu_physical_memory_get_dirty_flags(ram_addr_t addr)
  823. {
  824. return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS];
  825. }
  826. static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
  827. int dirty_flags)
  828. {
  829. return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
  830. }
  831. static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
  832. {
  833. ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
  834. }
  835. static inline int cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
  836. int dirty_flags)
  837. {
  838. return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] |= dirty_flags;
  839. }
  840. static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
  841. int length,
  842. int dirty_flags)
  843. {
  844. int i, mask, len;
  845. uint8_t *p;
  846. len = length >> TARGET_PAGE_BITS;
  847. mask = ~dirty_flags;
  848. p = ram_list.phys_dirty + (start >> TARGET_PAGE_BITS);
  849. for (i = 0; i < len; i++) {
  850. p[i] &= mask;
  851. }
  852. }
  853. void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
  854. int dirty_flags);
  855. void cpu_tlb_update_dirty(CPUState *env);
  856. int cpu_physical_memory_set_dirty_tracking(int enable);
  857. int cpu_physical_memory_get_dirty_tracking(void);
  858. int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
  859. target_phys_addr_t end_addr);
  860. int cpu_physical_log_start(target_phys_addr_t start_addr,
  861. ram_addr_t size);
  862. int cpu_physical_log_stop(target_phys_addr_t start_addr,
  863. ram_addr_t size);
  864. void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
  865. #endif /* !CONFIG_USER_ONLY */
  866. int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
  867. uint8_t *buf, int len, int is_write);
  868. #endif /* CPU_ALL_H */