mirror of
				https://github.com/yuzu-emu/unicorn.git
				synced 2025-11-04 13:24:57 +00:00 
			
		
		
		
	i386: Update new x86_apicid parsing rules with die_offset support
In new sockets/dies/cores/threads model, the apicid of logical cpu could imply die level info of guest cpu topology thus x86_apicid_from_cpu_idx() need to be refactored with #dies value, so does apicid_*_offset(). To keep semantic compatibility, the legacy pkg_offset which helps to generate CPUIDs such as 0x3 for L3 cache should be mapping to die_offset. Backports commit d65af288a84d8bf8c27e55d45545f52f016c08a7 from qemu
This commit is contained in:
		
							parent
							
								
									a82e4efa24
								
							
						
					
					
						commit
						d2410074d8
					
				| 
						 | 
				
			
			@ -194,7 +194,7 @@ uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
 | 
			
		|||
{
 | 
			
		||||
    uint32_t correct_id;
 | 
			
		||||
 | 
			
		||||
    correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
 | 
			
		||||
    correct_id = x86_apicid_from_cpu_idx(1, smp_cores, smp_threads, cpu_index);
 | 
			
		||||
    if (compat_apic_id_mode) {
 | 
			
		||||
        if (cpu_index != correct_id) {
 | 
			
		||||
            //error_report("APIC IDs set in compatibility mode, "
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3765,7 +3765,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
 | 
			
		|||
{
 | 
			
		||||
    X86CPU *cpu = env_archcpu(env);
 | 
			
		||||
    CPUState *cs = env_cpu(env);
 | 
			
		||||
    uint32_t pkg_offset;
 | 
			
		||||
    uint32_t die_offset;
 | 
			
		||||
    uint32_t limit;
 | 
			
		||||
    uint32_t signature[3];
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -3854,10 +3854,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
 | 
			
		|||
                                    eax, ebx, ecx, edx);
 | 
			
		||||
                break;
 | 
			
		||||
            case 3: /* L3 cache info */
 | 
			
		||||
                pkg_offset = apicid_pkg_offset(cs->nr_cores, cs->nr_threads);
 | 
			
		||||
                die_offset = apicid_die_offset(env->nr_dies,
 | 
			
		||||
                                        cs->nr_cores, cs->nr_threads);
 | 
			
		||||
                if (cpu->enable_l3_cache) {
 | 
			
		||||
                    encode_cache_cpuid4(env->cache_info_cpuid4.l3_cache,
 | 
			
		||||
                                        (1 << pkg_offset), cs->nr_cores,
 | 
			
		||||
                                        (1 << die_offset), cs->nr_cores,
 | 
			
		||||
                                        eax, ebx, ecx, edx);
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
| 
						 | 
				
			
			@ -3925,12 +3926,14 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
 | 
			
		|||
 | 
			
		||||
        switch (count) {
 | 
			
		||||
        case 0:
 | 
			
		||||
            *eax = apicid_core_offset(smp_cores, smp_threads);
 | 
			
		||||
            *eax = apicid_core_offset(env->nr_dies,
 | 
			
		||||
                                      cs->nr_cores, cs->nr_threads);
 | 
			
		||||
            *ebx = smp_threads;
 | 
			
		||||
            *ecx |= CPUID_TOPOLOGY_LEVEL_SMT;
 | 
			
		||||
            break;
 | 
			
		||||
        case 1:
 | 
			
		||||
            *eax = apicid_pkg_offset(smp_cores, smp_threads);
 | 
			
		||||
            *eax = apicid_pkg_offset(env->nr_dies,
 | 
			
		||||
                                     cs->nr_cores, cs->nr_threads);
 | 
			
		||||
            *ebx = smp_cores * smp_threads;
 | 
			
		||||
            *ecx |= CPUID_TOPOLOGY_LEVEL_CORE;
 | 
			
		||||
            break;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -65,88 +65,120 @@ static unsigned apicid_bitwidth_for_count(unsigned count)
 | 
			
		|||
 | 
			
		||||
/* Bit width of the SMT_ID (thread ID) field on the APIC ID
 | 
			
		||||
 */
 | 
			
		||||
static inline unsigned apicid_smt_width(unsigned nr_cores, unsigned nr_threads)
 | 
			
		||||
static inline unsigned apicid_smt_width(unsigned nr_dies,
 | 
			
		||||
                                        unsigned nr_cores,
 | 
			
		||||
                                        unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_bitwidth_for_count(nr_threads);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Bit width of the Core_ID field
 | 
			
		||||
 */
 | 
			
		||||
static inline unsigned apicid_core_width(unsigned nr_cores, unsigned nr_threads)
 | 
			
		||||
static inline unsigned apicid_core_width(unsigned nr_dies,
 | 
			
		||||
                                         unsigned nr_cores,
 | 
			
		||||
                                         unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_bitwidth_for_count(nr_cores);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Bit width of the Die_ID field */
 | 
			
		||||
static inline unsigned apicid_die_width(unsigned nr_dies,
 | 
			
		||||
                                        unsigned nr_cores,
 | 
			
		||||
                                        unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_bitwidth_for_count(nr_dies);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Bit offset of the Core_ID field
 | 
			
		||||
 */
 | 
			
		||||
static inline unsigned apicid_core_offset(unsigned nr_cores,
 | 
			
		||||
static inline unsigned apicid_core_offset(unsigned nr_dies,
 | 
			
		||||
                                          unsigned nr_cores,
 | 
			
		||||
                                          unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_smt_width(nr_cores, nr_threads);
 | 
			
		||||
    return apicid_smt_width(nr_dies, nr_cores, nr_threads);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Bit offset of the Die_ID field */
 | 
			
		||||
static inline unsigned apicid_die_offset(unsigned nr_dies,
 | 
			
		||||
                                          unsigned nr_cores,
 | 
			
		||||
                                           unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_core_offset(nr_dies, nr_cores, nr_threads) +
 | 
			
		||||
           apicid_core_width(nr_dies, nr_cores, nr_threads);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Bit offset of the Pkg_ID (socket ID) field
 | 
			
		||||
 */
 | 
			
		||||
static inline unsigned apicid_pkg_offset(unsigned nr_cores, unsigned nr_threads)
 | 
			
		||||
static inline unsigned apicid_pkg_offset(unsigned nr_dies,
 | 
			
		||||
                                         unsigned nr_cores,
 | 
			
		||||
                                         unsigned nr_threads)
 | 
			
		||||
{
 | 
			
		||||
    return apicid_core_offset(nr_cores, nr_threads) +
 | 
			
		||||
           apicid_core_width(nr_cores, nr_threads);
 | 
			
		||||
    return apicid_die_offset(nr_dies, nr_cores, nr_threads) +
 | 
			
		||||
           apicid_die_width(nr_dies, nr_cores, nr_threads);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Calculate thread/core/package IDs for a specific topology,
 | 
			
		||||
 * based on APIC ID
 | 
			
		||||
 */
 | 
			
		||||
static inline void x86_topo_ids_from_apicid(apic_id_t apicid,
 | 
			
		||||
                                            unsigned nr_dies,
 | 
			
		||||
                                            unsigned nr_cores,
 | 
			
		||||
                                            unsigned nr_threads,
 | 
			
		||||
                                            X86CPUTopoInfo *topo)
 | 
			
		||||
{
 | 
			
		||||
    topo->smt_id = apicid &
 | 
			
		||||
                   ~(0xFFFFFFFFUL << apicid_smt_width(nr_cores, nr_threads));
 | 
			
		||||
    topo->core_id = (apicid >> apicid_core_offset(nr_cores, nr_threads)) &
 | 
			
		||||
                   ~(0xFFFFFFFFUL << apicid_core_width(nr_cores, nr_threads));
 | 
			
		||||
    topo->pkg_id = apicid >> apicid_pkg_offset(nr_cores, nr_threads);
 | 
			
		||||
    topo->die_id = 0;
 | 
			
		||||
            ~(0xFFFFFFFFUL << apicid_smt_width(nr_dies, nr_cores, nr_threads));
 | 
			
		||||
    topo->core_id =
 | 
			
		||||
            (apicid >> apicid_core_offset(nr_dies, nr_cores, nr_threads)) &
 | 
			
		||||
            ~(0xFFFFFFFFUL << apicid_core_width(nr_dies, nr_cores, nr_threads));
 | 
			
		||||
    topo->die_id =
 | 
			
		||||
            (apicid >> apicid_die_offset(nr_dies, nr_cores, nr_threads)) &
 | 
			
		||||
            ~(0xFFFFFFFFUL << apicid_die_width(nr_dies, nr_cores, nr_threads));
 | 
			
		||||
    topo->pkg_id = apicid >> apicid_pkg_offset(nr_dies, nr_cores, nr_threads);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID
 | 
			
		||||
 *
 | 
			
		||||
 * The caller must make sure core_id < nr_cores and smt_id < nr_threads.
 | 
			
		||||
 */
 | 
			
		||||
static inline apic_id_t apicid_from_topo_ids(unsigned nr_cores,
 | 
			
		||||
static inline apic_id_t apicid_from_topo_ids(unsigned nr_dies,
 | 
			
		||||
                                             unsigned nr_cores,
 | 
			
		||||
                                             unsigned nr_threads,
 | 
			
		||||
                                             const X86CPUTopoInfo *topo)
 | 
			
		||||
{
 | 
			
		||||
    return (topo->pkg_id  << apicid_pkg_offset(nr_cores, nr_threads)) |
 | 
			
		||||
           (topo->core_id << apicid_core_offset(nr_cores, nr_threads)) |
 | 
			
		||||
    return (topo->pkg_id  << apicid_pkg_offset(nr_dies, nr_cores, nr_threads)) |
 | 
			
		||||
           (topo->die_id  << apicid_die_offset(nr_dies, nr_cores, nr_threads)) |
 | 
			
		||||
          (topo->core_id << apicid_core_offset(nr_dies, nr_cores, nr_threads)) |
 | 
			
		||||
           topo->smt_id;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Calculate thread/core/package IDs for a specific topology,
 | 
			
		||||
 * based on (contiguous) CPU index
 | 
			
		||||
 */
 | 
			
		||||
static inline void x86_topo_ids_from_idx(unsigned nr_cores,
 | 
			
		||||
static inline void x86_topo_ids_from_idx(unsigned nr_dies,
 | 
			
		||||
                                         unsigned nr_cores,
 | 
			
		||||
                                         unsigned nr_threads,
 | 
			
		||||
                                         unsigned cpu_index,
 | 
			
		||||
                                         X86CPUTopoInfo *topo)
 | 
			
		||||
{
 | 
			
		||||
    unsigned core_index = cpu_index / nr_threads;
 | 
			
		||||
    topo->pkg_id = cpu_index / (nr_dies * nr_cores * nr_threads);
 | 
			
		||||
    topo->die_id = cpu_index / (nr_cores * nr_threads) % nr_dies;
 | 
			
		||||
    topo->core_id = cpu_index / nr_threads % nr_cores;
 | 
			
		||||
    topo->smt_id = cpu_index % nr_threads;
 | 
			
		||||
    topo->core_id = core_index % nr_cores;
 | 
			
		||||
    topo->pkg_id = core_index / nr_cores;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Make APIC ID for the CPU 'cpu_index'
 | 
			
		||||
 *
 | 
			
		||||
 * 'cpu_index' is a sequential, contiguous ID for the CPU.
 | 
			
		||||
 */
 | 
			
		||||
static inline apic_id_t x86_apicid_from_cpu_idx(unsigned nr_cores,
 | 
			
		||||
static inline apic_id_t x86_apicid_from_cpu_idx(unsigned nr_dies,
 | 
			
		||||
                                                unsigned nr_cores,
 | 
			
		||||
                                                unsigned nr_threads,
 | 
			
		||||
                                                unsigned cpu_index)
 | 
			
		||||
{
 | 
			
		||||
    X86CPUTopoInfo topo;
 | 
			
		||||
    x86_topo_ids_from_idx(nr_cores, nr_threads, cpu_index, &topo);
 | 
			
		||||
    return apicid_from_topo_ids(nr_cores, nr_threads, &topo);
 | 
			
		||||
    x86_topo_ids_from_idx(nr_dies, nr_cores, nr_threads, cpu_index, &topo);
 | 
			
		||||
    return apicid_from_topo_ids(nr_dies, nr_cores, nr_threads, &topo);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* TARGET_I386_TOPOLOGY_H */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue