;===========================================================================
;
;FILE: armasmutils.s
;
;SERVICES: ARM assembly utilities
;
;DESCRIPTION
;  This file contains the implementation of the software assembly language 
;   functions that are called by armmutils.c
;
;
;
;INITIALIZATION AND SEQUENCING REQUIREMENTS:  N/A
;
;
;        Copyright © 1999-2003 QUALCOMM Incorporated.
;               All Rights Reserved.
;            QUALCOMM Proprietary/GTDR
;==========================================================================
   EXPORT asmbridle_get_current_mode
   EXPORT asmbridle_mmu_get_page_table_base
   EXPORT asmbridle_get_dac_register
   EXPORT asmbridle_get_dac_register
   EXPORT asmbridle_switch_to_user
   EXPORT asmbridle_mmu_get_controlregister
   EXPORT asmbridle_mmu_set_controlregister
   EXPORT bridle_CheckAccess
   EXPORT asmbridle_set_default_sr
   IMPORT bridle_CheckGenericAccess
   IMPORT bridle_access_abort
   AREA ARMSASMUTILS, CODE, READONLY
   CODE32

;============================================================================
;                       brew_access_abort
;
;  Aborts the caller by switching to user mode  and writing to an illegal
;  address.
;============================================================================
asmbridle_get_current_mode
     MRS r0, CPSR
     bx lr
     
asmbridle_get_dac_register
     MRC p15, 0, r0, c3,c0,0
     bx lr

asmbridle_set_dac_register
     MCR p15, 0 , r0, c3, c0, 0
     bx lr
     
asmbridle_switch_to_user
     MRS r12, CPSR
	 BIC r12,r12,#0x1f
	 ORR r12,r12,#0x10
     MSR cpsr_cxsf,r12
	 bx lr
	 
asmbridle_set_default_sr
	 mrc     p15, 0 , r0, c1, c0, 0
	 bic     r0, r0, #0x300
	 orr     r0, r0, #0x100
     mcr     p15, 0 , r0, c1, c0, 0

asmbridle_mmu_get_controlregister
   mrc     p15, 0 , r0, c1, c0, 0
   bx lr
 
asmbridle_mmu_set_controlregister
   mcr     p15, 0 , r0, c1, c0, 0
   bx      lr

asmbridle_mmu_get_page_table_base
     mrc     p15, 0 , r0, c2, c0, 0
     bx lr

   MACRO
   asmbridle_mmu_flush_tlb
   ldr     r0, =0x0
   mcr     p15, 0, r0, c8, c7, 0
   MEND

   MACRO
   asmbridle_mmu_flush_cache
   ldr     r0, =0x0
   mcr     p15, 0, r0, c7, c7, 0
   MEND

   MACRO
   asmbridle_mmu_set_page_table_base  $base
   ldr     r0, =$base   ; location of our page table descript.
   mcr     p15, 0 , r0, c2, c0, 0
   MEND


asmbridle_mmu_invalidate_data_cache
   ldr     r0, =0x0
   mcr     p15, 0, r0, c7, c6, 0
   bx      lr

asmbridle_mmu_clean_and_invalidate_data_cache
tci_loop
   mrc     p15, 0, r15, c7, c10, 3
   bne     tci_loop
   bx      lr

asmbridle_mmu_invalidate_instruction_cache
   ldr     r0, =0x0
   mcr     p15, 0, r0, c7, c5, 0
   bx      lr


asmbridle_mmu_disable
   mrc     p15, 0 , r0, c1, c0, 0
   bic     r0, r0, # 0x1
   mcr     p15, 0 , r0, c1, c0, 0
   bx      lr

asmbridle_mmu_enable
   mrc     p15, 0 , r0, c1, c0, 0
   orr     r0, r0, # 0x1
   mcr     p15, 0 , r0, c1, c0, 0
   bx      lr
bridle_CheckAccess
   STMFD    r13!,{r3-r9,r14}
   MOV      r9,r14
   MOV      r8,r2
   MOV      r7,r1
   MOV      r6,r0
   MOV      r5,r3
   BL       bridle_CheckGenericAccess ; C function
   MOV      r4,r0
   CMP      r0,#1 ; EFAILED?
   BNE      jump1
   CMP      r5,#0 ; swinum =0?
   MOVNE    r3,r8
   MOVNE    r2,r7
   MOVNE    r1,r6
   MOVNE    r0,r9
   BLNE     bridle_access_abort
jump1
   MOV      r0,r4
   LDMFD    r13!,{r3-r9,pc}
  
   END
