spapr_hcall.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. #include "qemu/osdep.h"
  2. #include "qapi/error.h"
  3. #include "sysemu/hw_accel.h"
  4. #include "sysemu/runstate.h"
  5. #include "qemu/log.h"
  6. #include "qemu/main-loop.h"
  7. #include "qemu/module.h"
  8. #include "qemu/error-report.h"
  9. #include "cpu.h"
  10. #include "exec/exec-all.h"
  11. #include "helper_regs.h"
  12. #include "hw/ppc/spapr.h"
  13. #include "hw/ppc/spapr_cpu_core.h"
  14. #include "mmu-hash64.h"
  15. #include "cpu-models.h"
  16. #include "trace.h"
  17. #include "kvm_ppc.h"
  18. #include "hw/ppc/spapr_ovec.h"
  19. #include "mmu-book3s-v3.h"
  20. #include "hw/mem/memory-device.h"
  21. static bool has_spr(PowerPCCPU *cpu, int spr)
  22. {
  23. /* We can test whether the SPR is defined by checking for a valid name */
  24. return cpu->env.spr_cb[spr].name != NULL;
  25. }
  26. static inline bool valid_ptex(PowerPCCPU *cpu, target_ulong ptex)
  27. {
  28. /*
  29. * hash value/pteg group index is normalized by HPT mask
  30. */
  31. if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~ppc_hash64_hpt_mask(cpu)) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. static bool is_ram_address(SpaprMachineState *spapr, hwaddr addr)
  37. {
  38. MachineState *machine = MACHINE(spapr);
  39. DeviceMemoryState *dms = machine->device_memory;
  40. if (addr < machine->ram_size) {
  41. return true;
  42. }
  43. if ((addr >= dms->base)
  44. && ((addr - dms->base) < memory_region_size(&dms->mr))) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. static target_ulong h_enter(PowerPCCPU *cpu, SpaprMachineState *spapr,
  50. target_ulong opcode, target_ulong *args)
  51. {
  52. target_ulong flags = args[0];
  53. target_ulong ptex = args[1];
  54. target_ulong pteh = args[2];
  55. target_ulong ptel = args[3];
  56. unsigned apshift;
  57. target_ulong raddr;
  58. target_ulong slot;
  59. const ppc_hash_pte64_t *hptes;
  60. apshift = ppc_hash64_hpte_page_shift_noslb(cpu, pteh, ptel);
  61. if (!apshift) {
  62. /* Bad page size encoding */
  63. return H_PARAMETER;
  64. }
  65. raddr = (ptel & HPTE64_R_RPN) & ~((1ULL << apshift) - 1);
  66. if (is_ram_address(spapr, raddr)) {
  67. /* Regular RAM - should have WIMG=0010 */
  68. if ((ptel & HPTE64_R_WIMG) != HPTE64_R_M) {
  69. return H_PARAMETER;
  70. }
  71. } else {
  72. target_ulong wimg_flags;
  73. /* Looks like an IO address */
  74. /* FIXME: What WIMG combinations could be sensible for IO?
  75. * For now we allow WIMG=010x, but are there others? */
  76. /* FIXME: Should we check against registered IO addresses? */
  77. wimg_flags = (ptel & (HPTE64_R_W | HPTE64_R_I | HPTE64_R_M));
  78. if (wimg_flags != HPTE64_R_I &&
  79. wimg_flags != (HPTE64_R_I | HPTE64_R_M)) {
  80. return H_PARAMETER;
  81. }
  82. }
  83. pteh &= ~0x60ULL;
  84. if (!valid_ptex(cpu, ptex)) {
  85. return H_PARAMETER;
  86. }
  87. slot = ptex & 7ULL;
  88. ptex = ptex & ~7ULL;
  89. if (likely((flags & H_EXACT) == 0)) {
  90. hptes = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
  91. for (slot = 0; slot < 8; slot++) {
  92. if (!(ppc_hash64_hpte0(cpu, hptes, slot) & HPTE64_V_VALID)) {
  93. break;
  94. }
  95. }
  96. ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
  97. if (slot == 8) {
  98. return H_PTEG_FULL;
  99. }
  100. } else {
  101. hptes = ppc_hash64_map_hptes(cpu, ptex + slot, 1);
  102. if (ppc_hash64_hpte0(cpu, hptes, 0) & HPTE64_V_VALID) {
  103. ppc_hash64_unmap_hptes(cpu, hptes, ptex + slot, 1);
  104. return H_PTEG_FULL;
  105. }
  106. ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
  107. }
  108. spapr_store_hpte(cpu, ptex + slot, pteh | HPTE64_V_HPTE_DIRTY, ptel);
  109. args[0] = ptex + slot;
  110. return H_SUCCESS;
  111. }
  112. typedef enum {
  113. REMOVE_SUCCESS = 0,
  114. REMOVE_NOT_FOUND = 1,
  115. REMOVE_PARM = 2,
  116. REMOVE_HW = 3,
  117. } RemoveResult;
  118. static RemoveResult remove_hpte(PowerPCCPU *cpu
  119. , target_ulong ptex,
  120. target_ulong avpn,
  121. target_ulong flags,
  122. target_ulong *vp, target_ulong *rp)
  123. {
  124. const ppc_hash_pte64_t *hptes;
  125. target_ulong v, r;
  126. if (!valid_ptex(cpu, ptex)) {
  127. return REMOVE_PARM;
  128. }
  129. hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
  130. v = ppc_hash64_hpte0(cpu, hptes, 0);
  131. r = ppc_hash64_hpte1(cpu, hptes, 0);
  132. ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
  133. if ((v & HPTE64_V_VALID) == 0 ||
  134. ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
  135. ((flags & H_ANDCOND) && (v & avpn) != 0)) {
  136. return REMOVE_NOT_FOUND;
  137. }
  138. *vp = v;
  139. *rp = r;
  140. spapr_store_hpte(cpu, ptex, HPTE64_V_HPTE_DIRTY, 0);
  141. ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
  142. return REMOVE_SUCCESS;
  143. }
  144. static target_ulong h_remove(PowerPCCPU *cpu, SpaprMachineState *spapr,
  145. target_ulong opcode, target_ulong *args)
  146. {
  147. CPUPPCState *env = &cpu->env;
  148. target_ulong flags = args[0];
  149. target_ulong ptex = args[1];
  150. target_ulong avpn = args[2];
  151. RemoveResult ret;
  152. ret = remove_hpte(cpu, ptex, avpn, flags,
  153. &args[0], &args[1]);
  154. switch (ret) {
  155. case REMOVE_SUCCESS:
  156. check_tlb_flush(env, true);
  157. return H_SUCCESS;
  158. case REMOVE_NOT_FOUND:
  159. return H_NOT_FOUND;
  160. case REMOVE_PARM:
  161. return H_PARAMETER;
  162. case REMOVE_HW:
  163. return H_HARDWARE;
  164. }
  165. g_assert_not_reached();
  166. }
  167. #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
  168. #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
  169. #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
  170. #define H_BULK_REMOVE_END 0xc000000000000000ULL
  171. #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
  172. #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
  173. #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
  174. #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
  175. #define H_BULK_REMOVE_HW 0x3000000000000000ULL
  176. #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
  177. #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
  178. #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
  179. #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
  180. #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
  181. #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
  182. #define H_BULK_REMOVE_MAX_BATCH 4
  183. static target_ulong h_bulk_remove(PowerPCCPU *cpu, SpaprMachineState *spapr,
  184. target_ulong opcode, target_ulong *args)
  185. {
  186. CPUPPCState *env = &cpu->env;
  187. int i;
  188. target_ulong rc = H_SUCCESS;
  189. for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
  190. target_ulong *tsh = &args[i*2];
  191. target_ulong tsl = args[i*2 + 1];
  192. target_ulong v, r, ret;
  193. if ((*tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
  194. break;
  195. } else if ((*tsh & H_BULK_REMOVE_TYPE) != H_BULK_REMOVE_REQUEST) {
  196. return H_PARAMETER;
  197. }
  198. *tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
  199. *tsh |= H_BULK_REMOVE_RESPONSE;
  200. if ((*tsh & H_BULK_REMOVE_ANDCOND) && (*tsh & H_BULK_REMOVE_AVPN)) {
  201. *tsh |= H_BULK_REMOVE_PARM;
  202. return H_PARAMETER;
  203. }
  204. ret = remove_hpte(cpu, *tsh & H_BULK_REMOVE_PTEX, tsl,
  205. (*tsh & H_BULK_REMOVE_FLAGS) >> 26,
  206. &v, &r);
  207. *tsh |= ret << 60;
  208. switch (ret) {
  209. case REMOVE_SUCCESS:
  210. *tsh |= (r & (HPTE64_R_C | HPTE64_R_R)) << 43;
  211. break;
  212. case REMOVE_PARM:
  213. rc = H_PARAMETER;
  214. goto exit;
  215. case REMOVE_HW:
  216. rc = H_HARDWARE;
  217. goto exit;
  218. }
  219. }
  220. exit:
  221. check_tlb_flush(env, true);
  222. return rc;
  223. }
  224. static target_ulong h_protect(PowerPCCPU *cpu, SpaprMachineState *spapr,
  225. target_ulong opcode, target_ulong *args)
  226. {
  227. CPUPPCState *env = &cpu->env;
  228. target_ulong flags = args[0];
  229. target_ulong ptex = args[1];
  230. target_ulong avpn = args[2];
  231. const ppc_hash_pte64_t *hptes;
  232. target_ulong v, r;
  233. if (!valid_ptex(cpu, ptex)) {
  234. return H_PARAMETER;
  235. }
  236. hptes = ppc_hash64_map_hptes(cpu, ptex, 1);
  237. v = ppc_hash64_hpte0(cpu, hptes, 0);
  238. r = ppc_hash64_hpte1(cpu, hptes, 0);
  239. ppc_hash64_unmap_hptes(cpu, hptes, ptex, 1);
  240. if ((v & HPTE64_V_VALID) == 0 ||
  241. ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
  242. return H_NOT_FOUND;
  243. }
  244. r &= ~(HPTE64_R_PP0 | HPTE64_R_PP | HPTE64_R_N |
  245. HPTE64_R_KEY_HI | HPTE64_R_KEY_LO);
  246. r |= (flags << 55) & HPTE64_R_PP0;
  247. r |= (flags << 48) & HPTE64_R_KEY_HI;
  248. r |= flags & (HPTE64_R_PP | HPTE64_R_N | HPTE64_R_KEY_LO);
  249. spapr_store_hpte(cpu, ptex,
  250. (v & ~HPTE64_V_VALID) | HPTE64_V_HPTE_DIRTY, 0);
  251. ppc_hash64_tlb_flush_hpte(cpu, ptex, v, r);
  252. /* Flush the tlb */
  253. check_tlb_flush(env, true);
  254. /* Don't need a memory barrier, due to qemu's global lock */
  255. spapr_store_hpte(cpu, ptex, v | HPTE64_V_HPTE_DIRTY, r);
  256. return H_SUCCESS;
  257. }
  258. static target_ulong h_read(PowerPCCPU *cpu, SpaprMachineState *spapr,
  259. target_ulong opcode, target_ulong *args)
  260. {
  261. target_ulong flags = args[0];
  262. target_ulong ptex = args[1];
  263. int i, ridx, n_entries = 1;
  264. const ppc_hash_pte64_t *hptes;
  265. if (!valid_ptex(cpu, ptex)) {
  266. return H_PARAMETER;
  267. }
  268. if (flags & H_READ_4) {
  269. /* Clear the two low order bits */
  270. ptex &= ~(3ULL);
  271. n_entries = 4;
  272. }
  273. hptes = ppc_hash64_map_hptes(cpu, ptex, n_entries);
  274. for (i = 0, ridx = 0; i < n_entries; i++) {
  275. args[ridx++] = ppc_hash64_hpte0(cpu, hptes, i);
  276. args[ridx++] = ppc_hash64_hpte1(cpu, hptes, i);
  277. }
  278. ppc_hash64_unmap_hptes(cpu, hptes, ptex, n_entries);
  279. return H_SUCCESS;
  280. }
  281. struct SpaprPendingHpt {
  282. /* These fields are read-only after initialization */
  283. int shift;
  284. QemuThread thread;
  285. /* These fields are protected by the BQL */
  286. bool complete;
  287. /* These fields are private to the preparation thread if
  288. * !complete, otherwise protected by the BQL */
  289. int ret;
  290. void *hpt;
  291. };
  292. static void free_pending_hpt(SpaprPendingHpt *pending)
  293. {
  294. if (pending->hpt) {
  295. qemu_vfree(pending->hpt);
  296. }
  297. g_free(pending);
  298. }
  299. static void *hpt_prepare_thread(void *opaque)
  300. {
  301. SpaprPendingHpt *pending = opaque;
  302. size_t size = 1ULL << pending->shift;
  303. pending->hpt = qemu_memalign(size, size);
  304. if (pending->hpt) {
  305. memset(pending->hpt, 0, size);
  306. pending->ret = H_SUCCESS;
  307. } else {
  308. pending->ret = H_NO_MEM;
  309. }
  310. qemu_mutex_lock_iothread();
  311. if (SPAPR_MACHINE(qdev_get_machine())->pending_hpt == pending) {
  312. /* Ready to go */
  313. pending->complete = true;
  314. } else {
  315. /* We've been cancelled, clean ourselves up */
  316. free_pending_hpt(pending);
  317. }
  318. qemu_mutex_unlock_iothread();
  319. return NULL;
  320. }
  321. /* Must be called with BQL held */
  322. static void cancel_hpt_prepare(SpaprMachineState *spapr)
  323. {
  324. SpaprPendingHpt *pending = spapr->pending_hpt;
  325. /* Let the thread know it's cancelled */
  326. spapr->pending_hpt = NULL;
  327. if (!pending) {
  328. /* Nothing to do */
  329. return;
  330. }
  331. if (!pending->complete) {
  332. /* thread will clean itself up */
  333. return;
  334. }
  335. free_pending_hpt(pending);
  336. }
  337. /* Convert a return code from the KVM ioctl()s implementing resize HPT
  338. * into a PAPR hypercall return code */
  339. static target_ulong resize_hpt_convert_rc(int ret)
  340. {
  341. if (ret >= 100000) {
  342. return H_LONG_BUSY_ORDER_100_SEC;
  343. } else if (ret >= 10000) {
  344. return H_LONG_BUSY_ORDER_10_SEC;
  345. } else if (ret >= 1000) {
  346. return H_LONG_BUSY_ORDER_1_SEC;
  347. } else if (ret >= 100) {
  348. return H_LONG_BUSY_ORDER_100_MSEC;
  349. } else if (ret >= 10) {
  350. return H_LONG_BUSY_ORDER_10_MSEC;
  351. } else if (ret > 0) {
  352. return H_LONG_BUSY_ORDER_1_MSEC;
  353. }
  354. switch (ret) {
  355. case 0:
  356. return H_SUCCESS;
  357. case -EPERM:
  358. return H_AUTHORITY;
  359. case -EINVAL:
  360. return H_PARAMETER;
  361. case -ENXIO:
  362. return H_CLOSED;
  363. case -ENOSPC:
  364. return H_PTEG_FULL;
  365. case -EBUSY:
  366. return H_BUSY;
  367. case -ENOMEM:
  368. return H_NO_MEM;
  369. default:
  370. return H_HARDWARE;
  371. }
  372. }
  373. static target_ulong h_resize_hpt_prepare(PowerPCCPU *cpu,
  374. SpaprMachineState *spapr,
  375. target_ulong opcode,
  376. target_ulong *args)
  377. {
  378. target_ulong flags = args[0];
  379. int shift = args[1];
  380. SpaprPendingHpt *pending = spapr->pending_hpt;
  381. uint64_t current_ram_size;
  382. int rc;
  383. if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED) {
  384. return H_AUTHORITY;
  385. }
  386. if (!spapr->htab_shift) {
  387. /* Radix guest, no HPT */
  388. return H_NOT_AVAILABLE;
  389. }
  390. trace_spapr_h_resize_hpt_prepare(flags, shift);
  391. if (flags != 0) {
  392. return H_PARAMETER;
  393. }
  394. if (shift && ((shift < 18) || (shift > 46))) {
  395. return H_PARAMETER;
  396. }
  397. current_ram_size = MACHINE(spapr)->ram_size + get_plugged_memory_size();
  398. /* We only allow the guest to allocate an HPT one order above what
  399. * we'd normally give them (to stop a small guest claiming a huge
  400. * chunk of resources in the HPT */
  401. if (shift > (spapr_hpt_shift_for_ramsize(current_ram_size) + 1)) {
  402. return H_RESOURCE;
  403. }
  404. rc = kvmppc_resize_hpt_prepare(cpu, flags, shift);
  405. if (rc != -ENOSYS) {
  406. return resize_hpt_convert_rc(rc);
  407. }
  408. if (pending) {
  409. /* something already in progress */
  410. if (pending->shift == shift) {
  411. /* and it's suitable */
  412. if (pending->complete) {
  413. return pending->ret;
  414. } else {
  415. return H_LONG_BUSY_ORDER_100_MSEC;
  416. }
  417. }
  418. /* not suitable, cancel and replace */
  419. cancel_hpt_prepare(spapr);
  420. }
  421. if (!shift) {
  422. /* nothing to do */
  423. return H_SUCCESS;
  424. }
  425. /* start new prepare */
  426. pending = g_new0(SpaprPendingHpt, 1);
  427. pending->shift = shift;
  428. pending->ret = H_HARDWARE;
  429. qemu_thread_create(&pending->thread, "sPAPR HPT prepare",
  430. hpt_prepare_thread, pending, QEMU_THREAD_DETACHED);
  431. spapr->pending_hpt = pending;
  432. /* In theory we could estimate the time more accurately based on
  433. * the new size, but there's not much point */
  434. return H_LONG_BUSY_ORDER_100_MSEC;
  435. }
  436. static uint64_t new_hpte_load0(void *htab, uint64_t pteg, int slot)
  437. {
  438. uint8_t *addr = htab;
  439. addr += pteg * HASH_PTEG_SIZE_64;
  440. addr += slot * HASH_PTE_SIZE_64;
  441. return ldq_p(addr);
  442. }
  443. static void new_hpte_store(void *htab, uint64_t pteg, int slot,
  444. uint64_t pte0, uint64_t pte1)
  445. {
  446. uint8_t *addr = htab;
  447. addr += pteg * HASH_PTEG_SIZE_64;
  448. addr += slot * HASH_PTE_SIZE_64;
  449. stq_p(addr, pte0);
  450. stq_p(addr + HASH_PTE_SIZE_64 / 2, pte1);
  451. }
  452. static int rehash_hpte(PowerPCCPU *cpu,
  453. const ppc_hash_pte64_t *hptes,
  454. void *old_hpt, uint64_t oldsize,
  455. void *new_hpt, uint64_t newsize,
  456. uint64_t pteg, int slot)
  457. {
  458. uint64_t old_hash_mask = (oldsize >> 7) - 1;
  459. uint64_t new_hash_mask = (newsize >> 7) - 1;
  460. target_ulong pte0 = ppc_hash64_hpte0(cpu, hptes, slot);
  461. target_ulong pte1;
  462. uint64_t avpn;
  463. unsigned base_pg_shift;
  464. uint64_t hash, new_pteg, replace_pte0;
  465. if (!(pte0 & HPTE64_V_VALID) || !(pte0 & HPTE64_V_BOLTED)) {
  466. return H_SUCCESS;
  467. }
  468. pte1 = ppc_hash64_hpte1(cpu, hptes, slot);
  469. base_pg_shift = ppc_hash64_hpte_page_shift_noslb(cpu, pte0, pte1);
  470. assert(base_pg_shift); /* H_ENTER shouldn't allow a bad encoding */
  471. avpn = HPTE64_V_AVPN_VAL(pte0) & ~(((1ULL << base_pg_shift) - 1) >> 23);
  472. if (pte0 & HPTE64_V_SECONDARY) {
  473. pteg = ~pteg;
  474. }
  475. if ((pte0 & HPTE64_V_SSIZE) == HPTE64_V_SSIZE_256M) {
  476. uint64_t offset, vsid;
  477. /* We only have 28 - 23 bits of offset in avpn */
  478. offset = (avpn & 0x1f) << 23;
  479. vsid = avpn >> 5;
  480. /* We can find more bits from the pteg value */
  481. if (base_pg_shift < 23) {
  482. offset |= ((vsid ^ pteg) & old_hash_mask) << base_pg_shift;
  483. }
  484. hash = vsid ^ (offset >> base_pg_shift);
  485. } else if ((pte0 & HPTE64_V_SSIZE) == HPTE64_V_SSIZE_1T) {
  486. uint64_t offset, vsid;
  487. /* We only have 40 - 23 bits of seg_off in avpn */
  488. offset = (avpn & 0x1ffff) << 23;
  489. vsid = avpn >> 17;
  490. if (base_pg_shift < 23) {
  491. offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask)
  492. << base_pg_shift;
  493. }
  494. hash = vsid ^ (vsid << 25) ^ (offset >> base_pg_shift);
  495. } else {
  496. error_report("rehash_pte: Bad segment size in HPTE");
  497. return H_HARDWARE;
  498. }
  499. new_pteg = hash & new_hash_mask;
  500. if (pte0 & HPTE64_V_SECONDARY) {
  501. assert(~pteg == (hash & old_hash_mask));
  502. new_pteg = ~new_pteg;
  503. } else {
  504. assert(pteg == (hash & old_hash_mask));
  505. }
  506. assert((oldsize != newsize) || (pteg == new_pteg));
  507. replace_pte0 = new_hpte_load0(new_hpt, new_pteg, slot);
  508. /*
  509. * Strictly speaking, we don't need all these tests, since we only
  510. * ever rehash bolted HPTEs. We might in future handle non-bolted
  511. * HPTEs, though so make the logic correct for those cases as
  512. * well.
  513. */
  514. if (replace_pte0 & HPTE64_V_VALID) {
  515. assert(newsize < oldsize);
  516. if (replace_pte0 & HPTE64_V_BOLTED) {
  517. if (pte0 & HPTE64_V_BOLTED) {
  518. /* Bolted collision, nothing we can do */
  519. return H_PTEG_FULL;
  520. } else {
  521. /* Discard this hpte */
  522. return H_SUCCESS;
  523. }
  524. }
  525. }
  526. new_hpte_store(new_hpt, new_pteg, slot, pte0, pte1);
  527. return H_SUCCESS;
  528. }
  529. static int rehash_hpt(PowerPCCPU *cpu,
  530. void *old_hpt, uint64_t oldsize,
  531. void *new_hpt, uint64_t newsize)
  532. {
  533. uint64_t n_ptegs = oldsize >> 7;
  534. uint64_t pteg;
  535. int slot;
  536. int rc;
  537. for (pteg = 0; pteg < n_ptegs; pteg++) {
  538. hwaddr ptex = pteg * HPTES_PER_GROUP;
  539. const ppc_hash_pte64_t *hptes
  540. = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
  541. if (!hptes) {
  542. return H_HARDWARE;
  543. }
  544. for (slot = 0; slot < HPTES_PER_GROUP; slot++) {
  545. rc = rehash_hpte(cpu, hptes, old_hpt, oldsize, new_hpt, newsize,
  546. pteg, slot);
  547. if (rc != H_SUCCESS) {
  548. ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
  549. return rc;
  550. }
  551. }
  552. ppc_hash64_unmap_hptes(cpu, hptes, ptex, HPTES_PER_GROUP);
  553. }
  554. return H_SUCCESS;
  555. }
  556. static void do_push_sregs_to_kvm_pr(CPUState *cs, run_on_cpu_data data)
  557. {
  558. int ret;
  559. cpu_synchronize_state(cs);
  560. ret = kvmppc_put_books_sregs(POWERPC_CPU(cs));
  561. if (ret < 0) {
  562. error_report("failed to push sregs to KVM: %s", strerror(-ret));
  563. exit(1);
  564. }
  565. }
  566. static void push_sregs_to_kvm_pr(SpaprMachineState *spapr)
  567. {
  568. CPUState *cs;
  569. /*
  570. * This is a hack for the benefit of KVM PR - it abuses the SDR1
  571. * slot in kvm_sregs to communicate the userspace address of the
  572. * HPT
  573. */
  574. if (!kvm_enabled() || !spapr->htab) {
  575. return;
  576. }
  577. CPU_FOREACH(cs) {
  578. run_on_cpu(cs, do_push_sregs_to_kvm_pr, RUN_ON_CPU_NULL);
  579. }
  580. }
  581. static target_ulong h_resize_hpt_commit(PowerPCCPU *cpu,
  582. SpaprMachineState *spapr,
  583. target_ulong opcode,
  584. target_ulong *args)
  585. {
  586. target_ulong flags = args[0];
  587. target_ulong shift = args[1];
  588. SpaprPendingHpt *pending = spapr->pending_hpt;
  589. int rc;
  590. size_t newsize;
  591. if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED) {
  592. return H_AUTHORITY;
  593. }
  594. if (!spapr->htab_shift) {
  595. /* Radix guest, no HPT */
  596. return H_NOT_AVAILABLE;
  597. }
  598. trace_spapr_h_resize_hpt_commit(flags, shift);
  599. rc = kvmppc_resize_hpt_commit(cpu, flags, shift);
  600. if (rc != -ENOSYS) {
  601. rc = resize_hpt_convert_rc(rc);
  602. if (rc == H_SUCCESS) {
  603. /* Need to set the new htab_shift in the machine state */
  604. spapr->htab_shift = shift;
  605. }
  606. return rc;
  607. }
  608. if (flags != 0) {
  609. return H_PARAMETER;
  610. }
  611. if (!pending || (pending->shift != shift)) {
  612. /* no matching prepare */
  613. return H_CLOSED;
  614. }
  615. if (!pending->complete) {
  616. /* prepare has not completed */
  617. return H_BUSY;
  618. }
  619. /* Shouldn't have got past PREPARE without an HPT */
  620. g_assert(spapr->htab_shift);
  621. newsize = 1ULL << pending->shift;
  622. rc = rehash_hpt(cpu, spapr->htab, HTAB_SIZE(spapr),
  623. pending->hpt, newsize);
  624. if (rc == H_SUCCESS) {
  625. qemu_vfree(spapr->htab);
  626. spapr->htab = pending->hpt;
  627. spapr->htab_shift = pending->shift;
  628. push_sregs_to_kvm_pr(spapr);
  629. pending->hpt = NULL; /* so it's not free()d */
  630. }
  631. /* Clean up */
  632. spapr->pending_hpt = NULL;
  633. free_pending_hpt(pending);
  634. return rc;
  635. }
  636. static target_ulong h_set_sprg0(PowerPCCPU *cpu, SpaprMachineState *spapr,
  637. target_ulong opcode, target_ulong *args)
  638. {
  639. cpu_synchronize_state(CPU(cpu));
  640. cpu->env.spr[SPR_SPRG0] = args[0];
  641. return H_SUCCESS;
  642. }
  643. static target_ulong h_set_dabr(PowerPCCPU *cpu, SpaprMachineState *spapr,
  644. target_ulong opcode, target_ulong *args)
  645. {
  646. if (!has_spr(cpu, SPR_DABR)) {
  647. return H_HARDWARE; /* DABR register not available */
  648. }
  649. cpu_synchronize_state(CPU(cpu));
  650. if (has_spr(cpu, SPR_DABRX)) {
  651. cpu->env.spr[SPR_DABRX] = 0x3; /* Use Problem and Privileged state */
  652. } else if (!(args[0] & 0x4)) { /* Breakpoint Translation set? */
  653. return H_RESERVED_DABR;
  654. }
  655. cpu->env.spr[SPR_DABR] = args[0];
  656. return H_SUCCESS;
  657. }
  658. static target_ulong h_set_xdabr(PowerPCCPU *cpu, SpaprMachineState *spapr,
  659. target_ulong opcode, target_ulong *args)
  660. {
  661. target_ulong dabrx = args[1];
  662. if (!has_spr(cpu, SPR_DABR) || !has_spr(cpu, SPR_DABRX)) {
  663. return H_HARDWARE;
  664. }
  665. if ((dabrx & ~0xfULL) != 0 || (dabrx & H_DABRX_HYPERVISOR) != 0
  666. || (dabrx & (H_DABRX_KERNEL | H_DABRX_USER)) == 0) {
  667. return H_PARAMETER;
  668. }
  669. cpu_synchronize_state(CPU(cpu));
  670. cpu->env.spr[SPR_DABRX] = dabrx;
  671. cpu->env.spr[SPR_DABR] = args[0];
  672. return H_SUCCESS;
  673. }
  674. static target_ulong h_page_init(PowerPCCPU *cpu, SpaprMachineState *spapr,
  675. target_ulong opcode, target_ulong *args)
  676. {
  677. target_ulong flags = args[0];
  678. hwaddr dst = args[1];
  679. hwaddr src = args[2];
  680. hwaddr len = TARGET_PAGE_SIZE;
  681. uint8_t *pdst, *psrc;
  682. target_long ret = H_SUCCESS;
  683. if (flags & ~(H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE
  684. | H_COPY_PAGE | H_ZERO_PAGE)) {
  685. qemu_log_mask(LOG_UNIMP, "h_page_init: Bad flags (" TARGET_FMT_lx "\n",
  686. flags);
  687. return H_PARAMETER;
  688. }
  689. /* Map-in destination */
  690. if (!is_ram_address(spapr, dst) || (dst & ~TARGET_PAGE_MASK) != 0) {
  691. return H_PARAMETER;
  692. }
  693. pdst = cpu_physical_memory_map(dst, &len, 1);
  694. if (!pdst || len != TARGET_PAGE_SIZE) {
  695. return H_PARAMETER;
  696. }
  697. if (flags & H_COPY_PAGE) {
  698. /* Map-in source, copy to destination, and unmap source again */
  699. if (!is_ram_address(spapr, src) || (src & ~TARGET_PAGE_MASK) != 0) {
  700. ret = H_PARAMETER;
  701. goto unmap_out;
  702. }
  703. psrc = cpu_physical_memory_map(src, &len, 0);
  704. if (!psrc || len != TARGET_PAGE_SIZE) {
  705. ret = H_PARAMETER;
  706. goto unmap_out;
  707. }
  708. memcpy(pdst, psrc, len);
  709. cpu_physical_memory_unmap(psrc, len, 0, len);
  710. } else if (flags & H_ZERO_PAGE) {
  711. memset(pdst, 0, len); /* Just clear the destination page */
  712. }
  713. if (kvm_enabled() && (flags & H_ICACHE_SYNCHRONIZE) != 0) {
  714. kvmppc_dcbst_range(cpu, pdst, len);
  715. }
  716. if (flags & (H_ICACHE_SYNCHRONIZE | H_ICACHE_INVALIDATE)) {
  717. if (kvm_enabled()) {
  718. kvmppc_icbi_range(cpu, pdst, len);
  719. } else {
  720. tb_flush(CPU(cpu));
  721. }
  722. }
  723. unmap_out:
  724. cpu_physical_memory_unmap(pdst, TARGET_PAGE_SIZE, 1, len);
  725. return ret;
  726. }
  727. #define FLAGS_REGISTER_VPA 0x0000200000000000ULL
  728. #define FLAGS_REGISTER_DTL 0x0000400000000000ULL
  729. #define FLAGS_REGISTER_SLBSHADOW 0x0000600000000000ULL
  730. #define FLAGS_DEREGISTER_VPA 0x0000a00000000000ULL
  731. #define FLAGS_DEREGISTER_DTL 0x0000c00000000000ULL
  732. #define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
  733. static target_ulong register_vpa(PowerPCCPU *cpu, target_ulong vpa)
  734. {
  735. CPUState *cs = CPU(cpu);
  736. CPUPPCState *env = &cpu->env;
  737. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  738. uint16_t size;
  739. uint8_t tmp;
  740. if (vpa == 0) {
  741. hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
  742. return H_HARDWARE;
  743. }
  744. if (vpa % env->dcache_line_size) {
  745. return H_PARAMETER;
  746. }
  747. /* FIXME: bounds check the address */
  748. size = lduw_be_phys(cs->as, vpa + 0x4);
  749. if (size < VPA_MIN_SIZE) {
  750. return H_PARAMETER;
  751. }
  752. /* VPA is not allowed to cross a page boundary */
  753. if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
  754. return H_PARAMETER;
  755. }
  756. spapr_cpu->vpa_addr = vpa;
  757. tmp = ldub_phys(cs->as, spapr_cpu->vpa_addr + VPA_SHARED_PROC_OFFSET);
  758. tmp |= VPA_SHARED_PROC_VAL;
  759. stb_phys(cs->as, spapr_cpu->vpa_addr + VPA_SHARED_PROC_OFFSET, tmp);
  760. return H_SUCCESS;
  761. }
  762. static target_ulong deregister_vpa(PowerPCCPU *cpu, target_ulong vpa)
  763. {
  764. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  765. if (spapr_cpu->slb_shadow_addr) {
  766. return H_RESOURCE;
  767. }
  768. if (spapr_cpu->dtl_addr) {
  769. return H_RESOURCE;
  770. }
  771. spapr_cpu->vpa_addr = 0;
  772. return H_SUCCESS;
  773. }
  774. static target_ulong register_slb_shadow(PowerPCCPU *cpu, target_ulong addr)
  775. {
  776. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  777. uint32_t size;
  778. if (addr == 0) {
  779. hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
  780. return H_HARDWARE;
  781. }
  782. size = ldl_be_phys(CPU(cpu)->as, addr + 0x4);
  783. if (size < 0x8) {
  784. return H_PARAMETER;
  785. }
  786. if ((addr / 4096) != ((addr + size - 1) / 4096)) {
  787. return H_PARAMETER;
  788. }
  789. if (!spapr_cpu->vpa_addr) {
  790. return H_RESOURCE;
  791. }
  792. spapr_cpu->slb_shadow_addr = addr;
  793. spapr_cpu->slb_shadow_size = size;
  794. return H_SUCCESS;
  795. }
  796. static target_ulong deregister_slb_shadow(PowerPCCPU *cpu, target_ulong addr)
  797. {
  798. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  799. spapr_cpu->slb_shadow_addr = 0;
  800. spapr_cpu->slb_shadow_size = 0;
  801. return H_SUCCESS;
  802. }
  803. static target_ulong register_dtl(PowerPCCPU *cpu, target_ulong addr)
  804. {
  805. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  806. uint32_t size;
  807. if (addr == 0) {
  808. hcall_dprintf("Can't cope with DTL at logical 0\n");
  809. return H_HARDWARE;
  810. }
  811. size = ldl_be_phys(CPU(cpu)->as, addr + 0x4);
  812. if (size < 48) {
  813. return H_PARAMETER;
  814. }
  815. if (!spapr_cpu->vpa_addr) {
  816. return H_RESOURCE;
  817. }
  818. spapr_cpu->dtl_addr = addr;
  819. spapr_cpu->dtl_size = size;
  820. return H_SUCCESS;
  821. }
  822. static target_ulong deregister_dtl(PowerPCCPU *cpu, target_ulong addr)
  823. {
  824. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  825. spapr_cpu->dtl_addr = 0;
  826. spapr_cpu->dtl_size = 0;
  827. return H_SUCCESS;
  828. }
  829. static target_ulong h_register_vpa(PowerPCCPU *cpu, SpaprMachineState *spapr,
  830. target_ulong opcode, target_ulong *args)
  831. {
  832. target_ulong flags = args[0];
  833. target_ulong procno = args[1];
  834. target_ulong vpa = args[2];
  835. target_ulong ret = H_PARAMETER;
  836. PowerPCCPU *tcpu;
  837. tcpu = spapr_find_cpu(procno);
  838. if (!tcpu) {
  839. return H_PARAMETER;
  840. }
  841. switch (flags) {
  842. case FLAGS_REGISTER_VPA:
  843. ret = register_vpa(tcpu, vpa);
  844. break;
  845. case FLAGS_DEREGISTER_VPA:
  846. ret = deregister_vpa(tcpu, vpa);
  847. break;
  848. case FLAGS_REGISTER_SLBSHADOW:
  849. ret = register_slb_shadow(tcpu, vpa);
  850. break;
  851. case FLAGS_DEREGISTER_SLBSHADOW:
  852. ret = deregister_slb_shadow(tcpu, vpa);
  853. break;
  854. case FLAGS_REGISTER_DTL:
  855. ret = register_dtl(tcpu, vpa);
  856. break;
  857. case FLAGS_DEREGISTER_DTL:
  858. ret = deregister_dtl(tcpu, vpa);
  859. break;
  860. }
  861. return ret;
  862. }
  863. static target_ulong h_cede(PowerPCCPU *cpu, SpaprMachineState *spapr,
  864. target_ulong opcode, target_ulong *args)
  865. {
  866. CPUPPCState *env = &cpu->env;
  867. CPUState *cs = CPU(cpu);
  868. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  869. env->msr |= (1ULL << MSR_EE);
  870. hreg_compute_hflags(env);
  871. if (spapr_cpu->prod) {
  872. spapr_cpu->prod = false;
  873. return H_SUCCESS;
  874. }
  875. if (!cpu_has_work(cs)) {
  876. cs->halted = 1;
  877. cs->exception_index = EXCP_HLT;
  878. cs->exit_request = 1;
  879. }
  880. return H_SUCCESS;
  881. }
  882. /*
  883. * Confer to self, aka join. Cede could use the same pattern as well, if
  884. * EXCP_HLT can be changed to ECXP_HALTED.
  885. */
  886. static target_ulong h_confer_self(PowerPCCPU *cpu)
  887. {
  888. CPUState *cs = CPU(cpu);
  889. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  890. if (spapr_cpu->prod) {
  891. spapr_cpu->prod = false;
  892. return H_SUCCESS;
  893. }
  894. cs->halted = 1;
  895. cs->exception_index = EXCP_HALTED;
  896. cs->exit_request = 1;
  897. return H_SUCCESS;
  898. }
  899. static target_ulong h_join(PowerPCCPU *cpu, SpaprMachineState *spapr,
  900. target_ulong opcode, target_ulong *args)
  901. {
  902. CPUPPCState *env = &cpu->env;
  903. CPUState *cs;
  904. bool last_unjoined = true;
  905. if (env->msr & (1ULL << MSR_EE)) {
  906. return H_BAD_MODE;
  907. }
  908. /*
  909. * Must not join the last CPU running. Interestingly, no such restriction
  910. * for H_CONFER-to-self, but that is probably not intended to be used
  911. * when H_JOIN is available.
  912. */
  913. CPU_FOREACH(cs) {
  914. PowerPCCPU *c = POWERPC_CPU(cs);
  915. CPUPPCState *e = &c->env;
  916. if (c == cpu) {
  917. continue;
  918. }
  919. /* Don't have a way to indicate joined, so use halted && MSR[EE]=0 */
  920. if (!cs->halted || (e->msr & (1ULL << MSR_EE))) {
  921. last_unjoined = false;
  922. break;
  923. }
  924. }
  925. if (last_unjoined) {
  926. return H_CONTINUE;
  927. }
  928. return h_confer_self(cpu);
  929. }
  930. static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr,
  931. target_ulong opcode, target_ulong *args)
  932. {
  933. target_long target = args[0];
  934. uint32_t dispatch = args[1];
  935. CPUState *cs = CPU(cpu);
  936. SpaprCpuState *spapr_cpu;
  937. /*
  938. * -1 means confer to all other CPUs without dispatch counter check,
  939. * otherwise it's a targeted confer.
  940. */
  941. if (target != -1) {
  942. PowerPCCPU *target_cpu = spapr_find_cpu(target);
  943. uint32_t target_dispatch;
  944. if (!target_cpu) {
  945. return H_PARAMETER;
  946. }
  947. /*
  948. * target == self is a special case, we wait until prodded, without
  949. * dispatch counter check.
  950. */
  951. if (cpu == target_cpu) {
  952. return h_confer_self(cpu);
  953. }
  954. spapr_cpu = spapr_cpu_state(target_cpu);
  955. if (!spapr_cpu->vpa_addr || ((dispatch & 1) == 0)) {
  956. return H_SUCCESS;
  957. }
  958. target_dispatch = ldl_be_phys(cs->as,
  959. spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
  960. if (target_dispatch != dispatch) {
  961. return H_SUCCESS;
  962. }
  963. /*
  964. * The targeted confer does not do anything special beyond yielding
  965. * the current vCPU, but even this should be better than nothing.
  966. * At least for single-threaded tcg, it gives the target a chance to
  967. * run before we run again. Multi-threaded tcg does not really do
  968. * anything with EXCP_YIELD yet.
  969. */
  970. }
  971. cs->exception_index = EXCP_YIELD;
  972. cs->exit_request = 1;
  973. cpu_loop_exit(cs);
  974. return H_SUCCESS;
  975. }
  976. static target_ulong h_prod(PowerPCCPU *cpu, SpaprMachineState *spapr,
  977. target_ulong opcode, target_ulong *args)
  978. {
  979. target_long target = args[0];
  980. PowerPCCPU *tcpu;
  981. CPUState *cs;
  982. SpaprCpuState *spapr_cpu;
  983. tcpu = spapr_find_cpu(target);
  984. cs = CPU(tcpu);
  985. if (!cs) {
  986. return H_PARAMETER;
  987. }
  988. spapr_cpu = spapr_cpu_state(tcpu);
  989. spapr_cpu->prod = true;
  990. cs->halted = 0;
  991. qemu_cpu_kick(cs);
  992. return H_SUCCESS;
  993. }
  994. static target_ulong h_rtas(PowerPCCPU *cpu, SpaprMachineState *spapr,
  995. target_ulong opcode, target_ulong *args)
  996. {
  997. target_ulong rtas_r3 = args[0];
  998. uint32_t token = rtas_ld(rtas_r3, 0);
  999. uint32_t nargs = rtas_ld(rtas_r3, 1);
  1000. uint32_t nret = rtas_ld(rtas_r3, 2);
  1001. return spapr_rtas_call(cpu, spapr, token, nargs, rtas_r3 + 12,
  1002. nret, rtas_r3 + 12 + 4*nargs);
  1003. }
  1004. static target_ulong h_logical_load(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1005. target_ulong opcode, target_ulong *args)
  1006. {
  1007. CPUState *cs = CPU(cpu);
  1008. target_ulong size = args[0];
  1009. target_ulong addr = args[1];
  1010. switch (size) {
  1011. case 1:
  1012. args[0] = ldub_phys(cs->as, addr);
  1013. return H_SUCCESS;
  1014. case 2:
  1015. args[0] = lduw_phys(cs->as, addr);
  1016. return H_SUCCESS;
  1017. case 4:
  1018. args[0] = ldl_phys(cs->as, addr);
  1019. return H_SUCCESS;
  1020. case 8:
  1021. args[0] = ldq_phys(cs->as, addr);
  1022. return H_SUCCESS;
  1023. }
  1024. return H_PARAMETER;
  1025. }
  1026. static target_ulong h_logical_store(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1027. target_ulong opcode, target_ulong *args)
  1028. {
  1029. CPUState *cs = CPU(cpu);
  1030. target_ulong size = args[0];
  1031. target_ulong addr = args[1];
  1032. target_ulong val = args[2];
  1033. switch (size) {
  1034. case 1:
  1035. stb_phys(cs->as, addr, val);
  1036. return H_SUCCESS;
  1037. case 2:
  1038. stw_phys(cs->as, addr, val);
  1039. return H_SUCCESS;
  1040. case 4:
  1041. stl_phys(cs->as, addr, val);
  1042. return H_SUCCESS;
  1043. case 8:
  1044. stq_phys(cs->as, addr, val);
  1045. return H_SUCCESS;
  1046. }
  1047. return H_PARAMETER;
  1048. }
  1049. static target_ulong h_logical_memop(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1050. target_ulong opcode, target_ulong *args)
  1051. {
  1052. CPUState *cs = CPU(cpu);
  1053. target_ulong dst = args[0]; /* Destination address */
  1054. target_ulong src = args[1]; /* Source address */
  1055. target_ulong esize = args[2]; /* Element size (0=1,1=2,2=4,3=8) */
  1056. target_ulong count = args[3]; /* Element count */
  1057. target_ulong op = args[4]; /* 0 = copy, 1 = invert */
  1058. uint64_t tmp;
  1059. unsigned int mask = (1 << esize) - 1;
  1060. int step = 1 << esize;
  1061. if (count > 0x80000000) {
  1062. return H_PARAMETER;
  1063. }
  1064. if ((dst & mask) || (src & mask) || (op > 1)) {
  1065. return H_PARAMETER;
  1066. }
  1067. if (dst >= src && dst < (src + (count << esize))) {
  1068. dst = dst + ((count - 1) << esize);
  1069. src = src + ((count - 1) << esize);
  1070. step = -step;
  1071. }
  1072. while (count--) {
  1073. switch (esize) {
  1074. case 0:
  1075. tmp = ldub_phys(cs->as, src);
  1076. break;
  1077. case 1:
  1078. tmp = lduw_phys(cs->as, src);
  1079. break;
  1080. case 2:
  1081. tmp = ldl_phys(cs->as, src);
  1082. break;
  1083. case 3:
  1084. tmp = ldq_phys(cs->as, src);
  1085. break;
  1086. default:
  1087. return H_PARAMETER;
  1088. }
  1089. if (op == 1) {
  1090. tmp = ~tmp;
  1091. }
  1092. switch (esize) {
  1093. case 0:
  1094. stb_phys(cs->as, dst, tmp);
  1095. break;
  1096. case 1:
  1097. stw_phys(cs->as, dst, tmp);
  1098. break;
  1099. case 2:
  1100. stl_phys(cs->as, dst, tmp);
  1101. break;
  1102. case 3:
  1103. stq_phys(cs->as, dst, tmp);
  1104. break;
  1105. }
  1106. dst = dst + step;
  1107. src = src + step;
  1108. }
  1109. return H_SUCCESS;
  1110. }
  1111. static target_ulong h_logical_icbi(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1112. target_ulong opcode, target_ulong *args)
  1113. {
  1114. /* Nothing to do on emulation, KVM will trap this in the kernel */
  1115. return H_SUCCESS;
  1116. }
  1117. static target_ulong h_logical_dcbf(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1118. target_ulong opcode, target_ulong *args)
  1119. {
  1120. /* Nothing to do on emulation, KVM will trap this in the kernel */
  1121. return H_SUCCESS;
  1122. }
  1123. static target_ulong h_set_mode_resource_le(PowerPCCPU *cpu,
  1124. target_ulong mflags,
  1125. target_ulong value1,
  1126. target_ulong value2)
  1127. {
  1128. if (value1) {
  1129. return H_P3;
  1130. }
  1131. if (value2) {
  1132. return H_P4;
  1133. }
  1134. switch (mflags) {
  1135. case H_SET_MODE_ENDIAN_BIG:
  1136. spapr_set_all_lpcrs(0, LPCR_ILE);
  1137. spapr_pci_switch_vga(true);
  1138. return H_SUCCESS;
  1139. case H_SET_MODE_ENDIAN_LITTLE:
  1140. spapr_set_all_lpcrs(LPCR_ILE, LPCR_ILE);
  1141. spapr_pci_switch_vga(false);
  1142. return H_SUCCESS;
  1143. }
  1144. return H_UNSUPPORTED_FLAG;
  1145. }
  1146. static target_ulong h_set_mode_resource_addr_trans_mode(PowerPCCPU *cpu,
  1147. target_ulong mflags,
  1148. target_ulong value1,
  1149. target_ulong value2)
  1150. {
  1151. PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
  1152. if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
  1153. return H_P2;
  1154. }
  1155. if (value1) {
  1156. return H_P3;
  1157. }
  1158. if (value2) {
  1159. return H_P4;
  1160. }
  1161. if (mflags == AIL_RESERVED) {
  1162. return H_UNSUPPORTED_FLAG;
  1163. }
  1164. spapr_set_all_lpcrs(mflags << LPCR_AIL_SHIFT, LPCR_AIL);
  1165. return H_SUCCESS;
  1166. }
  1167. static target_ulong h_set_mode(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1168. target_ulong opcode, target_ulong *args)
  1169. {
  1170. target_ulong resource = args[1];
  1171. target_ulong ret = H_P2;
  1172. switch (resource) {
  1173. case H_SET_MODE_RESOURCE_LE:
  1174. ret = h_set_mode_resource_le(cpu, args[0], args[2], args[3]);
  1175. break;
  1176. case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
  1177. ret = h_set_mode_resource_addr_trans_mode(cpu, args[0],
  1178. args[2], args[3]);
  1179. break;
  1180. }
  1181. return ret;
  1182. }
  1183. static target_ulong h_clean_slb(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1184. target_ulong opcode, target_ulong *args)
  1185. {
  1186. qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
  1187. opcode, " (H_CLEAN_SLB)");
  1188. return H_FUNCTION;
  1189. }
  1190. static target_ulong h_invalidate_pid(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1191. target_ulong opcode, target_ulong *args)
  1192. {
  1193. qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x"TARGET_FMT_lx"%s\n",
  1194. opcode, " (H_INVALIDATE_PID)");
  1195. return H_FUNCTION;
  1196. }
  1197. static void spapr_check_setup_free_hpt(SpaprMachineState *spapr,
  1198. uint64_t patbe_old, uint64_t patbe_new)
  1199. {
  1200. /*
  1201. * We have 4 Options:
  1202. * HASH->HASH || RADIX->RADIX || NOTHING->RADIX : Do Nothing
  1203. * HASH->RADIX : Free HPT
  1204. * RADIX->HASH : Allocate HPT
  1205. * NOTHING->HASH : Allocate HPT
  1206. * Note: NOTHING implies the case where we said the guest could choose
  1207. * later and so assumed radix and now it's called H_REG_PROC_TBL
  1208. */
  1209. if ((patbe_old & PATE1_GR) == (patbe_new & PATE1_GR)) {
  1210. /* We assume RADIX, so this catches all the "Do Nothing" cases */
  1211. } else if (!(patbe_old & PATE1_GR)) {
  1212. /* HASH->RADIX : Free HPT */
  1213. spapr_free_hpt(spapr);
  1214. } else if (!(patbe_new & PATE1_GR)) {
  1215. /* RADIX->HASH || NOTHING->HASH : Allocate HPT */
  1216. spapr_setup_hpt_and_vrma(spapr);
  1217. }
  1218. return;
  1219. }
  1220. #define FLAGS_MASK 0x01FULL
  1221. #define FLAG_MODIFY 0x10
  1222. #define FLAG_REGISTER 0x08
  1223. #define FLAG_RADIX 0x04
  1224. #define FLAG_HASH_PROC_TBL 0x02
  1225. #define FLAG_GTSE 0x01
  1226. static target_ulong h_register_process_table(PowerPCCPU *cpu,
  1227. SpaprMachineState *spapr,
  1228. target_ulong opcode,
  1229. target_ulong *args)
  1230. {
  1231. target_ulong flags = args[0];
  1232. target_ulong proc_tbl = args[1];
  1233. target_ulong page_size = args[2];
  1234. target_ulong table_size = args[3];
  1235. target_ulong update_lpcr = 0;
  1236. uint64_t cproc;
  1237. if (flags & ~FLAGS_MASK) { /* Check no reserved bits are set */
  1238. return H_PARAMETER;
  1239. }
  1240. if (flags & FLAG_MODIFY) {
  1241. if (flags & FLAG_REGISTER) {
  1242. if (flags & FLAG_RADIX) { /* Register new RADIX process table */
  1243. if (proc_tbl & 0xfff || proc_tbl >> 60) {
  1244. return H_P2;
  1245. } else if (page_size) {
  1246. return H_P3;
  1247. } else if (table_size > 24) {
  1248. return H_P4;
  1249. }
  1250. cproc = PATE1_GR | proc_tbl | table_size;
  1251. } else { /* Register new HPT process table */
  1252. if (flags & FLAG_HASH_PROC_TBL) { /* Hash with Segment Tables */
  1253. /* TODO - Not Supported */
  1254. /* Technically caused by flag bits => H_PARAMETER */
  1255. return H_PARAMETER;
  1256. } else { /* Hash with SLB */
  1257. if (proc_tbl >> 38) {
  1258. return H_P2;
  1259. } else if (page_size & ~0x7) {
  1260. return H_P3;
  1261. } else if (table_size > 24) {
  1262. return H_P4;
  1263. }
  1264. }
  1265. cproc = (proc_tbl << 25) | page_size << 5 | table_size;
  1266. }
  1267. } else { /* Deregister current process table */
  1268. /*
  1269. * Set to benign value: (current GR) | 0. This allows
  1270. * deregistration in KVM to succeed even if the radix bit
  1271. * in flags doesn't match the radix bit in the old PATE.
  1272. */
  1273. cproc = spapr->patb_entry & PATE1_GR;
  1274. }
  1275. } else { /* Maintain current registration */
  1276. if (!(flags & FLAG_RADIX) != !(spapr->patb_entry & PATE1_GR)) {
  1277. /* Technically caused by flag bits => H_PARAMETER */
  1278. return H_PARAMETER; /* Existing Process Table Mismatch */
  1279. }
  1280. cproc = spapr->patb_entry;
  1281. }
  1282. /* Check if we need to setup OR free the hpt */
  1283. spapr_check_setup_free_hpt(spapr, spapr->patb_entry, cproc);
  1284. spapr->patb_entry = cproc; /* Save new process table */
  1285. /* Update the UPRT, HR and GTSE bits in the LPCR for all cpus */
  1286. if (flags & FLAG_RADIX) /* Radix must use process tables, also set HR */
  1287. update_lpcr |= (LPCR_UPRT | LPCR_HR);
  1288. else if (flags & FLAG_HASH_PROC_TBL) /* Hash with process tables */
  1289. update_lpcr |= LPCR_UPRT;
  1290. if (flags & FLAG_GTSE) /* Guest translation shootdown enable */
  1291. update_lpcr |= LPCR_GTSE;
  1292. spapr_set_all_lpcrs(update_lpcr, LPCR_UPRT | LPCR_HR | LPCR_GTSE);
  1293. if (kvm_enabled()) {
  1294. return kvmppc_configure_v3_mmu(cpu, flags & FLAG_RADIX,
  1295. flags & FLAG_GTSE, cproc);
  1296. }
  1297. return H_SUCCESS;
  1298. }
  1299. #define H_SIGNAL_SYS_RESET_ALL -1
  1300. #define H_SIGNAL_SYS_RESET_ALLBUTSELF -2
  1301. static target_ulong h_signal_sys_reset(PowerPCCPU *cpu,
  1302. SpaprMachineState *spapr,
  1303. target_ulong opcode, target_ulong *args)
  1304. {
  1305. target_long target = args[0];
  1306. CPUState *cs;
  1307. if (target < 0) {
  1308. /* Broadcast */
  1309. if (target < H_SIGNAL_SYS_RESET_ALLBUTSELF) {
  1310. return H_PARAMETER;
  1311. }
  1312. CPU_FOREACH(cs) {
  1313. PowerPCCPU *c = POWERPC_CPU(cs);
  1314. if (target == H_SIGNAL_SYS_RESET_ALLBUTSELF) {
  1315. if (c == cpu) {
  1316. continue;
  1317. }
  1318. }
  1319. run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
  1320. }
  1321. return H_SUCCESS;
  1322. } else {
  1323. /* Unicast */
  1324. cs = CPU(spapr_find_cpu(target));
  1325. if (cs) {
  1326. run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
  1327. return H_SUCCESS;
  1328. }
  1329. return H_PARAMETER;
  1330. }
  1331. }
  1332. static uint32_t cas_check_pvr(SpaprMachineState *spapr, PowerPCCPU *cpu,
  1333. target_ulong *addr, bool *raw_mode_supported,
  1334. Error **errp)
  1335. {
  1336. bool explicit_match = false; /* Matched the CPU's real PVR */
  1337. uint32_t max_compat = spapr->max_compat_pvr;
  1338. uint32_t best_compat = 0;
  1339. int i;
  1340. /*
  1341. * We scan the supplied table of PVRs looking for two things
  1342. * 1. Is our real CPU PVR in the list?
  1343. * 2. What's the "best" listed logical PVR
  1344. */
  1345. for (i = 0; i < 512; ++i) {
  1346. uint32_t pvr, pvr_mask;
  1347. pvr_mask = ldl_be_phys(&address_space_memory, *addr);
  1348. pvr = ldl_be_phys(&address_space_memory, *addr + 4);
  1349. *addr += 8;
  1350. if (~pvr_mask & pvr) {
  1351. break; /* Terminator record */
  1352. }
  1353. if ((cpu->env.spr[SPR_PVR] & pvr_mask) == (pvr & pvr_mask)) {
  1354. explicit_match = true;
  1355. } else {
  1356. if (ppc_check_compat(cpu, pvr, best_compat, max_compat)) {
  1357. best_compat = pvr;
  1358. }
  1359. }
  1360. }
  1361. if ((best_compat == 0) && (!explicit_match || max_compat)) {
  1362. /* We couldn't find a suitable compatibility mode, and either
  1363. * the guest doesn't support "raw" mode for this CPU, or raw
  1364. * mode is disabled because a maximum compat mode is set */
  1365. error_setg(errp, "Couldn't negotiate a suitable PVR during CAS");
  1366. return 0;
  1367. }
  1368. *raw_mode_supported = explicit_match;
  1369. /* Parsing finished */
  1370. trace_spapr_cas_pvr(cpu->compat_pvr, explicit_match, best_compat);
  1371. return best_compat;
  1372. }
  1373. static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
  1374. SpaprMachineState *spapr,
  1375. target_ulong opcode,
  1376. target_ulong *args)
  1377. {
  1378. /* Working address in data buffer */
  1379. target_ulong addr = ppc64_phys_to_real(args[0]);
  1380. target_ulong ov_table;
  1381. uint32_t cas_pvr;
  1382. SpaprOptionVector *ov1_guest, *ov5_guest, *ov5_cas_old, *ov5_updates;
  1383. bool guest_radix;
  1384. Error *local_err = NULL;
  1385. bool raw_mode_supported = false;
  1386. bool guest_xive;
  1387. cas_pvr = cas_check_pvr(spapr, cpu, &addr, &raw_mode_supported, &local_err);
  1388. if (local_err) {
  1389. error_report_err(local_err);
  1390. return H_HARDWARE;
  1391. }
  1392. /* Update CPUs */
  1393. if (cpu->compat_pvr != cas_pvr) {
  1394. ppc_set_compat_all(cas_pvr, &local_err);
  1395. if (local_err) {
  1396. /* We fail to set compat mode (likely because running with KVM PR),
  1397. * but maybe we can fallback to raw mode if the guest supports it.
  1398. */
  1399. if (!raw_mode_supported) {
  1400. error_report_err(local_err);
  1401. return H_HARDWARE;
  1402. }
  1403. error_free(local_err);
  1404. local_err = NULL;
  1405. }
  1406. }
  1407. /* For the future use: here @ov_table points to the first option vector */
  1408. ov_table = addr;
  1409. ov1_guest = spapr_ovec_parse_vector(ov_table, 1);
  1410. ov5_guest = spapr_ovec_parse_vector(ov_table, 5);
  1411. if (spapr_ovec_test(ov5_guest, OV5_MMU_BOTH)) {
  1412. error_report("guest requested hash and radix MMU, which is invalid.");
  1413. exit(EXIT_FAILURE);
  1414. }
  1415. if (spapr_ovec_test(ov5_guest, OV5_XIVE_BOTH)) {
  1416. error_report("guest requested an invalid interrupt mode");
  1417. exit(EXIT_FAILURE);
  1418. }
  1419. /* The radix/hash bit in byte 24 requires special handling: */
  1420. guest_radix = spapr_ovec_test(ov5_guest, OV5_MMU_RADIX_300);
  1421. spapr_ovec_clear(ov5_guest, OV5_MMU_RADIX_300);
  1422. guest_xive = spapr_ovec_test(ov5_guest, OV5_XIVE_EXPLOIT);
  1423. /*
  1424. * HPT resizing is a bit of a special case, because when enabled
  1425. * we assume an HPT guest will support it until it says it
  1426. * doesn't, instead of assuming it won't support it until it says
  1427. * it does. Strictly speaking that approach could break for
  1428. * guests which don't make a CAS call, but those are so old we
  1429. * don't care about them. Without that assumption we'd have to
  1430. * make at least a temporary allocation of an HPT sized for max
  1431. * memory, which could be impossibly difficult under KVM HV if
  1432. * maxram is large.
  1433. */
  1434. if (!guest_radix && !spapr_ovec_test(ov5_guest, OV5_HPT_RESIZE)) {
  1435. int maxshift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
  1436. if (spapr->resize_hpt == SPAPR_RESIZE_HPT_REQUIRED) {
  1437. error_report(
  1438. "h_client_architecture_support: Guest doesn't support HPT resizing, but resize-hpt=required");
  1439. exit(1);
  1440. }
  1441. if (spapr->htab_shift < maxshift) {
  1442. /* Guest doesn't know about HPT resizing, so we
  1443. * pre-emptively resize for the maximum permitted RAM. At
  1444. * the point this is called, nothing should have been
  1445. * entered into the existing HPT */
  1446. spapr_reallocate_hpt(spapr, maxshift, &error_fatal);
  1447. push_sregs_to_kvm_pr(spapr);
  1448. }
  1449. }
  1450. /* NOTE: there are actually a number of ov5 bits where input from the
  1451. * guest is always zero, and the platform/QEMU enables them independently
  1452. * of guest input. To model these properly we'd want some sort of mask,
  1453. * but since they only currently apply to memory migration as defined
  1454. * by LoPAPR 1.1, 14.5.4.8, which QEMU doesn't implement, we don't need
  1455. * to worry about this for now.
  1456. */
  1457. ov5_cas_old = spapr_ovec_clone(spapr->ov5_cas);
  1458. /* also clear the radix/hash bit from the current ov5_cas bits to
  1459. * be in sync with the newly ov5 bits. Else the radix bit will be
  1460. * seen as being removed and this will generate a reset loop
  1461. */
  1462. spapr_ovec_clear(ov5_cas_old, OV5_MMU_RADIX_300);
  1463. /* full range of negotiated ov5 capabilities */
  1464. spapr_ovec_intersect(spapr->ov5_cas, spapr->ov5, ov5_guest);
  1465. spapr_ovec_cleanup(ov5_guest);
  1466. /* capabilities that have been added since CAS-generated guest reset.
  1467. * if capabilities have since been removed, generate another reset
  1468. */
  1469. ov5_updates = spapr_ovec_new();
  1470. spapr->cas_reboot = spapr_ovec_diff(ov5_updates,
  1471. ov5_cas_old, spapr->ov5_cas);
  1472. spapr_ovec_cleanup(ov5_cas_old);
  1473. /* Now that processing is finished, set the radix/hash bit for the
  1474. * guest if it requested a valid mode; otherwise terminate the boot. */
  1475. if (guest_radix) {
  1476. if (kvm_enabled() && !kvmppc_has_cap_mmu_radix()) {
  1477. error_report("Guest requested unavailable MMU mode (radix).");
  1478. exit(EXIT_FAILURE);
  1479. }
  1480. spapr_ovec_set(spapr->ov5_cas, OV5_MMU_RADIX_300);
  1481. } else {
  1482. if (kvm_enabled() && kvmppc_has_cap_mmu_radix()
  1483. && !kvmppc_has_cap_mmu_hash_v3()) {
  1484. error_report("Guest requested unavailable MMU mode (hash).");
  1485. exit(EXIT_FAILURE);
  1486. }
  1487. }
  1488. spapr->cas_pre_isa3_guest = !spapr_ovec_test(ov1_guest, OV1_PPC_3_00);
  1489. spapr_ovec_cleanup(ov1_guest);
  1490. if (!spapr->cas_reboot) {
  1491. /* If spapr_machine_reset() did not set up a HPT but one is necessary
  1492. * (because the guest isn't going to use radix) then set it up here. */
  1493. if ((spapr->patb_entry & PATE1_GR) && !guest_radix) {
  1494. /* legacy hash or new hash: */
  1495. spapr_setup_hpt_and_vrma(spapr);
  1496. }
  1497. spapr->cas_reboot =
  1498. (spapr_h_cas_compose_response(spapr, args[1], args[2],
  1499. ov5_updates) != 0);
  1500. }
  1501. /*
  1502. * Ensure the guest asks for an interrupt mode we support; otherwise
  1503. * terminate the boot.
  1504. */
  1505. if (guest_xive) {
  1506. if (!spapr->irq->xive) {
  1507. error_report(
  1508. "Guest requested unavailable interrupt mode (XIVE), try the ic-mode=xive or ic-mode=dual machine property");
  1509. exit(EXIT_FAILURE);
  1510. }
  1511. } else {
  1512. if (!spapr->irq->xics) {
  1513. error_report(
  1514. "Guest requested unavailable interrupt mode (XICS), either don't set the ic-mode machine property or try ic-mode=xics or ic-mode=dual");
  1515. exit(EXIT_FAILURE);
  1516. }
  1517. }
  1518. /*
  1519. * Generate a machine reset when we have an update of the
  1520. * interrupt mode. Only required when the machine supports both
  1521. * modes.
  1522. */
  1523. if (!spapr->cas_reboot) {
  1524. spapr->cas_reboot = spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT)
  1525. && spapr->irq->xics && spapr->irq->xive;
  1526. }
  1527. spapr_ovec_cleanup(ov5_updates);
  1528. if (spapr->cas_reboot) {
  1529. qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
  1530. }
  1531. return H_SUCCESS;
  1532. }
  1533. static target_ulong h_home_node_associativity(PowerPCCPU *cpu,
  1534. SpaprMachineState *spapr,
  1535. target_ulong opcode,
  1536. target_ulong *args)
  1537. {
  1538. target_ulong flags = args[0];
  1539. target_ulong procno = args[1];
  1540. PowerPCCPU *tcpu;
  1541. int idx;
  1542. /* only support procno from H_REGISTER_VPA */
  1543. if (flags != 0x1) {
  1544. return H_FUNCTION;
  1545. }
  1546. tcpu = spapr_find_cpu(procno);
  1547. if (tcpu == NULL) {
  1548. return H_P2;
  1549. }
  1550. /* sequence is the same as in the "ibm,associativity" property */
  1551. idx = 0;
  1552. #define ASSOCIATIVITY(a, b) (((uint64_t)(a) << 32) | \
  1553. ((uint64_t)(b) & 0xffffffff))
  1554. args[idx++] = ASSOCIATIVITY(0, 0);
  1555. args[idx++] = ASSOCIATIVITY(0, tcpu->node_id);
  1556. args[idx++] = ASSOCIATIVITY(procno, -1);
  1557. for ( ; idx < 6; idx++) {
  1558. args[idx] = -1;
  1559. }
  1560. #undef ASSOCIATIVITY
  1561. return H_SUCCESS;
  1562. }
  1563. static target_ulong h_get_cpu_characteristics(PowerPCCPU *cpu,
  1564. SpaprMachineState *spapr,
  1565. target_ulong opcode,
  1566. target_ulong *args)
  1567. {
  1568. uint64_t characteristics = H_CPU_CHAR_HON_BRANCH_HINTS &
  1569. ~H_CPU_CHAR_THR_RECONF_TRIG;
  1570. uint64_t behaviour = H_CPU_BEHAV_FAVOUR_SECURITY;
  1571. uint8_t safe_cache = spapr_get_cap(spapr, SPAPR_CAP_CFPC);
  1572. uint8_t safe_bounds_check = spapr_get_cap(spapr, SPAPR_CAP_SBBC);
  1573. uint8_t safe_indirect_branch = spapr_get_cap(spapr, SPAPR_CAP_IBS);
  1574. uint8_t count_cache_flush_assist = spapr_get_cap(spapr,
  1575. SPAPR_CAP_CCF_ASSIST);
  1576. switch (safe_cache) {
  1577. case SPAPR_CAP_WORKAROUND:
  1578. characteristics |= H_CPU_CHAR_L1D_FLUSH_ORI30;
  1579. characteristics |= H_CPU_CHAR_L1D_FLUSH_TRIG2;
  1580. characteristics |= H_CPU_CHAR_L1D_THREAD_PRIV;
  1581. behaviour |= H_CPU_BEHAV_L1D_FLUSH_PR;
  1582. break;
  1583. case SPAPR_CAP_FIXED:
  1584. break;
  1585. default: /* broken */
  1586. assert(safe_cache == SPAPR_CAP_BROKEN);
  1587. behaviour |= H_CPU_BEHAV_L1D_FLUSH_PR;
  1588. break;
  1589. }
  1590. switch (safe_bounds_check) {
  1591. case SPAPR_CAP_WORKAROUND:
  1592. characteristics |= H_CPU_CHAR_SPEC_BAR_ORI31;
  1593. behaviour |= H_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
  1594. break;
  1595. case SPAPR_CAP_FIXED:
  1596. break;
  1597. default: /* broken */
  1598. assert(safe_bounds_check == SPAPR_CAP_BROKEN);
  1599. behaviour |= H_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
  1600. break;
  1601. }
  1602. switch (safe_indirect_branch) {
  1603. case SPAPR_CAP_FIXED_NA:
  1604. break;
  1605. case SPAPR_CAP_FIXED_CCD:
  1606. characteristics |= H_CPU_CHAR_CACHE_COUNT_DIS;
  1607. break;
  1608. case SPAPR_CAP_FIXED_IBS:
  1609. characteristics |= H_CPU_CHAR_BCCTRL_SERIALISED;
  1610. break;
  1611. case SPAPR_CAP_WORKAROUND:
  1612. behaviour |= H_CPU_BEHAV_FLUSH_COUNT_CACHE;
  1613. if (count_cache_flush_assist) {
  1614. characteristics |= H_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  1615. }
  1616. break;
  1617. default: /* broken */
  1618. assert(safe_indirect_branch == SPAPR_CAP_BROKEN);
  1619. break;
  1620. }
  1621. args[0] = characteristics;
  1622. args[1] = behaviour;
  1623. return H_SUCCESS;
  1624. }
  1625. static target_ulong h_update_dt(PowerPCCPU *cpu, SpaprMachineState *spapr,
  1626. target_ulong opcode, target_ulong *args)
  1627. {
  1628. target_ulong dt = ppc64_phys_to_real(args[0]);
  1629. struct fdt_header hdr = { 0 };
  1630. unsigned cb;
  1631. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  1632. void *fdt;
  1633. cpu_physical_memory_read(dt, &hdr, sizeof(hdr));
  1634. cb = fdt32_to_cpu(hdr.totalsize);
  1635. if (!smc->update_dt_enabled) {
  1636. return H_SUCCESS;
  1637. }
  1638. /* Check that the fdt did not grow out of proportion */
  1639. if (cb > spapr->fdt_initial_size * 2) {
  1640. trace_spapr_update_dt_failed_size(spapr->fdt_initial_size, cb,
  1641. fdt32_to_cpu(hdr.magic));
  1642. return H_PARAMETER;
  1643. }
  1644. fdt = g_malloc0(cb);
  1645. cpu_physical_memory_read(dt, fdt, cb);
  1646. /* Check the fdt consistency */
  1647. if (fdt_check_full(fdt, cb)) {
  1648. trace_spapr_update_dt_failed_check(spapr->fdt_initial_size, cb,
  1649. fdt32_to_cpu(hdr.magic));
  1650. return H_PARAMETER;
  1651. }
  1652. g_free(spapr->fdt_blob);
  1653. spapr->fdt_size = cb;
  1654. spapr->fdt_blob = fdt;
  1655. trace_spapr_update_dt(cb);
  1656. return H_SUCCESS;
  1657. }
  1658. static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
  1659. static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
  1660. static spapr_hcall_fn svm_hypercall_table[(SVM_HCALL_MAX - SVM_HCALL_BASE) / 4 + 1];
  1661. void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
  1662. {
  1663. spapr_hcall_fn *slot;
  1664. if (opcode <= MAX_HCALL_OPCODE) {
  1665. assert((opcode & 0x3) == 0);
  1666. slot = &papr_hypercall_table[opcode / 4];
  1667. } else if (opcode >= SVM_HCALL_BASE && opcode <= SVM_HCALL_MAX) {
  1668. /* we only have SVM-related hcall numbers assigned in multiples of 4 */
  1669. assert((opcode & 0x3) == 0);
  1670. slot = &svm_hypercall_table[(opcode - SVM_HCALL_BASE) / 4];
  1671. } else {
  1672. assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
  1673. slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
  1674. }
  1675. assert(!(*slot));
  1676. *slot = fn;
  1677. }
  1678. target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
  1679. target_ulong *args)
  1680. {
  1681. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  1682. if ((opcode <= MAX_HCALL_OPCODE)
  1683. && ((opcode & 0x3) == 0)) {
  1684. spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
  1685. if (fn) {
  1686. return fn(cpu, spapr, opcode, args);
  1687. }
  1688. } else if ((opcode >= SVM_HCALL_BASE) &&
  1689. (opcode <= SVM_HCALL_MAX)) {
  1690. spapr_hcall_fn fn = svm_hypercall_table[(opcode - SVM_HCALL_BASE) / 4];
  1691. if (fn) {
  1692. return fn(cpu, spapr, opcode, args);
  1693. }
  1694. } else if ((opcode >= KVMPPC_HCALL_BASE) &&
  1695. (opcode <= KVMPPC_HCALL_MAX)) {
  1696. spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
  1697. if (fn) {
  1698. return fn(cpu, spapr, opcode, args);
  1699. }
  1700. }
  1701. qemu_log_mask(LOG_UNIMP, "Unimplemented SPAPR hcall 0x" TARGET_FMT_lx "\n",
  1702. opcode);
  1703. return H_FUNCTION;
  1704. }
  1705. static void hypercall_register_types(void)
  1706. {
  1707. /* hcall-pft */
  1708. spapr_register_hypercall(H_ENTER, h_enter);
  1709. spapr_register_hypercall(H_REMOVE, h_remove);
  1710. spapr_register_hypercall(H_PROTECT, h_protect);
  1711. spapr_register_hypercall(H_READ, h_read);
  1712. /* hcall-bulk */
  1713. spapr_register_hypercall(H_BULK_REMOVE, h_bulk_remove);
  1714. /* hcall-hpt-resize */
  1715. spapr_register_hypercall(H_RESIZE_HPT_PREPARE, h_resize_hpt_prepare);
  1716. spapr_register_hypercall(H_RESIZE_HPT_COMMIT, h_resize_hpt_commit);
  1717. /* hcall-splpar */
  1718. spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
  1719. spapr_register_hypercall(H_CEDE, h_cede);
  1720. spapr_register_hypercall(H_CONFER, h_confer);
  1721. spapr_register_hypercall(H_PROD, h_prod);
  1722. /* hcall-join */
  1723. spapr_register_hypercall(H_JOIN, h_join);
  1724. spapr_register_hypercall(H_SIGNAL_SYS_RESET, h_signal_sys_reset);
  1725. /* processor register resource access h-calls */
  1726. spapr_register_hypercall(H_SET_SPRG0, h_set_sprg0);
  1727. spapr_register_hypercall(H_SET_DABR, h_set_dabr);
  1728. spapr_register_hypercall(H_SET_XDABR, h_set_xdabr);
  1729. spapr_register_hypercall(H_PAGE_INIT, h_page_init);
  1730. spapr_register_hypercall(H_SET_MODE, h_set_mode);
  1731. /* In Memory Table MMU h-calls */
  1732. spapr_register_hypercall(H_CLEAN_SLB, h_clean_slb);
  1733. spapr_register_hypercall(H_INVALIDATE_PID, h_invalidate_pid);
  1734. spapr_register_hypercall(H_REGISTER_PROC_TBL, h_register_process_table);
  1735. /* hcall-get-cpu-characteristics */
  1736. spapr_register_hypercall(H_GET_CPU_CHARACTERISTICS,
  1737. h_get_cpu_characteristics);
  1738. /* "debugger" hcalls (also used by SLOF). Note: We do -not- differenciate
  1739. * here between the "CI" and the "CACHE" variants, they will use whatever
  1740. * mapping attributes qemu is using. When using KVM, the kernel will
  1741. * enforce the attributes more strongly
  1742. */
  1743. spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
  1744. spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
  1745. spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
  1746. spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
  1747. spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
  1748. spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
  1749. spapr_register_hypercall(KVMPPC_H_LOGICAL_MEMOP, h_logical_memop);
  1750. /* qemu/KVM-PPC specific hcalls */
  1751. spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
  1752. /* ibm,client-architecture-support support */
  1753. spapr_register_hypercall(KVMPPC_H_CAS, h_client_architecture_support);
  1754. spapr_register_hypercall(KVMPPC_H_UPDATE_DT, h_update_dt);
  1755. /* Virtual Processor Home Node */
  1756. spapr_register_hypercall(H_HOME_NODE_ASSOCIATIVITY,
  1757. h_home_node_associativity);
  1758. }
  1759. type_init(hypercall_register_types)