Rosetta 2:从x86到ARM64

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"随着计划将Macintosh产品线从英特尔CPU转向自家CPU,苹果公司还发布了一款名为Rosetta 2的二进制转换软件,旨在让这个CPU转换过程更加顺畅。有了Rosetta 2,大多数x86应用程序都可以在经过初始转换之后在新平台上执行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2006年,苹果公司开始使用二进制转换技术,当时他们从PowerPC CPU切换到x86。Rosetta基于最初由Transitive Corporation开发、后来被IBM收购的"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/QuickTransit","title":null,"type":null},"content":[{"type":"text","text":"QuickTransit"}]},{"type":"text","text":"技术,对用户来说基本上是透明的。用户唯一能感觉到的是他们的应用程序有时会启动或运行得更慢一些。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"应用程序在第一次启动时通常会很慢。事实上,这正是Rosetta的作用所在,特别是当操作系统检测到二进制文件只包含x86_64指令时。根据Joe Rossignol在Mac Rumors上发表的文章,微软表示他们开发的Mac应用程序通常在第一次启动时大约需要"},{"type":"link","attrs":{"href":"https:\/\/www.macrumors.com\/2020\/11\/12\/rosetta-2-translation-can-take-20-seconds\/","title":null,"type":null},"content":[{"type":"text","text":"20秒"}]},{"type":"text","text":",而随后的启动速度很快。为了降低这个初始转换步骤的影响,Rosetta 2可以在安装应用程序时对其进行转换。不过,目前还不清楚Rosetta AOT翻译将支持哪些应用程序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然macOS更倾向于运行arm64指令(在可用时),但用户可以重写这种行为。例如,运行一个已经移植到苹果芯片的应用程序就必须这样,除了一些遗留的插件或用户依赖的其他类型的二进制扩展。事实上,Rosetta的一个限制是它不能在同一个进程中混合使用X86_64和arm64指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于过去使用Rosetta的经验,对于终端用户和开发人员来说,Rosetta 2有助于他们顺利过渡到苹果芯片。不过,这并不排除在很多情况下用户需要等待,直到他们期待的软件原生支持arm64。Rosetta 2不支持转换内核扩展,也不支持x86_64平台虚拟化。后者意味着Rosetta 2不会转换VMWare、VirtualBox等虚拟机,也不会转换Docker。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rosetta的一个主要问题是性能。在PowerPC转换到x86过程中,导致Rosetta变慢的一个因素是这两个平台使用了不同的内存顺序,PowerPC采用了大端(big-endian)模式,而x86采用的是小端(little-endian)模式。这个问题也会影响基于ARM的微软Surface笔记本上的x86模拟。为了防止这种情况发生,"},{"type":"link","attrs":{"href":"https:\/\/twitter.com\/ErrataRob\/status\/1331735383193903104","title":null,"type":null},"content":[{"type":"text","text":"苹果在M1 CPU上增加了对x86内存顺序的支持"}]},{"type":"text","text":",正如Robert Graham在Twitter上指出的那样:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"苹果取巧了,他们将英特尔的内存顺序添加到他们的CPU中。在运行转换后的x86代码时,它们会切换CPU的模式,遵循英特尔的内存顺序。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除此之外,Graham还介绍了苹果公司为提高芯片性能而采用的其他一些“技巧”,包括加快JavaScript执行速度、更快地保留和释放内存,等等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rosetta 2的一个特别之处在Hacker News网站上引起了一些开发者的注意,那就是它还"},{"type":"link","attrs":{"href":"https:\/\/news.ycombinator.com\/item?id=23614650","title":null,"type":null},"content":[{"type":"text","text":"可以转换包含即时编译器的应用程序"}]},{"type":"text","text":"。有关这个问题的具体机制还没有公开的文档,不过,苹果公司可能会使用页面错误来检测代码是否试图跳转到最近创建的代码页——也就是说,一个页面被设置为可写模式,然后切换到只读和可执行模式。在页面错误得到处理之后,Rosetta开始转换页面内容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后需要说明的是,应用程序可以"},{"type":"link","attrs":{"href":"https:\/\/developer.apple.com\/documentation\/apple_silicon\/about_the_rosetta_translation_environment?language=objc#3616845","title":null,"type":null},"content":[{"type":"text","text":"检测"}]},{"type":"text","text":"到它们是否运行在Rosetta模式之下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文链接"},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/11\/rosetta-2-translation\/","title":null,"type":null},"content":[{"type":"text","text":"How x86 to arm64 Translation Works in Rosetta 2"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章