frederick00
Posts: 55
Joined: Mon Jul 13, 2020 8:38 pm

Check a specific bit in SCTLR_EL1 Register on Rasperry pi 4

Thu Jul 16, 2020 3:33 pm

Hello.

i have been trying to check the UCI bit (UCI, bit [26]) in SCTLR_EL1 Register on Rasperry Pi 4.
So i wrote a simple kernel module :

Code: Select all


#include <linux/module.h>
#include<linux/init.h>
#include <linux/kernel.h>

static inline void write_SCTLR_EL1(uint64_t val){
    asm volatile("msr s3_0_c1_c0_0 , %0" : : "r" (val));
    asm volatile("isb" : : : "memory");
    
}

static inline uint64_t read_SCTLR_EL1(void){
	uint64_t val;
	asm volatile("mrs %0, s3_0_c1_c0_0" : "=r" (val));
	return val;
}

int __init enable_cache_instructions(void){

	uint64_t value = read_SCTLR_EL1();
	printk(KERN_INFO "VALUE IS <0x%016llx> from SCTLR_EL1.", value);
	
	return 0;
}

void disable_cache(void) {
	printk(KERN_INFO "Instructions disabled \n");
}

module_init(enable_cache_instructions);
module_exit(disable_cache);

MODULE_LICENSE("GPL");

and the value of the Register is : 0x0000000034d5d83d (bynary format = 110100110101011101100000111101).
So i wrote a simple c code to check UCI bit value :

Code: Select all

 int main (){
 	unsigned long register_value = 886429757; // decimal format of the Register value
 	register_value |= 1ULL << 26; //trying to set UCI bit to 1 if it is disabled
	printf("After set : %lu\n",register_value);

	
 	return 0;
 }

Output : 886429757
Is that right ? The value of UCI bit is 1 ?
i mean, if the value is not set, the start and the end values should be different.

i tried checking the value in c code as well :

Code: Select all

	register_value  &=  (1ULL << 26);
	printf("After check : %lu\n",register_value);
but the output is : 67108864.

Thank You.

cleverca22
Posts: 1828
Joined: Sat Aug 18, 2012 2:33 pm

Re: Check a specific bit in SCTLR_EL1 Register on Rasperry pi 4

Thu Jul 16, 2020 5:48 pm

Code: Select all

> (67108864).toString(16)
'4000000'
> 1 << 26
67108864
> (886429757).toString(16)
'34d5d83d'
> (886429757 & (1<<26)).toString(16)
'4000000'
that looks pretty normal to me, you want to either print the numbers in hex or binary, and look at the right bit, not the entire base10 number

frederick00
Posts: 55
Joined: Mon Jul 13, 2020 8:38 pm

Re: Check a specific bit in SCTLR_EL1 Register on Rasperry pi 4

Thu Jul 16, 2020 10:00 pm

cleverca22 wrote:
Thu Jul 16, 2020 5:48 pm

Code: Select all

> (67108864).toString(16)
'4000000'
> 1 << 26
67108864
> (886429757).toString(16)
'34d5d83d'
> (886429757 & (1<<26)).toString(16)
'4000000'
that looks pretty normal to me, you want to either print the numbers in hex or binary, and look at the right bit, not the entire base10 number
Thank you for the answer.
My idea (i'm not sure that is a good way) is to read the value of the register first (converting it in hex format or decimal), then using or bitwise operation and finally checking the result.
My question is : If the bit is set before OR operation, the start value (register value gathered from read function in kernel module) and final value (after OR operation) should be the same, is that true ? and then, UCI bit is set to 1 ?.

Return to “General discussion”