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"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章