作者:沈永勝,技術(shù)副哩,晶心科技股份有限公司 EDM安全存取是AndesCoreTM內建的功能(option),應用在安全存取的控管。EDM安全存取有二種的控管方式:debug access indication和EDM access restriction。第一種控管方式(debug access indication)提供了一個(gè)sideband signal用于指示從調試器(Debug host)的請求。第二種控管方式, 控制AndesCoreTM的input port(edm_restrict_access )達到EDM存取的限制。更詳細的內容在后續章節會(huì )有更深入的介紹。 1. EDM功能介紹 一個(gè)debug system包含一個(gè)debug host和一個(gè)target system。EDM主要的功能就是translate debug host發(fā)出的TAP指令來(lái)存取系統memory或是CPU。下圖為基本的debug系統方塊圖。 ![]() 圖表1 基本的debug系統方塊圖 下圖說(shuō)明TAP 指令的種類(lèi) ![]() 圖表2 TAP 指令的種類(lèi) 2. 控制EDM存取的限制 使用EDM的訪(fǎng)問(wèn)方式會(huì )被一個(gè)sideband signal (edm_restrict_access) 所影響。當這個(gè)signal值是high,僅僅只能對EDM MISC registers做讀取的動(dòng)作。而想要存取CPU/System Bus/Local Memory的動(dòng)作將會(huì )被封鎖住并且會(huì )得到下面的結果: 讀為零寫(xiě)忽略 不正確的JTAG instruction(JTAG ICE debugger會(huì )timeout) 下圖說(shuō)明EDM限制存取方塊圖。 ![]() 圖表3 EDM限制存取方塊圖 在啟用存取限制功能后,下圖說(shuō)明出每個(gè)TAP指令的行為。 ![]() 圖表4 在啟用存取限制功能后,下圖說(shuō)明出每個(gè)TAP指令的行為 如何實(shí)現EDM存取限制,在系統設計上有很多種實(shí)現方法,以控制edm restrict access的signal。兩種基本的設計方案說(shuō)明如下: eFUSE方式使用Chip重新編程管理控制 SOC方式使用軟件管理控制 hardware實(shí)現控制edm_restrict_access的示意圖如下: ![]() 圖表5 hardware實(shí)現控制edm_restrict_access的示意圖 software實(shí)現控制edm_restrict_access的例子如下: sethi $r2,#0x80000 ori $r2,$r2,#0x8c sethi $r3,#0x04030 ori $r3,$r3,#0x201 swi $r3,[$r2+#0] 3. EDM 存取指示 AndesCoreTM增加一個(gè)額外的sideband signal,xdebug_access(active-high),根據此sideband signal來(lái)決定request的host是否為EDM。而device就能根據此sideband signal決定是否要把request的data內容傳回到host。 sideband signal的名稱(chēng)根據bus interface的類(lèi)型而有所不同。對于A(yíng)ndesCoreTM處理器,基本的信號名稱(chēng)如下所示: AHB/AHB-Lite => hdebug_access APB => pdebug_access EILM => eilm_debug_access EDLM => edlm_debug_access 3.1.debug存取識別信號控制 當debug exception發(fā)生后,CPU將進(jìn)入debug mode。然后CPU將會(huì )留在debug access mode直到CPU執行到IRET instruction并且trusted_debug_exit 是處于high后CPU將離開(kāi)debug access mode,反之trusted_debug_exit如果是low,CPU將會(huì )保留在debug access mode。 實(shí)現控制trusted_debug_exit信號,有二種可供選擇的方式如下: trusted_debug_exit信號總是給high 增加一個(gè)權限管理邏輯去控制trusted_debug_exit信號是high或是low權限管理邏輯方塊圖如下所示: ![]() 圖表6 權限管理邏輯方塊圖 如何控制trusted_debug_exit信號時(shí)序圖如下所示: ![]() 圖表7 如何控制trusted_debug_exit信號時(shí)序圖 如下例子說(shuō)明了如何產(chǎn)生trusted_debug_exit控制信號的verilog code: The code example (Verilog) of trusted_debug_exit generation is described below: // //--- Utilize passcode to generate trusted_debug_exit in AHB Bus Controller //* assume zero-wait-state AHB access … parameter AUTH_CODE = 32’h0a0b0c0d; ... always @(posedge hclk or negedge hreset_n) begin if (!hreset_n) begin passcode_reg <= 32'd0; end else if (passcode_wen) begin //debugger enters passcode through debug access passcode_reg <= hwdata[31:0]; end end … //validate passcode to generate trusted_debug_exit assign trusted_debug_exit = (passcode_reg == AUTH_CODE); 3.2.debug存取指示應用 下圖說(shuō)明AHB bus如何使用hdebug_access和驗證邏輯來(lái)防止惡意的debug存取 ![]() 圖表8 AHB bus如何使用hdebug_access和驗證邏輯來(lái)防止惡意的debug存取 如下verilog code說(shuō)明了如何使用hdebug_access信號: //--- Use hdebug_access to prevent malicious debug access in AHB Bus Controller //* assume zero-wait-state AHB access … parameter IRRELEVANT_DATA = 32’hcafe0001; parameter AUTH_CODE = 32’h01020304; … always @(posedge hclk or negedge hreset_n) begin if (!hreset_n) begin dbg_acc_d1 <= 1'b0; end else begin // data phase indication of debug access dbg_acc_d1 <= hdebug_access; end end ... always @(posedge hclk or negedge hreset_n) begin if (!hreset_n) begin passcode_reg <= 32'd0; end else if (passcode_wen) begin //debugger enters passcode through debug access passcode_reg <= hwdata[31:0]; end end … //validate passcode to check authentication assign auth_check_fail = (passcode_reg != AUTH_CODE); //return irrelevant data if the authentication check of debug access fails assign hrdata_out = {32{data_read_en}} & ((dbg_acc_d1 & auth_check_fail) IRRELEVANT_DATA : normal_data_out); 4. 實(shí)際的應用 用戶(hù)經(jīng)由上面的介紹完成了權限管理邏輯后,并且掛在A(yíng)ndesCoreTMAHB bus上,再經(jīng)由仿真器(Cadence)仿真此權限管理邏輯的行為,如下面幾張圖所示: edm_restrict_access信號控制 下圖說(shuō)明由sw code把edm_restrict_access signal disable ![]() 圖表9 由sw code把edm_restrict_access signal disable trusted_debug_exit信號控制 ![]() 圖表10 經(jīng)由debug access把trusted_debug_exit signal設定成high debug_access信號 下圖說(shuō)明經(jīng)由debug host來(lái)做存取時(shí),debug_access signal會(huì )從low變成high ![]() 圖表11 經(jīng)由debug host來(lái)做存取時(shí),debug_access signal會(huì )從low變成high 下圖說(shuō)明經(jīng)由執行IRTE instruction時(shí),debug_access signal會(huì )從high變成low ![]() 圖表12 經(jīng)由執行IRTE instruction時(shí),debug_access signal會(huì )從high變成low 5. 結語(yǔ) EDM安全存取是AndesCoreTM保護周邊裝置內容不被竊取的功能,也因為越來(lái)越多客戶(hù)使用到此功能,所以撰寫(xiě)此技術(shù)文章讓客戶(hù)更能進(jìn)一步了解到此功能的用途,讓客戶(hù)能夠很快速的上手,并且使用晶心開(kāi)發(fā)的EDM安全存取是一件愉快與簡(jiǎn)單的工作。 |