x86, amd_ucode: Support multiple container files appended together
PATCH 1/1 x86, amd_ucode: Support multiple container files appended together
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]
While at it, define HDR_SIZES and use these for clarity.
--- xen/arch/x86/microcode_amd.c | 120 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 17 deletions(-)
xen/arch/x86/microcode_amd.c +103 -17
@@ -29,6 +29,9 @@ #define pr_debug(x...) ((void)0) +#define CONT_HDR_SIZE 12 +#define SECTION_HDR_SIZE 8 +struct __packed equiv_cpu_entry {uint32_t installed_cpu;uint32_t fixed_errata_mask;
@@ -124,6 +127,25 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static void find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +break ; + } + } +} +static bool_t microcode_fits (const struct microcode_amd *mc_amd,int cpu) {struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu);
@@ -131,21 +153,13 @@ static bool_t microcode_fits(const struct microcode_amd *mc_amd, int cpu)const struct equiv_cpu_entry *equiv_cpu_table = mc_amd->equiv_cpu_table;unsigned int current_cpu_id;unsigned int equiv_cpu_id =0x0 ; -unsigned int i;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); current_cpu_id =cpuid_eax (0x00000001 ); -for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) - { -if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) - { - equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; -break ; - } - } +find_equiv_cpu_id (equiv_cpu_table, current_cpu_id, &equiv_cpu_id);if ( !equiv_cpu_id )return 0 ;
@@ -236,7 +250,15 @@ static int get_ucode_from_buffer_amd( mpbuf = (const struct mpbhdr *)&bufp[off];if ( mpbuf->type !=UCODE_UCODE_TYPE ) { -printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +/* +* We will hit this condition only if an update has succeeded +* but there is still another container file being parsed. +* In that case, there is no need of this ERR message to be +* printed. +*/ +if ( mpbuf->type !=UCODE_MAGIC ) +printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +return -EINVAL ; }
@@ -260,7 +282,7 @@ static int get_ucode_from_buffer_amd( }memcpy (mc_amd->mpb, mpbuf->data, mpbuf->len); - *offset = off + mpbuf->len +8 ; + *offset = off + mpbuf->len +SECTION_HDR_SIZE ; pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID %#x rev %#x\n", raw_smp_processor_id(), bufsize, mpbuf->len, off,
@@ -272,13 +294,16 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, -size_t *offset) +const uint8_t *data, +size_t *tot_size, +size_t *curr_offset) { +size_t off = *curr_offset; +uint32_t *buf = (uint32_t *) &data[off];const struct mpbhdr *mpbuf = (const struct mpbhdr *)&buf[1 ];/* No more data */ -if ( mpbuf->len +12 >= *offset ) +if ( mpbuf->len +CONT_HDR_SIZE >= *tot_size )return -EINVAL ;if ( mpbuf->type !=UCODE_EQUIV_CPU_TABLE_TYPE )
@@ -303,7 +328,32 @@ static int install_equiv_cpu_table(memcpy (mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len); mc_amd->equiv_cpu_table_size = mpbuf->len; - *offset = mpbuf->len +12 ;/* add header length */ + *curr_offset += mpbuf->len +CONT_HDR_SIZE ;/* add header length */ + +return 0 ; +} + +static int container_fast_forward (const uint8_t *data,size_t size_left,size_t *offset) +{ +size_t size, off; +uint32_t *header; + +while ( size_left ) + { + off = *offset; + header = (uint32_t *) &data[off]; +if ( header[0 ] ==UCODE_MAGIC && + header[1 ] ==UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = header[1 ] +SECTION_HDR_SIZE ; + *offset += size; + size_left -= size; + + } + +if ( !size_left ) +return -EINVAL ;return 0 ; }
@@ -311,14 +361,19 @@ static int install_equiv_cpu_table(static int cpu_request_microcode (int cpu,const void *buf,size_t bufsize) {struct microcode_amd *mc_amd, *mc_old; -size_t offset = bufsize; +size_t offset =0 ; +size_t tot_size = bufsize;size_t last_offset, applied_offset =0 ;int error =0 , save_error =1 ;struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu); +unsigned int current_cpu_id; +unsigned int equiv_cpu_id =0x0 ;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); + current_cpu_id =cpuid_eax (0x00000001 ); +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) {printk (KERN_ERR "microcode: Wrong microcode patch file magic\n" );
@@ -334,7 +389,30 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 ) + { +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id); +if ( equiv_cpu_id ) +break ; + + error =container_fast_forward (buf, (bufsize - offset), &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + error = -EINVAL ; + goto out; + } + } +if ( error ) {xfree (mc_amd);
@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); +/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ; }if ( save_error )
Signature · 1 line
1.7.9.5
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 19.06.14 at 17:14, <[email protected]> wrote:
xen/arch/x86/microcode_amd.c +8
-0@@ -124,6 +127,25 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static void find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return ;
xen/arch/x86/microcode_amd.c static bool_t verify_patch_size(uint32_t patch_size) · line 147
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+ +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +break ; + } + } +}
Please have this function return a found/not-found indication rather than having the callers rely on *equiv_cpu_id transitioning from zero to no-zero. That way you can also drop the respective initializers in the callers.
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 302
Quoted 12 lines
xen/arch/x86/microcode_amd.c +5
-2@@ -272,13 +294,16 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, -size_t *offset) +const uint8_t *data, +size_t *tot_size, +size_t *curr_offset) { +size_t off = *curr_offset; +uint32_t *buf = (uint32_t *) &data[off];
I realize there are may casts there in this file. But please don't add more of them without a strict need (replacing existing ones would also be much appreciated). In the case here, if "data" was "const void *" you'd get away with just "data + off". Furthermore there's no reason for having the one-time use variable "off" here - just use *offset directly.
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 344
Quoted 9 lines
xen/arch/x86/microcode_amd.c +9
-0+static int container_fast_forward (const uint8_t *data,size_t size_left,size_t *offset) +{ +size_t size, off; +uint32_t *header; + +while ( size_left ) + { + off = *offset; + header = (uint32_t *) &data[off];
Same comments as a above apply here.
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 351
Quoted 7 lines
xen/arch/x86/microcode_amd.c +7
-0+if ( header[0 ] ==UCODE_MAGIC && + header[1 ] ==UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = header[1 ] +SECTION_HDR_SIZE ; + *offset += size; + size_left -= size;
Don't you think you should first check for size_left >= size here?
Quoted 15 lines
xen/arch/x86/microcode_amd.c +10
-1@@ -334,7 +389,30 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 ) + { +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id);
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 411
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+if ( equiv_cpu_id ) +break ; + + error =container_fast_forward (buf, (bufsize - offset), &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + error = -EINVAL ;
Either you use the error value returned from container_fast_forward(), or you make that function simply return a bool_t.
Quoted 4 lines
xen/arch/x86/microcode_amd.c +0
-0@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset);
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 469
Quoted 10 lines
xen/arch/x86/microcode_amd.c +8
-0+/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ;
First of all I don't think this change is logically a part of $subject (as it also alters behavior for single container files). With that the question then is why the change is really correct. And if it was really correct, you could just drop the if() instead.
Jan
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 19.06.14 at 17:14, <[email protected]> wrote:
xen/arch/x86/microcode_amd.c +14
-0@@ -124,6 +127,25 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static void find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ;
Quoted 8 lines
xen/arch/x86/microcode_amd.c static bool_t verify_patch_size(uint32_t patch_size) · line 147
Quoted 4 lines
xen/arch/x86/microcode_amd.c +4
-0+break ; + } + } +}Please have this function return a found/not-found indication rather than having the callers rely on
*equiv_cpu_idtransitioning from zero to no-zero. That way you can also drop the respective initializers in the callers.
Ok, Will modify this.
Quoted 18 lines
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 302
Quoted 12 lines
xen/arch/x86/microcode_amd.c +5
-2@@ -272,13 +294,16 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, -size_t *offset) +const uint8_t *data, +size_t *tot_size, +size_t *curr_offset) { +size_t off = *curr_offset; +uint32_t *buf = (uint32_t *) &data[off];I realize there are may casts there in this file. But please don't add more of them without a strict need (replacing existing ones would also be much appreciated). In the case here, if "data" was "const void *" you'd get away with just "data + off". Furthermore there's no reason for having the one-time use variable "off" here - just use *offset directly.
Ok, I'll change this per your suggestion.
About replacing the existing casts, I'll generate a separate patch for it..
Quoted 8 lines
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 351
Quoted 7 lines
xen/arch/x86/microcode_amd.c +7
-0+if ( header[0 ] ==UCODE_MAGIC && + header[1 ] ==UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = header[1 ] +SECTION_HDR_SIZE ; + *offset += size; + size_left -= size;Don't you think you should first check for
size_left>= size here?
Yep. Thanks for pointing this out. I'll add it.
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/microcode_amd.c +14
-1@@ -334,7 +389,30 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 ) + { +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id); +if ( equiv_cpu_id ) +break ; + + error =container_fast_forward (buf, (bufsize - offset), &offset);
Quoted 8 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 411
Quoted 6 lines
xen/arch/x86/microcode_amd.c +6
-0+if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + error = -EINVAL ;Either you use the error value returned from
container_fast_forward(), or you make that function simply return abool_t.
Hmm, Yep. I'll fix this.
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/microcode_amd.c +0
-0@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset);
Quoted 16 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 469
Quoted 10 lines
xen/arch/x86/microcode_amd.c +8
-0+/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ;First of all I don't think this change is logically a part of $subject (as it also alters behavior for single container files). With that the question then is why the change is really correct. And if it was really correct, you could just drop the
if()instead.
The change is needed (as stated in the comments) because we end up returning err val (EINVAL) like so-
DEBUG: cpu_request_microcode : no save_err;error=-22
even though update succeeded-
(XEN) microcode: CPU0 found a matching microcode update with version 0x600063d (current=0x6000626) (XEN) microcode: CPU0 updated from revision 0x6000626 to 0x600063d
That said, I also understand your point that we can just drop the if(). So, I'll change this as per your suggestion.
The fact that we have to make this change as a _consequence_ of adding multiple container file support, might explain why it was done later right?
I can make this a separate patch as well. Let me know what you think.
Thanks,
-Aravind.
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 20.06.14 at 18:06, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 19.06.14 at 17:14, <[email protected]> wrote:
xen/arch/x86/microcode_amd.c +0
-0@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset);Quoted 16 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 469
Quoted 10 lines
xen/arch/x86/microcode_amd.c +8
-0+/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ;First of all I don't think this change is logically a part of $subject (as it also alters behavior for single container files). With that the question then is why the change is really correct. And if it was really correct, you could just drop the
if()instead.The change is needed (as stated in the comments) because we end up returning err val (EINVAL) like so-
DEBUG:cpu_request_microcode: no save_err;error=-22even though update succeeded-
(XEN) microcode: CPU0 found a matching microcode update with version0x600063d(current=0x6000626)
(XEN) microcode: CPU0 updated from revision0x6000626to0x600063d
Quoted 7 lines
That said, I also understand your point that we can just drop the
if(). So, I'll change this as per your suggestion.The fact that we have to make this change as a _consequence_ of adding multiple container file support,
might explain why it was done later right?
I can make this a separate patch as well. Let me know what you think.
I think I follow what you're saying, in which case this clearly shouldn't be a separate patch. But behavior for single-container files still should remain unaltered, so some change other than folding the if and else branches is going to be needed afaict.
Jan
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 20.06.14 at 18:06, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 19.06.14 at 17:14, <[email protected]> wrote:
xen/arch/x86/microcode_amd.c +0
-0@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset);Quoted 16 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 469
Quoted 10 lines
xen/arch/x86/microcode_amd.c +8
-0+/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ;First of all I don't think this change is logically a part of $subject (as it also alters behavior for single container files). With that the question then is why the change is really correct. And if it was really correct, you could just drop the
if()instead.The change is needed (as stated in the comments) because we end up returning err val (EINVAL) like so-
DEBUG:cpu_request_microcode: no save_err;error=-22even though update succeeded-
(XEN) microcode: CPU0 found a matching microcode update with version0x600063d(current=0x6000626)
(XEN) microcode: CPU0 updated from revision0x6000626to0x600063dThat said, I also understand your point that we can just drop the
if(). So, I'll change this as per your suggestion.
Quoted 10 lines
Quoted 4 lines
The fact that we have to make this change as a _consequence_ of adding multiple container file support,
might explain why it was done later right?
I can make this a separate patch as well. Let me know what you think.I think I follow what you're saying, in which case this clearly shouldn't be a separate patch. But behavior for single-container files still should remain unaltered, so some change other than folding the if and else branches is going to be needed afaict.
I have addressed your concern in a V2.
Thanks,
-Aravind.
Quoted 103 lines · Aravind Gopalakrishnan
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]While at it, define
HDR_SIZESand use these for clarity.Signed-off-by Aravind Gopalakrishnan <[email protected]>Reviewed-by Suravee Suthikulpanit <[email protected]>--- xen/arch/x86/microcode_amd.c | 120 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 17 deletions(-)xen/arch/x86/microcode_amd.c +33
-11@@ -29,6 +29,9 @@ #define pr_debug(x...) ((void)0) +#define CONT_HDR_SIZE 12 +#define SECTION_HDR_SIZE 8 +struct __packed equiv_cpu_entry {uint32_t installed_cpu;uint32_t fixed_errata_mask;@@ -124,6 +127,25 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static void find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +break ; + } + } +} +static bool_t microcode_fits (const struct microcode_amd *mc_amd,int cpu) {struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu);@@ -131,21 +153,13 @@ static bool_t microcode_fits(const struct microcode_amd *mc_amd, int cpu)const struct equiv_cpu_entry *equiv_cpu_table = mc_amd->equiv_cpu_table;unsigned int current_cpu_id;unsigned int equiv_cpu_id =0x0 ; -unsigned int i;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); current_cpu_id =cpuid_eax (0x00000001 ); -for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) - { -if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) - { - equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; -break ; - } - } +find_equiv_cpu_id (equiv_cpu_table, current_cpu_id, &equiv_cpu_id);if ( !equiv_cpu_id )return 0 ;@@ -236,7 +250,15 @@ static int get_ucode_from_buffer_amd( mpbuf = (const struct mpbhdr *)&bufp[off];if ( mpbuf->type !=UCODE_UCODE_TYPE ) { -printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +/* +* We will hit this condition only if an update has succeeded +* but there is still another container file being parsed. +* In that case, there is no need of this ERR message to be +* printed. +*/ +if ( mpbuf->type !=UCODE_MAGIC ) +printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +return -EINVAL ; }@@ -260,7 +282,7 @@ static int get_ucode_from_buffer_amd( }memcpy (mc_amd->mpb, mpbuf->data, mpbuf->len); - *offset = off + mpbuf->len +8 ; + *offset = off + mpbuf->len +SECTION_HDR_SIZE ; pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID %#x rev %#x\n", raw_smp_processor_id(), bufsize, mpbuf->len, off,
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 298
Quoted 7 lines
xen/arch/x86/microcode_amd.c +2
-2@@ -272,13 +294,16 @@ static int get_ucode_from_buffer_amd( static int install_equiv_cpu_table( struct microcode_amd *mc_amd, -const uint32_t *buf, -size_t *offset) + const uint8_t *data, + size_t *tot_size,
Why is this a pointer? This routine does not modify the value.
Quoted 69 lines
xen/arch/x86/microcode_amd.c +36
-3+ size_t *curr_offset) { +size_t off = *curr_offset; +uint32_t *buf = (uint32_t *) &data[off];const struct mpbhdr *mpbuf = (const struct mpbhdr *)&buf[1 ];/* No more data */ -if ( mpbuf->len +12 >= *offset ) +if ( mpbuf->len +CONT_HDR_SIZE >= *tot_size )return -EINVAL ;if ( mpbuf->type !=UCODE_EQUIV_CPU_TABLE_TYPE )@@ -303,7 +328,32 @@ static int install_equiv_cpu_table(memcpy (mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len); mc_amd->equiv_cpu_table_size = mpbuf->len; - *offset = mpbuf->len +12 ;/* add header length */ + *curr_offset += mpbuf->len +CONT_HDR_SIZE ;/* add header length */ + +return 0 ; +} + +static int container_fast_forward (const uint8_t *data,size_t size_left,size_t *offset) +{ +size_t size, off; +uint32_t *header; + +while ( size_left ) + { + off = *offset; + header = (uint32_t *) &data[off]; +if ( header[0 ] ==UCODE_MAGIC && + header[1 ] ==UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = header[1 ] +SECTION_HDR_SIZE ; + *offset += size; + size_left -= size; + + } + +if ( !size_left ) +return -EINVAL ;return 0 ; }@@ -311,14 +361,19 @@ static int install_equiv_cpu_table(static int cpu_request_microcode (int cpu,const void *buf,size_t bufsize) {struct microcode_amd *mc_amd, *mc_old; -size_t offset = bufsize; +size_t offset =0 ; +size_t tot_size = bufsize;size_t last_offset, applied_offset =0 ;int error =0 , save_error =1 ;struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu); +unsigned int current_cpu_id; +unsigned int equiv_cpu_id =0x0 ;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); + current_cpu_id =cpuid_eax (0x00000001 ); +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) {printk (KERN_ERR "microcode: Wrong microcode patch file magic\n" );@@ -334,7 +389,30 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out;
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 398
Quoted 10 lines
xen/arch/x86/microcode_amd.c +7
-1} - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 )
install_equiv_cpu_table() checks whether container size is larger than the buffer size. If we have multiple containers merged I think tot_size is the size of the merged file, not of an individual container. And this makes the first check in install_equiv_cpu_table() not especially meaningful.
-boris
Quoted 37 lines
xen/arch/x86/microcode_amd.c +25
-0+ { +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id); +if ( equiv_cpu_id ) +break ; + + error =container_fast_forward (buf, (bufsize - offset), &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + error = -EINVAL ; + goto out; + } + } +if ( error ) {xfree (mc_amd);@@ -379,8 +457,16 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); +/* +* If there happens to be multiple container files and if patch +* update succeeded on first container itself, a stale error val +* is returned from get_ucode_from_buffer_amd. So, force +* error = 0 here as we have already succeeded in the update. +*/ if ( save_error ) error = save_error; +else + error =0 ; }if ( save_error )
Quoted 104 lines · Boris Ostrovsky
Quoted 103 lines · Aravind Gopalakrishnan
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]While at it, define
HDR_SIZESand use these for clarity.Signed-off-by Aravind Gopalakrishnan <[email protected]>Reviewed-by Suravee Suthikulpanit <[email protected]>--- xen/arch/x86/microcode_amd.c | 120 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 17 deletions(-)xen/arch/x86/microcode_amd.c +20
-0@@ -29,6 +29,9 @@ #define pr_debug(x...) ((void)0) +#define CONT_HDR_SIZE 12 +#define SECTION_HDR_SIZE 8 +struct __packed equiv_cpu_entry {uint32_t installed_cpu;uint32_t fixed_errata_mask;@@ -124,6 +127,25 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static void find_equiv_cpu_id(const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +break ; + } + } +} + static bool_t microcode_fits(const struct microcode_amd *mc_amd,int cpu)
{ struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu);xen/arch/x86/microcode_amd.c +10
-9@@ -131,21 +153,13 @@ static bool_t microcode_fits(const struct microcode_amd *mc_amd, int cpu)const struct equiv_cpu_entry *equiv_cpu_table = mc_amd->equiv_cpu_table;unsigned int current_cpu_id;unsigned int equiv_cpu_id =0x0 ; -unsigned int i;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); current_cpu_id =cpuid_eax (0x00000001 ); - for ( i = 0; equiv_cpu_table[i].installed_cpu != 0; i++ ) - { -if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) - { - equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; -break ; - } - } +find_equiv_cpu_id (equiv_cpu_table, current_cpu_id, &equiv_cpu_id);if ( !equiv_cpu_id )return 0 ;@@ -236,7 +250,15 @@ static int get_ucode_from_buffer_amd( mpbuf = (const struct mpbhdr *)&bufp[off];if ( mpbuf->type !=UCODE_UCODE_TYPE ) { -printk (KERN_ERR "microcode: Wrong microcode payload type field\n"); +/* +* We will hit this condition only if an update has succeeded +* but there is still another container file being parsed. +* In that case, there is no need of this ERR message to be +* printed. + */ + if ( mpbuf->type != UCODE_MAGIC ) +printk (KERN_ERR "microcode: Wrong microcode payload type field\n"); +return -EINVAL ;} @@ -260,7 +282,7 @@ static int get_ucode_from_buffer_amd( } memcpy(mc_amd->mpb, mpbuf->data, mpbuf->len);- *offset = off +
mpbuf->len+ 8;
+ *offset = off +mpbuf->len+SECTION_HDR_SIZE;
pr_debug("microcode: CPU%d size %zu, block size %u offset %zu
equivID %#x rev %#x\n",raw_smp_processor_id(), bufsize, mpbuf->len, off,xen/arch/x86/microcode_amd.c +0
-0@@ -272,13 +294,16 @@ static int get_ucode_from_buffer_amd(
Quoted 9 lines
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 298
Quoted 6 lines
xen/arch/x86/microcode_amd.c +2
-2static int install_equiv_cpu_table( struct microcode_amd *mc_amd, -const uint32_t *buf, -size_t *offset) + const uint8_t *data, + size_t *tot_size,Why is this a pointer? This routine does not modify the value.
Yeah. No point. I'll fix it.
Thanks for the pointer :)
Quoted 71 lines
Quoted 71 lines
xen/arch/x86/microcode_amd.c +5
-1+ size_t *curr_offset) { +size_t off = *curr_offset; +uint32_t *buf = (uint32_t *) &data[off];const struct mpbhdr *mpbuf = (const struct mpbhdr *)&buf[1 ];/* No more data */ -if ( mpbuf->len +12 >= *offset ) +if ( mpbuf->len +CONT_HDR_SIZE >= *tot_size )return -EINVAL ;if ( mpbuf->type !=UCODE_EQUIV_CPU_TABLE_TYPE )@@ -303,7 +328,32 @@ static int install_equiv_cpu_table(memcpy (mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len); mc_amd->equiv_cpu_table_size = mpbuf->len; - *offset = mpbuf->len +12 ;/* add header length */ + *curr_offset += mpbuf->len +CONT_HDR_SIZE ;/* add header length */
xen/arch/x86/microcode_amd.c +5
-0+ +return 0 ; +} + +static int container_fast_forward(const uint8_t *data, size_t
size_left,size_t*offset)xen/arch/x86/microcode_amd.c +25
-1+{ +size_t size, off; +uint32_t *header; + +while ( size_left ) + { + off = *offset; + header = (uint32_t *) &data[off]; +if ( header[0 ] ==UCODE_MAGIC && + header[1 ] ==UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = header[1 ] +SECTION_HDR_SIZE ; + *offset += size; + size_left -= size; + + } + +if ( !size_left ) +return -EINVAL ;return 0 ; }@@ -311,14 +361,19 @@ static int install_equiv_cpu_table( static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) { struct microcode_amd *mc_amd, *mc_old; - size_t offset = bufsize; + size_t offset = 0; + size_t tot_size = bufsize; size_t last_offset, applied_offset = 0; int error = 0, save_error = 1; struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu); + unsigned int current_cpu_id; + unsigned int equiv_cpu_id = 0x0; /* We should bind the task to the CPU */ BUG_ON(cpu != raw_smp_processor_id()); + current_cpu_id = cpuid_eax(0x00000001); + if ( *(const uint32_t *)buf != UCODE_MAGIC ) { printk(KERN_ERR "microcode: Wrong microcode patch filemagic\n");
xen/arch/x86/microcode_amd.c +1
-0@@ -334,7 +389,30 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/*
Quoted 8 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 398
Quoted 6 lines
xen/arch/x86/microcode_amd.c +6
-0+* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 )
install_equiv_cpu_table()checks whether container size is larger than the buffer size.
Not really.
It is basically checking if we have data at all to parse (like the comment says:/* No more data */)
Quoted 1 line
If we have multiple containers merged I think
tot_sizeis the size of the merged file, not of an individual container.
That's right.
Quoted 2 lines
And this makes the first check in
install_equiv_cpu_table()not especially meaningful.
Theoretically-
If it so happens that we don't have any patch files and just the container header, then mpbuf->len would be zero and
if ( mpbuf->len + CONT_HDR_SIZE >= *tot_size )
will fail in this case- (which gives some meaning to the check being there)
1. If containers are concatenated, but there is no matching patch on the first one
2. So, we fast-fwd to next container file but this one has mpbuf->len=0
Which leads me to another scenario though-
1. If we do have multiple containers and if right patch is not on the first one, but on some subsequent container
2. If the first container has mpbuf->len=0
Then we'd just error out right there. Hmm.. I'll have to re-work the logic then.
Come to think, if the first container is corrupted for some reason and returns error (or there's an -ENOMEM), then we are going to return pre-maturely Instead, we should probably (try to) forward to next container and look for patches there as well.
Shall fix this logic in a v2..
Thanks,
-Aravind
Quoted 15 lines
Quoted 5 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 398
Quoted 3 lines
xen/arch/x86/microcode_amd.c +2
-0+while ( (error =install_equiv_cpu_table (mc_amd, buf, &tot_size, + &offset)) ==0 )
install_equiv_cpu_table()checks whether container size is larger than the buffer size.Not really.
It is basically checking if we have data at all to parse (like the comment says:/* No more data */)Quoted 1 line
If we have multiple containers merged I think
tot_sizeis the size of the merged file, not of an individual container.That's right.
Quoted 2 lines
And this makes the first check in
install_equiv_cpu_table()not especially meaningful.
Quoted 7 lines
Theoretically-
If it so happens that we don't have any patch files and just the container header, thenmpbuf->lenwould be zero and
if (mpbuf->len+CONT_HDR_SIZE>=*tot_size)
will fail in this case- (which gives some meaning to the check being there)
1. If containers are concatenated, but there is no matching patch on the first one
2. So, we fast-fwd to next container file but this one has mpbuf->len=0
So if you have two merged containers, the first one being 1K and the second one empty, then while processing the second container you will instead of returning -EINVAL continue because 'if (0+12 >= 1K) ' will evaluate to false. And it seems to me that you then may well go beyond the end of the buffer.
Quoted 5 lines
Which leads me to another scenario though-
1. If we do have multiple containers and if right patch is not on the first one, but on some subsequent container
2. If the first container has mpbuf->len=0
Then we'd just error out right there. Hmm.. I'll have to re-work the logic then.
Isn't it the other way around? We won't error out, we will continue. Which is wrong.
Quoted 6 lines
Come to think, if the first container is corrupted for some reason and returns error (or there's an
-ENOMEM), then we are going to return pre-maturely Instead, we should probably (try to) forward to next container and look for patches there as well.Shall fix this logic in a v2..
BTW, it may be worth documenting container format in English. As it stands now, one has to look at the code to figure out the layout.
-boris
Quoted 14 lines · Boris Ostrovsky
Quoted 6 lines
Quoted 5 lines
And this makes the first check in
install_equiv_cpu_table()not especially meaningful.Quoted 7 lines
Theoretically-
If it so happens that we don't have any patch files and just the container header, thenmpbuf->lenwould be zero and
if (mpbuf->len+CONT_HDR_SIZE>=*tot_size)
will fail in this case- (which gives some meaning to the check being there)
1. If containers are concatenated, but there is no matching patch on the first one
2. So, we fast-fwd to next container file but this one has mpbuf->len=0
Quoted 1 line
So if you have two merged containers, the first one being 1K and the second one empty, then while processing the second container you will instead of returning
-EINVALcontinue because 'if (0+12 >= 1K) ' will evaluate to false.
Hmm. Yes, You are right. This check here seems meaningless..
Quoted 3 lines
And it seems to me that you then may well go beyond the end of the buffer.
I don't think so as we will hit if ( mpbuf->len == 0 ) and return -EINVAL;
Quoted 8 lines
Quoted 5 lines
Which leads me to another scenario though-
1. If we do have multiple containers and if right patch is not on the first one, but on some subsequent container
2. If the first container has mpbuf->len=0
Then we'd just error out right there. Hmm.. I'll have to re-work the logic then.Isn't it the other way around? We won't error out, we will continue. Which is wrong.
No. We should hit-
if ( mpbuf->len == 0 )
{
printk(KERN_ERR "microcode: Wrong microcode equivalent cpu
table length\n");
return -EINVAL;
}And with the while loop as it is now will not look for a possible 2nd container.
Also, as said below, in case we hit if ( mpbuf->type != UCODE_EQUIV_CPU_TABLE_TYPE ) or if ( !mc_amd->equiv_cpu_table ) then we will not enter the loop.
I have fixed this now.
Will send it in a V2.
Quoted 9 lines
Quoted 6 lines
Come to think, if the first container is corrupted for some reason and returns error (or there's an
-ENOMEM), then we are going to return pre-maturely Instead, we should probably (try to) forward to next container and look for patches there as well.Shall fix this logic in a v2..
BTW, it may be worth documenting container format in English. As it stands now, one has to look at the code to figure out the layout.
Yeah. I shall work on a draft version and send it as RFC.
Thanks,
-Aravind.