test-i386.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223
  1. /*
  2. * x86 CPU test
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _GNU_SOURCE
  20. #include "compiler.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <inttypes.h>
  25. #include <math.h>
  26. #include <signal.h>
  27. #include <setjmp.h>
  28. #include <errno.h>
  29. #include <sys/ucontext.h>
  30. #include <sys/mman.h>
  31. #include <sys/user.h>
  32. #if !defined(__x86_64__)
  33. #define TEST_VM86
  34. #define TEST_SEGS
  35. #endif
  36. //#define LINUX_VM86_IOPL_FIX
  37. //#define TEST_P4_FLAGS
  38. //#ifdef __SSE__
  39. #if 1
  40. #define TEST_SSE
  41. #define TEST_CMOV 1
  42. #define TEST_FCOMI 1
  43. #else
  44. #undef TEST_SSE
  45. #define TEST_CMOV 1
  46. #define TEST_FCOMI 1
  47. #endif
  48. #if defined(__x86_64__)
  49. #define FMT64X "%016lx"
  50. #define FMTLX "%016lx"
  51. #define X86_64_ONLY(x) x
  52. #else
  53. #define FMT64X "%016" PRIx64
  54. #define FMTLX "%08lx"
  55. #define X86_64_ONLY(x)
  56. #endif
  57. #ifdef TEST_VM86
  58. #include <asm/vm86.h>
  59. #endif
  60. #define xglue(x, y) x ## y
  61. #define glue(x, y) xglue(x, y)
  62. #define stringify(s) tostring(s)
  63. #define tostring(s) #s
  64. #define UNUSED(s) (void)(s)
  65. #define CC_C 0x0001
  66. #define CC_P 0x0004
  67. #define CC_A 0x0010
  68. #define CC_Z 0x0040
  69. #define CC_S 0x0080
  70. #define CC_O 0x0800
  71. #define __init_call __attribute__ ((unused,__section__ ("initcall")))
  72. #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
  73. #if defined(__x86_64__)
  74. static inline long i2l(long v)
  75. {
  76. return v | ((v ^ 0xabcd) << 32);
  77. }
  78. #else
  79. static inline long i2l(long v)
  80. {
  81. return v;
  82. }
  83. #endif
  84. #define OP add
  85. #include "test-i386.h"
  86. #define OP sub
  87. #include "test-i386.h"
  88. #define OP xor
  89. #include "test-i386.h"
  90. #define OP and
  91. #include "test-i386.h"
  92. #define OP or
  93. #include "test-i386.h"
  94. #define OP cmp
  95. #include "test-i386.h"
  96. #define OP adc
  97. #define OP_CC
  98. #include "test-i386.h"
  99. #define OP sbb
  100. #define OP_CC
  101. #include "test-i386.h"
  102. #define OP inc
  103. #define OP_CC
  104. #define OP1
  105. #include "test-i386.h"
  106. #define OP dec
  107. #define OP_CC
  108. #define OP1
  109. #include "test-i386.h"
  110. #define OP neg
  111. #define OP_CC
  112. #define OP1
  113. #include "test-i386.h"
  114. #define OP not
  115. #define OP_CC
  116. #define OP1
  117. #include "test-i386.h"
  118. #undef CC_MASK
  119. #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
  120. #define OP shl
  121. #include "test-i386-shift.h"
  122. #define OP shr
  123. #include "test-i386-shift.h"
  124. #define OP sar
  125. #include "test-i386-shift.h"
  126. #define OP rol
  127. #include "test-i386-shift.h"
  128. #define OP ror
  129. #include "test-i386-shift.h"
  130. #define OP rcr
  131. #define OP_CC
  132. #include "test-i386-shift.h"
  133. #define OP rcl
  134. #define OP_CC
  135. #include "test-i386-shift.h"
  136. #define OP shld
  137. #define OP_SHIFTD
  138. #define OP_NOBYTE
  139. #include "test-i386-shift.h"
  140. #define OP shrd
  141. #define OP_SHIFTD
  142. #define OP_NOBYTE
  143. #include "test-i386-shift.h"
  144. /* XXX: should be more precise ? */
  145. #undef CC_MASK
  146. #define CC_MASK (CC_C)
  147. #define OP bt
  148. #define OP_NOBYTE
  149. #include "test-i386-shift.h"
  150. #define OP bts
  151. #define OP_NOBYTE
  152. #include "test-i386-shift.h"
  153. #define OP btr
  154. #define OP_NOBYTE
  155. #include "test-i386-shift.h"
  156. #define OP btc
  157. #define OP_NOBYTE
  158. #include "test-i386-shift.h"
  159. /* lea test (modrm support) */
  160. #define TEST_LEAQ(STR)\
  161. {\
  162. asm("lea " STR ", %0"\
  163. : "=r" (res)\
  164. : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
  165. printf("lea %s = " FMTLX "\n", STR, res);\
  166. }
  167. #define TEST_LEA(STR)\
  168. {\
  169. asm("lea " STR ", %0"\
  170. : "=r" (res)\
  171. : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
  172. printf("lea %s = " FMTLX "\n", STR, res);\
  173. }
  174. #define TEST_LEA16(STR)\
  175. {\
  176. asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
  177. : "=r" (res)\
  178. : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
  179. printf("lea %s = %08lx\n", STR, res);\
  180. }
  181. void test_lea(void)
  182. {
  183. long eax, ebx, ecx, edx, esi, edi, res;
  184. eax = i2l(0x0001);
  185. ebx = i2l(0x0002);
  186. ecx = i2l(0x0004);
  187. edx = i2l(0x0008);
  188. esi = i2l(0x0010);
  189. edi = i2l(0x0020);
  190. TEST_LEA("0x4000");
  191. TEST_LEA("(%%eax)");
  192. TEST_LEA("(%%ebx)");
  193. TEST_LEA("(%%ecx)");
  194. TEST_LEA("(%%edx)");
  195. TEST_LEA("(%%esi)");
  196. TEST_LEA("(%%edi)");
  197. TEST_LEA("0x40(%%eax)");
  198. TEST_LEA("0x40(%%ebx)");
  199. TEST_LEA("0x40(%%ecx)");
  200. TEST_LEA("0x40(%%edx)");
  201. TEST_LEA("0x40(%%esi)");
  202. TEST_LEA("0x40(%%edi)");
  203. TEST_LEA("0x4000(%%eax)");
  204. TEST_LEA("0x4000(%%ebx)");
  205. TEST_LEA("0x4000(%%ecx)");
  206. TEST_LEA("0x4000(%%edx)");
  207. TEST_LEA("0x4000(%%esi)");
  208. TEST_LEA("0x4000(%%edi)");
  209. TEST_LEA("(%%eax, %%ecx)");
  210. TEST_LEA("(%%ebx, %%edx)");
  211. TEST_LEA("(%%ecx, %%ecx)");
  212. TEST_LEA("(%%edx, %%ecx)");
  213. TEST_LEA("(%%esi, %%ecx)");
  214. TEST_LEA("(%%edi, %%ecx)");
  215. TEST_LEA("0x40(%%eax, %%ecx)");
  216. TEST_LEA("0x4000(%%ebx, %%edx)");
  217. TEST_LEA("(%%ecx, %%ecx, 2)");
  218. TEST_LEA("(%%edx, %%ecx, 4)");
  219. TEST_LEA("(%%esi, %%ecx, 8)");
  220. TEST_LEA("(,%%eax, 2)");
  221. TEST_LEA("(,%%ebx, 4)");
  222. TEST_LEA("(,%%ecx, 8)");
  223. TEST_LEA("0x40(,%%eax, 2)");
  224. TEST_LEA("0x40(,%%ebx, 4)");
  225. TEST_LEA("0x40(,%%ecx, 8)");
  226. TEST_LEA("-10(%%ecx, %%ecx, 2)");
  227. TEST_LEA("-10(%%edx, %%ecx, 4)");
  228. TEST_LEA("-10(%%esi, %%ecx, 8)");
  229. TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
  230. TEST_LEA("0x4000(%%edx, %%ecx, 4)");
  231. TEST_LEA("0x4000(%%esi, %%ecx, 8)");
  232. #if defined(__x86_64__)
  233. TEST_LEAQ("0x4000");
  234. TEST_LEAQ("0x4000(%%rip)");
  235. TEST_LEAQ("(%%rax)");
  236. TEST_LEAQ("(%%rbx)");
  237. TEST_LEAQ("(%%rcx)");
  238. TEST_LEAQ("(%%rdx)");
  239. TEST_LEAQ("(%%rsi)");
  240. TEST_LEAQ("(%%rdi)");
  241. TEST_LEAQ("0x40(%%rax)");
  242. TEST_LEAQ("0x40(%%rbx)");
  243. TEST_LEAQ("0x40(%%rcx)");
  244. TEST_LEAQ("0x40(%%rdx)");
  245. TEST_LEAQ("0x40(%%rsi)");
  246. TEST_LEAQ("0x40(%%rdi)");
  247. TEST_LEAQ("0x4000(%%rax)");
  248. TEST_LEAQ("0x4000(%%rbx)");
  249. TEST_LEAQ("0x4000(%%rcx)");
  250. TEST_LEAQ("0x4000(%%rdx)");
  251. TEST_LEAQ("0x4000(%%rsi)");
  252. TEST_LEAQ("0x4000(%%rdi)");
  253. TEST_LEAQ("(%%rax, %%rcx)");
  254. TEST_LEAQ("(%%rbx, %%rdx)");
  255. TEST_LEAQ("(%%rcx, %%rcx)");
  256. TEST_LEAQ("(%%rdx, %%rcx)");
  257. TEST_LEAQ("(%%rsi, %%rcx)");
  258. TEST_LEAQ("(%%rdi, %%rcx)");
  259. TEST_LEAQ("0x40(%%rax, %%rcx)");
  260. TEST_LEAQ("0x4000(%%rbx, %%rdx)");
  261. TEST_LEAQ("(%%rcx, %%rcx, 2)");
  262. TEST_LEAQ("(%%rdx, %%rcx, 4)");
  263. TEST_LEAQ("(%%rsi, %%rcx, 8)");
  264. TEST_LEAQ("(,%%rax, 2)");
  265. TEST_LEAQ("(,%%rbx, 4)");
  266. TEST_LEAQ("(,%%rcx, 8)");
  267. TEST_LEAQ("0x40(,%%rax, 2)");
  268. TEST_LEAQ("0x40(,%%rbx, 4)");
  269. TEST_LEAQ("0x40(,%%rcx, 8)");
  270. TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
  271. TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
  272. TEST_LEAQ("-10(%%rsi, %%rcx, 8)");
  273. TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
  274. TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
  275. TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
  276. #else
  277. /* limited 16 bit addressing test */
  278. TEST_LEA16("0x4000");
  279. TEST_LEA16("(%%bx)");
  280. TEST_LEA16("(%%si)");
  281. TEST_LEA16("(%%di)");
  282. TEST_LEA16("0x40(%%bx)");
  283. TEST_LEA16("0x40(%%si)");
  284. TEST_LEA16("0x40(%%di)");
  285. TEST_LEA16("0x4000(%%bx)");
  286. TEST_LEA16("0x4000(%%si)");
  287. TEST_LEA16("(%%bx,%%si)");
  288. TEST_LEA16("(%%bx,%%di)");
  289. TEST_LEA16("0x40(%%bx,%%si)");
  290. TEST_LEA16("0x40(%%bx,%%di)");
  291. TEST_LEA16("0x4000(%%bx,%%si)");
  292. TEST_LEA16("0x4000(%%bx,%%di)");
  293. #endif
  294. }
  295. #define TEST_JCC(JCC, v1, v2)\
  296. {\
  297. int res;\
  298. asm("movl $1, %0\n\t"\
  299. "cmpl %2, %1\n\t"\
  300. "j" JCC " 1f\n\t"\
  301. "movl $0, %0\n\t"\
  302. "1:\n\t"\
  303. : "=r" (res)\
  304. : "r" (v1), "r" (v2));\
  305. printf("%-10s %d\n", "j" JCC, res);\
  306. \
  307. asm("movl $0, %0\n\t"\
  308. "cmpl %2, %1\n\t"\
  309. "set" JCC " %b0\n\t"\
  310. : "=r" (res)\
  311. : "r" (v1), "r" (v2));\
  312. printf("%-10s %d\n", "set" JCC, res);\
  313. if (TEST_CMOV) {\
  314. long val = i2l(1);\
  315. long res = i2l(0x12345678);\
  316. X86_64_ONLY(\
  317. asm("cmpl %2, %1\n\t"\
  318. "cmov" JCC "q %3, %0\n\t"\
  319. : "=r" (res)\
  320. : "r" (v1), "r" (v2), "m" (val), "0" (res));\
  321. printf("%-10s R=" FMTLX "\n", "cmov" JCC "q", res);)\
  322. asm("cmpl %2, %1\n\t"\
  323. "cmov" JCC "l %k3, %k0\n\t"\
  324. : "=r" (res)\
  325. : "r" (v1), "r" (v2), "m" (val), "0" (res));\
  326. printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
  327. asm("cmpl %2, %1\n\t"\
  328. "cmov" JCC "w %w3, %w0\n\t"\
  329. : "=r" (res)\
  330. : "r" (v1), "r" (v2), "r" (1), "0" (res));\
  331. printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
  332. } \
  333. }
  334. /* various jump tests */
  335. void test_jcc(void)
  336. {
  337. TEST_JCC("ne", 1, 1);
  338. TEST_JCC("ne", 1, 0);
  339. TEST_JCC("e", 1, 1);
  340. TEST_JCC("e", 1, 0);
  341. TEST_JCC("l", 1, 1);
  342. TEST_JCC("l", 1, 0);
  343. TEST_JCC("l", 1, -1);
  344. TEST_JCC("le", 1, 1);
  345. TEST_JCC("le", 1, 0);
  346. TEST_JCC("le", 1, -1);
  347. TEST_JCC("ge", 1, 1);
  348. TEST_JCC("ge", 1, 0);
  349. TEST_JCC("ge", -1, 1);
  350. TEST_JCC("g", 1, 1);
  351. TEST_JCC("g", 1, 0);
  352. TEST_JCC("g", 1, -1);
  353. TEST_JCC("b", 1, 1);
  354. TEST_JCC("b", 1, 0);
  355. TEST_JCC("b", 1, -1);
  356. TEST_JCC("be", 1, 1);
  357. TEST_JCC("be", 1, 0);
  358. TEST_JCC("be", 1, -1);
  359. TEST_JCC("ae", 1, 1);
  360. TEST_JCC("ae", 1, 0);
  361. TEST_JCC("ae", 1, -1);
  362. TEST_JCC("a", 1, 1);
  363. TEST_JCC("a", 1, 0);
  364. TEST_JCC("a", 1, -1);
  365. TEST_JCC("p", 1, 1);
  366. TEST_JCC("p", 1, 0);
  367. TEST_JCC("np", 1, 1);
  368. TEST_JCC("np", 1, 0);
  369. TEST_JCC("o", 0x7fffffff, 0);
  370. TEST_JCC("o", 0x7fffffff, -1);
  371. TEST_JCC("no", 0x7fffffff, 0);
  372. TEST_JCC("no", 0x7fffffff, -1);
  373. TEST_JCC("s", 0, 1);
  374. TEST_JCC("s", 0, -1);
  375. TEST_JCC("s", 0, 0);
  376. TEST_JCC("ns", 0, 1);
  377. TEST_JCC("ns", 0, -1);
  378. TEST_JCC("ns", 0, 0);
  379. }
  380. #define TEST_LOOP(insn) \
  381. {\
  382. for(i = 0; i < sizeof(ecx_vals) / sizeof(long); i++) {\
  383. ecx = ecx_vals[i];\
  384. for(zf = 0; zf < 2; zf++) {\
  385. asm("test %2, %2\n\t"\
  386. "movl $1, %0\n\t"\
  387. insn " 1f\n\t" \
  388. "movl $0, %0\n\t"\
  389. "1:\n\t"\
  390. : "=a" (res)\
  391. : "c" (ecx), "b" (!zf)); \
  392. printf("%-10s ECX=" FMTLX " ZF=%ld r=%d\n", insn, ecx, zf, res); \
  393. }\
  394. }\
  395. }
  396. void test_loop(void)
  397. {
  398. long ecx, zf;
  399. const long ecx_vals[] = {
  400. 0,
  401. 1,
  402. 0x10000,
  403. 0x10001,
  404. #if defined(__x86_64__)
  405. 0x100000000L,
  406. 0x100000001L,
  407. #endif
  408. };
  409. int i, res;
  410. #if !defined(__x86_64__)
  411. TEST_LOOP("jcxz");
  412. TEST_LOOP("loopw");
  413. TEST_LOOP("loopzw");
  414. TEST_LOOP("loopnzw");
  415. #endif
  416. TEST_LOOP("jecxz");
  417. TEST_LOOP("loopl");
  418. TEST_LOOP("loopzl");
  419. TEST_LOOP("loopnzl");
  420. }
  421. #undef CC_MASK
  422. #ifdef TEST_P4_FLAGS
  423. #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
  424. #else
  425. #define CC_MASK (CC_O | CC_C)
  426. #endif
  427. #define OP mul
  428. #include "test-i386-muldiv.h"
  429. #define OP imul
  430. #include "test-i386-muldiv.h"
  431. void test_imulw2(long op0, long op1)
  432. {
  433. long res, s1, s0, flags;
  434. s0 = op0;
  435. s1 = op1;
  436. res = s0;
  437. flags = 0;
  438. asm volatile ("push %4\n\t"
  439. "popf\n\t"
  440. "imulw %w2, %w0\n\t"
  441. "pushf\n\t"
  442. "pop %1\n\t"
  443. : "=q" (res), "=g" (flags)
  444. : "q" (s1), "0" (res), "1" (flags));
  445. printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
  446. "imulw", s0, s1, res, flags & CC_MASK);
  447. }
  448. void test_imull2(long op0, long op1)
  449. {
  450. long res, s1, s0, flags;
  451. s0 = op0;
  452. s1 = op1;
  453. res = s0;
  454. flags = 0;
  455. asm volatile ("push %4\n\t"
  456. "popf\n\t"
  457. "imull %k2, %k0\n\t"
  458. "pushf\n\t"
  459. "pop %1\n\t"
  460. : "=q" (res), "=g" (flags)
  461. : "q" (s1), "0" (res), "1" (flags));
  462. printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
  463. "imull", s0, s1, res, flags & CC_MASK);
  464. }
  465. #if defined(__x86_64__)
  466. void test_imulq2(long op0, long op1)
  467. {
  468. long res, s1, s0, flags;
  469. s0 = op0;
  470. s1 = op1;
  471. res = s0;
  472. flags = 0;
  473. asm volatile ("push %4\n\t"
  474. "popf\n\t"
  475. "imulq %2, %0\n\t"
  476. "pushf\n\t"
  477. "pop %1\n\t"
  478. : "=q" (res), "=g" (flags)
  479. : "q" (s1), "0" (res), "1" (flags));
  480. printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
  481. "imulq", s0, s1, res, flags & CC_MASK);
  482. }
  483. #endif
  484. #define TEST_IMUL_IM(size, rsize, op0, op1)\
  485. {\
  486. long res, flags, s1;\
  487. flags = 0;\
  488. res = 0;\
  489. s1 = op1;\
  490. asm volatile ("push %3\n\t"\
  491. "popf\n\t"\
  492. "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
  493. "pushf\n\t"\
  494. "pop %1\n\t"\
  495. : "=r" (res), "=g" (flags)\
  496. : "r" (s1), "1" (flags), "0" (res));\
  497. printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
  498. "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
  499. }
  500. #undef CC_MASK
  501. #define CC_MASK (0)
  502. #define OP div
  503. #include "test-i386-muldiv.h"
  504. #define OP idiv
  505. #include "test-i386-muldiv.h"
  506. void test_mul(void)
  507. {
  508. test_imulb(0x1234561d, 4);
  509. test_imulb(3, -4);
  510. test_imulb(0x80, 0x80);
  511. test_imulb(0x10, 0x10);
  512. test_imulw(0, 0x1234001d, 45);
  513. test_imulw(0, 23, -45);
  514. test_imulw(0, 0x8000, 0x8000);
  515. test_imulw(0, 0x100, 0x100);
  516. test_imull(0, 0x1234001d, 45);
  517. test_imull(0, 23, -45);
  518. test_imull(0, 0x80000000, 0x80000000);
  519. test_imull(0, 0x10000, 0x10000);
  520. test_mulb(0x1234561d, 4);
  521. test_mulb(3, -4);
  522. test_mulb(0x80, 0x80);
  523. test_mulb(0x10, 0x10);
  524. test_mulw(0, 0x1234001d, 45);
  525. test_mulw(0, 23, -45);
  526. test_mulw(0, 0x8000, 0x8000);
  527. test_mulw(0, 0x100, 0x100);
  528. test_mull(0, 0x1234001d, 45);
  529. test_mull(0, 23, -45);
  530. test_mull(0, 0x80000000, 0x80000000);
  531. test_mull(0, 0x10000, 0x10000);
  532. test_mull(0, 0xffffffff, 0xffffffff);
  533. test_mull(0, 0xfffffffe, 0xffffffff);
  534. test_mull(0, 0xffffffff, 0xfffffffe);
  535. test_mull(0, 0xffffffff, 0);
  536. test_mull(0, 0xffffffff, 1);
  537. test_mull(0, 0xffffffff, 2);
  538. test_mull(0, 0xffffffff, 3);
  539. test_mull(0, 0, 0xffffffff);
  540. test_mull(0, 1, 0xffffffff);
  541. test_mull(0, 2, 0xffffffff);
  542. test_mull(0, 3, 0xffffffff);
  543. test_imulw2(0x1234001d, 45);
  544. test_imulw2(23, -45);
  545. test_imulw2(0x8000, 0x8000);
  546. test_imulw2(0x100, 0x100);
  547. test_imull2(0x1234001d, 45);
  548. test_imull2(23, -45);
  549. test_imull2(0x80000000, 0x80000000);
  550. test_imull2(0x10000, 0x10000);
  551. TEST_IMUL_IM("w", "w", 45, 0x1234);
  552. TEST_IMUL_IM("w", "w", -45, 23);
  553. TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
  554. TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
  555. TEST_IMUL_IM("l", "k", 45, 0x1234);
  556. TEST_IMUL_IM("l", "k", -45, 23);
  557. TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
  558. TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
  559. test_idivb(0x12341678, 0x127e);
  560. test_idivb(0x43210123, -5);
  561. test_idivb(0x12340004, -1);
  562. test_idivb(-20, 3);
  563. test_idivb(20, -3);
  564. test_idivb(-20, -3);
  565. test_idivw(0, 0x12345678, 12347);
  566. test_idivw(0, -23223, -45);
  567. test_idivw(0, 0x12348000, -1);
  568. test_idivw(0x12343, 0x12345678, 0x81238567);
  569. test_idivw(-20, 0, 300);
  570. test_idivw(20, 0, -300);
  571. test_idivw(-20, 0, -300);
  572. test_idivl(0, 0x12345678, 12347);
  573. test_idivl(0, -233223, -45);
  574. test_idivl(0, 0x80000000, -1);
  575. test_idivl(0x12343, 0x12345678, 0x81234567);
  576. test_divb(0x12341678, 0x127e);
  577. test_divb(0x43210123, -5);
  578. test_divb(0x12340004, -1);
  579. test_divw(0, 0x12345678, 12347);
  580. test_divw(0, -23223, -45);
  581. test_divw(0, 0x12348000, -1);
  582. test_divw(0x12343, 0x12345678, 0x81238567);
  583. test_divl(0, 0x12345678, 12347);
  584. test_divl(0, -233223, -45);
  585. test_divl(0, 0x80000000, -1);
  586. test_divl(0x12343, 0x12345678, 0x81234567);
  587. test_divl(0xfffffffe, 0xffffffff, 0xffffffff);
  588. test_divl(0xffffffe, 0xffffffff, 0xfffffff);
  589. test_divl(0xfffffe, 0xffffffff, 0xffffff);
  590. test_divl(0xffffe, 0xffffffff, 0xfffff);
  591. test_divl(0xfffe, 0xffffffff, 0xffff);
  592. test_divl(0xffe, 0xffffffff, 0xfff);
  593. test_divl(0xfe, 0xffffffff, 0xff);
  594. test_divl(0xe, 0xffffffff, 0xf);
  595. test_divl(0x7ffffffe, 0xffffffff, 0x7fffffff);
  596. test_divl(0x7fffffe, 0xffffffff, 0x7ffffff);
  597. test_divl(0x7ffffe, 0xffffffff, 0x7fffff);
  598. test_divl(0x7fffe, 0xffffffff, 0x7ffff);
  599. test_divl(0x7ffe, 0xffffffff, 0x7fff);
  600. test_divl(0x7fe, 0xffffffff, 0x7ff);
  601. test_divl(0x7e, 0xffffffff, 0x7f);
  602. test_divl(0x3ffffffe, 0xffffffff, 0x3fffffff);
  603. test_divl(0x3fffffe, 0xffffffff, 0x3ffffff);
  604. test_divl(0x3ffffe, 0xffffffff, 0x3fffff);
  605. test_divl(0x3fffe, 0xffffffff, 0x3ffff);
  606. test_divl(0x3ffe, 0xffffffff, 0x3fff);
  607. test_divl(0x3fe, 0xffffffff, 0x3ff);
  608. test_divl(0x3e, 0xffffffff, 0x3f);
  609. test_divl(0x1ffffffe, 0xffffffff, 0x1fffffff);
  610. test_divl(0x1fffffe, 0xffffffff, 0x1ffffff);
  611. test_divl(0x1ffffe, 0xffffffff, 0x1fffff);
  612. test_divl(0x1fffe, 0xffffffff, 0x1ffff);
  613. test_divl(0x1ffe, 0xffffffff, 0x1fff);
  614. test_divl(0x1fe, 0xffffffff, 0x1ff);
  615. test_divl(0x1e, 0xffffffff, 0x1f);
  616. int i;
  617. for(i = 0; i < 16; i++)
  618. {
  619. test_divl(0, 0xfffffffe, i + 1);
  620. test_divl(0, 0xffffffff, i + 1);
  621. test_divl(1, 0xfffffffe, i + 2);
  622. test_divl(1, 0xffffffff, i + 2);
  623. test_divl(2, 0xfffffffe, i + 3);
  624. test_divl(2, 0xffffffff, i + 3);
  625. test_divl(3, 0xfffffffe, i + 4);
  626. test_divl(3, 0xffffffff, i + 4);
  627. test_divl(4, 0xfffffffe, i + 5);
  628. test_divl(4, 0xffffffff, i + 5);
  629. test_divl(0xfffffffd, 0x00000000 + i, 0xfffffffe);
  630. test_divl(0xfffffffd, 0xfffffff0 + i, 0xfffffffe);
  631. test_divl(0xfffffffe, 0x00000000 + i, 0xffffffff);
  632. test_divl(0xfffffffe, 0xfffffff0 + i, 0xffffffff);
  633. test_divl(0, i, 0xfffffffa);
  634. test_divl(0, i, 0xfffffffb);
  635. test_divl(0, i, 0xfffffffc);
  636. test_divl(0, i, 0xfffffffd);
  637. test_divl(0, i, 0xfffffffe);
  638. test_divl(0, i, 0xffffffff);
  639. test_idivl(0, 1, i + 1);
  640. test_idivl(-1, -1, i + 1);
  641. test_idivl(0, 1, -(i + 1));
  642. test_idivl(-1, -1, -(i + 1));
  643. test_idivl(0, 0x7fffffff, i + 1);
  644. test_idivl(-1, 0x80000001, i + 1);
  645. test_idivl(0, 0x7fffffff, -(i + 1));
  646. test_idivl(-1, 0x80000001, -(i + 1));
  647. }
  648. #if defined(__x86_64__)
  649. test_imulq(0, 0x1234001d1234001d, 45);
  650. test_imulq(0, 23, -45);
  651. test_imulq(0, 0x8000000000000000, 0x8000000000000000);
  652. test_imulq(0, 0x100000000, 0x100000000);
  653. test_mulq(0, 0x1234001d1234001d, 45);
  654. test_mulq(0, 23, -45);
  655. test_mulq(0, 0x8000000000000000, 0x8000000000000000);
  656. test_mulq(0, 0x100000000, 0x100000000);
  657. test_imulq2(0x1234001d1234001d, 45);
  658. test_imulq2(23, -45);
  659. test_imulq2(0x8000000000000000, 0x8000000000000000);
  660. test_imulq2(0x100000000, 0x100000000);
  661. TEST_IMUL_IM("q", "", 45, 0x12341234);
  662. TEST_IMUL_IM("q", "", -45, 23);
  663. TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
  664. TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);
  665. test_idivq(0, 0x12345678abcdef, 12347);
  666. test_idivq(0, -233223, -45);
  667. test_idivq(0, 0x8000000000000000, -1);
  668. test_idivq(0x12343, 0x12345678, 0x81234567);
  669. test_divq(0, 0x12345678abcdef, 12347);
  670. test_divq(0, -233223, -45);
  671. test_divq(0, 0x8000000000000000, -1);
  672. test_divq(0x12343, 0x12345678, 0x81234567);
  673. #endif
  674. }
  675. #define TEST_BSX(op, size, op0)\
  676. {\
  677. long res, val, resz;\
  678. val = op0;\
  679. asm("xor %1, %1\n"\
  680. "mov $0x12345678, %0\n"\
  681. #op " %" size "2, %" size "0 ; setz %b1" \
  682. : "=&r" (res), "=&q" (resz)\
  683. : "r" (val));\
  684. printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
  685. }
  686. void test_bsx(void)
  687. {
  688. TEST_BSX(bsrw, "w", 0);
  689. TEST_BSX(bsrw, "w", 0x12340128);
  690. TEST_BSX(bsrw, "w", 0xffffffff);
  691. TEST_BSX(bsrw, "w", 0xffff7fff);
  692. TEST_BSX(bsfw, "w", 0);
  693. TEST_BSX(bsfw, "w", 0x12340128);
  694. TEST_BSX(bsfw, "w", 0xffffffff);
  695. TEST_BSX(bsfw, "w", 0xfffffff7);
  696. TEST_BSX(bsrl, "k", 0);
  697. TEST_BSX(bsrl, "k", 0x00340128);
  698. TEST_BSX(bsrl, "k", 0xffffffff);
  699. TEST_BSX(bsrl, "k", 0x7fffffff);
  700. TEST_BSX(bsfl, "k", 0);
  701. TEST_BSX(bsfl, "k", 0x00340128);
  702. TEST_BSX(bsfl, "k", 0xffffffff);
  703. TEST_BSX(bsfl, "k", 0xfffffff7);
  704. #if defined(__x86_64__)
  705. TEST_BSX(bsrq, "", 0);
  706. TEST_BSX(bsrq, "", 0x003401281234);
  707. TEST_BSX(bsfq, "", 0);
  708. TEST_BSX(bsfq, "", 0x003401281234);
  709. #endif
  710. }
  711. #define TEST_POPCNT(size, op0)\
  712. {\
  713. long res, val, resz;\
  714. val = op0;\
  715. asm("xor %1, %1\n"\
  716. "mov $0x12345678, %0\n"\
  717. "popcnt %" size "2, %" size "0 ; pushf; pop %1;" \
  718. : "=&r" (res), "=&q" (resz)\
  719. : "r" (val));\
  720. printf("popcnt A=" FMTLX " R=" FMTLX " flags=%lx\n", val, res, resz);\
  721. }
  722. void test_popcnt(void)
  723. {
  724. TEST_POPCNT("w", 0);
  725. }
  726. /**********************************************/
  727. union float64u {
  728. double d;
  729. uint64_t l;
  730. };
  731. union float64u q_nan = { .l = 0xFFF8000000000000LL };
  732. union float64u s_nan = { .l = 0xFFF0000000000000LL };
  733. void test_fops(double a, double b)
  734. {
  735. //int ib = (int)b;
  736. //int dest = 0;
  737. // XXX: Tests below are disabled since libc (which is statically linked)
  738. // contains sse instructions, some of which aren't supported.
  739. printf("a=%f b=%f a+b=%f\n", a, b, a + b);
  740. printf("a=%f b=%f a-b=%f\n", a, b, a - b);
  741. printf("a=%f b=%f a*b=%f\n", a, b, a * b);
  742. printf("a=%f b=%f a/b=%f\n", a, b, a / b);
  743. printf("a=%f b=%f =%f\n", a, b, a + a + a + 3 * b / a * (a * a * a / b / b / (a + 1.0) - 3.5 + a * b / (3.7 * a / (a - b * b) + 6.5 * a / (b * b * a / -b - a * b) + 5.5 * (b - a))));
  744. printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
  745. //printf("a=%f fma(a,b,a)=%f\n", a, fma(a, b, a));
  746. //printf("a=%f fdim(a,b)=%f\n", a, fdim(a, b));
  747. printf("a=%f copysign(a,b)=%f\n", a, copysign(a, b));
  748. printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
  749. //printf("a=%f sin(a)=%f\n", a, sin(a));
  750. //printf("a=%f cos(a)=%f\n", a, cos(a));
  751. //printf("a=%f tan(a)=%f\n", a, tan(a));
  752. //printf("a=%f log(a)=%f\n", a, log(a));
  753. //printf("a=%f log10(a)=%f\n", a, log10(a));
  754. //printf("a=%f log1p(a)=%f\n", a, log1p(a));
  755. //printf("a=%f log2(a)=%f\n", a, log2(a));
  756. //printf("a=%f logb(a)=%f\n", a, logb(a));
  757. //printf("a=%f ilogb(a)=%d\n", a, ilogb(a));
  758. printf("a=%f exp(a)=%f\n", a, exp(a));
  759. //printf("a=%f exp2(a)=%f\n", a, exp2(a));
  760. //printf("a=%f frexp(a)=%f, %d\n", a, frexp(a, &dest), dest);
  761. //printf("a=%f ldexp(a,b)=%f\n", a, ldexp(a, ib));
  762. //printf("a=%f scalbn(a,b)=%f\n", a, scalbn(a, ib));
  763. //printf("a=%f sihh(a)=%f\n", a, sinh(a));
  764. //printf("a=%f cosh(a)=%f\n", a, cosh(a));
  765. //printf("a=%f tanh(a)=%f\n", a, tanh(a));
  766. //printf("a=%f fabs(a)=%f\n", a, fabs(a));
  767. //printf("a=%f pow(a,b)=%f\n", a, pow(a,b));
  768. //printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
  769. ///* just to test some op combining */
  770. //printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
  771. //printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
  772. //printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
  773. }
  774. void fpu_clear_exceptions(void)
  775. {
  776. struct QEMU_PACKED {
  777. uint16_t fpuc;
  778. uint16_t dummy1;
  779. uint16_t fpus;
  780. uint16_t dummy2;
  781. uint16_t fptag;
  782. uint16_t dummy3;
  783. uint32_t ignored[4];
  784. long double fpregs[8];
  785. } float_env32;
  786. asm volatile ("fnstenv %0\n" : "=m" (float_env32));
  787. float_env32.fpus &= ~0x7f;
  788. asm volatile ("fldenv %0\n" : : "m" (float_env32));
  789. }
  790. /* XXX: display exception bits when supported */
  791. #define FPUS_EMASK 0x0000
  792. //#define FPUS_EMASK 0x007f
  793. void test_fcmp(double a, double b)
  794. {
  795. long eflags, fpus;
  796. fpu_clear_exceptions();
  797. asm("fcom %2\n"
  798. "fstsw %%ax\n"
  799. : "=a" (fpus)
  800. : "t" (a), "u" (b));
  801. printf("fcom(%f %f)=%04lx\n",
  802. a, b, fpus & (0x4500 | FPUS_EMASK));
  803. fpu_clear_exceptions();
  804. asm("fucom %2\n"
  805. "fstsw %%ax\n"
  806. : "=a" (fpus)
  807. : "t" (a), "u" (b));
  808. printf("fucom(%f %f)=%04lx\n",
  809. a, b, fpus & (0x4500 | FPUS_EMASK));
  810. if (TEST_FCOMI) {
  811. /* test f(u)comi instruction */
  812. fpu_clear_exceptions();
  813. asm("fcomi %3, %2\n"
  814. "fstsw %%ax\n"
  815. "pushf\n"
  816. "pop %0\n"
  817. : "=r" (eflags), "=a" (fpus)
  818. : "t" (a), "u" (b));
  819. printf("fcomi(%f %f)=%04lx %02lx\n",
  820. a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
  821. fpu_clear_exceptions();
  822. asm("fucomi %3, %2\n"
  823. "fstsw %%ax\n"
  824. "pushf\n"
  825. "pop %0\n"
  826. : "=r" (eflags), "=a" (fpus)
  827. : "t" (a), "u" (b));
  828. printf("fucomi(%f %f)=%04lx %02lx\n",
  829. a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
  830. }
  831. fpu_clear_exceptions();
  832. asm volatile("fxam\n"
  833. "fstsw %%ax\n"
  834. : "=a" (fpus)
  835. : "t" (a));
  836. printf("fxam(%f)=%04lx\n", a, fpus & 0x4700);
  837. fpu_clear_exceptions();
  838. }
  839. void test_fcvt(double a)
  840. {
  841. float fa;
  842. long double la;
  843. int16_t fpuc;
  844. int i;
  845. int64_t lla;
  846. int ia;
  847. int16_t wa;
  848. double ra;
  849. fa = a;
  850. la = a;
  851. printf("(float)%f = %f\n", a, fa);
  852. printf("(long double)%f = %Lf\n", a, la);
  853. printf("a=" FMT64X "\n", *(uint64_t *)&a);
  854. printf("la=" FMT64X " %04x\n", *(uint64_t *)&la,
  855. *(unsigned short *)((char *)(&la) + 8));
  856. /* test all roundings */
  857. asm volatile ("fstcw %0" : "=m" (fpuc));
  858. for(i=0;i<4;i++) {
  859. uint16_t val16;
  860. val16 = (fpuc & ~0x0c00) | (i << 10);
  861. asm volatile ("fldcw %0" : : "m" (val16));
  862. asm volatile ("fist %0" : "=m" (wa) : "t" (a));
  863. asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
  864. asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
  865. asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
  866. asm volatile ("fldcw %0" : : "m" (fpuc));
  867. printf("(short)a = %d\n", wa);
  868. printf("(int)a = %d\n", ia);
  869. printf("(int64_t)a = " FMT64X "\n", lla);
  870. printf("rint(a) = %f\n", ra);
  871. }
  872. }
  873. #define TEST(N) \
  874. asm("fld" #N : "=t" (a)); \
  875. printf("fld" #N "= %f\n", a);
  876. void test_fconst(void)
  877. {
  878. double a;
  879. TEST(1);
  880. TEST(l2t);
  881. TEST(l2e);
  882. TEST(pi);
  883. TEST(lg2);
  884. TEST(ln2);
  885. TEST(z);
  886. }
  887. void test_fbcd(double a)
  888. {
  889. unsigned short bcd[5];
  890. double b;
  891. asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
  892. asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
  893. printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
  894. a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
  895. }
  896. #define TEST_ENV(env, save, restore)\
  897. {\
  898. memset((env), 0xaa, sizeof(*(env)));\
  899. for(i=0;i<5;i++)\
  900. asm volatile ("fldl %0" : : "m" (dtab[i]));\
  901. asm volatile (save " %0\n" : : "m" (*(env)));\
  902. asm volatile (restore " %0\n": : "m" (*(env)));\
  903. for(i=0;i<5;i++)\
  904. asm volatile ("fstpl %0" : "=m" (rtab[i]));\
  905. for(i=0;i<5;i++)\
  906. printf("res[%d]=%f\n", i, rtab[i]);\
  907. printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
  908. (env)->fpuc,\
  909. (env)->fpus & 0xff00,\
  910. (env)->fptag);\
  911. }
  912. void test_fenv(void)
  913. {
  914. struct __attribute__((__packed__)) {
  915. uint16_t fpuc;
  916. uint16_t dummy1;
  917. uint16_t fpus;
  918. uint16_t dummy2;
  919. uint16_t fptag;
  920. uint16_t dummy3;
  921. uint32_t ignored[4];
  922. long double fpregs[8];
  923. } float_env32;
  924. struct __attribute__((__packed__)) {
  925. uint16_t fpuc;
  926. uint16_t fpus;
  927. uint16_t fptag;
  928. uint16_t ignored[4];
  929. long double fpregs[8];
  930. } float_env16;
  931. double dtab[8];
  932. double rtab[8];
  933. int i;
  934. for(i=0;i<8;i++)
  935. dtab[i] = i + 1;
  936. //TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
  937. //TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
  938. TEST_ENV(&float_env32, "fnstenv", "fldenv");
  939. TEST_ENV(&float_env32, "fnsave", "frstor");
  940. /* test for ffree */
  941. for(i=0;i<5;i++)
  942. asm volatile ("fldl %0" : : "m" (dtab[i]));
  943. asm volatile("ffree %st(2)");
  944. asm volatile ("fnstenv %0\n" : : "m" (float_env32));
  945. asm volatile ("fninit");
  946. printf("fptag=%04x\n", float_env32.fptag);
  947. }
  948. #define TEST_FCMOV(a, b, eflags, CC)\
  949. {\
  950. double res;\
  951. asm("push %3\n"\
  952. "popf\n"\
  953. "fcmov" CC " %2, %0\n"\
  954. : "=t" (res)\
  955. : "0" (a), "u" (b), "g" (eflags));\
  956. printf("fcmov%s eflags=0x%04lx-> %f\n", \
  957. CC, (long)eflags, res);\
  958. }
  959. void test_fcmov(void)
  960. {
  961. double a, b;
  962. long eflags, i;
  963. a = 1.0;
  964. b = 2.0;
  965. for(i = 0; i < 4; i++) {
  966. eflags = 0;
  967. if (i & 1)
  968. eflags |= CC_C;
  969. if (i & 2)
  970. eflags |= CC_Z;
  971. TEST_FCMOV(a, b, eflags, "b");
  972. TEST_FCMOV(a, b, eflags, "e");
  973. TEST_FCMOV(a, b, eflags, "be");
  974. TEST_FCMOV(a, b, eflags, "nb");
  975. TEST_FCMOV(a, b, eflags, "ne");
  976. TEST_FCMOV(a, b, eflags, "nbe");
  977. }
  978. TEST_FCMOV(a, b, 0, "u");
  979. TEST_FCMOV(a, b, CC_P, "u");
  980. TEST_FCMOV(a, b, 0, "nu");
  981. TEST_FCMOV(a, b, CC_P, "nu");
  982. }
  983. void test_floats(void)
  984. {
  985. test_fops(2, 3);
  986. test_fops(1.4, -5);
  987. test_fops(-20.5, 128);
  988. test_fops(-0.5, -4);
  989. test_fcmp(2, -1);
  990. test_fcmp(2, 2);
  991. test_fcmp(2, 3);
  992. test_fcmp(2, q_nan.d);
  993. test_fcmp(q_nan.d, -1);
  994. test_fcmp(-1.0/0.0, -1);
  995. test_fcmp(1.0/0.0, -1);
  996. test_fcvt(0.5);
  997. test_fcvt(-0.5);
  998. test_fcvt(1.0/7.0);
  999. test_fcvt(-1.0/9.0);
  1000. test_fcvt(32768);
  1001. // largest and smallest, odd and even numbers that have one bit left for the fractional part (2**52-1)
  1002. test_fcvt(4503599627370494.5);
  1003. test_fcvt(4503599627370495.5);
  1004. test_fcvt(-4503599627370494.5);
  1005. test_fcvt(-4503599627370495.5);
  1006. test_fcvt(-1e20);
  1007. test_fcvt(-1.0/0.0);
  1008. test_fcvt(1.0/0.0);
  1009. test_fcvt(q_nan.d);
  1010. test_fconst();
  1011. //test_fbcd(1234567890123456.0);
  1012. //test_fbcd(-123451234567890.0);
  1013. test_fenv();
  1014. if (TEST_CMOV) {
  1015. test_fcmov();
  1016. }
  1017. }
  1018. /**********************************************/
  1019. #if !defined(__x86_64__)
  1020. #define TEST_BCD(op, op0, cc_in, cc_mask)\
  1021. {\
  1022. int res, flags;\
  1023. res = op0;\
  1024. flags = cc_in;\
  1025. asm ("push %3\n\t"\
  1026. "popf\n\t"\
  1027. #op "\n\t"\
  1028. "pushf\n\t"\
  1029. "pop %1\n\t"\
  1030. : "=a" (res), "=g" (flags)\
  1031. : "0" (res), "1" (flags));\
  1032. printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
  1033. #op, op0, res, cc_in, flags & cc_mask);\
  1034. }
  1035. void test_bcd(void)
  1036. {
  1037. TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1038. TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1039. TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1040. TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1041. TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1042. TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1043. TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1044. TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1045. TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1046. TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1047. TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1048. TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1049. TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1050. TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1051. TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1052. TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1053. TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1054. TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1055. TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1056. TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1057. TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1058. TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1059. TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1060. TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1061. TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1062. TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
  1063. TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
  1064. TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
  1065. TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
  1066. TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
  1067. TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
  1068. TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
  1069. TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
  1070. TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
  1071. TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
  1072. TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
  1073. TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
  1074. TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
  1075. TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
  1076. TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
  1077. TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
  1078. TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
  1079. TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
  1080. TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
  1081. }
  1082. #endif
  1083. #define TEST_XCHG(op, size, opconst)\
  1084. {\
  1085. long op0, op1;\
  1086. op0 = i2l(0x12345678);\
  1087. op1 = i2l(0xfbca7654);\
  1088. asm(#op " %" size "0, %" size "1" \
  1089. : "=q" (op0), opconst (op1) \
  1090. : "0" (op0));\
  1091. printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
  1092. #op, op0, op1);\
  1093. }
  1094. #define TEST_CMPXCHG(op, size, opconst, eax)\
  1095. {\
  1096. long op0, op1, op2;\
  1097. op0 = i2l(0x12345678);\
  1098. op1 = i2l(0xfbca7654);\
  1099. op2 = i2l(eax);\
  1100. asm(#op " %" size "0, %" size "1" \
  1101. : "=q" (op0), opconst (op1) \
  1102. : "0" (op0), "a" (op2));\
  1103. printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
  1104. #op, op2, op0, op1);\
  1105. }
  1106. void test_xchg(void)
  1107. {
  1108. #if defined(__x86_64__)
  1109. TEST_XCHG(xchgq, "", "+q");
  1110. #endif
  1111. TEST_XCHG(xchgl, "k", "+q");
  1112. TEST_XCHG(xchgw, "w", "+q");
  1113. TEST_XCHG(xchgb, "b", "+q");
  1114. #if defined(__x86_64__)
  1115. TEST_XCHG(xchgq, "", "=m");
  1116. #endif
  1117. TEST_XCHG(xchgl, "k", "+m");
  1118. TEST_XCHG(xchgw, "w", "+m");
  1119. TEST_XCHG(xchgb, "b", "+m");
  1120. #if defined(__x86_64__)
  1121. TEST_XCHG(xaddq, "", "+q");
  1122. #endif
  1123. TEST_XCHG(xaddl, "k", "+q");
  1124. TEST_XCHG(xaddw, "w", "+q");
  1125. TEST_XCHG(xaddb, "b", "+q");
  1126. {
  1127. int res;
  1128. res = 0x12345678;
  1129. asm("xaddl %1, %0" : "=r" (res) : "0" (res));
  1130. printf("xaddl same res=%08x\n", res);
  1131. }
  1132. #if defined(__x86_64__)
  1133. TEST_XCHG(xaddq, "", "+m");
  1134. #endif
  1135. TEST_XCHG(xaddl, "k", "+m");
  1136. TEST_XCHG(xaddw, "w", "+m");
  1137. TEST_XCHG(xaddb, "b", "+m");
  1138. #if defined(__x86_64__)
  1139. TEST_CMPXCHG(cmpxchgq, "", "+q", 0xfbca7654);
  1140. #endif
  1141. TEST_CMPXCHG(cmpxchgl, "k", "+q", 0xfbca7654);
  1142. TEST_CMPXCHG(cmpxchgw, "w", "+q", 0xfbca7654);
  1143. TEST_CMPXCHG(cmpxchgb, "b", "+q", 0xfbca7654);
  1144. #if defined(__x86_64__)
  1145. TEST_CMPXCHG(cmpxchgq, "", "+q", 0xfffefdfc);
  1146. #endif
  1147. TEST_CMPXCHG(cmpxchgl, "k", "+q", 0xfffefdfc);
  1148. TEST_CMPXCHG(cmpxchgw, "w", "+q", 0xfffefdfc);
  1149. TEST_CMPXCHG(cmpxchgb, "b", "+q", 0xfffefdfc);
  1150. #if defined(__x86_64__)
  1151. TEST_CMPXCHG(cmpxchgq, "", "+m", 0xfbca7654);
  1152. #endif
  1153. TEST_CMPXCHG(cmpxchgl, "k", "+m", 0xfbca7654);
  1154. TEST_CMPXCHG(cmpxchgw, "w", "+m", 0xfbca7654);
  1155. TEST_CMPXCHG(cmpxchgb, "b", "+m", 0xfbca7654);
  1156. #if defined(__x86_64__)
  1157. TEST_CMPXCHG(cmpxchgq, "", "+m", 0xfffefdfc);
  1158. #endif
  1159. TEST_CMPXCHG(cmpxchgl, "k", "+m", 0xfffefdfc);
  1160. TEST_CMPXCHG(cmpxchgw, "w", "+m", 0xfffefdfc);
  1161. TEST_CMPXCHG(cmpxchgb, "b", "+m", 0xfffefdfc);
  1162. {
  1163. uint64_t op0, op1, op2;
  1164. long eax, edx;
  1165. long i, eflags;
  1166. for(i = 0; i < 2; i++) {
  1167. op0 = 0x123456789abcdLL;
  1168. eax = i2l(op0 & 0xffffffff);
  1169. edx = i2l(op0 >> 32);
  1170. if (i == 0)
  1171. op1 = 0xfbca765423456LL;
  1172. else
  1173. op1 = op0;
  1174. op2 = 0x6532432432434LL;
  1175. asm("cmpxchg8b %2\n"
  1176. "pushf\n"
  1177. "pop %3\n"
  1178. : "=a" (eax), "=d" (edx), "=m" (op1), "=g" (eflags)
  1179. : "0" (eax), "1" (edx), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
  1180. printf("cmpxchg8b: eax=" FMTLX " edx=" FMTLX " op1=" FMT64X " CC=%02lx\n",
  1181. eax, edx, op1, eflags & CC_Z);
  1182. }
  1183. }
  1184. }
  1185. #ifdef TEST_SEGS
  1186. /**********************************************/
  1187. /* segmentation tests */
  1188. #include <sys/syscall.h>
  1189. #include <unistd.h>
  1190. #include <asm/ldt.h>
  1191. #include <linux/version.h>
  1192. static inline int modify_ldt(int func, void * ptr, unsigned long bytecount)
  1193. {
  1194. int result = syscall(__NR_modify_ldt, func, ptr, bytecount);
  1195. if(result == -1)
  1196. {
  1197. fprintf(stderr, "Error: modify_ldt not available on this kernel. Check MODIFY_LDT_SYSCALL in /proc/config.gz.\n");
  1198. exit(1);
  1199. }
  1200. return result;
  1201. }
  1202. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
  1203. #define modify_ldt_ldt_s user_desc
  1204. #endif
  1205. #define MK_SEL(n) (((n) << 3) | 7)
  1206. uint8_t seg_data1[4096];
  1207. uint8_t seg_data2[4096];
  1208. #define TEST_LR(op, size, seg, mask)\
  1209. {\
  1210. int res, res2;\
  1211. uint16_t mseg = seg;\
  1212. res = 0x12345678;\
  1213. asm (op " %" size "2, %" size "0\n" \
  1214. "movl $0, %1\n"\
  1215. "jnz 1f\n"\
  1216. "movl $1, %1\n"\
  1217. "1:\n"\
  1218. : "=r" (res), "=r" (res2) : "m" (mseg), "0" (res));\
  1219. printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
  1220. }
  1221. #define TEST_ARPL(op, size, op1, op2)\
  1222. {\
  1223. long a, b, c; \
  1224. a = (op1); \
  1225. b = (op2); \
  1226. asm volatile(op " %" size "3, %" size "0\n"\
  1227. "movl $0,%1\n"\
  1228. "jnz 1f\n"\
  1229. "movl $1,%1\n"\
  1230. "1:\n"\
  1231. : "=r" (a), "=r" (c) : "0" (a), "r" (b)); \
  1232. printf(op size " A=" FMTLX " B=" FMTLX " R=" FMTLX " z=%ld\n",\
  1233. (long)(op1), (long)(op2), a, c);\
  1234. }
  1235. /* NOTE: we use Linux modify_ldt syscall */
  1236. void test_segs(void)
  1237. {
  1238. struct modify_ldt_ldt_s ldt;
  1239. long long ldt_table[3];
  1240. int res, res2;
  1241. char tmp;
  1242. struct {
  1243. uint32_t offset;
  1244. uint16_t seg;
  1245. } __attribute__((__packed__)) segoff;
  1246. ldt.entry_number = 1;
  1247. ldt.base_addr = (unsigned long)&seg_data1;
  1248. ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
  1249. ldt.seg_32bit = 1;
  1250. ldt.contents = MODIFY_LDT_CONTENTS_DATA;
  1251. ldt.read_exec_only = 0;
  1252. ldt.limit_in_pages = 1;
  1253. ldt.seg_not_present = 0;
  1254. ldt.useable = 1;
  1255. modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
  1256. ldt.entry_number = 2;
  1257. ldt.base_addr = (unsigned long)&seg_data2;
  1258. ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
  1259. ldt.seg_32bit = 1;
  1260. ldt.contents = MODIFY_LDT_CONTENTS_DATA;
  1261. ldt.read_exec_only = 0;
  1262. ldt.limit_in_pages = 1;
  1263. ldt.seg_not_present = 0;
  1264. ldt.useable = 1;
  1265. modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
  1266. modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
  1267. {
  1268. int i;
  1269. for(i=0;i<3;i++)
  1270. printf("%d: %016Lx\n", i, ldt_table[i]);
  1271. }
  1272. /* do some tests with fs or gs */
  1273. asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
  1274. seg_data1[1] = 0xaa;
  1275. seg_data2[1] = 0x55;
  1276. asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
  1277. printf("FS[1] = %02x\n", res);
  1278. asm volatile ("pushl %%gs\n"
  1279. "movl %1, %%gs\n"
  1280. "gs movzbl 0x1, %0\n"
  1281. "popl %%gs\n"
  1282. : "=r" (res)
  1283. : "r" (MK_SEL(2)));
  1284. printf("GS[1] = %02x\n", res);
  1285. /* tests with ds/ss (implicit segment case) */
  1286. tmp = 0xa5;
  1287. asm volatile ("pushl %%ebp\n\t"
  1288. "pushl %%ds\n\t"
  1289. "movl %2, %%ds\n\t"
  1290. "movl %3, %%ebp\n\t"
  1291. "movzbl 0x1, %0\n\t"
  1292. "movzbl (%%ebp), %1\n\t"
  1293. "popl %%ds\n\t"
  1294. "popl %%ebp\n\t"
  1295. : "=r" (res), "=r" (res2)
  1296. : "r" (MK_SEL(1)), "r" (&tmp));
  1297. printf("DS[1] = %02x\n", res);
  1298. printf("SS[tmp] = %02x\n", res2);
  1299. segoff.seg = MK_SEL(2);
  1300. segoff.offset = 0xabcdef12;
  1301. asm volatile("lfs %2, %0\n\t"
  1302. "movl %%fs, %1\n\t"
  1303. : "=r" (res), "=g" (res2)
  1304. : "m" (segoff));
  1305. printf("FS:reg = %04x:%08x\n", res2, res);
  1306. TEST_LR("larw", "w", MK_SEL(2), 0x0100);
  1307. TEST_LR("larl", "", MK_SEL(2), 0x0100);
  1308. TEST_LR("lslw", "w", MK_SEL(2), 0);
  1309. TEST_LR("lsll", "", MK_SEL(2), 0);
  1310. TEST_LR("larw", "w", 0xfff8, 0);
  1311. TEST_LR("larl", "", 0xfff8, 0);
  1312. TEST_LR("lslw", "w", 0xfff8, 0);
  1313. TEST_LR("lsll", "", 0xfff8, 0);
  1314. TEST_ARPL("arpl", "w", 0x12345678 | 3, 0x762123c | 1);
  1315. TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 3);
  1316. TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 1);
  1317. }
  1318. /* 16 bit code test */
  1319. extern char code16_start, code16_end;
  1320. extern char code16_func1;
  1321. extern char code16_func2;
  1322. extern char code16_func3;
  1323. void test_code16(void)
  1324. {
  1325. struct modify_ldt_ldt_s ldt;
  1326. int res, res2;
  1327. /* build a code segment */
  1328. ldt.entry_number = 1;
  1329. ldt.base_addr = (unsigned long)&code16_start;
  1330. ldt.limit = &code16_end - &code16_start;
  1331. ldt.seg_32bit = 0;
  1332. ldt.contents = MODIFY_LDT_CONTENTS_CODE;
  1333. ldt.read_exec_only = 0;
  1334. ldt.limit_in_pages = 0;
  1335. ldt.seg_not_present = 0;
  1336. ldt.useable = 1;
  1337. modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
  1338. /* call the first function */
  1339. // XXX: Temporarily disabled: Fails to compile on newer gcc, wait for upstream fix
  1340. #if 0
  1341. asm volatile ("lcall %1, %2"
  1342. : "=a" (res)
  1343. : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
  1344. printf("func1() = 0x%08x\n", res);
  1345. asm volatile ("lcall %2, %3"
  1346. : "=a" (res), "=c" (res2)
  1347. : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
  1348. printf("func2() = 0x%08x spdec=%d\n", res, res2);
  1349. asm volatile ("lcall %1, %2"
  1350. : "=a" (res)
  1351. : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
  1352. printf("func3() = 0x%08x\n", res);
  1353. #endif
  1354. }
  1355. #endif
  1356. #if defined(__x86_64__)
  1357. asm(".globl func_lret\n"
  1358. "func_lret:\n"
  1359. "movl $0x87654641, %eax\n"
  1360. "lretq\n");
  1361. #else
  1362. asm(".globl func_lret\n"
  1363. "func_lret:\n"
  1364. "movl $0x87654321, %eax\n"
  1365. "lret\n"
  1366. ".globl func_iret\n"
  1367. "func_iret:\n"
  1368. "movl $0xabcd4321, %eax\n"
  1369. "iret\n");
  1370. #endif
  1371. extern char func_lret;
  1372. extern char func_iret;
  1373. void test_misc(void)
  1374. {
  1375. char table[256];
  1376. long res, i;
  1377. for(i=0;i<256;i++) table[i] = 256 - i;
  1378. res = 0x12345678;
  1379. asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
  1380. printf("xlat: EAX=" FMTLX "\n", res);
  1381. #if defined(__x86_64__)
  1382. #if 0
  1383. {
  1384. /* XXX: see if Intel Core2 and AMD64 behavior really
  1385. differ. Here we implemented the Intel way which is not
  1386. compatible yet with QEMU. */
  1387. static struct QEMU_PACKED {
  1388. uint64_t offset;
  1389. uint16_t seg;
  1390. } desc;
  1391. long cs_sel;
  1392. asm volatile ("mov %%cs, %0" : "=r" (cs_sel));
  1393. asm volatile ("push %1\n"
  1394. "call func_lret\n"
  1395. : "=a" (res)
  1396. : "r" (cs_sel) : "memory", "cc");
  1397. printf("func_lret=" FMTLX "\n", res);
  1398. desc.offset = (long)&func_lret;
  1399. desc.seg = cs_sel;
  1400. asm volatile ("xor %%rax, %%rax\n"
  1401. "rex64 lcall *(%%rcx)\n"
  1402. : "=a" (res)
  1403. : "c" (&desc)
  1404. : "memory", "cc");
  1405. printf("func_lret2=" FMTLX "\n", res);
  1406. asm volatile ("push %2\n"
  1407. "mov $ 1f, %%rax\n"
  1408. "push %%rax\n"
  1409. "rex64 ljmp *(%%rcx)\n"
  1410. "1:\n"
  1411. : "=a" (res)
  1412. : "c" (&desc), "b" (cs_sel)
  1413. : "memory", "cc");
  1414. printf("func_lret3=" FMTLX "\n", res);
  1415. }
  1416. #endif
  1417. #else
  1418. // XXX: Temporarily disabled: Fails to compile on newer gcc, wait for upstream fix
  1419. #if 0
  1420. asm volatile ("push %%cs ; call %1"
  1421. : "=a" (res)
  1422. : "m" (func_lret): "memory", "cc");
  1423. printf("func_lret=" FMTLX "\n", res);
  1424. asm volatile ("pushf ; push %%cs ; call %1"
  1425. : "=a" (res)
  1426. : "m" (func_iret): "memory", "cc");
  1427. printf("func_iret=" FMTLX "\n", res);
  1428. #endif
  1429. #endif
  1430. #if defined(__x86_64__)
  1431. /* specific popl test */
  1432. asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
  1433. : "=g" (res));
  1434. printf("popl esp=" FMTLX "\n", res);
  1435. #else
  1436. /* specific popl test */
  1437. asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
  1438. : "=g" (res));
  1439. printf("popl esp=" FMTLX "\n", res);
  1440. /* specific popw test */
  1441. asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
  1442. : "=g" (res));
  1443. printf("popw esp=" FMTLX "\n", res);
  1444. #endif
  1445. }
  1446. uint8_t str_buffer[4096];
  1447. #define TEST_STRING1(OP, size, DF, REP)\
  1448. {\
  1449. long esi, edi, eax, ecx, eflags;\
  1450. \
  1451. esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
  1452. edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
  1453. eax = i2l(0x12345678);\
  1454. ecx = 17;\
  1455. \
  1456. asm volatile ("push $0\n\t"\
  1457. "popf\n\t"\
  1458. DF "\n\t"\
  1459. REP #OP size "\n\t"\
  1460. "cld\n\t"\
  1461. "pushf\n\t"\
  1462. "pop %4\n\t"\
  1463. : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
  1464. : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
  1465. printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
  1466. REP #OP size, esi, edi, eax, ecx,\
  1467. (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
  1468. }
  1469. #define TEST_STRING(OP, REP)\
  1470. TEST_STRING1(OP, "b", "", REP);\
  1471. TEST_STRING1(OP, "w", "", REP);\
  1472. TEST_STRING1(OP, "l", "", REP);\
  1473. X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
  1474. TEST_STRING1(OP, "b", "std", REP);\
  1475. TEST_STRING1(OP, "w", "std", REP);\
  1476. TEST_STRING1(OP, "l", "std", REP);\
  1477. X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
  1478. void test_string(void)
  1479. {
  1480. int i;
  1481. for(i = 0;i < sizeof(str_buffer); i++)
  1482. str_buffer[i] = i + 0x56;
  1483. TEST_STRING(stos, "");
  1484. TEST_STRING(stos, "rep ");
  1485. TEST_STRING(lods, ""); /* to verify stos */
  1486. TEST_STRING(lods, "rep ");
  1487. TEST_STRING(movs, "");
  1488. TEST_STRING(movs, "rep ");
  1489. TEST_STRING(lods, ""); /* to verify stos */
  1490. /* XXX: better tests */
  1491. TEST_STRING(scas, "");
  1492. TEST_STRING(scas, "repz ");
  1493. TEST_STRING(scas, "repnz ");
  1494. TEST_STRING(cmps, "");
  1495. TEST_STRING(cmps, "repz ");
  1496. TEST_STRING(cmps, "repnz ");
  1497. }
  1498. #ifdef TEST_VM86
  1499. /* VM86 test */
  1500. static inline void set_bit(uint8_t *a, unsigned int bit)
  1501. {
  1502. a[bit / 8] |= (1 << (bit % 8));
  1503. }
  1504. static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
  1505. {
  1506. return (uint8_t *)((seg << 4) + (reg & 0xffff));
  1507. }
  1508. static inline void pushw(struct vm86_regs *r, int val)
  1509. {
  1510. r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
  1511. *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
  1512. }
  1513. static inline int vm86(int func, struct vm86plus_struct *v86)
  1514. {
  1515. return syscall(__NR_vm86, func, v86);
  1516. }
  1517. extern char vm86_code_start;
  1518. extern char vm86_code_end;
  1519. #define VM86_CODE_CS 0x100
  1520. #define VM86_CODE_IP 0x100
  1521. void test_vm86(void)
  1522. {
  1523. struct vm86plus_struct ctx;
  1524. struct vm86_regs *r;
  1525. uint8_t *vm86_mem;
  1526. int seg, ret;
  1527. vm86_mem = mmap((void *)0x00000000, 0x110000,
  1528. PROT_WRITE | PROT_READ | PROT_EXEC,
  1529. MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
  1530. if (vm86_mem == MAP_FAILED) {
  1531. printf("ERROR: could not map vm86 memory");
  1532. return;
  1533. }
  1534. memset(&ctx, 0, sizeof(ctx));
  1535. /* init basic registers */
  1536. r = &ctx.regs;
  1537. r->eip = VM86_CODE_IP;
  1538. r->esp = 0xfffe;
  1539. seg = VM86_CODE_CS;
  1540. r->cs = seg;
  1541. r->ss = seg;
  1542. r->ds = seg;
  1543. r->es = seg;
  1544. r->fs = seg;
  1545. r->gs = seg;
  1546. //r->eflags = VIF_MASK;
  1547. /* move code to proper address. We use the same layout as a .com
  1548. dos program. */
  1549. memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
  1550. &vm86_code_start, &vm86_code_end - &vm86_code_start);
  1551. /* mark int 0x21 as being emulated */
  1552. set_bit((uint8_t *)&ctx.int_revectored, 0x21);
  1553. for(;;) {
  1554. ret = vm86(VM86_ENTER, &ctx);
  1555. switch(VM86_TYPE(ret)) {
  1556. case VM86_INTx:
  1557. {
  1558. int int_num, ah, v;
  1559. int_num = VM86_ARG(ret);
  1560. if (int_num != 0x21)
  1561. goto unknown_int;
  1562. ah = (r->eax >> 8) & 0xff;
  1563. switch(ah) {
  1564. case 0x00: /* exit */
  1565. goto the_end;
  1566. case 0x02: /* write char */
  1567. {
  1568. uint8_t c = r->edx;
  1569. putchar(c);
  1570. }
  1571. break;
  1572. case 0x09: /* write string */
  1573. {
  1574. uint8_t c, *ptr;
  1575. ptr = seg_to_linear(r->ds, r->edx);
  1576. for(;;) {
  1577. c = *ptr++;
  1578. if (c == '$')
  1579. break;
  1580. putchar(c);
  1581. }
  1582. r->eax = (r->eax & ~0xff) | '$';
  1583. }
  1584. break;
  1585. case 0xff: /* extension: write eflags number in edx */
  1586. v = (int)r->edx;
  1587. #ifndef LINUX_VM86_IOPL_FIX
  1588. v &= ~0x3000;
  1589. #endif
  1590. printf("%08x\n", v);
  1591. break;
  1592. default:
  1593. unknown_int:
  1594. printf("unsupported int 0x%02x\n", int_num);
  1595. goto the_end;
  1596. }
  1597. }
  1598. break;
  1599. case VM86_SIGNAL:
  1600. /* a signal came, we just ignore that */
  1601. break;
  1602. case VM86_STI:
  1603. break;
  1604. default:
  1605. printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
  1606. goto the_end;
  1607. }
  1608. }
  1609. the_end:
  1610. printf("VM86 end\n");
  1611. munmap(vm86_mem, 0x110000);
  1612. }
  1613. #endif
  1614. /* exception tests */
  1615. #if defined(__i386__) && !defined(REG_EAX)
  1616. #define REG_EAX EAX
  1617. #define REG_EBX EBX
  1618. #define REG_ECX ECX
  1619. #define REG_EDX EDX
  1620. #define REG_ESI ESI
  1621. #define REG_EDI EDI
  1622. #define REG_EBP EBP
  1623. #define REG_ESP ESP
  1624. #define REG_EIP EIP
  1625. #define REG_EFL EFL
  1626. #define REG_TRAPNO TRAPNO
  1627. #define REG_ERR ERR
  1628. #endif
  1629. #if defined(__x86_64__)
  1630. #define REG_EIP REG_RIP
  1631. #endif
  1632. jmp_buf jmp_env;
  1633. int v1;
  1634. int tab[2];
  1635. void sig_handler(int sig, siginfo_t *info, void *puc)
  1636. {
  1637. ucontext_t *uc = puc;
  1638. printf("si_signo=%d si_errno=%d si_code=%d",
  1639. info->si_signo, info->si_errno, info->si_code);
  1640. printf(" si_addr=0x%08lx",
  1641. (unsigned long)info->si_addr);
  1642. printf("\n");
  1643. printf("trapno=" FMTLX " err=" FMTLX,
  1644. (long)uc->uc_mcontext.gregs[REG_TRAPNO],
  1645. (long)uc->uc_mcontext.gregs[REG_ERR]);
  1646. printf(" EIP=" FMTLX, (long)uc->uc_mcontext.gregs[REG_EIP]);
  1647. printf("\n");
  1648. longjmp(jmp_env, 1);
  1649. }
  1650. void test_exceptions(void)
  1651. {
  1652. struct sigaction act;
  1653. volatile int val;
  1654. act.sa_sigaction = sig_handler;
  1655. sigemptyset(&act.sa_mask);
  1656. act.sa_flags = SA_SIGINFO | SA_NODEFER;
  1657. sigaction(SIGFPE, &act, NULL);
  1658. sigaction(SIGILL, &act, NULL);
  1659. sigaction(SIGSEGV, &act, NULL);
  1660. sigaction(SIGBUS, &act, NULL);
  1661. sigaction(SIGTRAP, &act, NULL);
  1662. /* test division by zero reporting */
  1663. printf("DIVZ exception:\n");
  1664. if (setjmp(jmp_env) == 0) {
  1665. /* now divide by zero */
  1666. v1 = 0;
  1667. v1 = 2 / v1;
  1668. }
  1669. #if 0
  1670. #if !defined(__x86_64__)
  1671. printf("BOUND exception:\n");
  1672. if (setjmp(jmp_env) == 0) {
  1673. /* bound exception */
  1674. tab[0] = 1;
  1675. tab[1] = 10;
  1676. asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
  1677. }
  1678. #endif
  1679. #endif
  1680. #ifdef TEST_SEGS
  1681. printf("segment exceptions:\n");
  1682. if (setjmp(jmp_env) == 0) {
  1683. /* load an invalid segment */
  1684. asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
  1685. }
  1686. if (setjmp(jmp_env) == 0) {
  1687. /* null data segment is valid */
  1688. asm volatile ("movl %0, %%fs" : : "r" (3));
  1689. /* null stack segment */
  1690. asm volatile ("movl %0, %%ss" : : "r" (3));
  1691. }
  1692. {
  1693. struct modify_ldt_ldt_s ldt;
  1694. ldt.entry_number = 1;
  1695. ldt.base_addr = (unsigned long)&seg_data1;
  1696. ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
  1697. ldt.seg_32bit = 1;
  1698. ldt.contents = MODIFY_LDT_CONTENTS_DATA;
  1699. ldt.read_exec_only = 0;
  1700. ldt.limit_in_pages = 1;
  1701. ldt.seg_not_present = 1;
  1702. ldt.useable = 1;
  1703. modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
  1704. if (setjmp(jmp_env) == 0) {
  1705. /* segment not present */
  1706. asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
  1707. }
  1708. }
  1709. #endif
  1710. /* test SEGV reporting */
  1711. printf("PF exception:\n");
  1712. if (setjmp(jmp_env) == 0) {
  1713. val = 1;
  1714. /* we add a nop to test a weird PC retrieval case */
  1715. asm volatile ("nop");
  1716. /* now store in an invalid address */
  1717. *(char *)0x1234 = 1;
  1718. }
  1719. /* test SEGV reporting */
  1720. printf("PF exception:\n");
  1721. if (setjmp(jmp_env) == 0) {
  1722. val = 1;
  1723. /* read from an invalid address */
  1724. v1 = *(char *)0x1234;
  1725. }
  1726. /* test illegal instruction reporting */
  1727. printf("UD2 exception:\n");
  1728. if (setjmp(jmp_env) == 0) {
  1729. /* now execute an invalid instruction */
  1730. asm volatile("ud2");
  1731. }
  1732. #if 0
  1733. printf("lock nop exception:\n");
  1734. if (setjmp(jmp_env) == 0) {
  1735. /* now execute an invalid instruction */
  1736. asm volatile(".byte 0xf0, 0x90");
  1737. }
  1738. #endif
  1739. printf("INT exception:\n");
  1740. if (setjmp(jmp_env) == 0) {
  1741. asm volatile ("int $0xfd");
  1742. }
  1743. if (setjmp(jmp_env) == 0) {
  1744. asm volatile ("int $0x01");
  1745. }
  1746. if (setjmp(jmp_env) == 0) {
  1747. asm volatile (".byte 0xcd, 0x03");
  1748. }
  1749. if (setjmp(jmp_env) == 0) {
  1750. asm volatile ("int $0x04");
  1751. }
  1752. if (setjmp(jmp_env) == 0) {
  1753. asm volatile ("int $0x05");
  1754. }
  1755. printf("INT3 exception:\n");
  1756. if (setjmp(jmp_env) == 0) {
  1757. asm volatile ("int3");
  1758. }
  1759. printf("CLI exception:\n");
  1760. if (setjmp(jmp_env) == 0) {
  1761. asm volatile ("cli");
  1762. }
  1763. printf("STI exception:\n");
  1764. if (setjmp(jmp_env) == 0) {
  1765. asm volatile ("cli");
  1766. }
  1767. #if !defined(__x86_64__)
  1768. printf("INTO exception:\n");
  1769. if (setjmp(jmp_env) == 0) {
  1770. /* overflow exception */
  1771. asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
  1772. }
  1773. #endif
  1774. printf("OUTB exception:\n");
  1775. if (setjmp(jmp_env) == 0) {
  1776. asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
  1777. }
  1778. printf("INB exception:\n");
  1779. if (setjmp(jmp_env) == 0) {
  1780. asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
  1781. }
  1782. printf("REP OUTSB exception:\n");
  1783. if (setjmp(jmp_env) == 0) {
  1784. asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
  1785. }
  1786. printf("REP INSB exception:\n");
  1787. if (setjmp(jmp_env) == 0) {
  1788. asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
  1789. }
  1790. printf("HLT exception:\n");
  1791. if (setjmp(jmp_env) == 0) {
  1792. asm volatile ("hlt");
  1793. }
  1794. #if 0
  1795. printf("single step exception:\n");
  1796. val = 0;
  1797. if (setjmp(jmp_env) == 0) {
  1798. asm volatile ("pushf\n"
  1799. "orl $0x00100, (%%esp)\n"
  1800. "popf\n"
  1801. "movl $0xabcd, %0\n"
  1802. "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
  1803. }
  1804. printf("val=0x%x\n", val);
  1805. #endif
  1806. }
  1807. #if !defined(__x86_64__)
  1808. /* specific precise single step test */
  1809. void sig_trap_handler(int sig, siginfo_t *info, void *puc)
  1810. {
  1811. ucontext_t *uc = puc;
  1812. printf("EIP=" FMTLX "\n", (long)uc->uc_mcontext.gregs[REG_EIP]);
  1813. }
  1814. const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
  1815. uint8_t sstep_buf2[4];
  1816. void test_single_step(void)
  1817. {
  1818. struct sigaction act;
  1819. volatile int val;
  1820. int i;
  1821. val = 0;
  1822. act.sa_sigaction = sig_trap_handler;
  1823. sigemptyset(&act.sa_mask);
  1824. act.sa_flags = SA_SIGINFO;
  1825. sigaction(SIGTRAP, &act, NULL);
  1826. asm volatile ("pushf\n"
  1827. "orl $0x00100, (%%esp)\n"
  1828. "popf\n"
  1829. "movl $0xabcd, %0\n"
  1830. /* jmp test */
  1831. "movl $3, %%ecx\n"
  1832. "1:\n"
  1833. "addl $1, %0\n"
  1834. "decl %%ecx\n"
  1835. "jnz 1b\n"
  1836. /* movsb: the single step should stop at each movsb iteration */
  1837. "movl $sstep_buf1, %%esi\n"
  1838. "movl $sstep_buf2, %%edi\n"
  1839. "movl $0, %%ecx\n"
  1840. "rep movsb\n"
  1841. "movl $3, %%ecx\n"
  1842. "rep movsb\n"
  1843. "movl $1, %%ecx\n"
  1844. "rep movsb\n"
  1845. /* cmpsb: the single step should stop at each cmpsb iteration */
  1846. "movl $sstep_buf1, %%esi\n"
  1847. "movl $sstep_buf2, %%edi\n"
  1848. "movl $0, %%ecx\n"
  1849. "rep cmpsb\n"
  1850. "movl $4, %%ecx\n"
  1851. "rep cmpsb\n"
  1852. /* getpid() syscall: single step should skip one
  1853. instruction */
  1854. "movl $20, %%eax\n"
  1855. "int $0x80\n"
  1856. "movl $0, %%eax\n"
  1857. /* when modifying SS, trace is not done on the next
  1858. instruction */
  1859. "movl %%ss, %%ecx\n"
  1860. "movl %%ecx, %%ss\n"
  1861. "addl $1, %0\n"
  1862. "movl $1, %%eax\n"
  1863. "movl %%ecx, %%ss\n"
  1864. "jmp 1f\n"
  1865. "addl $1, %0\n"
  1866. "1:\n"
  1867. "movl $1, %%eax\n"
  1868. "pushl %%ecx\n"
  1869. "popl %%ss\n"
  1870. "addl $1, %0\n"
  1871. "movl $1, %%eax\n"
  1872. "pushf\n"
  1873. "andl $~0x00100, (%%esp)\n"
  1874. "popf\n"
  1875. : "=m" (val)
  1876. :
  1877. : "cc", "memory", "eax", "ecx", "esi", "edi");
  1878. printf("val=%d\n", val);
  1879. for(i = 0; i < 4; i++)
  1880. printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
  1881. }
  1882. /* self modifying code test */
  1883. uint8_t code[] = {
  1884. 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
  1885. 0xc3, /* ret */
  1886. };
  1887. asm(".section \".data\"\n"
  1888. "smc_code2:\n"
  1889. "movl 4(%esp), %eax\n"
  1890. "movl %eax, smc_patch_addr2 + 1\n"
  1891. "nop\n"
  1892. "nop\n"
  1893. "nop\n"
  1894. "nop\n"
  1895. "nop\n"
  1896. "nop\n"
  1897. "nop\n"
  1898. "nop\n"
  1899. "smc_patch_addr2:\n"
  1900. "movl $1, %eax\n"
  1901. "ret\n"
  1902. ".previous\n"
  1903. );
  1904. typedef int FuncType(void);
  1905. extern int smc_code2(int);
  1906. void test_self_modifying_code(void)
  1907. {
  1908. int i;
  1909. printf("self modifying code:\n");
  1910. printf("func1 = 0x%x\n", ((FuncType *)code)());
  1911. for(i = 2; i <= 4; i++) {
  1912. code[1] = i;
  1913. printf("func%d = 0x%x\n", i, ((FuncType *)code)());
  1914. }
  1915. /* more difficult test : the modified code is just after the
  1916. modifying instruction. It is forbidden in Intel specs, but it
  1917. is used by old DOS programs */
  1918. for(i = 2; i <= 4; i++) {
  1919. printf("smc_code2(%d) = %d\n", i, smc_code2(i));
  1920. }
  1921. }
  1922. #endif
  1923. long enter_stack[4096];
  1924. #if defined(__x86_64__)
  1925. #define RSP "%%rsp"
  1926. #define RBP "%%rbp"
  1927. #else
  1928. #define RSP "%%esp"
  1929. #define RBP "%%ebp"
  1930. #endif
  1931. #if !defined(__x86_64__)
  1932. /* causes an infinite loop, disable it for now. */
  1933. #define TEST_ENTER(size, stack_type, level)
  1934. #else
  1935. #define TEST_ENTER(size, stack_type, level)\
  1936. {\
  1937. long esp_save, esp_val, ebp_val, ebp_save, i;\
  1938. stack_type *ptr, *stack_end, *stack_ptr;\
  1939. memset(enter_stack, 0, sizeof(enter_stack));\
  1940. stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
  1941. ebp_val = (long)stack_ptr;\
  1942. for(i=1;i<=32;i++)\
  1943. *--stack_ptr = i;\
  1944. esp_val = (long)stack_ptr;\
  1945. asm("mov " RSP ", %[esp_save]\n"\
  1946. "mov " RBP ", %[ebp_save]\n"\
  1947. "mov %[esp_val], " RSP "\n"\
  1948. "mov %[ebp_val], " RBP "\n"\
  1949. "enter" size " $8, $" #level "\n"\
  1950. "mov " RSP ", %[esp_val]\n"\
  1951. "mov " RBP ", %[ebp_val]\n"\
  1952. "mov %[esp_save], " RSP "\n"\
  1953. "mov %[ebp_save], " RBP "\n"\
  1954. : [esp_save] "=r" (esp_save),\
  1955. [ebp_save] "=r" (ebp_save),\
  1956. [esp_val] "=r" (esp_val),\
  1957. [ebp_val] "=r" (ebp_val)\
  1958. : "[esp_val]" (esp_val),\
  1959. "[ebp_val]" (ebp_val));\
  1960. printf("level=%d:\n", level);\
  1961. printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
  1962. printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
  1963. for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
  1964. printf(FMTLX "\n", (long)ptr[0]);\
  1965. }
  1966. #endif
  1967. static void test_enter(void)
  1968. {
  1969. #if defined(__x86_64__)
  1970. TEST_ENTER("q", uint64_t, 0);
  1971. TEST_ENTER("q", uint64_t, 1);
  1972. TEST_ENTER("q", uint64_t, 2);
  1973. TEST_ENTER("q", uint64_t, 31);
  1974. #else
  1975. TEST_ENTER("l", uint32_t, 0);
  1976. TEST_ENTER("l", uint32_t, 1);
  1977. TEST_ENTER("l", uint32_t, 2);
  1978. TEST_ENTER("l", uint32_t, 31);
  1979. #endif
  1980. TEST_ENTER("w", uint16_t, 0);
  1981. TEST_ENTER("w", uint16_t, 1);
  1982. TEST_ENTER("w", uint16_t, 2);
  1983. TEST_ENTER("w", uint16_t, 31);
  1984. }
  1985. #ifdef TEST_SSE
  1986. typedef int __m64 __attribute__ ((__mode__ (__V2SI__)));
  1987. typedef float __m128 __attribute__ ((__mode__(__V4SF__)));
  1988. typedef union {
  1989. double d[2];
  1990. float s[4];
  1991. uint32_t l[4];
  1992. uint64_t q[2];
  1993. __m128 dq;
  1994. } XMMReg;
  1995. static uint64_t __attribute__((aligned(16))) test_values[4][2] = {
  1996. { 0x456723c698694873, 0xdc515cff944a58ec },
  1997. { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
  1998. { 0x007c62c2085427f8, 0x231be9e8cde7438d },
  1999. { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
  2000. };
  2001. #define SSE_OP(op) {}
  2002. /* \
  2003. {\
  2004. asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
  2005. printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
  2006. #op,\
  2007. a.q[1], a.q[0],\
  2008. b.q[1], b.q[0],\
  2009. r.q[1], r.q[0]);\
  2010. }
  2011. */
  2012. #define SSE_OP2(op) {}
  2013. /* \
  2014. {\
  2015. int i;\
  2016. for(i=0;i<2;i++) {\
  2017. a.q[0] = test_values[2*i][0];\
  2018. a.q[1] = test_values[2*i][1];\
  2019. b.q[0] = test_values[2*i+1][0];\
  2020. b.q[1] = test_values[2*i+1][1];\
  2021. SSE_OP(op);\
  2022. }\
  2023. }
  2024. */
  2025. #define MMX_OP2(op)\
  2026. {\
  2027. int i;\
  2028. for(i=0;i<2;i++) {\
  2029. a.q[0] = test_values[2*i][0];\
  2030. b.q[0] = test_values[2*i+1][0];\
  2031. asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
  2032. printf("%-9s: a=" FMT64X " b=" FMT64X " r=" FMT64X "\n",\
  2033. #op,\
  2034. a.q[0],\
  2035. b.q[0],\
  2036. r.q[0]);\
  2037. }\
  2038. SSE_OP2(op);\
  2039. }
  2040. #define SHUF_OP(op, ib)\
  2041. {\
  2042. int i;\
  2043. for(i=0;i<2;i++) {\
  2044. a.q[0] = test_values[2*i][0];\
  2045. b.q[0] = test_values[2*i+1][0];\
  2046. asm volatile (#op " $" #ib ", %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0])); \
  2047. printf("%-9s: a=" FMT64X " b=" FMT64X " ib=%02x r=" FMT64X "\n",\
  2048. #op,\
  2049. a.q[0],\
  2050. b.q[0],\
  2051. ib,\
  2052. r.q[0]);\
  2053. }\
  2054. }
  2055. /*
  2056. #define SHUF_OP(op, ib)\
  2057. {\
  2058. a.q[0] = test_values[0][0];\
  2059. a.q[1] = test_values[0][1];\
  2060. b.q[0] = test_values[1][0];\
  2061. b.q[1] = test_values[1][1];\
  2062. asm volatile (#op " $" #ib ", %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
  2063. printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
  2064. #op,\
  2065. a.q[1], a.q[0],\
  2066. b.q[1], b.q[0],\
  2067. ib,\
  2068. r.q[1], r.q[0]);\
  2069. }
  2070. */
  2071. #define PSHUF_OP(op, ib)\
  2072. {\
  2073. int i;\
  2074. for(i=0;i<2;i++) {\
  2075. a.q[0] = test_values[2*i][0];\
  2076. a.q[1] = test_values[2*i][1];\
  2077. asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
  2078. printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
  2079. #op,\
  2080. a.q[1], a.q[0],\
  2081. ib,\
  2082. r.q[1], r.q[0]);\
  2083. }\
  2084. }
  2085. // To use mm0-7 registers instead of xmm registers
  2086. #define SHIFT_IM(op, ib) \
  2087. {\
  2088. int i;\
  2089. for(i=0;i<2;i++) {\
  2090. a.q[0] = test_values[2*i][0];\
  2091. asm volatile (#op " $" #ib ", %0" : "=y" (r.q[0]) : "0" (a.q[0]));\
  2092. printf("%-9s: a=" FMT64X " ib=%02x r=" FMT64X "\n",\
  2093. #op,\
  2094. a.q[0],\
  2095. ib,\
  2096. r.q[0]);\
  2097. }\
  2098. }
  2099. /*
  2100. #define SHIFT_IM(op, ib)\
  2101. {\
  2102. int i;\
  2103. for(i=0;i<2;i++) {\
  2104. a.q[0] = test_values[2*i][0];\
  2105. a.q[1] = test_values[2*i][1];\
  2106. asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
  2107. printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
  2108. #op,\
  2109. a.q[1], a.q[0],\
  2110. ib,\
  2111. r.q[1], r.q[0]);\
  2112. }\
  2113. }
  2114. */
  2115. // To use mm0-7 registers instead of xmm registers
  2116. #define SHIFT_OP(op, ib)\
  2117. {\
  2118. int i;\
  2119. SHIFT_IM(op, ib);\
  2120. for(i=0;i<2;i++) {\
  2121. a.q[0] = test_values[2*i][0];\
  2122. b.q[0] = ib;\
  2123. asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
  2124. printf("%-9s: a=" FMT64X " b=" FMT64X " ib=%02x r=" FMT64X "\n",\
  2125. #op,\
  2126. a.q[0],\
  2127. b.q[0],\
  2128. ib,\
  2129. r.q[0]);\
  2130. }\
  2131. }
  2132. /*
  2133. #define SHIFT_OP(op, ib)\
  2134. {\
  2135. int i;\
  2136. SHIFT_IM(op, ib);\
  2137. for(i=0;i<2;i++) {\
  2138. a.q[0] = test_values[2*i][0];\
  2139. a.q[1] = test_values[2*i][1];\
  2140. b.q[0] = ib;\
  2141. b.q[1] = 0;\
  2142. asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
  2143. printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
  2144. #op,\
  2145. a.q[1], a.q[0],\
  2146. b.q[1], b.q[0],\
  2147. r.q[1], r.q[0]);\
  2148. }\
  2149. }
  2150. */
  2151. #define MOVMSK(op)\
  2152. {\
  2153. int i, reg;\
  2154. for(i=0;i<2;i++) {\
  2155. a.q[0] = test_values[2*i][0];\
  2156. a.q[1] = test_values[2*i][1];\
  2157. asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
  2158. printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
  2159. #op,\
  2160. a.q[1], a.q[0],\
  2161. reg);\
  2162. }\
  2163. }
  2164. #define SSE_OPS(a) \
  2165. SSE_OP(a ## ps);\
  2166. SSE_OP(a ## ss);
  2167. #define SSE_OPD(a) \
  2168. SSE_OP(a ## pd);\
  2169. SSE_OP(a ## sd);
  2170. #define SSE_COMI(op, field)\
  2171. {\
  2172. unsigned int eflags;\
  2173. XMMReg a, b;\
  2174. a.field[0] = a1;\
  2175. b.field[0] = b1;\
  2176. asm volatile (#op " %2, %1\n"\
  2177. "pushf\n"\
  2178. "pop %0\n"\
  2179. : "=m" (eflags)\
  2180. : "x" (a.dq), "x" (b.dq));\
  2181. printf("%-9s: a=%f b=%f cc=%04x\n",\
  2182. #op, a1, b1,\
  2183. eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
  2184. }
  2185. void test_sse_comi(double a1, double b1)
  2186. {
  2187. /*
  2188. SSE_COMI(ucomiss, s);
  2189. SSE_COMI(ucomisd, d);
  2190. SSE_COMI(comiss, s);
  2191. SSE_COMI(comisd, d);
  2192. */
  2193. }
  2194. #define CVT_OP_XMM(op)\
  2195. {\
  2196. asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
  2197. printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
  2198. #op,\
  2199. a.q[1], a.q[0],\
  2200. r.q[1], r.q[0]);\
  2201. }
  2202. /* Force %xmm0 usage to avoid the case where both register index are 0
  2203. to test instruction decoding more extensively */
  2204. #define CVT_OP_XMM2MMX(op)\
  2205. {\
  2206. asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq) \
  2207. : "%xmm0"); \
  2208. asm volatile("emms\n"); \
  2209. printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "\n",\
  2210. #op,\
  2211. a.q[1], a.q[0],\
  2212. r.q[0]);\
  2213. }
  2214. #define CVT_OP_MMX2XMM(op)\
  2215. {\
  2216. asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
  2217. asm volatile("emms\n"); \
  2218. printf("%-9s: a=" FMT64X " r=" FMT64X "" FMT64X "\n",\
  2219. #op,\
  2220. a.q[0],\
  2221. r.q[1], r.q[0]);\
  2222. }
  2223. #define CVT_OP_REG2XMM(op)\
  2224. {\
  2225. asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
  2226. printf("%-9s: a=%08x r=" FMT64X "" FMT64X "\n",\
  2227. #op,\
  2228. a.l[0],\
  2229. r.q[1], r.q[0]);\
  2230. }
  2231. #define CVT_OP_XMM2REG(op)\
  2232. {\
  2233. asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
  2234. printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
  2235. #op,\
  2236. a.q[1], a.q[0],\
  2237. r.l[0]);\
  2238. }
  2239. struct fpxstate {
  2240. uint16_t fpuc;
  2241. uint16_t fpus;
  2242. uint16_t fptag;
  2243. uint16_t fop;
  2244. uint32_t fpuip;
  2245. uint16_t cs_sel;
  2246. uint16_t dummy0;
  2247. uint32_t fpudp;
  2248. uint16_t ds_sel;
  2249. uint16_t dummy1;
  2250. uint32_t mxcsr;
  2251. uint32_t mxcsr_mask;
  2252. uint8_t fpregs1[8 * 16];
  2253. uint8_t xmm_regs[8 * 16];
  2254. uint8_t dummy2[224];
  2255. };
  2256. static struct fpxstate fpx_state __attribute__((aligned(16)));
  2257. static struct fpxstate fpx_state2 __attribute__((aligned(16)));
  2258. void test_fxsave(void)
  2259. {
  2260. struct fpxstate *fp = &fpx_state;
  2261. struct fpxstate *fp2 = &fpx_state2;
  2262. int i, nb_xmm;
  2263. XMMReg a, b;
  2264. a.q[0] = test_values[0][0];
  2265. a.q[1] = test_values[0][1];
  2266. b.q[0] = test_values[1][0];
  2267. b.q[1] = test_values[1][1];
  2268. asm("movdqa %2, %%xmm0\n"
  2269. "movdqa %3, %%xmm7\n"
  2270. #if defined(__x86_64__)
  2271. "movdqa %2, %%xmm15\n"
  2272. #endif
  2273. " fld1\n"
  2274. " fld1\n"
  2275. " fldz\n"
  2276. " fxsave %0\n"
  2277. " fxrstor %0\n"
  2278. " fxsave %1\n"
  2279. " fninit\n"
  2280. : "=m" (*(uint32_t *)fp2), "=m" (*(uint32_t *)fp)
  2281. : "m" (a), "m" (b));
  2282. printf("fpuc=%04x\n", fp->fpuc);
  2283. printf("fpus=%04x\n", fp->fpus);
  2284. printf("fptag=%04x\n", fp->fptag);
  2285. for(i = 0; i < 3; i++) {
  2286. printf("ST%d: " FMT64X " %04x\n",
  2287. i,
  2288. *(uint64_t *)&fp->fpregs1[i * 16],
  2289. *(uint16_t *)&fp->fpregs1[i * 16 + 8]);
  2290. }
  2291. printf("mxcsr=%08x\n", fp->mxcsr & 0x1f80);
  2292. #if defined(__x86_64__)
  2293. nb_xmm = 16;
  2294. #else
  2295. nb_xmm = 8;
  2296. #endif
  2297. for(i = 0; i < nb_xmm; i++) {
  2298. printf("xmm%d: " FMT64X "" FMT64X "\n",
  2299. i,
  2300. *(uint64_t *)&fp->xmm_regs[i * 16],
  2301. *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);
  2302. }
  2303. }
  2304. void test_sse(void)
  2305. {
  2306. XMMReg r, a, b;
  2307. int i;
  2308. MMX_OP2(punpcklbw);
  2309. MMX_OP2(punpcklwd);
  2310. MMX_OP2(punpckldq);
  2311. MMX_OP2(packsswb);
  2312. MMX_OP2(pcmpgtb);
  2313. MMX_OP2(pcmpgtw);
  2314. MMX_OP2(pcmpgtd);
  2315. MMX_OP2(packuswb);
  2316. MMX_OP2(punpckhbw);
  2317. MMX_OP2(punpckhwd);
  2318. MMX_OP2(punpckhdq);
  2319. MMX_OP2(packssdw);
  2320. MMX_OP2(pcmpeqb);
  2321. MMX_OP2(pcmpeqw);
  2322. MMX_OP2(pcmpeqd);
  2323. // MMX_OP2(paddq);
  2324. MMX_OP2(pmullw);
  2325. MMX_OP2(psubusb);
  2326. MMX_OP2(psubusw);
  2327. // MMX_OP2(pminub);
  2328. MMX_OP2(pand);
  2329. MMX_OP2(paddusb);
  2330. MMX_OP2(paddusw);
  2331. // MMX_OP2(pmaxub);
  2332. MMX_OP2(pandn);
  2333. // MMX_OP2(pmulhuw);
  2334. MMX_OP2(pmulhw);
  2335. MMX_OP2(psubsb);
  2336. MMX_OP2(psubsw);
  2337. // MMX_OP2(pminsw);
  2338. MMX_OP2(por);
  2339. MMX_OP2(paddsb);
  2340. MMX_OP2(paddsw);
  2341. // MMX_OP2(pmaxsw);
  2342. MMX_OP2(pxor);
  2343. // MMX_OP2(pmuludq);
  2344. MMX_OP2(pmaddwd);
  2345. // MMX_OP2(psadbw);
  2346. MMX_OP2(psubb);
  2347. MMX_OP2(psubw);
  2348. MMX_OP2(psubd);
  2349. // MMX_OP2(psubq);
  2350. MMX_OP2(paddb);
  2351. MMX_OP2(paddw);
  2352. MMX_OP2(psrlw);
  2353. MMX_OP2(paddd);
  2354. /*
  2355. MMX_OP2(pavgb);
  2356. MMX_OP2(pavgw);
  2357. asm volatile ("pinsrw $1, %1, %0" : "=y" (r.q[0]) : "r" (0x12345678));
  2358. printf("%-9s: r=" FMT64X "\n", "pinsrw", r.q[0]);
  2359. asm volatile ("pinsrw $5, %1, %0" : "=x" (r.dq) : "r" (0x12345678));
  2360. printf("%-9s: r=" FMT64X "" FMT64X "\n", "pinsrw", r.q[1], r.q[0]);
  2361. a.q[0] = test_values[0][0];
  2362. a.q[1] = test_values[0][1];
  2363. asm volatile ("pextrw $1, %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
  2364. printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
  2365. asm volatile ("pextrw $5, %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
  2366. printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
  2367. asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
  2368. printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
  2369. asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
  2370. printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
  2371. {
  2372. r.q[0] = -1;
  2373. r.q[1] = -1;
  2374. a.q[0] = test_values[0][0];
  2375. a.q[1] = test_values[0][1];
  2376. b.q[0] = test_values[1][0];
  2377. b.q[1] = test_values[1][1];
  2378. asm volatile("maskmovq %1, %0" :
  2379. : "y" (a.q[0]), "y" (b.q[0]), "D" (&r)
  2380. : "memory");
  2381. printf("%-9s: r=" FMT64X " a=" FMT64X " b=" FMT64X "\n",
  2382. "maskmov",
  2383. r.q[0],
  2384. a.q[0],
  2385. b.q[0]);
  2386. asm volatile("maskmovdqu %1, %0" :
  2387. : "x" (a.dq), "x" (b.dq), "D" (&r)
  2388. : "memory");
  2389. printf("%-9s: r=" FMT64X "" FMT64X " a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X "\n",
  2390. "maskmov",
  2391. r.q[1], r.q[0],
  2392. a.q[1], a.q[0],
  2393. b.q[1], b.q[0]);
  2394. }
  2395. asm volatile ("emms");
  2396. SSE_OP2(punpcklqdq);
  2397. SSE_OP2(punpckhqdq);
  2398. SSE_OP2(andps);
  2399. SSE_OP2(andpd);
  2400. SSE_OP2(andnps);
  2401. SSE_OP2(andnpd);
  2402. SSE_OP2(orps);
  2403. SSE_OP2(orpd);
  2404. SSE_OP2(xorps);
  2405. SSE_OP2(xorpd);
  2406. SSE_OP2(unpcklps);
  2407. SSE_OP2(unpcklpd);
  2408. SSE_OP2(unpckhps);
  2409. SSE_OP2(unpckhpd);
  2410. SHUF_OP(shufps, 0x78);
  2411. SHUF_OP(shufpd, 0x02);
  2412. */
  2413. SHUF_OP(pshufw, 0x78);
  2414. SHUF_OP(pshufw, 0x02);
  2415. /*
  2416. PSHUF_OP(pshufd, 0x78);
  2417. PSHUF_OP(pshuflw, 0x78);
  2418. PSHUF_OP(pshufhw, 0x78);
  2419. */
  2420. SHIFT_OP(psrlw, 7);
  2421. SHIFT_OP(psrlw, 16);
  2422. SHIFT_OP(psraw, 7);
  2423. SHIFT_OP(psraw, 16);
  2424. SHIFT_OP(psllw, 7);
  2425. SHIFT_OP(psllw, 16);
  2426. SHIFT_OP(psrld, 7);
  2427. SHIFT_OP(psrld, 32);
  2428. SHIFT_OP(psrad, 7);
  2429. SHIFT_OP(psrad, 32);
  2430. SHIFT_OP(pslld, 7);
  2431. SHIFT_OP(pslld, 32);
  2432. SHIFT_OP(psrlq, 7);
  2433. SHIFT_OP(psrlq, 32);
  2434. SHIFT_OP(psllq, 7);
  2435. SHIFT_OP(psllq, 32);
  2436. /*
  2437. SHIFT_IM(psrldq, 16);
  2438. SHIFT_IM(psrldq, 7);
  2439. SHIFT_IM(pslldq, 16);
  2440. SHIFT_IM(pslldq, 7);
  2441. MOVMSK(movmskps);
  2442. MOVMSK(movmskpd);
  2443. */
  2444. /* FPU specific ops */
  2445. /*
  2446. {
  2447. uint32_t mxcsr;
  2448. asm volatile("stmxcsr %0" : "=m" (mxcsr));
  2449. printf("mxcsr=%08x\n", mxcsr & 0x1f80);
  2450. asm volatile("ldmxcsr %0" : : "m" (mxcsr));
  2451. }
  2452. test_sse_comi(2, -1);
  2453. test_sse_comi(2, 2);
  2454. test_sse_comi(2, 3);
  2455. test_sse_comi(2, q_nan.d);
  2456. test_sse_comi(q_nan.d, -1);
  2457. for(i = 0; i < 2; i++) {
  2458. a.s[0] = 2.7;
  2459. a.s[1] = 3.4;
  2460. a.s[2] = 4;
  2461. a.s[3] = -6.3;
  2462. b.s[0] = 45.7;
  2463. b.s[1] = 353.4;
  2464. b.s[2] = 4;
  2465. b.s[3] = 56.3;
  2466. if (i == 1) {
  2467. a.s[0] = q_nan.d;
  2468. b.s[3] = q_nan.d;
  2469. }
  2470. SSE_OPS(add);
  2471. SSE_OPS(mul);
  2472. SSE_OPS(sub);
  2473. SSE_OPS(min);
  2474. SSE_OPS(div);
  2475. SSE_OPS(max);
  2476. SSE_OPS(sqrt);
  2477. SSE_OPS(cmpeq);
  2478. SSE_OPS(cmplt);
  2479. SSE_OPS(cmple);
  2480. SSE_OPS(cmpunord);
  2481. SSE_OPS(cmpneq);
  2482. SSE_OPS(cmpnlt);
  2483. SSE_OPS(cmpnle);
  2484. SSE_OPS(cmpord);
  2485. a.d[0] = 2.7;
  2486. a.d[1] = -3.4;
  2487. b.d[0] = 45.7;
  2488. b.d[1] = -53.4;
  2489. if (i == 1) {
  2490. a.d[0] = q_nan.d;
  2491. b.d[1] = q_nan.d;
  2492. }
  2493. SSE_OPD(add);
  2494. SSE_OPD(mul);
  2495. SSE_OPD(sub);
  2496. SSE_OPD(min);
  2497. SSE_OPD(div);
  2498. SSE_OPD(max);
  2499. SSE_OPD(sqrt);
  2500. SSE_OPD(cmpeq);
  2501. SSE_OPD(cmplt);
  2502. SSE_OPD(cmple);
  2503. SSE_OPD(cmpunord);
  2504. SSE_OPD(cmpneq);
  2505. SSE_OPD(cmpnlt);
  2506. SSE_OPD(cmpnle);
  2507. SSE_OPD(cmpord);
  2508. }
  2509. */
  2510. /* float to float/int */
  2511. /*
  2512. a.s[0] = 2.7;
  2513. a.s[1] = 3.4;
  2514. a.s[2] = 4;
  2515. a.s[3] = -6.3;
  2516. CVT_OP_XMM(cvtps2pd);
  2517. CVT_OP_XMM(cvtss2sd);
  2518. CVT_OP_XMM2MMX(cvtps2pi);
  2519. CVT_OP_XMM2MMX(cvttps2pi);
  2520. CVT_OP_XMM2REG(cvtss2si);
  2521. CVT_OP_XMM2REG(cvttss2si);
  2522. CVT_OP_XMM(cvtps2dq);
  2523. CVT_OP_XMM(cvttps2dq);
  2524. a.d[0] = 2.6;
  2525. a.d[1] = -3.4;
  2526. CVT_OP_XMM(cvtpd2ps);
  2527. CVT_OP_XMM(cvtsd2ss);
  2528. CVT_OP_XMM2MMX(cvtpd2pi);
  2529. CVT_OP_XMM2MMX(cvttpd2pi);
  2530. CVT_OP_XMM2REG(cvtsd2si);
  2531. CVT_OP_XMM2REG(cvttsd2si);
  2532. CVT_OP_XMM(cvtpd2dq);
  2533. CVT_OP_XMM(cvttpd2dq);
  2534. */
  2535. /* sse/mmx moves */
  2536. /*
  2537. CVT_OP_XMM2MMX(movdq2q);
  2538. CVT_OP_MMX2XMM(movq2dq);
  2539. */
  2540. /* int to float */
  2541. /*
  2542. a.l[0] = -6;
  2543. a.l[1] = 2;
  2544. a.l[2] = 100;
  2545. a.l[3] = -60000;
  2546. CVT_OP_MMX2XMM(cvtpi2ps);
  2547. CVT_OP_MMX2XMM(cvtpi2pd);
  2548. CVT_OP_REG2XMM(cvtsi2ss);
  2549. CVT_OP_REG2XMM(cvtsi2sd);
  2550. CVT_OP_XMM(cvtdq2ps);
  2551. CVT_OP_XMM(cvtdq2pd);
  2552. */
  2553. /* XXX: test PNI insns */
  2554. #if 0
  2555. // SSE_OP2(movshdup);
  2556. #endif
  2557. asm volatile ("emms");
  2558. }
  2559. #endif
  2560. #define TEST_CONV_RAX(op)\
  2561. {\
  2562. unsigned long a, r;\
  2563. a = i2l(0x8234a6f8);\
  2564. r = a;\
  2565. asm volatile(#op : "=a" (r) : "0" (r));\
  2566. printf("%-10s A=" FMTLX " R=" FMTLX "\n", #op, a, r);\
  2567. }
  2568. #define TEST_CONV_RAX_RDX(op)\
  2569. {\
  2570. unsigned long a, d, r, rh; \
  2571. a = i2l(0x8234a6f8);\
  2572. d = i2l(0x8345a1f2);\
  2573. r = a;\
  2574. rh = d;\
  2575. asm volatile(#op : "=a" (r), "=d" (rh) : "0" (r), "1" (rh)); \
  2576. printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \
  2577. }
  2578. void test_conv(void)
  2579. {
  2580. TEST_CONV_RAX(cbw);
  2581. TEST_CONV_RAX(cwde);
  2582. #if defined(__x86_64__)
  2583. TEST_CONV_RAX(cdqe);
  2584. #endif
  2585. TEST_CONV_RAX_RDX(cwd);
  2586. TEST_CONV_RAX_RDX(cdq);
  2587. #if defined(__x86_64__)
  2588. TEST_CONV_RAX_RDX(cqo);
  2589. #endif
  2590. {
  2591. unsigned long a, r;
  2592. a = i2l(0x12345678);
  2593. asm volatile("bswapl %k0" : "=r" (r) : "0" (a));
  2594. printf("%-10s: A=" FMTLX " R=" FMTLX "\n", "bswapl", a, r);
  2595. }
  2596. #if defined(__x86_64__)
  2597. {
  2598. unsigned long a, r;
  2599. a = i2l(0x12345678);
  2600. asm volatile("bswapq %0" : "=r" (r) : "0" (a));
  2601. printf("%-10s: A=" FMTLX " R=" FMTLX "\n", "bswapq", a, r);
  2602. }
  2603. #endif
  2604. }
  2605. void fatal(char *msg)
  2606. {
  2607. fprintf(stderr, "*** FATAL ERROR: %s\n", (msg ? msg : "no message"));
  2608. fflush(stderr);
  2609. abort();
  2610. }
  2611. void byte_read(uint8_t* buffer, uint16_t offset, size_t num_bytes)
  2612. {
  2613. printf("%-12s: offset=%x value=", "byte_r", offset);
  2614. size_t i = num_bytes;
  2615. while(i > 0)
  2616. {
  2617. i--;
  2618. printf("%02" PRIx8, buffer[offset + i]);
  2619. }
  2620. printf("\n");
  2621. }
  2622. uint64_t seq_counter = 0x8070605040302010;
  2623. uint64_t get_seq64()
  2624. {
  2625. seq_counter += 0x0101010101010101;
  2626. return seq_counter;
  2627. }
  2628. void byte_write_seq(uint8_t* target, uint16_t offset, size_t num_bytes)
  2629. {
  2630. printf("%-12s: offset=%x value=", "byte_w", offset);
  2631. size_t i = num_bytes;
  2632. while(i > 0)
  2633. {
  2634. i--;
  2635. uint8_t byte = get_seq64();
  2636. target[offset + i] = byte;
  2637. printf("%02" PRIx8, byte);
  2638. }
  2639. printf("\n");
  2640. }
  2641. #define GENERATE_CHUNK_READ(INSTR, BITS, CONSTR) \
  2642. void chunk_read ## BITS(uint8_t* addr, uint16_t offset) \
  2643. { \
  2644. uint ## BITS ## _t chunk = 0; \
  2645. asm volatile(INSTR " %1, %0" : \
  2646. "=" CONSTR (chunk) : \
  2647. "m" (*(addr + offset)), "0" (chunk)); \
  2648. printf("%-12s: offset=%x value=%" PRIx ## BITS "\n", \
  2649. "chunk" #BITS "_r", \
  2650. offset, \
  2651. chunk); \
  2652. }
  2653. #define GENERATE_CHUNK_WRITE(INSTR, BITS, CONSTR) \
  2654. void chunk_write ## BITS(uint8_t* addr, uint16_t offset) \
  2655. { \
  2656. uint ## BITS ## _t chunk = get_seq64(); \
  2657. asm volatile(INSTR " %0, %1" : \
  2658. "=" CONSTR (chunk) : \
  2659. "m" (*(addr + offset)), "0" (chunk)); \
  2660. printf("%-12s: offset=%x value=%" PRIx ## BITS "\n", \
  2661. "chunk" #BITS "_w", \
  2662. offset, \
  2663. chunk); \
  2664. }
  2665. #define GENERATE_CHUNK_FNS(INSTR, BITS, CONSTR) \
  2666. GENERATE_CHUNK_READ(INSTR, BITS, CONSTR) \
  2667. GENERATE_CHUNK_WRITE(INSTR, BITS, CONSTR)
  2668. #define TEST_CHUNK_READ(BITS, ADDR, OFFSET) \
  2669. byte_write_seq(ADDR, OFFSET, (BITS) >> 3); \
  2670. chunk_read ## BITS(ADDR, OFFSET);
  2671. #define TEST_CHUNK_WRITE(BITS, ADDR, OFFSET) \
  2672. chunk_write ## BITS(ADDR, OFFSET); \
  2673. byte_read(ADDR, OFFSET, (BITS) >> 3);
  2674. #define TEST_CHUNK_READ_WRITE(BITS, ADDR, OFFSET) \
  2675. byte_write_seq(ADDR, OFFSET, (BITS) >> 3); \
  2676. chunk_read_write ## BITS(ADDR, OFFSET); \
  2677. byte_read(ADDR, OFFSET, (BITS) >> 3); \
  2678. // Based on BITS, we calculate the offset where cross-page reads/writes would begin
  2679. #define TEST_CROSS_PAGE(BITS, ADDR) \
  2680. for(size_t offset = (PAGE_SIZE + 1 - (BITS >> 3)); \
  2681. offset < PAGE_SIZE; offset++) \
  2682. { \
  2683. TEST_CHUNK_READ(BITS, ADDR, offset); \
  2684. TEST_CHUNK_WRITE(BITS, ADDR, offset); \
  2685. TEST_CHUNK_READ_WRITE(BITS, ADDR, offset); \
  2686. }
  2687. GENERATE_CHUNK_FNS("movw", 16, "r");
  2688. GENERATE_CHUNK_FNS("mov", 32, "r");
  2689. #ifdef TEST_SSE
  2690. GENERATE_CHUNK_FNS("movq", 64, "y");
  2691. void chunk_read_write16(uint8_t* addr, uint16_t offset)
  2692. {
  2693. uint16_t chunk = get_seq64();
  2694. asm volatile("addw %0, %1" :
  2695. "=r" (chunk) :
  2696. "m" (*(addr + offset)), "0" (chunk));
  2697. printf("%-12s: offset=%x value=%" PRIx16 "\n",
  2698. "chunk16_rw",
  2699. offset,
  2700. chunk);
  2701. }
  2702. void chunk_read_write32(uint8_t* addr, uint16_t offset)
  2703. {
  2704. uint32_t chunk = get_seq64();
  2705. asm volatile("add %0, %1" :
  2706. "=r" (chunk) :
  2707. "m" (*(addr + offset)), "0" (chunk));
  2708. printf("%-12s: offset=%x value=%" PRIx32 "\n",
  2709. "chunk32_rw",
  2710. offset,
  2711. chunk);
  2712. }
  2713. // No 64 or 128-bit read-write x86 instructions support a memory address as the destination
  2714. void chunk_read_write64(uint8_t* addr, uint16_t offset)
  2715. {
  2716. UNUSED(addr);
  2717. UNUSED(offset);
  2718. }
  2719. void chunk_read_write128(uint8_t* addr, uint16_t offset)
  2720. {
  2721. UNUSED(addr);
  2722. UNUSED(offset);
  2723. }
  2724. void chunk_read128(uint8_t* addr, uint16_t offset)
  2725. {
  2726. XMMReg chunk;
  2727. chunk.q[0] = chunk.q[1] = 0.0;
  2728. asm volatile("movdqu %1, %0" :
  2729. "=x" (chunk.dq) :
  2730. "m" (*(addr + offset)), "0" (chunk.dq)
  2731. );
  2732. printf("%-12s: offset=%x value=" FMT64X FMT64X "\n",
  2733. "chunk128_r",
  2734. offset,
  2735. chunk.q[1],
  2736. chunk.q[0]);
  2737. }
  2738. void chunk_write128(uint8_t* addr, uint16_t offset)
  2739. {
  2740. XMMReg chunk;
  2741. chunk.q[0] = get_seq64();
  2742. chunk.q[1] = get_seq64();
  2743. asm volatile("movdqu %0, %1" :
  2744. "=x" (chunk.dq) :
  2745. "m" (*(addr + offset)), "0" (chunk.dq)
  2746. );
  2747. printf("%-12s: offset=%x value=" FMT64X FMT64X "\n",
  2748. "chunk128_w",
  2749. offset,
  2750. chunk.q[1],
  2751. chunk.q[0]);
  2752. }
  2753. #endif
  2754. void test_page_boundaries()
  2755. {
  2756. // mmap 2 consecutive pages
  2757. uint8_t *const page0 = mmap(NULL, 2 * PAGE_SIZE,
  2758. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  2759. // throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames
  2760. uint8_t *const throwaway = mmap(NULL, PAGE_SIZE,
  2761. PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  2762. uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,
  2763. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  2764. if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)
  2765. {
  2766. fatal("mmap");
  2767. }
  2768. // Trigger page-faults causing virtual pages to be allocated to physical frames
  2769. page0[0] = 0x42;
  2770. throwaway[0] = 0x42;
  2771. page1[0] = 0x42;
  2772. TEST_CROSS_PAGE(16, page0);
  2773. TEST_CROSS_PAGE(32, page0);
  2774. #ifdef TEST_SSE
  2775. TEST_CROSS_PAGE(64, page0);
  2776. TEST_CROSS_PAGE(128, page0);
  2777. #endif
  2778. munmap(page0, PAGE_SIZE);
  2779. munmap(page1, PAGE_SIZE);
  2780. }
  2781. extern void *__start_initcall;
  2782. extern void *__stop_initcall;
  2783. int main(int argc, char **argv)
  2784. {
  2785. // Uncomment to disable buffering, useful for debugging segfaults
  2786. //setvbuf(stdout, NULL, _IONBF, 0);
  2787. void **ptr;
  2788. void (*func)(void);
  2789. ptr = &__start_initcall;
  2790. while (ptr != &__stop_initcall) {
  2791. func = *ptr++;
  2792. func();
  2793. }
  2794. test_bsx();
  2795. test_popcnt();
  2796. test_mul();
  2797. test_jcc();
  2798. test_loop();
  2799. test_floats();
  2800. #if !defined(__x86_64__)
  2801. test_bcd();
  2802. #endif
  2803. test_xchg();
  2804. test_string();
  2805. test_misc();
  2806. test_lea();
  2807. #ifdef TEST_SEGS
  2808. test_segs();
  2809. test_code16();
  2810. #endif
  2811. #ifdef TEST_VM86
  2812. test_vm86();
  2813. #endif
  2814. #if !defined(__x86_64__)
  2815. test_exceptions();
  2816. test_self_modifying_code();
  2817. //test_single_step();
  2818. #endif
  2819. test_enter();
  2820. test_conv();
  2821. #ifdef TEST_SSE
  2822. test_sse();
  2823. //test_fxsave();
  2824. #endif
  2825. test_page_boundaries();
  2826. return 0;
  2827. }